syspch.inc 3.2 KB

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