cobjects.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This module provides some basic objects
  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. {$ifdef tp}
  19. {$E+,N+,D+,F+}
  20. {$endif}
  21. {$I-}
  22. {$R-}{ necessary for crc calculation }
  23. unit cobjects;
  24. interface
  25. uses
  26. strings
  27. {$ifndef linux}
  28. ,dos
  29. {$else}
  30. ,linux
  31. {$endif}
  32. ;
  33. const hasharraysize = 253; {The size of a hasharray should be a prime
  34. number for better spreading of nodes in
  35. the array!! (DM)}
  36. type
  37. pstring = ^string;
  38. {$ifdef TP}
  39. { redeclare dword only in case of emergency, some small things
  40. of the compiler won't work then correctly (FK)
  41. }
  42. dword = longint;
  43. {$endif TP}
  44. pfileposinfo = ^tfileposinfo;
  45. tfileposinfo = record
  46. line : longint;
  47. column : word;
  48. fileindex : word;
  49. end;
  50. { some help data types }
  51. pstringitem = ^tstringitem;
  52. tstringitem = record
  53. data : pstring;
  54. next : pstringitem;
  55. fileinfo : tfileposinfo; { pointer to tinputfile }
  56. end;
  57. plinkedlist_item = ^tlinkedlist_item;
  58. tlinkedlist_item = object
  59. next,previous : plinkedlist_item;
  60. { does nothing }
  61. constructor init;
  62. destructor done;virtual;
  63. function getcopy:plinkedlist_item;virtual;
  64. end;
  65. pstring_item = ^tstring_item;
  66. tstring_item = object(tlinkedlist_item)
  67. str : pstring;
  68. constructor init(const s : string);
  69. destructor done;virtual;
  70. end;
  71. { this implements a double linked list }
  72. plinkedlist = ^tlinkedlist;
  73. tlinkedlist = object
  74. first,last : plinkedlist_item;
  75. constructor init;
  76. destructor done;
  77. { disposes the items of the list }
  78. procedure clear;
  79. { concats a new item at the end }
  80. procedure concat(p : plinkedlist_item);
  81. { inserts a new item at the begin }
  82. procedure insert(p : plinkedlist_item);
  83. { inserts another list at the begin and make this list empty }
  84. procedure insertlist(p : plinkedlist);
  85. { concats another list at the end and make this list empty }
  86. procedure concatlist(p : plinkedlist);
  87. procedure concatlistcopy(p : plinkedlist);
  88. { removes p from the list (p isn't disposed) }
  89. { it's not tested if p is in the list ! }
  90. procedure remove(p : plinkedlist_item);
  91. { is the linkedlist empty ? }
  92. function empty:boolean;
  93. end;
  94. { String Queue}
  95. PStringQueue=^TStringQueue;
  96. TStringQueue=object
  97. first,last : PStringItem;
  98. constructor Init;
  99. destructor Done;
  100. function Empty:boolean;
  101. function Get:string;
  102. procedure Insert(const s:string);
  103. procedure Concat(const s:string);
  104. procedure Clear;
  105. end;
  106. { string container }
  107. pstringcontainer = ^tstringcontainer;
  108. tstringcontainer = object
  109. root,
  110. last : pstringitem;
  111. doubles : boolean; { if this is set to true, doubles are allowed }
  112. constructor init;
  113. constructor init_no_double;
  114. destructor done;
  115. { true when the container is empty }
  116. function empty:boolean;
  117. { inserts a string }
  118. procedure insert(const s : string);
  119. procedure insert_with_tokeninfo(const s : string;const file_info : tfileposinfo);
  120. { gets a string }
  121. function get : string;
  122. function get_with_tokeninfo(var file_info : tfileposinfo) : string;
  123. { true if string is in the container }
  124. function find(const s:string):boolean;
  125. { deletes all strings }
  126. procedure clear;
  127. end;
  128. Pdictionary=^Tdictionary;
  129. Pdictionaryobject=^Tdictionaryobject;
  130. Tdictionaryobject=object
  131. _name : Pstring;
  132. speedvalue : longint;
  133. left,right : Pdictionaryobject;
  134. owner : Pdictionary;
  135. constructor init(const n:string);
  136. destructor done;virtual;
  137. function name:string;
  138. end;
  139. Pdictionaryhasharray=^Tdictionaryhasharray;
  140. Tdictionaryhasharray=array[0..hasharraysize-1] of Pdictionaryobject;
  141. Tdictionarycallback = procedure(p:Pdictionaryobject);
  142. Tdictionary=object
  143. noclear : boolean;
  144. replace_existing : boolean;
  145. constructor init(usehash:boolean);
  146. procedure clear;virtual;
  147. procedure foreach(proc2call:Tdictionarycallback);
  148. function insert(obj:Pdictionaryobject):Pdictionaryobject;virtual;
  149. function rename(const olds,news : string):pdictionaryobject;
  150. function search(const s:string):Pdictionaryobject;
  151. function speedsearch(const s:string;speedvalue:longint):Pdictionaryobject;virtual;
  152. destructor done;virtual;
  153. private
  154. root : Pdictionaryobject;
  155. hasharray : Pdictionaryhasharray;
  156. function insertnode(newnode:pdictionaryobject;var currnode:pdictionaryobject):pdictionaryobject;
  157. procedure inserttree(currtree,currroot:pdictionaryobject);
  158. end;
  159. pdynamicarray = ^tdynamicarray;
  160. tdynamicarray = object
  161. posn,
  162. count,
  163. limit,
  164. elemlen,
  165. growcount : longint;
  166. data : pchar;
  167. constructor init(Aelemlen,Agrow:longint);
  168. destructor done;
  169. function size:longint;
  170. function usedsize:longint;
  171. procedure grow;
  172. procedure align(i:longint);
  173. procedure seek(i:longint);
  174. procedure write(var d;len:longint);
  175. procedure read(var d;len:longint);
  176. procedure writepos(pos:longint;var d;len:longint);
  177. procedure readpos(pos:longint;var d;len:longint);
  178. end;
  179. pindexobject=^tindexobject;
  180. tindexobject=object
  181. indexnr : longint;
  182. next : pindexobject;
  183. constructor init;
  184. destructor done;virtual;
  185. end;
  186. tindexcallback=procedure(p:pindexobject);
  187. tindexobjectarray=array[1..16000] of pindexobject;
  188. pindexobjectarray=^tindexobjectarray;
  189. pindexarray=^tindexarray;
  190. tindexarray=object
  191. first : pindexobject;
  192. count : longint;
  193. constructor init(Agrowsize:longint);
  194. destructor done;
  195. procedure clear;
  196. procedure foreach(proc2call : tindexcallback);
  197. procedure delete(p:pindexobject);
  198. procedure deletenr(p:pindexobject);
  199. procedure insert(p:pindexobject);
  200. procedure insertnr(p:pindexobject);
  201. function search(nr:longint):pindexobject;
  202. private
  203. growsize,
  204. size : longint;
  205. data : pindexobjectarray;
  206. procedure grow(gsize:longint);
  207. end;
  208. {$ifdef BUFFEREDFILE}
  209. { this is implemented to allow buffered binary I/O }
  210. pbufferedfile = ^tbufferedfile;
  211. tbufferedfile = object
  212. f : file;
  213. buf : pchar;
  214. bufsize,buflast,bufpos : longint;
  215. { 0 closed, 1 input, 2 output }
  216. iomode : byte;
  217. { true, if the compile should change the endian of the output }
  218. change_endian : boolean;
  219. { calcules a crc for the file, }
  220. { but it's assumed, that there no seek while do_crc is true }
  221. do_crc : boolean;
  222. crc : longint;
  223. { temporary closing feature }
  224. tempclosed : boolean;
  225. tempmode : byte;
  226. temppos : longint;
  227. { inits a buffer with the size bufsize which is assigned to }
  228. { the file filename }
  229. constructor init(const filename : string;_bufsize : longint);
  230. { closes the file, if needed, and releases the memory }
  231. destructor done;virtual;
  232. { opens the file for input, other accesses are rejected }
  233. function reset:boolean;
  234. { opens the file for output, other accesses are rejected }
  235. procedure rewrite;
  236. { reads or writes the buffer from or to disk }
  237. procedure flush;
  238. { writes a string to the file }
  239. { the string is written without a length byte }
  240. procedure write_string(const s : string);
  241. { writes a zero terminated string }
  242. procedure write_pchar(p : pchar);
  243. { write specific data types, takes care of }
  244. { byte order }
  245. procedure write_byte(b : byte);
  246. procedure write_word(w : word);
  247. procedure write_long(l : longint);
  248. procedure write_double(d : double);
  249. { writes any data }
  250. procedure write_data(var data;count : longint);
  251. { reads any data }
  252. procedure read_data(var data;bytes : longint;var count : longint);
  253. { closes the file and releases the buffer }
  254. procedure close;
  255. { temporary closing }
  256. procedure tempclose;
  257. procedure tempreopen;
  258. { goto the given position }
  259. procedure seek(l : longint);
  260. { installes an user defined buffer }
  261. { and releases the old one, but be }
  262. { careful, if the old buffer contains }
  263. { data, this data is lost }
  264. procedure setbuf(p : pchar;s : longint);
  265. { reads the file time stamp of the file, }
  266. { the file must be opened }
  267. function getftime : longint;
  268. { returns filesize }
  269. function getsize : longint;
  270. { returns the path }
  271. function getpath : string;
  272. { resets the crc }
  273. procedure clear_crc;
  274. { returns the crc }
  275. function getcrc : longint;
  276. end;
  277. {$endif BUFFEREDFILE}
  278. { releases the string p and assignes nil to p }
  279. { if p=nil then freemem isn't called }
  280. procedure stringdispose(var p : pstring);
  281. { idem for ansistrings }
  282. procedure ansistringdispose(var p : pchar;length : longint);
  283. { allocates mem for a copy of s, copies s to this mem and returns }
  284. { a pointer to this mem }
  285. function stringdup(const s : string) : pstring;
  286. { allocates memory for s and copies s as zero terminated string
  287. to that mem and returns a pointer to that mem }
  288. function strpnew(const s : string) : pchar;
  289. { makes a char lowercase, with spanish, french and german char set }
  290. function lowercase(c : char) : char;
  291. { makes zero terminated string to a pascal string }
  292. { the data in p is modified and p is returned }
  293. function pchar2pstring(p : pchar) : pstring;
  294. { ambivalent to pchar2pstring }
  295. function pstring2pchar(p : pstring) : pchar;
  296. implementation
  297. {$ifdef FPC}
  298. function getspeedvalue(const s : string) : longint;
  299. var
  300. p1,p2:^byte;
  301. begin
  302. p1:=@s;
  303. longint(p2):=longint(p1)+p1^+1;
  304. inc(longint(p1));
  305. getspeedvalue:=0;
  306. while p1<>p2 do
  307. begin
  308. inc(getspeedvalue,p1^);
  309. inc(longint(p1));
  310. end;
  311. end;
  312. {$else}
  313. function getspeedvalue(const s : string) : longint;
  314. type
  315. ptrrec=record
  316. ofs,seg:word;
  317. end;
  318. var
  319. l,w : longint;
  320. p1,p2 : ^byte;
  321. begin
  322. p1:=@s;
  323. ptrrec(p2).seg:=ptrrec(p1).seg;
  324. ptrrec(p2).ofs:=ptrrec(p1).ofs+p1^+1;
  325. inc(p1);
  326. l:=0;
  327. while p1<>p2 do
  328. begin
  329. l:=l+p1^;
  330. inc(p1);
  331. end;
  332. getspeedvalue:=l;
  333. end;
  334. {$endif}
  335. function pchar2pstring(p : pchar) : pstring;
  336. var
  337. w,i : longint;
  338. begin
  339. w:=strlen(p);
  340. for i:=w-1 downto 0 do
  341. p[i+1]:=p[i];
  342. p[0]:=chr(w);
  343. pchar2pstring:=pstring(p);
  344. end;
  345. function pstring2pchar(p : pstring) : pchar;
  346. var
  347. w,i : longint;
  348. begin
  349. w:=length(p^);
  350. for i:=1 to w do
  351. p^[i-1]:=p^[i];
  352. p^[w]:=#0;
  353. pstring2pchar:=pchar(p);
  354. end;
  355. function lowercase(c : char) : char;
  356. begin
  357. case c of
  358. #65..#90 : c := chr(ord (c) + 32);
  359. #154 : c:=#129; { german }
  360. #142 : c:=#132; { german }
  361. #153 : c:=#148; { german }
  362. #144 : c:=#130; { french }
  363. #128 : c:=#135; { french }
  364. #143 : c:=#134; { swedish/norge (?) }
  365. #165 : c:=#164; { spanish }
  366. #228 : c:=#229; { greek }
  367. #226 : c:=#231; { greek }
  368. #232 : c:=#227; { greek }
  369. end;
  370. lowercase := c;
  371. end;
  372. function strpnew(const s : string) : pchar;
  373. var
  374. p : pchar;
  375. begin
  376. getmem(p,length(s)+1);
  377. strpcopy(p,s);
  378. strpnew:=p;
  379. end;
  380. procedure stringdispose(var p : pstring);
  381. begin
  382. if assigned(p) then
  383. freemem(p,length(p^)+1);
  384. p:=nil;
  385. end;
  386. procedure ansistringdispose(var p : pchar;length : longint);
  387. begin
  388. if assigned(p) then
  389. freemem(p,length+1);
  390. p:=nil;
  391. end;
  392. function stringdup(const s : string) : pstring;
  393. var
  394. p : pstring;
  395. begin
  396. getmem(p,length(s)+1);
  397. p^:=s;
  398. stringdup:=p;
  399. end;
  400. {****************************************************************************
  401. TStringQueue
  402. ****************************************************************************}
  403. constructor TStringQueue.Init;
  404. begin
  405. first:=nil;
  406. end;
  407. function TStringQueue.Empty:boolean;
  408. begin
  409. Empty:=(first=nil);
  410. end;
  411. function TStringQueue.Get:string;
  412. var
  413. newnode : pstringitem;
  414. begin
  415. if first=nil then
  416. begin
  417. Get:='';
  418. exit;
  419. end;
  420. Get:=first^.data^;
  421. stringdispose(first^.data);
  422. newnode:=first;
  423. first:=first^.next;
  424. dispose(newnode);
  425. end;
  426. procedure TStringQueue.Insert(const s:string);
  427. var
  428. newnode : pstringitem;
  429. begin
  430. new(newnode);
  431. newnode^.next:=first;
  432. newnode^.data:=stringdup(s);
  433. first:=newnode;
  434. if last=nil then
  435. last:=newnode;
  436. end;
  437. procedure TStringQueue.Concat(const s:string);
  438. var
  439. newnode : pstringitem;
  440. begin
  441. new(newnode);
  442. newnode^.next:=nil;
  443. newnode^.data:=stringdup(s);
  444. if first=nil then
  445. first:=newnode
  446. else
  447. last^.next:=newnode;
  448. last:=newnode;
  449. end;
  450. procedure TStringQueue.Clear;
  451. var
  452. newnode : pstringitem;
  453. begin
  454. while (first<>nil) do
  455. begin
  456. newnode:=first;
  457. stringdispose(first^.data);
  458. first:=first^.next;
  459. dispose(newnode);
  460. end;
  461. end;
  462. destructor TStringQueue.Done;
  463. begin
  464. Clear;
  465. end;
  466. {****************************************************************************
  467. TSTRINGCONTAINER
  468. ****************************************************************************}
  469. constructor tstringcontainer.init;
  470. begin
  471. root:=nil;
  472. last:=nil;
  473. doubles:=true;
  474. end;
  475. constructor tstringcontainer.init_no_double;
  476. begin
  477. root:=nil;
  478. last:=nil;
  479. doubles:=false;
  480. end;
  481. destructor tstringcontainer.done;
  482. begin
  483. clear;
  484. end;
  485. function tstringcontainer.empty:boolean;
  486. begin
  487. empty:=(root=nil);
  488. end;
  489. procedure tstringcontainer.insert(const s : string);
  490. var
  491. newnode : pstringitem;
  492. begin
  493. if not(doubles) then
  494. begin
  495. newnode:=root;
  496. while assigned(newnode) do
  497. begin
  498. if newnode^.data^=s then exit;
  499. newnode:=newnode^.next;
  500. end;
  501. end;
  502. new(newnode);
  503. newnode^.next:=nil;
  504. newnode^.data:=stringdup(s);
  505. if root=nil then root:=newnode
  506. else last^.next:=newnode;
  507. last:=newnode;
  508. end;
  509. procedure tstringcontainer.insert_with_tokeninfo(const s : string; const file_info : tfileposinfo);
  510. var
  511. newnode : pstringitem;
  512. begin
  513. if not(doubles) then
  514. begin
  515. newnode:=root;
  516. while assigned(newnode) do
  517. begin
  518. if newnode^.data^=s then exit;
  519. newnode:=newnode^.next;
  520. end;
  521. end;
  522. new(newnode);
  523. newnode^.next:=nil;
  524. newnode^.data:=stringdup(s);
  525. newnode^.fileinfo:=file_info;
  526. if root=nil then root:=newnode
  527. else last^.next:=newnode;
  528. last:=newnode;
  529. end;
  530. procedure tstringcontainer.clear;
  531. var
  532. newnode : pstringitem;
  533. begin
  534. newnode:=root;
  535. while assigned(newnode) do
  536. begin
  537. stringdispose(newnode^.data);
  538. root:=newnode^.next;
  539. dispose(newnode);
  540. newnode:=root;
  541. end;
  542. last:=nil;
  543. root:=nil;
  544. end;
  545. function tstringcontainer.get : string;
  546. var
  547. newnode : pstringitem;
  548. begin
  549. if root=nil then
  550. get:=''
  551. else
  552. begin
  553. get:=root^.data^;
  554. newnode:=root;
  555. root:=root^.next;
  556. stringdispose(newnode^.data);
  557. dispose(newnode);
  558. end;
  559. end;
  560. function tstringcontainer.get_with_tokeninfo(var file_info : tfileposinfo) : string;
  561. var
  562. newnode : pstringitem;
  563. begin
  564. if root=nil then
  565. begin
  566. get_with_tokeninfo:='';
  567. file_info.fileindex:=0;
  568. file_info.line:=0;
  569. file_info.column:=0;
  570. end
  571. else
  572. begin
  573. get_with_tokeninfo:=root^.data^;
  574. newnode:=root;
  575. root:=root^.next;
  576. stringdispose(newnode^.data);
  577. file_info:=newnode^.fileinfo;
  578. dispose(newnode);
  579. end;
  580. end;
  581. function tstringcontainer.find(const s:string):boolean;
  582. var
  583. newnode : pstringitem;
  584. begin
  585. find:=false;
  586. newnode:=root;
  587. while assigned(newnode) do
  588. begin
  589. if newnode^.data^=s then
  590. begin
  591. find:=true;
  592. exit;
  593. end;
  594. newnode:=newnode^.next;
  595. end;
  596. end;
  597. {****************************************************************************
  598. TLINKEDLIST_ITEM
  599. ****************************************************************************}
  600. constructor tlinkedlist_item.init;
  601. begin
  602. previous:=nil;
  603. next:=nil;
  604. end;
  605. destructor tlinkedlist_item.done;
  606. begin
  607. end;
  608. function tlinkedlist_item.getcopy:plinkedlist_item;
  609. var
  610. l : longint;
  611. p : plinkedlist_item;
  612. begin
  613. l:=sizeof(self);
  614. getmem(p,l);
  615. move(self,p^,l);
  616. getcopy:=p;
  617. end;
  618. {****************************************************************************
  619. TSTRING_ITEM
  620. ****************************************************************************}
  621. constructor tstring_item.init(const s : string);
  622. begin
  623. str:=stringdup(s);
  624. end;
  625. destructor tstring_item.done;
  626. begin
  627. stringdispose(str);
  628. inherited done;
  629. end;
  630. {****************************************************************************
  631. TLINKEDLIST
  632. ****************************************************************************}
  633. constructor tlinkedlist.init;
  634. begin
  635. first:=nil;
  636. last:=nil;
  637. end;
  638. destructor tlinkedlist.done;
  639. begin
  640. clear;
  641. end;
  642. procedure tlinkedlist.clear;
  643. var
  644. newnode : plinkedlist_item;
  645. begin
  646. newnode:=first;
  647. while assigned(newnode) do
  648. begin
  649. first:=newnode^.next;
  650. dispose(newnode,done);
  651. newnode:=first;
  652. end;
  653. end;
  654. procedure tlinkedlist.insertlist(p : plinkedlist);
  655. begin
  656. { empty list ? }
  657. if not(assigned(p^.first)) then
  658. exit;
  659. p^.last^.next:=first;
  660. { we have a double linked list }
  661. if assigned(first) then
  662. first^.previous:=p^.last;
  663. first:=p^.first;
  664. if not(assigned(last)) then
  665. last:=p^.last;
  666. { p becomes empty }
  667. p^.first:=nil;
  668. p^.last:=nil;
  669. end;
  670. procedure tlinkedlist.concat(p : plinkedlist_item);
  671. begin
  672. if not(assigned(first)) then
  673. begin
  674. first:=p;
  675. p^.previous:=nil;
  676. p^.next:=nil;
  677. end
  678. else
  679. begin
  680. last^.next:=p;
  681. p^.previous:=last;
  682. p^.next:=nil;
  683. end;
  684. last:=p;
  685. end;
  686. procedure tlinkedlist.insert(p : plinkedlist_item);
  687. begin
  688. if not(assigned(first)) then
  689. begin
  690. last:=p;
  691. p^.previous:=nil;
  692. p^.next:=nil;
  693. end
  694. else
  695. begin
  696. first^.previous:=p;
  697. p^.previous:=nil;
  698. p^.next:=first;
  699. end;
  700. first:=p;
  701. end;
  702. procedure tlinkedlist.remove(p : plinkedlist_item);
  703. begin
  704. if not(assigned(p)) then
  705. exit;
  706. if (first=p) and (last=p) then
  707. begin
  708. first:=nil;
  709. last:=nil;
  710. end
  711. else if first=p then
  712. begin
  713. first:=p^.next;
  714. if assigned(first) then
  715. first^.previous:=nil;
  716. end
  717. else if last=p then
  718. begin
  719. last:=last^.previous;
  720. if assigned(last) then
  721. last^.next:=nil;
  722. end
  723. else
  724. begin
  725. p^.previous^.next:=p^.next;
  726. p^.next^.previous:=p^.previous;
  727. end;
  728. p^.next:=nil;
  729. p^.previous:=nil;
  730. end;
  731. procedure tlinkedlist.concatlist(p : plinkedlist);
  732. begin
  733. if not(assigned(p^.first)) then
  734. exit;
  735. if not(assigned(first)) then
  736. first:=p^.first
  737. else
  738. begin
  739. last^.next:=p^.first;
  740. p^.first^.previous:=last;
  741. end;
  742. last:=p^.last;
  743. { make p empty }
  744. p^.last:=nil;
  745. p^.first:=nil;
  746. end;
  747. procedure tlinkedlist.concatlistcopy(p : plinkedlist);
  748. var
  749. newnode,newnode2 : plinkedlist_item;
  750. begin
  751. newnode:=p^.first;
  752. while assigned(newnode) do
  753. begin
  754. newnode2:=newnode^.getcopy;
  755. if assigned(newnode2) then
  756. begin
  757. if not(assigned(first)) then
  758. begin
  759. first:=newnode2;
  760. newnode2^.previous:=nil;
  761. newnode2^.next:=nil;
  762. end
  763. else
  764. begin
  765. last^.next:=newnode2;
  766. newnode2^.previous:=last;
  767. newnode2^.next:=nil;
  768. end;
  769. last:=newnode2;
  770. end;
  771. newnode:=newnode^.next;
  772. end;
  773. end;
  774. function tlinkedlist.empty:boolean;
  775. begin
  776. empty:=(first=nil);
  777. end;
  778. {****************************************************************************
  779. Tdictionaryobject
  780. ****************************************************************************}
  781. constructor Tdictionaryobject.init(const n:string);
  782. begin
  783. left:=nil;
  784. right:=nil;
  785. _name:=stringdup(n);
  786. speedvalue:=getspeedvalue(n);
  787. end;
  788. destructor Tdictionaryobject.done;
  789. begin
  790. stringdispose(_name);
  791. if assigned(left) then
  792. dispose(left,done);
  793. if assigned(right) then
  794. dispose(right,done);
  795. end;
  796. function Tdictionaryobject.name:string;
  797. begin
  798. name:=_name^;
  799. end;
  800. {****************************************************************************
  801. TDICTIONARY
  802. ****************************************************************************}
  803. constructor Tdictionary.init(usehash:boolean);
  804. begin
  805. root:=nil;
  806. hasharray:=nil;
  807. noclear:=false;
  808. replace_existing:=false;
  809. if usehash then
  810. begin
  811. new(hasharray);
  812. fillchar(hasharray^,sizeof(hasharray^),0);
  813. end;
  814. end;
  815. destructor Tdictionary.done;
  816. begin
  817. clear;
  818. if assigned(hasharray) then
  819. dispose(hasharray);
  820. end;
  821. procedure Tdictionary.clear;
  822. var
  823. w : longint;
  824. begin
  825. if assigned(root) then
  826. dispose(root,done);
  827. if assigned(hasharray) then
  828. for w:=0 to hasharraysize-1 do
  829. if assigned(hasharray^[w]) then
  830. begin
  831. dispose(hasharray^[w],done);
  832. hasharray^[w]:=nil;
  833. end;
  834. end;
  835. procedure Tdictionary.foreach(proc2call:Tdictionarycallback);
  836. procedure a(p:Pdictionaryobject);
  837. begin
  838. proc2call(p);
  839. if assigned(p^.left) then
  840. a(p^.left);
  841. if assigned(p^.right) then
  842. a(p^.right);
  843. end;
  844. var
  845. i : longint;
  846. begin
  847. if assigned(hasharray) then
  848. begin
  849. for i:=0 to hasharraysize-1 do
  850. if assigned(hasharray^[i]) then
  851. a(hasharray^[i]);
  852. end
  853. else
  854. if assigned(root) then
  855. a(root);
  856. end;
  857. function Tdictionary.insert(obj:Pdictionaryobject):Pdictionaryobject;
  858. begin
  859. obj^.owner:=@self;
  860. obj^.speedvalue:=getspeedvalue(obj^._name^);
  861. if assigned(hasharray) then
  862. insert:=insertnode(obj,hasharray^[obj^.speedvalue mod hasharraysize])
  863. else
  864. insert:=insertnode(obj,root);
  865. end;
  866. function tdictionary.insertnode(newnode:pdictionaryobject;var currnode:pdictionaryobject):pdictionaryobject;
  867. var
  868. s1,s2:^string;
  869. begin
  870. if currnode=nil then
  871. begin
  872. currnode:=newnode;
  873. insertnode:=currnode;
  874. end
  875. { first check speedvalue, to allow a fast insert }
  876. else
  877. if currnode^.speedvalue>newnode^.speedvalue then
  878. insertnode:=insertnode(newnode,currnode^.right)
  879. else
  880. if currnode^.speedvalue<newnode^.speedvalue then
  881. insertnode:=insertnode(newnode,currnode^.left)
  882. else
  883. begin
  884. new(s1);
  885. new(s2);
  886. s1^:=currnode^._name^;
  887. s2^:=newnode^._name^;
  888. if s1^>s2^ then
  889. begin
  890. dispose(s2);
  891. dispose(s1);
  892. insertnode:=insertnode(newnode,currnode^.right);
  893. end
  894. else
  895. if s1^<s2^ then
  896. begin
  897. dispose(s2);
  898. dispose(s1);
  899. insertnode:=insertnode(newnode,currnode^.left);
  900. end
  901. else
  902. begin
  903. dispose(s2);
  904. dispose(s1);
  905. if replace_existing and
  906. assigned(currnode) then
  907. begin
  908. newnode^.left:=currnode^.left;
  909. newnode^.right:=currnode^.right;
  910. currnode:=newnode;
  911. insertnode:=newnode;
  912. end
  913. else
  914. insertnode:=currnode;
  915. end;
  916. end;
  917. end;
  918. procedure tdictionary.inserttree(currtree,currroot:pdictionaryobject);
  919. begin
  920. if assigned(currtree) then
  921. begin
  922. inserttree(currtree^.left,currroot);
  923. inserttree(currtree^.right,currroot);
  924. insertnode(currtree,currroot);
  925. end;
  926. end;
  927. function tdictionary.rename(const olds,news : string):pdictionaryobject;
  928. var
  929. spdval : longint;
  930. lasthp,
  931. hp,hp2,hp3 : pdictionaryobject;
  932. begin
  933. spdval:=getspeedvalue(olds);
  934. if assigned(hasharray) then
  935. hp:=hasharray^[spdval mod hasharraysize]
  936. else
  937. hp:=root;
  938. lasthp:=nil;
  939. while assigned(hp) do
  940. begin
  941. if spdval>hp^.speedvalue then
  942. begin
  943. lasthp:=hp;
  944. hp:=hp^.left
  945. end
  946. else
  947. if spdval<hp^.speedvalue then
  948. begin
  949. lasthp:=hp;
  950. hp:=hp^.right
  951. end
  952. else
  953. begin
  954. if (hp^.name=olds) then
  955. begin
  956. { get in hp2 the replacer for the root or hasharr }
  957. hp2:=hp^.left;
  958. hp3:=hp^.right;
  959. if not assigned(hp2) then
  960. begin
  961. hp2:=hp^.right;
  962. hp3:=hp^.left;
  963. end;
  964. { remove entry from the tree }
  965. if assigned(lasthp) then
  966. begin
  967. if lasthp^.left=hp then
  968. lasthp^.left:=hp2
  969. else
  970. lasthp^.right:=hp2;
  971. end
  972. else
  973. begin
  974. if assigned(hasharray) then
  975. hasharray^[spdval mod hasharraysize]:=hp2
  976. else
  977. root:=hp2;
  978. end;
  979. { reinsert the hp3 in the tree from hp2 }
  980. inserttree(hp3,hp2);
  981. { reinsert }
  982. stringdispose(hp^._name);
  983. hp^._name:=stringdup(news);
  984. hp^.speedvalue:=getspeedvalue(news);
  985. if assigned(hasharray) then
  986. rename:=insertnode(hp,hasharray^[hp^.speedvalue mod hasharraysize])
  987. else
  988. rename:=insertnode(hp,root);
  989. exit;
  990. end
  991. else
  992. if olds>hp^.name then
  993. begin
  994. lasthp:=hp;
  995. hp:=hp^.left
  996. end
  997. else
  998. begin
  999. lasthp:=hp;
  1000. hp:=hp^.right;
  1001. end;
  1002. end;
  1003. end;
  1004. end;
  1005. function Tdictionary.search(const s:string):Pdictionaryobject;
  1006. begin
  1007. search:=speedsearch(s,getspeedvalue(s));
  1008. end;
  1009. function Tdictionary.speedsearch(const s:string;speedvalue:longint):Pdictionaryobject;
  1010. var
  1011. newnode:Pdictionaryobject;
  1012. begin
  1013. if assigned(hasharray) then
  1014. newnode:=hasharray^[speedvalue mod hasharraysize]
  1015. else
  1016. newnode:=root;
  1017. while assigned(newnode) do
  1018. begin
  1019. if speedvalue>newnode^.speedvalue then
  1020. newnode:=newnode^.left
  1021. else
  1022. if speedvalue<newnode^.speedvalue then
  1023. newnode:=newnode^.right
  1024. else
  1025. begin
  1026. if (newnode^._name^=s) then
  1027. begin
  1028. speedsearch:=newnode;
  1029. exit;
  1030. end
  1031. else
  1032. if s>newnode^._name^ then
  1033. newnode:=newnode^.left
  1034. else
  1035. newnode:=newnode^.right;
  1036. end;
  1037. end;
  1038. speedsearch:=nil;
  1039. end;
  1040. {****************************************************************************
  1041. tdynamicarray
  1042. ****************************************************************************}
  1043. constructor tdynamicarray.init(Aelemlen,Agrow:longint);
  1044. begin
  1045. posn:=0;
  1046. count:=0;
  1047. limit:=0;
  1048. data:=nil;
  1049. elemlen:=Aelemlen;
  1050. growcount:=Agrow;
  1051. grow;
  1052. end;
  1053. destructor tindexarray.done;
  1054. begin
  1055. clear;
  1056. if assigned(data) then
  1057. freemem(data,size*4);
  1058. end;
  1059. function tdynamicarray.size:longint;
  1060. begin
  1061. size:=limit*elemlen;
  1062. end;
  1063. function tdynamicarray.usedsize:longint;
  1064. begin
  1065. usedsize:=count*elemlen;
  1066. end;
  1067. procedure tdynamicarray.grow;
  1068. var
  1069. osize : longint;
  1070. odata : pchar;
  1071. begin
  1072. osize:=size;
  1073. odata:=data;
  1074. inc(limit,growcount);
  1075. getmem(data,size);
  1076. if assigned(odata) then
  1077. begin
  1078. move(odata^,data^,osize);
  1079. freemem(odata,osize);
  1080. end;
  1081. fillchar(data[osize],growcount*elemlen,0);
  1082. end;
  1083. procedure tdynamicarray.align(i:longint);
  1084. var
  1085. j : longint;
  1086. begin
  1087. j:=(posn*elemlen mod i);
  1088. if j<>0 then
  1089. begin
  1090. j:=i-j;
  1091. while limit<(posn+j) do
  1092. grow;
  1093. inc(posn,j);
  1094. if (posn>count) then
  1095. count:=posn;
  1096. end;
  1097. end;
  1098. procedure tdynamicarray.seek(i:longint);
  1099. begin
  1100. while limit<i do
  1101. grow;
  1102. posn:=i;
  1103. if (posn>count) then
  1104. count:=posn;
  1105. end;
  1106. procedure tdynamicarray.write(var d;len:longint);
  1107. begin
  1108. while limit<(posn+len) do
  1109. grow;
  1110. move(d,data[posn*elemlen],len*elemlen);
  1111. inc(posn,len);
  1112. if (posn>count) then
  1113. count:=posn;
  1114. end;
  1115. procedure tdynamicarray.read(var d;len:longint);
  1116. begin
  1117. move(data[posn*elemlen],d,len*elemlen);
  1118. inc(posn,len);
  1119. if (posn>count) then
  1120. count:=posn;
  1121. end;
  1122. procedure tdynamicarray.writepos(pos:longint;var d;len:longint);
  1123. begin
  1124. while limit<(pos+len) do
  1125. grow;
  1126. move(d,data[pos*elemlen],len*elemlen);
  1127. posn:=pos+len;
  1128. if (posn>count) then
  1129. count:=posn;
  1130. end;
  1131. procedure tdynamicarray.readpos(pos:longint;var d;len:longint);
  1132. begin
  1133. while limit<(pos+len) do
  1134. grow;
  1135. move(data[pos*elemlen],d,len*elemlen);
  1136. posn:=pos+len;
  1137. if (posn>count) then
  1138. count:=posn;
  1139. end;
  1140. destructor tdynamicarray.done;
  1141. begin
  1142. if assigned(data) then
  1143. freemem(data,size);
  1144. end;
  1145. {****************************************************************************
  1146. tindexobject
  1147. ****************************************************************************}
  1148. constructor tindexobject.init;
  1149. begin
  1150. indexnr:=-1;
  1151. next:=nil;
  1152. end;
  1153. destructor tindexobject.done;
  1154. begin
  1155. end;
  1156. {****************************************************************************
  1157. tindexarray
  1158. ****************************************************************************}
  1159. constructor tindexarray.init(Agrowsize:longint);
  1160. begin
  1161. growsize:=Agrowsize;
  1162. size:=0;
  1163. count:=0;
  1164. data:=nil;
  1165. first:=nil;
  1166. end;
  1167. function tindexarray.search(nr:longint):pindexobject;
  1168. begin
  1169. if nr<=count then
  1170. search:=data^[nr]
  1171. else
  1172. search:=nil;
  1173. end;
  1174. procedure tindexarray.clear;
  1175. var
  1176. i : longint;
  1177. begin
  1178. for i:=1to count do
  1179. if assigned(data^[i]) then
  1180. begin
  1181. dispose(data^[i],done);
  1182. data^[i]:=nil;
  1183. end;
  1184. count:=0;
  1185. end;
  1186. procedure tindexarray.foreach(proc2call : tindexcallback);
  1187. var
  1188. i : longint;
  1189. begin
  1190. for i:=1to count do
  1191. if assigned(data^[i]) then
  1192. proc2call(data^[i]);
  1193. end;
  1194. procedure tindexarray.grow(gsize:longint);
  1195. var
  1196. osize : longint;
  1197. odata : pindexobjectarray;
  1198. begin
  1199. osize:=size;
  1200. odata:=data;
  1201. inc(size,gsize);
  1202. getmem(data,size*4);
  1203. if assigned(odata) then
  1204. begin
  1205. move(odata^,data^,osize*4);
  1206. freemem(odata,osize*4);
  1207. end;
  1208. fillchar(data^[osize+1],gsize*4,0);
  1209. end;
  1210. procedure tindexarray.deletenr(p:pindexobject);
  1211. var
  1212. i : longint;
  1213. begin
  1214. { update linked list }
  1215. i:=p^.indexnr;
  1216. while (i>0) do
  1217. begin
  1218. dec(i);
  1219. if (i>0) and assigned(data^[i]) then
  1220. begin
  1221. data^[i]^.next:=data^[p^.indexnr]^.next;
  1222. break;
  1223. end;
  1224. end;
  1225. if i=0 then
  1226. first:=p;
  1227. { remove entry }
  1228. p^.next:=nil;
  1229. data^[p^.indexnr]:=nil;
  1230. end;
  1231. procedure tindexarray.delete(p:pindexobject);
  1232. begin
  1233. deletenr(p);
  1234. dispose(p,done);
  1235. end;
  1236. procedure tindexarray.insertnr(p:pindexobject);
  1237. var
  1238. i : longint;
  1239. begin
  1240. if p^.indexnr>count then
  1241. count:=p^.indexnr;
  1242. if count>size then
  1243. grow(((count div growsize)+1)*growsize);
  1244. data^[p^.indexnr]:=p;
  1245. { update linked list backward }
  1246. i:=p^.indexnr;
  1247. while (i>0) do
  1248. begin
  1249. dec(i);
  1250. if (i>0) and assigned(data^[i]) then
  1251. begin
  1252. data^[i]^.next:=p;
  1253. break;
  1254. end;
  1255. end;
  1256. if i=0 then
  1257. first:=p;
  1258. { update linked list forward }
  1259. i:=p^.indexnr;
  1260. while (i<=count) do
  1261. begin
  1262. inc(i);
  1263. if (i<=count) and assigned(data^[i]) then
  1264. begin
  1265. p^.next:=data^[i];
  1266. exit;
  1267. end;
  1268. end;
  1269. if i>count then
  1270. p^.next:=nil;
  1271. end;
  1272. procedure tindexarray.insert(p:pindexobject);
  1273. begin
  1274. inc(count);
  1275. p^.indexnr:=count;
  1276. insertnr(p);
  1277. end;
  1278. {$ifdef BUFFEREDFILE}
  1279. {****************************************************************************
  1280. TBUFFEREDFILE
  1281. ****************************************************************************}
  1282. Const
  1283. crcseed = $ffffffff;
  1284. crctable : array[0..255] of longint = (
  1285. $00000000,$77073096,$ee0e612c,$990951ba,$076dc419,$706af48f,
  1286. $e963a535,$9e6495a3,$0edb8832,$79dcb8a4,$e0d5e91e,$97d2d988,
  1287. $09b64c2b,$7eb17cbd,$e7b82d07,$90bf1d91,$1db71064,$6ab020f2,
  1288. $f3b97148,$84be41de,$1adad47d,$6ddde4eb,$f4d4b551,$83d385c7,
  1289. $136c9856,$646ba8c0,$fd62f97a,$8a65c9ec,$14015c4f,$63066cd9,
  1290. $fa0f3d63,$8d080df5,$3b6e20c8,$4c69105e,$d56041e4,$a2677172,
  1291. $3c03e4d1,$4b04d447,$d20d85fd,$a50ab56b,$35b5a8fa,$42b2986c,
  1292. $dbbbc9d6,$acbcf940,$32d86ce3,$45df5c75,$dcd60dcf,$abd13d59,
  1293. $26d930ac,$51de003a,$c8d75180,$bfd06116,$21b4f4b5,$56b3c423,
  1294. $cfba9599,$b8bda50f,$2802b89e,$5f058808,$c60cd9b2,$b10be924,
  1295. $2f6f7c87,$58684c11,$c1611dab,$b6662d3d,$76dc4190,$01db7106,
  1296. $98d220bc,$efd5102a,$71b18589,$06b6b51f,$9fbfe4a5,$e8b8d433,
  1297. $7807c9a2,$0f00f934,$9609a88e,$e10e9818,$7f6a0dbb,$086d3d2d,
  1298. $91646c97,$e6635c01,$6b6b51f4,$1c6c6162,$856530d8,$f262004e,
  1299. $6c0695ed,$1b01a57b,$8208f4c1,$f50fc457,$65b0d9c6,$12b7e950,
  1300. $8bbeb8ea,$fcb9887c,$62dd1ddf,$15da2d49,$8cd37cf3,$fbd44c65,
  1301. $4db26158,$3ab551ce,$a3bc0074,$d4bb30e2,$4adfa541,$3dd895d7,
  1302. $a4d1c46d,$d3d6f4fb,$4369e96a,$346ed9fc,$ad678846,$da60b8d0,
  1303. $44042d73,$33031de5,$aa0a4c5f,$dd0d7cc9,$5005713c,$270241aa,
  1304. $be0b1010,$c90c2086,$5768b525,$206f85b3,$b966d409,$ce61e49f,
  1305. $5edef90e,$29d9c998,$b0d09822,$c7d7a8b4,$59b33d17,$2eb40d81,
  1306. $b7bd5c3b,$c0ba6cad,$edb88320,$9abfb3b6,$03b6e20c,$74b1d29a,
  1307. $ead54739,$9dd277af,$04db2615,$73dc1683,$e3630b12,$94643b84,
  1308. $0d6d6a3e,$7a6a5aa8,$e40ecf0b,$9309ff9d,$0a00ae27,$7d079eb1,
  1309. $f00f9344,$8708a3d2,$1e01f268,$6906c2fe,$f762575d,$806567cb,
  1310. $196c3671,$6e6b06e7,$fed41b76,$89d32be0,$10da7a5a,$67dd4acc,
  1311. $f9b9df6f,$8ebeeff9,$17b7be43,$60b08ed5,$d6d6a3e8,$a1d1937e,
  1312. $38d8c2c4,$4fdff252,$d1bb67f1,$a6bc5767,$3fb506dd,$48b2364b,
  1313. $d80d2bda,$af0a1b4c,$36034af6,$41047a60,$df60efc3,$a867df55,
  1314. $316e8eef,$4669be79,$cb61b38c,$bc66831a,$256fd2a0,$5268e236,
  1315. $cc0c7795,$bb0b4703,$220216b9,$5505262f,$c5ba3bbe,$b2bd0b28,
  1316. $2bb45a92,$5cb36a04,$c2d7ffa7,$b5d0cf31,$2cd99e8b,$5bdeae1d,
  1317. $9b64c2b0,$ec63f226,$756aa39c,$026d930a,$9c0906a9,$eb0e363f,
  1318. $72076785,$05005713,$95bf4a82,$e2b87a14,$7bb12bae,$0cb61b38,
  1319. $92d28e9b,$e5d5be0d,$7cdcefb7,$0bdbdf21,$86d3d2d4,$f1d4e242,
  1320. $68ddb3f8,$1fda836e,$81be16cd,$f6b9265b,$6fb077e1,$18b74777,
  1321. $88085ae6,$ff0f6a70,$66063bca,$11010b5c,$8f659eff,$f862ae69,
  1322. $616bffd3,$166ccf45,$a00ae278,$d70dd2ee,$4e048354,$3903b3c2,
  1323. $a7672661,$d06016f7,$4969474d,$3e6e77db,$aed16a4a,$d9d65adc,
  1324. $40df0b66,$37d83bf0,$a9bcae53,$debb9ec5,$47b2cf7f,$30b5ffe9,
  1325. $bdbdf21c,$cabac28a,$53b39330,$24b4a3a6,$bad03605,$cdd70693,
  1326. $54de5729,$23d967bf,$b3667a2e,$c4614ab8,$5d681b02,$2a6f2b94,
  1327. $b40bbe37,$c30c8ea1,$5a05df1b,$2d02ef8d);
  1328. constructor tbufferedfile.init(const filename : string;_bufsize : longint);
  1329. begin
  1330. assign(f,filename);
  1331. bufsize:=_bufsize;
  1332. bufpos:=0;
  1333. buflast:=0;
  1334. do_crc:=false;
  1335. iomode:=0;
  1336. tempclosed:=false;
  1337. change_endian:=false;
  1338. clear_crc;
  1339. end;
  1340. destructor tbufferedfile.done;
  1341. begin
  1342. close;
  1343. end;
  1344. procedure tbufferedfile.clear_crc;
  1345. begin
  1346. crc:=crcseed;
  1347. end;
  1348. procedure tbufferedfile.setbuf(p : pchar;s : longint);
  1349. begin
  1350. flush;
  1351. freemem(buf,bufsize);
  1352. bufsize:=s;
  1353. buf:=p;
  1354. end;
  1355. function tbufferedfile.reset:boolean;
  1356. var
  1357. ofm : byte;
  1358. begin
  1359. ofm:=filemode;
  1360. iomode:=1;
  1361. getmem(buf,bufsize);
  1362. filemode:=0;
  1363. {$I-}
  1364. system.reset(f,1);
  1365. {$I+}
  1366. reset:=(ioresult=0);
  1367. filemode:=ofm;
  1368. end;
  1369. procedure tbufferedfile.rewrite;
  1370. begin
  1371. iomode:=2;
  1372. getmem(buf,bufsize);
  1373. system.rewrite(f,1);
  1374. end;
  1375. procedure tbufferedfile.flush;
  1376. var
  1377. {$ifdef FPC}
  1378. count : longint;
  1379. {$else}
  1380. count : integer;
  1381. {$endif}
  1382. begin
  1383. if iomode=2 then
  1384. begin
  1385. if bufpos=0 then
  1386. exit;
  1387. blockwrite(f,buf^,bufpos)
  1388. end
  1389. else if iomode=1 then
  1390. if buflast=bufpos then
  1391. begin
  1392. blockread(f,buf^,bufsize,count);
  1393. buflast:=count;
  1394. end;
  1395. bufpos:=0;
  1396. end;
  1397. function tbufferedfile.getftime : longint;
  1398. var
  1399. l : longint;
  1400. {$ifdef linux}
  1401. Info : Stat;
  1402. {$endif}
  1403. begin
  1404. {$ifndef linux}
  1405. { this only works if the file is open !! }
  1406. dos.getftime(f,l);
  1407. {$else}
  1408. Fstat(f,Info);
  1409. l:=info.mtime;
  1410. {$endif}
  1411. getftime:=l;
  1412. end;
  1413. function tbufferedfile.getsize : longint;
  1414. begin
  1415. getsize:=filesize(f);
  1416. end;
  1417. procedure tbufferedfile.seek(l : longint);
  1418. begin
  1419. if iomode=2 then
  1420. begin
  1421. flush;
  1422. system.seek(f,l);
  1423. end
  1424. else if iomode=1 then
  1425. begin
  1426. { forces a reload }
  1427. bufpos:=buflast;
  1428. system.seek(f,l);
  1429. flush;
  1430. end;
  1431. end;
  1432. type
  1433. {$ifdef tp}
  1434. bytearray1 = array [1..65535] of byte;
  1435. {$else}
  1436. bytearray1 = array [1..10000000] of byte;
  1437. {$endif}
  1438. procedure tbufferedfile.read_data(var data;bytes : longint;var count : longint);
  1439. var
  1440. p : pchar;
  1441. c,i : longint;
  1442. begin
  1443. p:=pchar(@data);
  1444. count:=0;
  1445. while bytes-count>0 do
  1446. begin
  1447. if bytes-count>buflast-bufpos then
  1448. begin
  1449. move((buf+bufpos)^,(p+count)^,buflast-bufpos);
  1450. inc(count,buflast-bufpos);
  1451. bufpos:=buflast;
  1452. flush;
  1453. { can't we read anything ? }
  1454. if bufpos=buflast then
  1455. break;
  1456. end
  1457. else
  1458. begin
  1459. move((buf+bufpos)^,(p+count)^,bytes-count);
  1460. inc(bufpos,bytes-count);
  1461. count:=bytes;
  1462. break;
  1463. end;
  1464. end;
  1465. if do_crc then
  1466. begin
  1467. c:=crc;
  1468. for i:=1 to bytes do
  1469. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1470. crc:=c;
  1471. end;
  1472. end;
  1473. procedure tbufferedfile.write_data(var data;count : longint);
  1474. var
  1475. c,i : longint;
  1476. begin
  1477. if bufpos+count>bufsize then
  1478. flush;
  1479. move(data,(buf+bufpos)^,count);
  1480. inc(bufpos,count);
  1481. if do_crc then
  1482. begin
  1483. c:=crc;
  1484. for i:=1 to count do
  1485. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1486. crc:=c;
  1487. end;
  1488. end;
  1489. function tbufferedfile.getcrc : longint;
  1490. begin
  1491. getcrc:=crc xor crcseed;
  1492. end;
  1493. procedure tbufferedfile.write_string(const s : string);
  1494. begin
  1495. if bufpos+length(s)>bufsize then
  1496. flush;
  1497. { why is there not CRC here ??? }
  1498. move(s[1],(buf+bufpos)^,length(s));
  1499. inc(bufpos,length(s));
  1500. { should be
  1501. write_data(s[1],length(s)); }
  1502. end;
  1503. procedure tbufferedfile.write_pchar(p : pchar);
  1504. var
  1505. l : longint;
  1506. begin
  1507. l:=strlen(p);
  1508. if l>=bufsize then
  1509. runerror(222);
  1510. { why is there not CRC here ???}
  1511. if bufpos+l>bufsize then
  1512. flush;
  1513. move(p^,(buf+bufpos)^,l);
  1514. inc(bufpos,l);
  1515. { should be
  1516. write_data(p^,l); }
  1517. end;
  1518. procedure tbufferedfile.write_byte(b : byte);
  1519. begin
  1520. write_data(b,sizeof(byte));
  1521. end;
  1522. procedure tbufferedfile.write_long(l : longint);
  1523. var
  1524. w1,w2 : word;
  1525. begin
  1526. if change_endian then
  1527. begin
  1528. w1:=l and $ffff;
  1529. w2:=l shr 16;
  1530. l:=swap(w2)+(longint(swap(w1)) shl 16);
  1531. write_data(l,sizeof(longint))
  1532. end
  1533. else
  1534. write_data(l,sizeof(longint))
  1535. end;
  1536. procedure tbufferedfile.write_word(w : word);
  1537. begin
  1538. if change_endian then
  1539. begin
  1540. w:=swap(w);
  1541. write_data(w,sizeof(word))
  1542. end
  1543. else
  1544. write_data(w,sizeof(word));
  1545. end;
  1546. procedure tbufferedfile.write_double(d : double);
  1547. begin
  1548. write_data(d,sizeof(double));
  1549. end;
  1550. function tbufferedfile.getpath : string;
  1551. begin
  1552. {$ifdef dummy}
  1553. getpath:=strpas(filerec(f).name);
  1554. {$endif}
  1555. getpath:='';
  1556. end;
  1557. procedure tbufferedfile.close;
  1558. begin
  1559. if iomode<>0 then
  1560. begin
  1561. flush;
  1562. system.close(f);
  1563. freemem(buf,bufsize);
  1564. buf:=nil;
  1565. iomode:=0;
  1566. end;
  1567. end;
  1568. procedure tbufferedfile.tempclose;
  1569. begin
  1570. if iomode<>0 then
  1571. begin
  1572. temppos:=system.filepos(f);
  1573. tempmode:=iomode;
  1574. tempclosed:=true;
  1575. system.close(f);
  1576. iomode:=0;
  1577. end
  1578. else
  1579. tempclosed:=false;
  1580. end;
  1581. procedure tbufferedfile.tempreopen;
  1582. var
  1583. ofm : byte;
  1584. begin
  1585. if tempclosed then
  1586. begin
  1587. case tempmode of
  1588. 1 : begin
  1589. ofm:=filemode;
  1590. iomode:=1;
  1591. filemode:=0;
  1592. system.reset(f,1);
  1593. filemode:=ofm;
  1594. end;
  1595. 2 : begin
  1596. iomode:=2;
  1597. system.rewrite(f,1);
  1598. end;
  1599. end;
  1600. system.seek(f,temppos);
  1601. tempclosed:=false;
  1602. end;
  1603. end;
  1604. {$endif BUFFEREDFILE}
  1605. end.
  1606. {
  1607. $Log$
  1608. Revision 1.24 1999-04-14 09:14:47 peter
  1609. * first things to store the symbol/def number in the ppu
  1610. Revision 1.23 1999/04/08 20:59:39 florian
  1611. * fixed problem with default properties which are a class
  1612. * case bug (from the mailing list with -O2) fixed, the
  1613. distance of the case labels can be greater than the positive
  1614. range of a longint => it is now a dword for fpc
  1615. Revision 1.22 1999/03/31 13:55:10 peter
  1616. * assembler inlining working for ag386bin
  1617. Revision 1.21 1999/03/19 16:35:29 pierre
  1618. * Tdictionaryobject done also removed left and right
  1619. Revision 1.20 1999/03/18 20:30:45 peter
  1620. + .a writer
  1621. Revision 1.19 1999/03/01 13:32:00 pierre
  1622. * external used before implemented problem fixed
  1623. Revision 1.18 1999/02/24 00:59:13 peter
  1624. * small updates for ag386bin
  1625. Revision 1.17 1999/01/19 11:00:33 daniel
  1626. + Tdictionary object: Tsymtable will become object(TTdictionary) in the
  1627. future
  1628. + Tnamed_item object: Tsym will become object(Tnamed_item) in the future
  1629. Revision 1.16 1998/11/04 10:11:37 peter
  1630. * ansistring fixes
  1631. Revision 1.15 1998/10/19 18:04:40 peter
  1632. + tstringcontainer.init_no_doubles
  1633. Revision 1.14 1998/09/18 16:03:37 florian
  1634. * some changes to compile with Delphi
  1635. Revision 1.13 1998/08/12 19:28:16 peter
  1636. * better libc support
  1637. Revision 1.12 1998/07/14 14:46:47 peter
  1638. * released NEWINPUT
  1639. Revision 1.11 1998/07/07 11:19:54 peter
  1640. + NEWINPUT for a better inputfile and scanner object
  1641. Revision 1.10 1998/07/01 15:26:59 peter
  1642. * better bufferfile.reset error handling
  1643. Revision 1.9 1998/06/03 23:40:37 peter
  1644. + unlimited file support, release tempclose
  1645. Revision 1.8 1998/05/20 09:42:33 pierre
  1646. + UseTokenInfo now default
  1647. * unit in interface uses and implementation uses gives error now
  1648. * only one error for unknown symbol (uses lastsymknown boolean)
  1649. the problem came from the label code !
  1650. + first inlined procedures and function work
  1651. (warning there might be allowed cases were the result is still wrong !!)
  1652. * UseBrower updated gives a global list of all position of all used symbols
  1653. with switch -gb
  1654. Revision 1.7 1998/05/06 18:36:53 peter
  1655. * tai_section extended with code,data,bss sections and enumerated type
  1656. * ident 'compiled by FPC' moved to pmodules
  1657. * small fix for smartlink
  1658. Revision 1.6 1998/05/06 08:38:37 pierre
  1659. * better position info with UseTokenInfo
  1660. UseTokenInfo greatly simplified
  1661. + added check for changed tree after first time firstpass
  1662. (if we could remove all the cases were it happen
  1663. we could skip all firstpass if firstpasscount > 1)
  1664. Only with ExtDebug
  1665. Revision 1.5 1998/04/30 15:59:40 pierre
  1666. * GDB works again better :
  1667. correct type info in one pass
  1668. + UseTokenInfo for better source position
  1669. * fixed one remaining bug in scanner for line counts
  1670. * several little fixes
  1671. Revision 1.4 1998/04/29 10:33:50 pierre
  1672. + added some code for ansistring (not complete nor working yet)
  1673. * corrected operator overloading
  1674. * corrected nasm output
  1675. + started inline procedures
  1676. + added starstarn : use ** for exponentiation (^ gave problems)
  1677. + started UseTokenInfo cond to get accurate positions
  1678. Revision 1.3 1998/04/27 23:10:28 peter
  1679. + new scanner
  1680. * $makelib -> if smartlink
  1681. * small filename fixes pmodule.setfilename
  1682. * moved import from files.pas -> import.pas
  1683. Revision 1.2 1998/04/07 11:09:04 peter
  1684. + filemode is set correct in tbufferedfile.reset
  1685. }