syspch.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. { PChar functions }
  20. type
  21. pbyte = ^byte;
  22. CharArray = array[0..0] of char;
  23. { Processor dependent part, shared withs strings unit }
  24. {$i strings.inc }
  25. { Processor independent part, shared with strings unit }
  26. {$i stringsi.inc }
  27. { StrPas converts a PChar to a pascal string }
  28. function StrPas(Str: PChar): string;
  29. begin
  30. SetLength(result, StrLen(Str));
  31. Move(Str^, result[1], Length(result));
  32. end ;
  33. { StrAlloc allocates a buffer of Size + 4
  34. the size of the allocated buffer is stored at result - 4
  35. StrDispose should be used to destroy the buffer }
  36. function StrAlloc(Size: cardinal): PChar;
  37. var Temp: pointer;
  38. begin
  39. GetMem(Temp, Size + SizeOf(cardinal));
  40. Move(Size, Temp^, SizeOf(cardinal));
  41. pbyte(Temp + SizeOf(cardinal))^ := 0;
  42. result := PChar(Temp + SizeOf(cardinal));
  43. end ;
  44. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  45. function StrPCopy(Dest: PChar; Source: string): PChar;
  46. begin
  47. result := StrMove(Dest, PChar(Source), length(Source));
  48. end ;
  49. { StrPLCopy copies MaxLen or less characters from the pascal string
  50. Source to Dest and returns Dest }
  51. function StrPLCopy(Dest: PChar; Source: string; MaxLen: cardinal): PChar;
  52. var Count: cardinal;
  53. begin
  54. result := Dest;
  55. if (Result <> Nil) and (MaxLen <> 0) then begin
  56. Count := Length(Source);
  57. if Count > MaxLen then
  58. Count := MaxLen;
  59. StrMove(Dest, PChar(Source), Count);
  60. CharArray(result^)[Count] := #0; { terminate ! }
  61. end ;
  62. end ;
  63. { StrDispose clears the memory allocated with StrAlloc }
  64. procedure StrDispose(var Str: PChar);
  65. var Size: cardinal;
  66. begin
  67. if (Str <> Nil) then begin
  68. Str := PChar(Str - SizeOf(cardinal));
  69. Move(Str^, Size, SizeOf(cardinal));
  70. FreeMem(Str, Size + SizeOf(cardinal));
  71. Str := Nil;
  72. end ;
  73. end ;
  74. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  75. function StrBufSize(var Str: PChar): cardinal;
  76. begin
  77. if Str <> Nil then
  78. result := Cardinal(pointer(Str - SizeOf(cardinal))^)
  79. else
  80. result := 0;
  81. end ;
  82. {
  83. $Log$
  84. Revision 1.4 1999-02-25 07:39:57 michael
  85. * Joined strings and sysutils
  86. Revision 1.3 1999/02/10 22:15:11 michael
  87. + Changed to ansistrings
  88. Revision 1.2 1998/09/16 08:28:40 michael
  89. Update from gertjan Schouten, plus small fix for linux
  90. 1998/08/26 Gertjan
  91. Most functions rewritten in pascal.
  92. Revision 1.1 1998/04/10 15:17:46 michael
  93. + Initial implementation; Donated by Gertjan Schouten
  94. His file was split into several files, to keep it a little bit structured.
  95. }