syspch.inc 3.1 KB

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