pcq.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. {
  2. This file is part of the Free Pascal run time library.
  3. A file in Amiga system run time library.
  4. Copyright (c) 2002-2003 by Nils Sjoholm
  5. member of the Amiga RTL development team.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$I useamigasmartlink.inc}
  13. {$ifdef use_amiga_smartlink}
  14. {$smartlink on}
  15. {$endif use_amiga_smartlink}
  16. unit pcq;
  17. {
  18. A unit to help port program from pcq pascal.
  19. These are some of the common C pchar functions.
  20. Changed a few of the functions.
  21. ToUpper,
  22. ToLower,
  23. strnieq,
  24. strieq,
  25. strnieq,
  26. stricmp
  27. and strnicmp
  28. They all use the utility.library for the checking or
  29. the conversion. The utility.library is opened by all
  30. programs as of version 1.3 of PCQ, so you don't need
  31. to do that.
  32. THIS IS CHANGED!
  33. Looks like the strcompare functions in utility and locale
  34. is buggy so I have redone this functions to use an
  35. internal strcompare instead.
  36. Added the define use_amiga_smartlink.
  37. 13 Jan 2003.
  38. Changed integer > smallint.
  39. 10 Feb 2003.
  40. Nils Sjoholm < [email protected]
  41. }
  42. interface
  43. uses exec,strings;
  44. function CheckBreak: boolean;
  45. Function isupper(c : Char) : Boolean;
  46. {
  47. Returns True if the character is in A..Z
  48. }
  49. Function islower(c : Char) : Boolean;
  50. {
  51. Returns True if the character is in a..z
  52. }
  53. Function isalpha(c : Char) : Boolean;
  54. {
  55. Returns True if the character is in A..Z or a..z
  56. }
  57. Function isdigit(c : Char) : Boolean;
  58. {
  59. Returns True if the character is in 0..9
  60. }
  61. Function isalnum(c : Char) : Boolean;
  62. {
  63. Returns True if isalpha or isdigit is true
  64. }
  65. Function isspace(c : Char) : Boolean;
  66. {
  67. Returns true if the character is "white space", like a space,
  68. form feed, line feed, carraige return, tab, whatever.
  69. }
  70. Function toupper(c : Char) : Char;
  71. {
  72. If the character is in a..z, the function returns the capital.
  73. Otherwise it returns c. Not true, this function use the utility.library
  74. to make the conversion.
  75. }
  76. Function tolower(c : Char) : Char;
  77. {
  78. If c is in A..Z, the function returns the lower case letter.
  79. Otherwise it returns c. Not true this function use the utility.library
  80. to make the conversion.
  81. }
  82. function lowercase(c : char) : char;
  83. {
  84. If the character is in a..z, the function returns the capital.
  85. Otherwise it returns c. Not true, this function use the utility.library
  86. to make the conversion.
  87. }
  88. function lowercase(c : pchar): pchar;
  89. {
  90. Will turn the pchar till lowercase.
  91. }
  92. function uppercase(c : char): char;
  93. {
  94. If the character is in a..z, the function returns the capital.
  95. Otherwise it returns c. Not true, this function use the utility.library
  96. to make the conversion.
  97. }
  98. function uppercase(c: pchar): pchar;
  99. {
  100. Will turn the pchar till capital letters.
  101. }
  102. Function streq(s1, s2 : pchar) : Boolean;
  103. {
  104. Returns True if s1 and s2 are the same.
  105. }
  106. Function strneq(s1, s2 : pchar; n : longint) : Boolean;
  107. {
  108. Returns True if the first n characters of s1 and s2 are identical.
  109. }
  110. Function strieq(s1, s2 : pchar) : Boolean;
  111. {
  112. The same as streq(), but is case insensitive.
  113. }
  114. Function strnieq(s1, s2 : pchar; n : longint) : Boolean;
  115. {
  116. The same as strneq(), but case insensitive.
  117. }
  118. Function strcmp(s1, s2 : pchar) : longint;
  119. {
  120. Returns an longint < 0 if s1 < s2, zero if they are equal, and > 0
  121. if s1 > s2.
  122. }
  123. Function stricmp(s1, s2 : pchar) : longint;
  124. {
  125. The same as strcmp, but not case sensitive
  126. }
  127. Function strncmp(s1, s2 : pchar; n : longint) : longint;
  128. {
  129. Same as strcmp(), but only considers the first n characters.
  130. }
  131. Function strnicmp(s1, s2 : pchar; n : longint) : longint;
  132. {
  133. Same as strncmp, but not case sensitive
  134. }
  135. Procedure strcpy(s1, s2 : pchar);
  136. {
  137. Copies s2 into s1, appending a trailing zero. This is the same
  138. as C, but opposite from 1.0.
  139. }
  140. Procedure strncpy(s1, s2 : pchar; n : smallint);
  141. {
  142. Copies s2 into s1, with a maximum of n characters. Appends a
  143. trailing zero.
  144. }
  145. Procedure strncat(s1, s2 : pchar; n : smallint);
  146. {
  147. Appends at most n characters from s2 onto s1.
  148. }
  149. Function strdup(s : pchar) : pchar;
  150. {
  151. This allocates a copy of the pchar 's', and returns a ptr
  152. }
  153. Function strpos(s1 : pchar; c : Char) : longint;
  154. {
  155. Return the position, starting at zero, of the first (leftmost)
  156. occurance of c in s1. If there is no c, it returns -1.
  157. }
  158. Function strrpos(s1 : pchar; c : Char) : longint;
  159. {
  160. Returns the longint position of the right-most occurance of c in s1.
  161. If c is not in s1, it returns -1.
  162. }
  163. Function AllocString(l : longint) : pchar;
  164. {
  165. Allocates l bytes, and returns a pointer to the allocated memory.
  166. This memory is allocated through the new() function, so it will be returned
  167. to the system at the end of your program. Note that the proper amount of RAM
  168. to allocate is strlen(s) + 1.
  169. }
  170. Procedure FreeString(s : pchar);
  171. {
  172. This returns memory allocated by AllocString to the system. Since
  173. the Amiga is a multitasking computer, you should always return memory you
  174. don't need to the system.
  175. }
  176. implementation
  177. const
  178. SIGBREAKF_CTRL_C = $1000;
  179. function CheckBreak: boolean;
  180. begin
  181. { check for Ctrl-C break by user }
  182. if (Setsignal(0,0) AND SIGBREAKF_CTRL_C) <> 0 then Begin
  183. SetSignal(0,SIGBREAKF_CTRL_C);
  184. CheckBreak := true;
  185. end else CheckBreak := false;
  186. end;
  187. Function isupper(c : Char) : Boolean;
  188. begin
  189. if ((ord(c) >= 192) and (ord(c) <= 223)) or ((c >= 'A') and (c <= 'Z'))
  190. then isupper := true
  191. else isupper := false;
  192. end;
  193. Function islower(c : Char) : Boolean;
  194. begin
  195. if ((ord(c) >= 224) and (ord(c) <= 254)) or ((c >= 'a') and (c <= 'z'))
  196. then islower := true
  197. else islower := false;
  198. end;
  199. Function isalpha(c : Char) : Boolean;
  200. begin
  201. if ((ord(c) >= 192) and (ord(c) <= 223)) or ((c >= 'A') and (c <= 'Z'))
  202. or ((ord(c) >= 224) and (ord(c) <= 254)) or ((c >= 'a') and (c <= 'z'))
  203. then isalpha := true
  204. else isalpha := false;
  205. end;
  206. Function isdigit(c : Char) : Boolean;
  207. begin
  208. if c in ['0'..'9'] then isdigit := true
  209. else isdigit := false;
  210. end;
  211. Function isalnum(c : Char) : Boolean;
  212. begin
  213. if isalpha(c) or isdigit(c) then isalnum := true
  214. else isalnum := false;
  215. end;
  216. Function isspace(c : Char) : Boolean;
  217. begin
  218. if c in [#9..#13,#32] then isspace := true
  219. else isspace := false;
  220. end;
  221. Function toupper(c : Char) : Char;
  222. begin
  223. if ((ord(c) >= 224) and (ord(c) <= 254)) or ((c >= 'a') and (c <= 'z'))
  224. then c := char(ord(c)-32);
  225. toupper := c;
  226. end;
  227. Function tolower(c : Char) : Char;
  228. begin
  229. if ((ord(c) >= 192) and (ord(c) <= 223)) or ((c >= 'A') and (c <= 'Z'))
  230. then c := char(ord(c)+32);
  231. tolower := c;
  232. end;
  233. function lowercase(c : char) : char;
  234. begin
  235. lowercase := tolower(c);
  236. end;
  237. function lowercase(c : pchar): pchar;
  238. var
  239. i : longint;
  240. begin
  241. i := 0;
  242. while c[i] <> #0 do begin
  243. c[i] := tolower(c[i]);
  244. i := succ(i);
  245. end;
  246. lowercase := c;
  247. end;
  248. function uppercase(c : char): char;
  249. begin
  250. uppercase := toupper(c);
  251. end;
  252. function uppercase(c: pchar): pchar;
  253. var
  254. i : longint;
  255. begin
  256. i := 0;
  257. while c[i] <> #0 do begin
  258. c[i] := toupper(c[i]);
  259. i := succ(i);
  260. end;
  261. uppercase := c;
  262. end;
  263. Function streq(s1, s2 : pchar) : Boolean;
  264. begin
  265. streq := (strcomp(s1,s2) = 0);
  266. end;
  267. Function strneq(s1, s2 : pchar; n : longint) : Boolean;
  268. begin
  269. strneq := (strlcomp(s1,s2,n) = 0);
  270. end;
  271. Function strieq(s1, s2 : pchar) : Boolean;
  272. begin
  273. s1 := uppercase(s1);
  274. s2 := uppercase(s2);
  275. strieq := (strcomp(s1,s2)=0);
  276. end;
  277. Function strnieq(s1, s2 : pchar; n : longint) : Boolean;
  278. begin
  279. s1 := uppercase(s1);
  280. s2 := uppercase(s2);
  281. strnieq := (strlcomp(s1,s2,n)=0);
  282. end;
  283. Function strcmp(s1, s2 : pchar) : longint;
  284. begin
  285. strcmp := strcomp(s1,s2);
  286. end;
  287. Function stricmp(s1, s2 : pchar) : longint;
  288. begin
  289. s1 := uppercase(s1);
  290. s2 := uppercase(s2);
  291. stricmp := strcomp(s1,s2);
  292. end;
  293. Function strncmp(s1, s2 : pchar; n : longint) : longint;
  294. begin
  295. strncmp := strlcomp(s1,s2,n);
  296. end;
  297. Function strnicmp(s1, s2 : pchar; n : longint) : longint;
  298. begin
  299. s1 := uppercase(s1);
  300. s2 := uppercase(s2);
  301. strnicmp := strlcomp(s1,s2,n);
  302. end;
  303. Procedure strcpy(s1, s2 : pchar);
  304. begin
  305. strcopy(s1,s2)
  306. end;
  307. Procedure strncpy(s1, s2 : pchar; n : smallint);
  308. begin
  309. strlcopy(s1,s2,n);
  310. end;
  311. Procedure strncat(s1, s2 : pchar; n : smallint);
  312. begin
  313. strlcat(s1,s2,n);
  314. end;
  315. Function strdup(s : pchar) : pchar;
  316. begin
  317. strdup := StrNew(s);
  318. end;
  319. Function strpos(s1 : pchar; c : Char) : longint;
  320. Var
  321. count: Longint;
  322. Begin
  323. count := 0;
  324. { As in Borland Pascal , if looking for NULL return null }
  325. if c = #0 then
  326. begin
  327. strpos := -1;
  328. exit;
  329. end;
  330. { Find first matching character of Ch in Str }
  331. while S1[count] <> #0 do
  332. begin
  333. if C = S1[count] then
  334. begin
  335. strpos := count;
  336. exit;
  337. end;
  338. Inc(count);
  339. end;
  340. { nothing found. }
  341. strpos := -1;
  342. end;
  343. Function strrpos(s1 : pchar; c : Char) : longint;
  344. Var
  345. count: Longint;
  346. index: Longint;
  347. Begin
  348. count := Strlen(S1);
  349. { As in Borland Pascal , if looking for NULL return null }
  350. if c = #0 then
  351. begin
  352. strrpos := -1;
  353. exit;
  354. end;
  355. Dec(count);
  356. for index := count downto 0 do
  357. begin
  358. if C = S1[index] then
  359. begin
  360. strrpos := index;
  361. exit;
  362. end;
  363. end;
  364. { nothing found. }
  365. strrpos := -1;
  366. end;
  367. Function AllocString(l : longint) : pchar;
  368. begin
  369. AllocString := StrAlloc(l);
  370. end;
  371. Procedure FreeString(s : pchar);
  372. begin
  373. StrDispose(s);
  374. end;
  375. end.
  376. {
  377. $Log$
  378. Revision 1.3 2003-02-10 17:59:46 nils
  379. * fixes for delphi mode
  380. Revision 1.2 2003/01/13 18:14:56 nils
  381. * added the define use_amiga_smartlink
  382. Revision 1.1 2002/11/22 21:34:59 nils
  383. * initial release
  384. }