syspch.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. {$IFNDEF VIRTUALPASCAL} // in system there
  25. {$ifdef FPC_USE_LIBC}
  26. {$i cgenstr.inc}
  27. {$endif FPC_USE_LIBC}
  28. {$i strings.inc }
  29. {$ENDIF}
  30. { Read generic string functions that are not implemented for the processor }
  31. {$i genstr.inc}
  32. { Processor independent part, shared with strings unit }
  33. {$i stringsi.inc }
  34. { StrPas converts a PChar to a pascal string }
  35. function StrPas(Str: PChar): string;
  36. begin
  37. Result:=Str;
  38. end ;
  39. { StrAlloc allocates a buffer of Size + 4
  40. the size of the allocated buffer is stored at result - 4
  41. StrDispose should be used to destroy the buffer }
  42. function StrAlloc(Size: cardinal): PChar;
  43. begin
  44. inc(size,sizeof(cardinal));
  45. getmem(result,size);
  46. cardinal(pointer(result)^):=size;
  47. inc(result,sizeof(cardinal));
  48. end;
  49. { Allocates a new string using StrAlloc, you need StrDispose to dispose the
  50. string }
  51. function strnew(p : pchar) : pchar;
  52. var
  53. len : longint;
  54. begin
  55. Result:=nil;
  56. if (p=nil) or (p^=#0) then
  57. exit;
  58. len:=strlen(p)+1;
  59. Result:=StrAlloc(Len);
  60. if Result<>nil then
  61. strmove(Result,p,len);
  62. end;
  63. { StrPCopy copies the pascal string Source to Dest and returns Dest }
  64. function StrPCopy(Dest: PChar; Source: string): PChar;
  65. begin
  66. result := StrMove(Dest, PChar(Source), length(Source)+1);
  67. end ;
  68. { StrPLCopy copies MaxLen or less characters from the pascal string
  69. Source to Dest and returns Dest }
  70. function StrPLCopy(Dest: PChar; Source: string; MaxLen: SizeUInt): PChar;
  71. var Count: SizeUInt;
  72. begin
  73. result := Dest;
  74. if (Result <> Nil) and (MaxLen <> 0) then begin
  75. Count := Length(Source);
  76. if Count > MaxLen then
  77. Count := MaxLen;
  78. StrMove(Dest, PChar(Source), Count);
  79. CharArray(result^)[Count] := #0; { terminate ! }
  80. end ;
  81. end ;
  82. { StrDispose clears the memory allocated with StrAlloc }
  83. procedure StrDispose(Str: PChar);
  84. begin
  85. if (Str <> Nil) then
  86. begin
  87. dec(Str,sizeof(cardinal));
  88. Freemem(str,cardinal(pointer(str)^));
  89. end;
  90. end;
  91. { StrBufSize returns the amount of memory allocated for pchar Str allocated with StrAlloc }
  92. function StrBufSize(Str: PChar): SizeUInt;
  93. begin
  94. if Str <> Nil then
  95. result := SizeUInt(pointer(Str - SizeOf(SizeUInt))^)-sizeof(SizeUInt)
  96. else
  97. result := 0;
  98. end ;
  99. {
  100. $Log$
  101. Revision 1.4 2004-08-09 16:44:23 jonas
  102. * use C versions of string routines if FPC_USE_LIBC is defined
  103. Revision 1.3 2004/05/01 23:55:18 peter
  104. * replace strlenint with sizeint
  105. Revision 1.2 2004/02/20 22:15:16 florian
  106. + x86_64 dependend sysutils part added
  107. * some 64 bit adaptions
  108. Revision 1.1 2003/10/06 21:01:06 peter
  109. * moved classes unit to rtl
  110. Revision 1.8 2003/09/06 21:52:24 marco
  111. * commited.
  112. Revision 1.7 2003/09/01 20:46:59 peter
  113. * small fixes for sparc
  114. Revision 1.6 2002/09/07 16:01:22 peter
  115. * old logs removed and tabs fixed
  116. Revision 1.5 2002/08/01 16:53:14 jonas
  117. * fix for StrPas() by Sergey Korshunoff <[email protected]> (merged)
  118. }