cutils.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 by Florian Klaempfl
  4. This unit implements some support functions
  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. }
  18. unit cutils;
  19. {$i defines.inc}
  20. interface
  21. {$ifdef delphi}
  22. type
  23. dword = cardinal;
  24. qword = int64;
  25. {$endif}
  26. type
  27. pstring = ^string;
  28. function min(a,b : longint) : longint;
  29. function max(a,b : longint) : longint;
  30. function align(i,a:longint):longint;
  31. function align_from_size(datasize:longint;length:longint):longint;
  32. procedure Replace(var s:string;s1:string;const s2:string);
  33. procedure ReplaceCase(var s:string;const s1,s2:string);
  34. function upper(const s : string) : string;
  35. function lower(const s : string) : string;
  36. function trimspace(const s:string):string;
  37. procedure uppervar(var s : string);
  38. function hexstr(val : longint;cnt : byte) : string;
  39. function tostru(i:cardinal) : string;
  40. function tostr(i : longint) : string;
  41. function int64tostr(i : int64) : string;
  42. function tostr_with_plus(i : longint) : string;
  43. procedure valint(S : string;var V : longint;var code : integer);
  44. function is_number(const s : string) : boolean;
  45. function ispowerof2(value : longint;var power : longint) : boolean;
  46. { releases the string p and assignes nil to p }
  47. { if p=nil then freemem isn't called }
  48. procedure stringdispose(var p : pstring);
  49. { allocates mem for a copy of s, copies s to this mem and returns }
  50. { a pointer to this mem }
  51. function stringdup(const s : string) : pstring;
  52. { allocates memory for s and copies s as zero terminated string
  53. to that mem and returns a pointer to that mem }
  54. function strpnew(const s : string) : pchar;
  55. procedure strdispose(var p : pchar);
  56. { makes a char lowercase, with spanish, french and german char set }
  57. function lowercase(c : char) : char;
  58. { makes zero terminated string to a pascal string }
  59. { the data in p is modified and p is returned }
  60. function pchar2pstring(p : pchar) : pstring;
  61. { ambivalent to pchar2pstring }
  62. function pstring2pchar(p : pstring) : pchar;
  63. { Speed/Hash value }
  64. function getspeedvalue(const s : string) : longint;
  65. { Ansistring (pchar+length) support }
  66. procedure ansistringdispose(var p : pchar;length : longint);
  67. function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
  68. function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
  69. {*****************************************************************************
  70. File Functions
  71. *****************************************************************************}
  72. function DeleteFile(const fn:string):boolean;
  73. implementation
  74. uses
  75. {$ifdef delphi}
  76. sysutils
  77. {$else}
  78. strings
  79. {$endif}
  80. ;
  81. var
  82. uppertbl,
  83. lowertbl : array[char] of char;
  84. function min(a,b : longint) : longint;
  85. {
  86. return the minimal of a and b
  87. }
  88. begin
  89. if a>b then
  90. min:=b
  91. else
  92. min:=a;
  93. end;
  94. function max(a,b : longint) : longint;
  95. {
  96. return the maximum of a and b
  97. }
  98. begin
  99. if a<b then
  100. max:=b
  101. else
  102. max:=a;
  103. end;
  104. function align_from_size(datasize:longint;length:longint):longint;
  105. {Increases the datasize with the required alignment; i.e. on pentium
  106. words should be aligned word; and dwords should be aligned dword.
  107. So for a word (len=2), datasize is increased to the nearest multiple
  108. of 2, and for len=4, datasize is increased to the nearest multiple of
  109. 4.}
  110. var data_align:word;
  111. begin
  112. {$IFDEF I386}
  113. if length>2 then
  114. data_align:=4
  115. else if length>1 then
  116. data_align:=2
  117. else
  118. data_align:=1;
  119. {$ENDIF}
  120. {$IFDEF M68K}
  121. data_align:=2;
  122. {$ENDIF}
  123. align_from_size:=(datasize+data_align-1) and not(data_align-1);
  124. end;
  125. function align(i,a:longint):longint;
  126. {
  127. return value <i> aligned <a> boundary
  128. }
  129. begin
  130. { for 0 and 1 no aligning is needed }
  131. if a<=1 then
  132. align:=i
  133. else
  134. align:=(i+a-1) and not(a-1);
  135. end;
  136. procedure Replace(var s:string;s1:string;const s2:string);
  137. var
  138. last,
  139. i : longint;
  140. begin
  141. s1:=upper(s1);
  142. last:=0;
  143. repeat
  144. i:=pos(s1,upper(s));
  145. if i=last then
  146. i:=0;
  147. if (i>0) then
  148. begin
  149. Delete(s,i,length(s1));
  150. Insert(s2,s,i);
  151. last:=i;
  152. end;
  153. until (i=0);
  154. end;
  155. procedure ReplaceCase(var s:string;const s1,s2:string);
  156. var
  157. last,
  158. i : longint;
  159. begin
  160. last:=0;
  161. repeat
  162. i:=pos(s1,s);
  163. if i=last then
  164. i:=0;
  165. if (i>0) then
  166. begin
  167. Delete(s,i,length(s1));
  168. Insert(s2,s,i);
  169. last:=i;
  170. end;
  171. until (i=0);
  172. end;
  173. function upper(const s : string) : string;
  174. {
  175. return uppercased string of s
  176. }
  177. var
  178. i : longint;
  179. begin
  180. for i:=1 to length(s) do
  181. upper[i]:=uppertbl[s[i]];
  182. upper[0]:=s[0];
  183. end;
  184. function lower(const s : string) : string;
  185. {
  186. return lowercased string of s
  187. }
  188. var
  189. i : longint;
  190. begin
  191. for i:=1 to length(s) do
  192. lower[i]:=lowertbl[s[i]];
  193. lower[0]:=s[0];
  194. end;
  195. procedure uppervar(var s : string);
  196. {
  197. uppercase string s
  198. }
  199. var
  200. i : longint;
  201. begin
  202. for i:=1 to length(s) do
  203. s[i]:=uppertbl[s[i]];
  204. end;
  205. procedure initupperlower;
  206. var
  207. c : char;
  208. begin
  209. for c:=#0 to #255 do
  210. begin
  211. lowertbl[c]:=c;
  212. uppertbl[c]:=c;
  213. case c of
  214. 'A'..'Z' :
  215. lowertbl[c]:=char(byte(c)+32);
  216. 'a'..'z' :
  217. uppertbl[c]:=char(byte(c)-32);
  218. end;
  219. end;
  220. end;
  221. function hexstr(val : longint;cnt : byte) : string;
  222. const
  223. HexTbl : array[0..15] of char='0123456789ABCDEF';
  224. var
  225. i : longint;
  226. begin
  227. hexstr[0]:=char(cnt);
  228. for i:=cnt downto 1 do
  229. begin
  230. hexstr[i]:=hextbl[val and $f];
  231. val:=val shr 4;
  232. end;
  233. end;
  234. function tostru(i:cardinal):string;
  235. {
  236. return string of value i, but for cardinals
  237. }
  238. var
  239. hs : string;
  240. begin
  241. str(i,hs);
  242. tostru:=hs;
  243. end;
  244. function trimspace(const s:string):string;
  245. {
  246. return s with all leading and ending spaces and tabs removed
  247. }
  248. var
  249. i,j : longint;
  250. begin
  251. i:=length(s);
  252. while (i>0) and (s[i] in [#9,' ']) do
  253. dec(i);
  254. j:=1;
  255. while (j<i) and (s[j] in [#9,' ']) do
  256. inc(j);
  257. trimspace:=Copy(s,j,i-j+1);
  258. end;
  259. function tostr(i : longint) : string;
  260. {
  261. return string of value i
  262. }
  263. var
  264. hs : string;
  265. begin
  266. str(i,hs);
  267. tostr:=hs;
  268. end;
  269. function int64tostr(i : int64) : string;
  270. {
  271. return string of value i
  272. }
  273. var
  274. hs : string;
  275. begin
  276. str(i,hs);
  277. int64tostr:=hs;
  278. end;
  279. function tostr_with_plus(i : longint) : string;
  280. {
  281. return string of value i, but always include a + when i>=0
  282. }
  283. var
  284. hs : string;
  285. begin
  286. str(i,hs);
  287. if i>=0 then
  288. tostr_with_plus:='+'+hs
  289. else
  290. tostr_with_plus:=hs;
  291. end;
  292. procedure valint(S : string;var V : longint;var code : integer);
  293. {
  294. val() with support for octal, which is not supported under tp7
  295. }
  296. {$ifndef FPC}
  297. var
  298. vs : longint;
  299. c : byte;
  300. begin
  301. if s[1]='%' then
  302. begin
  303. vs:=0;
  304. longint(v):=0;
  305. for c:=2 to length(s) do
  306. begin
  307. if s[c]='0' then
  308. vs:=vs shl 1
  309. else
  310. if s[c]='1' then
  311. vs:=vs shl 1+1
  312. else
  313. begin
  314. code:=c;
  315. exit;
  316. end;
  317. end;
  318. code:=0;
  319. longint(v):=vs;
  320. end
  321. else
  322. system.val(S,V,code);
  323. end;
  324. {$else not FPC}
  325. begin
  326. system.val(S,V,code);
  327. end;
  328. {$endif not FPC}
  329. function is_number(const s : string) : boolean;
  330. {
  331. is string a correct number ?
  332. }
  333. var
  334. w : integer;
  335. l : longint;
  336. begin
  337. valint(s,l,w);
  338. is_number:=(w=0);
  339. end;
  340. function ispowerof2(value : longint;var power : longint) : boolean;
  341. {
  342. return if value is a power of 2. And if correct return the power
  343. }
  344. var
  345. hl : longint;
  346. i : longint;
  347. begin
  348. hl:=1;
  349. ispowerof2:=true;
  350. for i:=0 to 31 do
  351. begin
  352. if hl=value then
  353. begin
  354. power:=i;
  355. exit;
  356. end;
  357. hl:=hl shl 1;
  358. end;
  359. ispowerof2:=false;
  360. end;
  361. function pchar2pstring(p : pchar) : pstring;
  362. var
  363. w,i : longint;
  364. begin
  365. w:=strlen(p);
  366. for i:=w-1 downto 0 do
  367. p[i+1]:=p[i];
  368. p[0]:=chr(w);
  369. pchar2pstring:=pstring(p);
  370. end;
  371. function pstring2pchar(p : pstring) : pchar;
  372. var
  373. w,i : longint;
  374. begin
  375. w:=length(p^);
  376. for i:=1 to w do
  377. p^[i-1]:=p^[i];
  378. p^[w]:=#0;
  379. pstring2pchar:=pchar(p);
  380. end;
  381. function lowercase(c : char) : char;
  382. begin
  383. case c of
  384. #65..#90 : c := chr(ord (c) + 32);
  385. #154 : c:=#129; { german }
  386. #142 : c:=#132; { german }
  387. #153 : c:=#148; { german }
  388. #144 : c:=#130; { french }
  389. #128 : c:=#135; { french }
  390. #143 : c:=#134; { swedish/norge (?) }
  391. #165 : c:=#164; { spanish }
  392. #228 : c:=#229; { greek }
  393. #226 : c:=#231; { greek }
  394. #232 : c:=#227; { greek }
  395. end;
  396. lowercase := c;
  397. end;
  398. function strpnew(const s : string) : pchar;
  399. var
  400. p : pchar;
  401. begin
  402. getmem(p,length(s)+1);
  403. strpcopy(p,s);
  404. strpnew:=p;
  405. end;
  406. procedure strdispose(var p : pchar);
  407. begin
  408. if assigned(p) then
  409. begin
  410. freemem(p,strlen(p)+1);
  411. p:=nil;
  412. end;
  413. end;
  414. procedure stringdispose(var p : pstring);
  415. begin
  416. if assigned(p) then
  417. freemem(p,length(p^)+1);
  418. p:=nil;
  419. end;
  420. function stringdup(const s : string) : pstring;
  421. var
  422. p : pstring;
  423. begin
  424. getmem(p,length(s)+1);
  425. p^:=s;
  426. stringdup:=p;
  427. end;
  428. {*****************************************************************************
  429. GetSpeedValue
  430. *****************************************************************************}
  431. var
  432. Crc32Tbl : array[0..255] of longint;
  433. procedure MakeCRC32Tbl;
  434. var
  435. crc : longint;
  436. i,n : byte;
  437. begin
  438. for i:=0 to 255 do
  439. begin
  440. crc:=i;
  441. for n:=1 to 8 do
  442. if odd(crc) then
  443. crc:=(crc shr 1) xor longint($edb88320)
  444. else
  445. crc:=crc shr 1;
  446. Crc32Tbl[i]:=crc;
  447. end;
  448. end;
  449. {$ifopt R+}
  450. {$define Range_check_on}
  451. {$endif opt R+}
  452. {$R- needed here }
  453. {CRC 32}
  454. Function GetSpeedValue(Const s:String):longint;
  455. var
  456. i,InitCrc : longint;
  457. begin
  458. if Crc32Tbl[1]=0 then
  459. MakeCrc32Tbl;
  460. InitCrc:=-1;
  461. for i:=1 to Length(s) do
  462. InitCrc:=Crc32Tbl[byte(InitCrc) xor ord(s[i])] xor (InitCrc shr 8);
  463. GetSpeedValue:=InitCrc;
  464. end;
  465. {$ifdef Range_check_on}
  466. {$R+}
  467. {$undef Range_check_on}
  468. {$endif Range_check_on}
  469. {*****************************************************************************
  470. Ansistring (PChar+Length)
  471. *****************************************************************************}
  472. procedure ansistringdispose(var p : pchar;length : longint);
  473. begin
  474. if assigned(p) then
  475. freemem(p,length+1);
  476. p:=nil;
  477. end;
  478. { enable ansistring comparison }
  479. { 0 means equal }
  480. { 1 means p1 > p2 }
  481. { -1 means p1 < p2 }
  482. function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
  483. var
  484. i,j : longint;
  485. begin
  486. compareansistrings:=0;
  487. j:=min(length1,length2);
  488. i:=0;
  489. while (i<j) do
  490. begin
  491. if p1[i]>p2[i] then
  492. begin
  493. compareansistrings:=1;
  494. exit;
  495. end
  496. else
  497. if p1[i]<p2[i] then
  498. begin
  499. compareansistrings:=-1;
  500. exit;
  501. end;
  502. inc(i);
  503. end;
  504. if length1>length2 then
  505. compareansistrings:=1
  506. else
  507. if length1<length2 then
  508. compareansistrings:=-1;
  509. end;
  510. function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
  511. var
  512. p : pchar;
  513. begin
  514. getmem(p,length1+length2+1);
  515. move(p1[0],p[0],length1);
  516. move(p2[0],p[length1],length2+1);
  517. concatansistrings:=p;
  518. end;
  519. {*****************************************************************************
  520. File Functions
  521. *****************************************************************************}
  522. function DeleteFile(const fn:string):boolean;
  523. var
  524. f : file;
  525. begin
  526. {$I-}
  527. assign(f,fn);
  528. erase(f);
  529. {$I-}
  530. DeleteFile:=(IOResult=0);
  531. end;
  532. initialization
  533. initupperlower;
  534. end.
  535. {
  536. $Log$
  537. Revision 1.5 2000-12-24 12:25:31 peter
  538. + cstreams unit
  539. * dynamicarray object to class
  540. Revision 1.4 2000/11/28 00:17:43 pierre
  541. + int64tostr function added
  542. Revision 1.3 2000/11/07 20:47:35 peter
  543. * use tables for upper/lower
  544. Revision 1.2 2000/09/24 15:06:14 peter
  545. * use defines.inc
  546. Revision 1.1 2000/08/27 16:11:50 peter
  547. * moved some util functions from globals,cobjects to cutils
  548. * splitted files into finput,fmodule
  549. }