syspch.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. var
  30. l: Integer;
  31. begin
  32. l := StrLen(Str);
  33. SetLength(result, l);
  34. if l > 0 then
  35. Move(Str^, result[1], l);
  36. end ;
  37. { StrAlloc allocates a buffer of Size + 4
  38. the size of the allocated buffer is stored at result - 4
  39. StrDispose should be used to destroy the buffer }
  40. function StrAlloc(Size: cardinal): PChar;
  41. begin
  42. inc(size,sizeof(cardinal));
  43. getmem(result,size);
  44. cardinal(pointer(result)^):=size;
  45. inc(result,sizeof(cardinal));
  46. end;
  47. { Allocates a new string using StrAlloc, you need StrDispose to dispose the
  48. string }
  49. function strnew(p : pchar) : pchar;
  50. var
  51. len : longint;
  52. begin
  53. strnew:=nil;
  54. if (p=nil) or (p^=#0) then
  55. exit;
  56. len:=strlen(p)+1;
  57. StrNew:=StrAlloc(Len);
  58. if strnew<>nil then
  59. strmove(strnew,p,len);
  60. end;
  61. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  62. function StrPCopy(Dest: PChar; Source: string): PChar;
  63. begin
  64. result := StrMove(Dest, PChar(Source), length(Source)+1);
  65. end ;
  66. { StrPLCopy copies MaxLen or less characters from the pascal string
  67. Source to Dest and returns Dest }
  68. function StrPLCopy(Dest: PChar; Source: string; MaxLen: cardinal): PChar;
  69. var Count: cardinal;
  70. begin
  71. result := Dest;
  72. if (Result <> Nil) and (MaxLen <> 0) then begin
  73. Count := Length(Source);
  74. if Count > MaxLen then
  75. Count := MaxLen;
  76. StrMove(Dest, PChar(Source), Count);
  77. CharArray(result^)[Count] := #0; { terminate ! }
  78. end ;
  79. end ;
  80. { StrDispose clears the memory allocated with StrAlloc }
  81. procedure StrDispose(Str: PChar);
  82. begin
  83. if (Str <> Nil) then
  84. begin
  85. dec(Str,sizeof(cardinal));
  86. Freemem(str,cardinal(pointer(str)^));
  87. end;
  88. end;
  89. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  90. function StrBufSize(Str: PChar): cardinal;
  91. begin
  92. if Str <> Nil then
  93. result := cardinal(pointer(Str - SizeOf(cardinal))^)-sizeof(cardinal)
  94. else
  95. result := 0;
  96. end ;
  97. {
  98. $Log$
  99. Revision 1.4 2001-07-30 10:21:09 sg
  100. * Two moves with a string as target are now only exectuted if the number of
  101. bytes to move is greater than 0. This prevents RTE201's when compiled
  102. with range checks enabled.
  103. Revision 1.3 2000/11/23 11:04:26 sg
  104. * Protected some Move()'s by 'if' clauses so that the Move won't be
  105. executed when the length would be 0. Otherwise, the corresponding
  106. routines might get an RTE when compiled with $R+.
  107. Revision 1.2 2000/07/13 11:33:51 michael
  108. + removed logs
  109. }