syspch.inc 2.7 KB

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