cobjects.pas 53 KB

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