syspch.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. function NewStr(s:string):pchar;
  21. var p:pchar;l:longint;
  22. begin
  23. l := length(s);
  24. p := StrAlloc(l + 1);
  25. move(s[1], p^, l);
  26. byte(pchar(p + l)^) := 0;
  27. NewStr := p;
  28. end ;
  29. function StrAlloc(Size:longint):pchar;
  30. var p:pointer;
  31. begin
  32. Getmem(p, size + sizeof(longint));
  33. Move(Size, p^, sizeof(longint));
  34. pbyte(p + sizeof(longint))^ := 0;
  35. StrAlloc := pchar(p + sizeof(longint));
  36. end ;
  37. procedure StrDispose(var p:pchar);
  38. var l:longint;
  39. begin
  40. if (p = nil) then exit;
  41. p := pchar(p - sizeof(longint));
  42. move(p^, l, sizeof(longint));
  43. freemem(p, l + sizeof(longint));
  44. p := nil;
  45. end ;
  46. function StrPas(p:pchar):string;
  47. begin
  48. asm
  49. movl P,%eax
  50. movl %eax,%esi
  51. movl __RESULT,%eax
  52. movl %eax,%edi
  53. pushl %edi
  54. incl %edi
  55. xorl %eax,%eax
  56. movw $255,%cx
  57. STR_LOOP1:
  58. lodsb
  59. orb %al,%al
  60. jz STR_END
  61. stosb
  62. loop STR_LOOP1
  63. STR_END:
  64. popl %edi
  65. movw $255,%ax
  66. subw %cx,%ax
  67. movb %al,(%edi)
  68. end ;
  69. end ;
  70. function StrLen(p:pchar):longint;
  71. begin
  72. asm
  73. movl p,%eax
  74. movl %eax,%esi
  75. xorl %eax,%eax
  76. movl $0xFFFFFFFF,%ecx
  77. STRLEN_LOOP:
  78. incl %ecx
  79. lodsb
  80. orb %al,%al
  81. jnz STRLEN_LOOP
  82. movl %ecx,__RESULT
  83. end ;
  84. end ;
  85. function StrEnd(p:pchar):pchar;
  86. begin
  87. asm
  88. movl p,%eax
  89. movl %eax,%esi
  90. STREND_LOOP:
  91. lodsb
  92. orb %al,%al
  93. jnz STREND_LOOP
  94. movl %esi,__RESULT
  95. end ;
  96. end ;
  97. function StrMove(Dest, Source: PChar; Count: longint): PChar;
  98. begin
  99. asm
  100. movl source,%eax
  101. movl %eax,%esi
  102. movl dest,%eax
  103. movl %eax,%edi
  104. movl %edi,__RESULT
  105. movl COUNT,%ecx
  106. movl %ecx,%edx
  107. cmpl %esi,%edi
  108. jg STRMOVE_BACK
  109. shrl $2,%ecx
  110. rep
  111. movsl
  112. movl %edx,%ecx
  113. andl $3,%ecx
  114. rep
  115. movsb
  116. jmp STRMOVE_END
  117. STRMOVE_BACK:
  118. addl %ecx,%edi
  119. decl %edi
  120. addl %ecx,%esi
  121. decl %esi
  122. andl $3,%ecx
  123. STD
  124. rep
  125. movsb
  126. subl $3,%esi
  127. subl $3,%edi
  128. movl %edx,%ecx
  129. shrl $2,%ecx
  130. rep
  131. movsl
  132. CLD
  133. STRMOVE_END:
  134. end ;
  135. end ;
  136. function StrCat(Dest, Source: PChar): PChar;
  137. begin
  138. StrCat := Dest;
  139. while char(dest^) <> #0 do
  140. dest := dest + 1;
  141. while char(source^) <> #0 do begin
  142. char(dest^) := char(source^);
  143. dest := dest + 1;
  144. source := source + 1;
  145. end ;
  146. char(dest^) := #0;
  147. end ;
  148. function StrCat(Dest:pchar; Source: string): PChar;
  149. var l:longint;
  150. begin
  151. StrCat := Dest;
  152. while char(dest^) <> #0 do
  153. dest := dest + 1;
  154. l := length(source);
  155. move(source[1], dest^, l);
  156. dest := dest + l;
  157. char(dest^) := #0;
  158. end ;
  159. function StrCat(var Dest:string; Source: pchar): String;
  160. var count,l:longint;
  161. begin
  162. l := length(Dest);
  163. count := setLength(Dest, l + StrLen(Source)) - l;
  164. if (count > 0) then
  165. move(source^, dest[l + 1], count);
  166. StrCat := Dest;
  167. end ;
  168. function StrIns(Dest:pchar; Source: string): PChar;
  169. var len:longint;
  170. begin
  171. len := length(Source);
  172. StrMove(dest + len, dest, 1 + strlen(dest));
  173. Move(Source[1], dest^, len);
  174. StrIns := dest;
  175. end ;
  176. function StrCopy(Dest, Source: PChar): Pchar;
  177. begin
  178. asm
  179. movl Dest,%eax
  180. movl %eax,%edi
  181. movl %eax,__RESULT
  182. movl Source,%eax
  183. movl %eax,%esi
  184. STRCOPY_LOOP:
  185. lodsb
  186. stosb
  187. orb %al,%al
  188. jnz STRCOPY_LOOP
  189. end ;
  190. end ;
  191. function StrLCopy(Dest, Source: PChar; MaxLen: longint): PChar;
  192. begin
  193. asm
  194. movl Dest,%eax
  195. movl %eax,__RESULT
  196. movl %eax,%edi
  197. movl Source,%eax
  198. movl %eax,%esi
  199. movl MaxLen,%ecx
  200. orl %ecx,%ecx
  201. jz STRLCOPY_END
  202. STRLCOPY_LOOP:
  203. lodsb
  204. orb %al,%al
  205. jz STRLCOPY_END
  206. stosb
  207. loop STRLCOPY_LOOP
  208. STRLCOPY_END:
  209. xorb %al,%al
  210. stosb
  211. end ;
  212. end ;
  213. function StrScan(str:pchar;ch:char):pchar;
  214. begin
  215. asm
  216. movl str,%eax
  217. movl %eax,%esi
  218. movb ch,%bl
  219. STRSCAN_LOOP:
  220. lodsb
  221. cmpb %al,%bl
  222. je STRSCAN_END
  223. orb %al,%al
  224. jnz STRSCAN_LOOP
  225. STRSCAN_END:
  226. decl %esi
  227. movl %esi,__RESULT
  228. end ;
  229. end ;
  230. function StrRScan(str:pchar;ch:char):pchar;
  231. begin
  232. asm
  233. movl str,%eax
  234. movl %eax,%esi
  235. movl %eax,%edx
  236. movb ch,%bl
  237. STRRSCAN_LOOP:
  238. lodsb
  239. cmpb %al,%bl
  240. jne STRRSCAN_NOTFOUND
  241. movl %esi,%edx
  242. decl %edx
  243. STRRSCAN_NOTFOUND:
  244. orb %al,%al
  245. jnz STRRSCAN_LOOP
  246. movl %edx,__RESULT
  247. end ;
  248. end ;
  249. function StrTer(str:pchar;l:longint):pchar;
  250. begin
  251. asm
  252. movl str,%eax
  253. movl %eax,__RESULT
  254. addl l,%eax
  255. movl %eax,%edi
  256. xorb %al,%al
  257. movb %al,(%edi)
  258. end ;
  259. end ;
  260. {
  261. $Log$
  262. Revision 1.1 1998-04-10 15:17:46 michael
  263. + Initial implementation; Donated by Gertjan Schouten
  264. His file was split into several files, to keep it a little bit structured.
  265. }