syspch.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. { PChar functions }
  19. { Processor dependent part, shared withs strings unit }
  20. {$ifdef FPC_USE_LIBC}
  21. {$i cgenstr.inc}
  22. {$endif FPC_USE_LIBC}
  23. {$i strings.inc }
  24. { Read generic string functions that are not implemented for the processor }
  25. {$i genstr.inc}
  26. { Processor independent part, shared with strings unit }
  27. {$i stringsi.inc }
  28. { StrPas converts a PChar to a pascal string }
  29. function StrPas(Str: PChar): string;
  30. begin
  31. Result:=Str;
  32. end ;
  33. { StrAlloc allocates a buffer of Size + 4
  34. the size of the allocated buffer is stored at result - 4
  35. StrDispose should be used to destroy the buffer }
  36. function StrAlloc(Size: cardinal): PChar;
  37. begin
  38. inc(size,sizeof(cardinal));
  39. getmem(result,size);
  40. cardinal(pointer(result)^):=size;
  41. inc(result,sizeof(cardinal));
  42. end;
  43. { Allocates a new string using StrAlloc, you need StrDispose to dispose the
  44. string }
  45. function strnew(p : pchar) : pchar;
  46. var
  47. len : longint;
  48. begin
  49. Result:=nil;
  50. if (p=nil) or (p^=#0) then
  51. exit;
  52. len:=strlen(p)+1;
  53. Result:=StrAlloc(Len);
  54. if Result<>nil then
  55. strmove(Result,p,len);
  56. end;
  57. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  58. function StrPCopy(Dest: PChar; Source: string): PChar;
  59. begin
  60. result := StrMove(Dest, PChar(Source), length(Source)+1);
  61. end ;
  62. { StrPLCopy copies MaxLen or less characters from the pascal string
  63. Source to Dest and returns Dest }
  64. function StrPLCopy(Dest: PChar; Source: string; MaxLen: SizeUInt): PChar;
  65. var Count: SizeUInt;
  66. begin
  67. result := Dest;
  68. if (Result <> Nil) and (MaxLen <> 0) then
  69. begin
  70. Count := Length(Source);
  71. if Count > MaxLen then
  72. Count := MaxLen;
  73. StrMove(Dest, PChar(Source), Count);
  74. result[Count] := #0; { terminate ! }
  75. end ;
  76. end ;
  77. { StrDispose clears the memory allocated with StrAlloc }
  78. procedure StrDispose(Str: PChar);
  79. begin
  80. if (Str <> Nil) then
  81. begin
  82. dec(Str,sizeof(cardinal));
  83. Freemem(str,cardinal(pointer(str)^));
  84. end;
  85. end;
  86. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  87. function StrBufSize(Str: PChar): SizeUInt;
  88. begin
  89. if Str <> Nil then
  90. result := SizeUInt(pointer(Str - SizeOf(SizeUInt))^)-sizeof(SizeUInt)
  91. else
  92. result := 0;
  93. end ;