cobjects.pas 54 KB

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