syspch.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. type
  20. pbyte = ^byte;
  21. CharArray = array[0..0] of char;
  22. { Processor dependent part, shared withs strings unit }
  23. {$IFNDEF VIRTUALPASCAL} // in system there
  24. {$ifdef FPC_USE_LIBC}
  25. {$i cgenstr.inc}
  26. {$endif FPC_USE_LIBC}
  27. {$i strings.inc }
  28. {$ENDIF}
  29. { Read generic string functions that are not implemented for the processor }
  30. {$i genstr.inc}
  31. { Processor independent part, shared with strings unit }
  32. {$i stringsi.inc }
  33. { StrPas converts a PChar to a pascal string }
  34. function StrPas(Str: PChar): string;
  35. begin
  36. Result:=Str;
  37. end ;
  38. { StrAlloc allocates a buffer of Size + 4
  39. the size of the allocated buffer is stored at result - 4
  40. StrDispose should be used to destroy the buffer }
  41. function StrAlloc(Size: cardinal): PChar;
  42. begin
  43. inc(size,sizeof(cardinal));
  44. getmem(result,size);
  45. cardinal(pointer(result)^):=size;
  46. inc(result,sizeof(cardinal));
  47. end;
  48. { Allocates a new string using StrAlloc, you need StrDispose to dispose the
  49. string }
  50. function strnew(p : pchar) : pchar;
  51. var
  52. len : longint;
  53. begin
  54. Result:=nil;
  55. if (p=nil) or (p^=#0) then
  56. exit;
  57. len:=strlen(p)+1;
  58. Result:=StrAlloc(Len);
  59. if Result<>nil then
  60. strmove(Result,p,len);
  61. end;
  62. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  63. function StrPCopy(Dest: PChar; Source: string): PChar;
  64. begin
  65. result := StrMove(Dest, PChar(Source), length(Source)+1);
  66. end ;
  67. { StrPLCopy copies MaxLen or less characters from the pascal string
  68. Source to Dest and returns Dest }
  69. function StrPLCopy(Dest: PChar; Source: string; MaxLen: SizeUInt): PChar;
  70. var Count: SizeUInt;
  71. begin
  72. result := Dest;
  73. if (Result <> Nil) and (MaxLen <> 0) then begin
  74. Count := Length(Source);
  75. if Count > MaxLen then
  76. Count := MaxLen;
  77. StrMove(Dest, PChar(Source), Count);
  78. CharArray(result^)[Count] := #0; { terminate ! }
  79. end ;
  80. end ;
  81. { StrDispose clears the memory allocated with StrAlloc }
  82. procedure StrDispose(Str: PChar);
  83. begin
  84. if (Str <> Nil) then
  85. begin
  86. dec(Str,sizeof(cardinal));
  87. Freemem(str,cardinal(pointer(str)^));
  88. end;
  89. end;
  90. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  91. function StrBufSize(Str: PChar): SizeUInt;
  92. begin
  93. if Str <> Nil then
  94. result := SizeUInt(pointer(Str - SizeOf(SizeUInt))^)-sizeof(SizeUInt)
  95. else
  96. result := 0;
  97. end ;