syspch.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. begin
  38. inc(size,sizeof(cardinal));
  39. getmem(result,size);
  40. cardinal(pointer(result)^):=size;
  41. inc(result,sizeof(cardinal));
  42. end;
  43. { Allocates a new string using StrAlloc, you need StrDispose to dispose the
  44. string }
  45. function strnew(p : pchar) : pchar;
  46. var
  47. len : longint;
  48. begin
  49. strnew:=nil;
  50. if (p=nil) or (p^=#0) then
  51. exit;
  52. len:=strlen(p)+1;
  53. StrNew:=StrAlloc(Len);
  54. if strnew<>nil then
  55. strmove(strnew,p,len);
  56. end;
  57. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  58. function StrPCopy(Dest: PChar; Source: string): PChar;
  59. begin
  60. result := StrMove(Dest, PChar(Source), length(Source)+1);
  61. end ;
  62. { StrPLCopy copies MaxLen or less characters from the pascal string
  63. Source to Dest and returns Dest }
  64. function StrPLCopy(Dest: PChar; Source: string; MaxLen: cardinal): PChar;
  65. var Count: cardinal;
  66. begin
  67. result := Dest;
  68. if (Result <> Nil) and (MaxLen <> 0) then begin
  69. Count := Length(Source);
  70. if Count > MaxLen then
  71. Count := MaxLen;
  72. StrMove(Dest, PChar(Source), Count);
  73. CharArray(result^)[Count] := #0; { terminate ! }
  74. end ;
  75. end ;
  76. { StrDispose clears the memory allocated with StrAlloc }
  77. procedure StrDispose(Str: PChar);
  78. begin
  79. if (Str <> Nil) then
  80. begin
  81. dec(Str,sizeof(cardinal));
  82. Freemem(str,cardinal(pointer(str)^));
  83. end;
  84. end;
  85. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  86. function StrBufSize(Str: PChar): cardinal;
  87. begin
  88. if Str <> Nil then
  89. result := cardinal(pointer(Str - SizeOf(cardinal))^)-sizeof(cardinal)
  90. else
  91. result := 0;
  92. end ;
  93. {
  94. $Log$
  95. Revision 1.9 2000-02-09 16:59:32 peter
  96. * truncated log
  97. Revision 1.8 1999/12/10 15:02:12 peter
  98. * strnew is ofcourse also different between sysutils and strings, just
  99. like stralloc/strdispose.
  100. Revision 1.7 1999/11/06 14:41:31 peter
  101. * truncated log
  102. Revision 1.6 1999/08/24 13:14:50 peter
  103. * disposestr allocstr compatible with delphi
  104. }