syspch.inc 2.8 KB

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