cobjects.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  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. currtree^.right:=nil;
  1030. currtree^.left:=nil;
  1031. insertnode(currtree,currroot);
  1032. end;
  1033. end;
  1034. function tdictionary.rename(const olds,news : string):Pnamedindexobject;
  1035. var
  1036. spdval : longint;
  1037. lasthp,
  1038. hp,hp2,hp3 : Pnamedindexobject;
  1039. begin
  1040. spdval:=getspeedvalue(olds);
  1041. if assigned(hasharray) then
  1042. hp:=hasharray^[spdval mod hasharraysize]
  1043. else
  1044. hp:=root;
  1045. lasthp:=nil;
  1046. while assigned(hp) do
  1047. begin
  1048. if spdval>hp^.speedvalue then
  1049. begin
  1050. lasthp:=hp;
  1051. hp:=hp^.left
  1052. end
  1053. else
  1054. if spdval<hp^.speedvalue then
  1055. begin
  1056. lasthp:=hp;
  1057. hp:=hp^.right
  1058. end
  1059. else
  1060. begin
  1061. if (hp^.name=olds) then
  1062. begin
  1063. { get in hp2 the replacer for the root or hasharr }
  1064. hp2:=hp^.left;
  1065. hp3:=hp^.right;
  1066. if not assigned(hp2) then
  1067. begin
  1068. hp2:=hp^.right;
  1069. hp3:=hp^.left;
  1070. end;
  1071. { remove entry from the tree }
  1072. if assigned(lasthp) then
  1073. begin
  1074. if lasthp^.left=hp then
  1075. lasthp^.left:=hp2
  1076. else
  1077. lasthp^.right:=hp2;
  1078. end
  1079. else
  1080. begin
  1081. if assigned(hasharray) then
  1082. hasharray^[spdval mod hasharraysize]:=hp2
  1083. else
  1084. root:=hp2;
  1085. end;
  1086. { reinsert the hp3 in the tree from hp2 }
  1087. inserttree(hp3,hp2);
  1088. { reset node with new values }
  1089. stringdispose(hp^._name);
  1090. hp^._name:=stringdup(news);
  1091. hp^.speedvalue:=getspeedvalue(news);
  1092. hp^.left:=nil;
  1093. hp^.right:=nil;
  1094. { reinsert }
  1095. if assigned(hasharray) then
  1096. rename:=insertnode(hp,hasharray^[hp^.speedvalue mod hasharraysize])
  1097. else
  1098. rename:=insertnode(hp,root);
  1099. exit;
  1100. end
  1101. else
  1102. if olds>hp^.name then
  1103. begin
  1104. lasthp:=hp;
  1105. hp:=hp^.left
  1106. end
  1107. else
  1108. begin
  1109. lasthp:=hp;
  1110. hp:=hp^.right;
  1111. end;
  1112. end;
  1113. end;
  1114. end;
  1115. function Tdictionary.search(const s:string):Pnamedindexobject;
  1116. begin
  1117. search:=speedsearch(s,getspeedvalue(s));
  1118. end;
  1119. function Tdictionary.speedsearch(const s:string;speedvalue:longint):Pnamedindexobject;
  1120. var
  1121. newnode:Pnamedindexobject;
  1122. begin
  1123. if assigned(hasharray) then
  1124. newnode:=hasharray^[speedvalue mod hasharraysize]
  1125. else
  1126. newnode:=root;
  1127. while assigned(newnode) do
  1128. begin
  1129. if speedvalue>newnode^.speedvalue then
  1130. newnode:=newnode^.left
  1131. else
  1132. if speedvalue<newnode^.speedvalue then
  1133. newnode:=newnode^.right
  1134. else
  1135. begin
  1136. if (newnode^._name^=s) then
  1137. begin
  1138. speedsearch:=newnode;
  1139. exit;
  1140. end
  1141. else
  1142. if s>newnode^._name^ then
  1143. newnode:=newnode^.left
  1144. else
  1145. newnode:=newnode^.right;
  1146. end;
  1147. end;
  1148. speedsearch:=nil;
  1149. end;
  1150. {****************************************************************************
  1151. tdynamicarray
  1152. ****************************************************************************}
  1153. constructor tdynamicarray.init(Aelemlen,Agrow:longint);
  1154. begin
  1155. posn:=0;
  1156. count:=0;
  1157. limit:=0;
  1158. data:=nil;
  1159. elemlen:=Aelemlen;
  1160. growcount:=Agrow;
  1161. grow;
  1162. end;
  1163. function tdynamicarray.size:longint;
  1164. begin
  1165. size:=limit*elemlen;
  1166. end;
  1167. function tdynamicarray.usedsize:longint;
  1168. begin
  1169. usedsize:=count*elemlen;
  1170. end;
  1171. procedure tdynamicarray.grow;
  1172. var
  1173. osize : longint;
  1174. odata : pchar;
  1175. begin
  1176. osize:=size;
  1177. odata:=data;
  1178. inc(limit,growcount);
  1179. getmem(data,size);
  1180. if assigned(odata) then
  1181. begin
  1182. move(odata^,data^,osize);
  1183. freemem(odata,osize);
  1184. end;
  1185. fillchar(data[osize],growcount*elemlen,0);
  1186. end;
  1187. procedure tdynamicarray.align(i:longint);
  1188. var
  1189. j : longint;
  1190. begin
  1191. j:=(posn*elemlen mod i);
  1192. if j<>0 then
  1193. begin
  1194. j:=i-j;
  1195. while limit<(posn+j) do
  1196. grow;
  1197. inc(posn,j);
  1198. if (posn>count) then
  1199. count:=posn;
  1200. end;
  1201. end;
  1202. procedure tdynamicarray.seek(i:longint);
  1203. begin
  1204. while limit<i do
  1205. grow;
  1206. posn:=i;
  1207. if (posn>count) then
  1208. count:=posn;
  1209. end;
  1210. procedure tdynamicarray.write(var d;len:longint);
  1211. begin
  1212. while limit<(posn+len) do
  1213. grow;
  1214. move(d,data[posn*elemlen],len*elemlen);
  1215. inc(posn,len);
  1216. if (posn>count) then
  1217. count:=posn;
  1218. end;
  1219. procedure tdynamicarray.read(var d;len:longint);
  1220. begin
  1221. move(data[posn*elemlen],d,len*elemlen);
  1222. inc(posn,len);
  1223. if (posn>count) then
  1224. count:=posn;
  1225. end;
  1226. procedure tdynamicarray.writepos(pos:longint;var d;len:longint);
  1227. begin
  1228. while limit<(pos+len) do
  1229. grow;
  1230. move(d,data[pos*elemlen],len*elemlen);
  1231. posn:=pos+len;
  1232. if (posn>count) then
  1233. count:=posn;
  1234. end;
  1235. procedure tdynamicarray.readpos(pos:longint;var d;len:longint);
  1236. begin
  1237. while limit<(pos+len) do
  1238. grow;
  1239. move(data[pos*elemlen],d,len*elemlen);
  1240. posn:=pos+len;
  1241. if (posn>count) then
  1242. count:=posn;
  1243. end;
  1244. destructor tdynamicarray.done;
  1245. begin
  1246. if assigned(data) then
  1247. freemem(data,size);
  1248. end;
  1249. {****************************************************************************
  1250. tindexarray
  1251. ****************************************************************************}
  1252. constructor tindexarray.init(Agrowsize:longint);
  1253. begin
  1254. growsize:=Agrowsize;
  1255. size:=0;
  1256. count:=0;
  1257. data:=nil;
  1258. first:=nil;
  1259. end;
  1260. destructor tindexarray.done;
  1261. begin
  1262. if assigned(data) then
  1263. begin
  1264. clear;
  1265. freemem(data,size*4);
  1266. data:=nil;
  1267. end;
  1268. end;
  1269. function tindexarray.search(nr:longint):Pnamedindexobject;
  1270. begin
  1271. if nr<=count then
  1272. search:=data^[nr]
  1273. else
  1274. search:=nil;
  1275. end;
  1276. procedure tindexarray.clear;
  1277. var
  1278. i : longint;
  1279. begin
  1280. for i:=1 to count do
  1281. if assigned(data^[i]) then
  1282. begin
  1283. dispose(data^[i],done);
  1284. data^[i]:=nil;
  1285. end;
  1286. count:=0;
  1287. first:=nil;
  1288. end;
  1289. procedure tindexarray.foreach(proc2call : Tnamedindexcallback);
  1290. var
  1291. i : longint;
  1292. begin
  1293. for i:=1 to count do
  1294. if assigned(data^[i]) then
  1295. proc2call(data^[i]);
  1296. end;
  1297. procedure tindexarray.grow(gsize:longint);
  1298. var
  1299. osize : longint;
  1300. odata : Pnamedindexobjectarray;
  1301. begin
  1302. osize:=size;
  1303. odata:=data;
  1304. inc(size,gsize);
  1305. getmem(data,size*4);
  1306. if assigned(odata) then
  1307. begin
  1308. move(odata^,data^,osize*4);
  1309. freemem(odata,osize*4);
  1310. end;
  1311. fillchar(data^[osize+1],gsize*4,0);
  1312. end;
  1313. procedure tindexarray.deleteindex(p:Pnamedindexobject);
  1314. var
  1315. i : longint;
  1316. begin
  1317. i:=p^.indexnr;
  1318. { update counter }
  1319. if i=count then
  1320. dec(count);
  1321. { update linked list }
  1322. while (i>0) do
  1323. begin
  1324. dec(i);
  1325. if (i>0) and assigned(data^[i]) then
  1326. begin
  1327. data^[i]^.next:=data^[p^.indexnr]^.next;
  1328. break;
  1329. end;
  1330. end;
  1331. if i=0 then
  1332. first:=p^.next;
  1333. data^[p^.indexnr]:=nil;
  1334. { clear entry }
  1335. p^.indexnr:=-1;
  1336. p^.next:=nil;
  1337. end;
  1338. procedure tindexarray.delete(p:Pnamedindexobject);
  1339. begin
  1340. deleteindex(p);
  1341. dispose(p,done);
  1342. p:=nil;
  1343. end;
  1344. procedure tindexarray.insert(p:Pnamedindexobject);
  1345. var
  1346. i : longint;
  1347. begin
  1348. if p^.indexnr=-1 then
  1349. begin
  1350. inc(count);
  1351. p^.indexnr:=count;
  1352. end;
  1353. if p^.indexnr>count then
  1354. count:=p^.indexnr;
  1355. if count>size then
  1356. grow(((count div growsize)+1)*growsize);
  1357. data^[p^.indexnr]:=p;
  1358. { update linked list backward }
  1359. i:=p^.indexnr;
  1360. while (i>0) do
  1361. begin
  1362. dec(i);
  1363. if (i>0) and assigned(data^[i]) then
  1364. begin
  1365. data^[i]^.next:=p;
  1366. break;
  1367. end;
  1368. end;
  1369. if i=0 then
  1370. first:=p;
  1371. { update linked list forward }
  1372. i:=p^.indexnr;
  1373. while (i<=count) do
  1374. begin
  1375. inc(i);
  1376. if (i<=count) and assigned(data^[i]) then
  1377. begin
  1378. p^.next:=data^[i];
  1379. exit;
  1380. end;
  1381. end;
  1382. if i>count then
  1383. p^.next:=nil;
  1384. end;
  1385. {$ifdef BUFFEREDFILE}
  1386. {****************************************************************************
  1387. TBUFFEREDFILE
  1388. ****************************************************************************}
  1389. Const
  1390. crcseed = $ffffffff;
  1391. crctable : array[0..255] of longint = (
  1392. $00000000,$77073096,$ee0e612c,$990951ba,$076dc419,$706af48f,
  1393. $e963a535,$9e6495a3,$0edb8832,$79dcb8a4,$e0d5e91e,$97d2d988,
  1394. $09b64c2b,$7eb17cbd,$e7b82d07,$90bf1d91,$1db71064,$6ab020f2,
  1395. $f3b97148,$84be41de,$1adad47d,$6ddde4eb,$f4d4b551,$83d385c7,
  1396. $136c9856,$646ba8c0,$fd62f97a,$8a65c9ec,$14015c4f,$63066cd9,
  1397. $fa0f3d63,$8d080df5,$3b6e20c8,$4c69105e,$d56041e4,$a2677172,
  1398. $3c03e4d1,$4b04d447,$d20d85fd,$a50ab56b,$35b5a8fa,$42b2986c,
  1399. $dbbbc9d6,$acbcf940,$32d86ce3,$45df5c75,$dcd60dcf,$abd13d59,
  1400. $26d930ac,$51de003a,$c8d75180,$bfd06116,$21b4f4b5,$56b3c423,
  1401. $cfba9599,$b8bda50f,$2802b89e,$5f058808,$c60cd9b2,$b10be924,
  1402. $2f6f7c87,$58684c11,$c1611dab,$b6662d3d,$76dc4190,$01db7106,
  1403. $98d220bc,$efd5102a,$71b18589,$06b6b51f,$9fbfe4a5,$e8b8d433,
  1404. $7807c9a2,$0f00f934,$9609a88e,$e10e9818,$7f6a0dbb,$086d3d2d,
  1405. $91646c97,$e6635c01,$6b6b51f4,$1c6c6162,$856530d8,$f262004e,
  1406. $6c0695ed,$1b01a57b,$8208f4c1,$f50fc457,$65b0d9c6,$12b7e950,
  1407. $8bbeb8ea,$fcb9887c,$62dd1ddf,$15da2d49,$8cd37cf3,$fbd44c65,
  1408. $4db26158,$3ab551ce,$a3bc0074,$d4bb30e2,$4adfa541,$3dd895d7,
  1409. $a4d1c46d,$d3d6f4fb,$4369e96a,$346ed9fc,$ad678846,$da60b8d0,
  1410. $44042d73,$33031de5,$aa0a4c5f,$dd0d7cc9,$5005713c,$270241aa,
  1411. $be0b1010,$c90c2086,$5768b525,$206f85b3,$b966d409,$ce61e49f,
  1412. $5edef90e,$29d9c998,$b0d09822,$c7d7a8b4,$59b33d17,$2eb40d81,
  1413. $b7bd5c3b,$c0ba6cad,$edb88320,$9abfb3b6,$03b6e20c,$74b1d29a,
  1414. $ead54739,$9dd277af,$04db2615,$73dc1683,$e3630b12,$94643b84,
  1415. $0d6d6a3e,$7a6a5aa8,$e40ecf0b,$9309ff9d,$0a00ae27,$7d079eb1,
  1416. $f00f9344,$8708a3d2,$1e01f268,$6906c2fe,$f762575d,$806567cb,
  1417. $196c3671,$6e6b06e7,$fed41b76,$89d32be0,$10da7a5a,$67dd4acc,
  1418. $f9b9df6f,$8ebeeff9,$17b7be43,$60b08ed5,$d6d6a3e8,$a1d1937e,
  1419. $38d8c2c4,$4fdff252,$d1bb67f1,$a6bc5767,$3fb506dd,$48b2364b,
  1420. $d80d2bda,$af0a1b4c,$36034af6,$41047a60,$df60efc3,$a867df55,
  1421. $316e8eef,$4669be79,$cb61b38c,$bc66831a,$256fd2a0,$5268e236,
  1422. $cc0c7795,$bb0b4703,$220216b9,$5505262f,$c5ba3bbe,$b2bd0b28,
  1423. $2bb45a92,$5cb36a04,$c2d7ffa7,$b5d0cf31,$2cd99e8b,$5bdeae1d,
  1424. $9b64c2b0,$ec63f226,$756aa39c,$026d930a,$9c0906a9,$eb0e363f,
  1425. $72076785,$05005713,$95bf4a82,$e2b87a14,$7bb12bae,$0cb61b38,
  1426. $92d28e9b,$e5d5be0d,$7cdcefb7,$0bdbdf21,$86d3d2d4,$f1d4e242,
  1427. $68ddb3f8,$1fda836e,$81be16cd,$f6b9265b,$6fb077e1,$18b74777,
  1428. $88085ae6,$ff0f6a70,$66063bca,$11010b5c,$8f659eff,$f862ae69,
  1429. $616bffd3,$166ccf45,$a00ae278,$d70dd2ee,$4e048354,$3903b3c2,
  1430. $a7672661,$d06016f7,$4969474d,$3e6e77db,$aed16a4a,$d9d65adc,
  1431. $40df0b66,$37d83bf0,$a9bcae53,$debb9ec5,$47b2cf7f,$30b5ffe9,
  1432. $bdbdf21c,$cabac28a,$53b39330,$24b4a3a6,$bad03605,$cdd70693,
  1433. $54de5729,$23d967bf,$b3667a2e,$c4614ab8,$5d681b02,$2a6f2b94,
  1434. $b40bbe37,$c30c8ea1,$5a05df1b,$2d02ef8d);
  1435. constructor tbufferedfile.init(const filename : string;_bufsize : longint);
  1436. begin
  1437. assign(f,filename);
  1438. bufsize:=_bufsize;
  1439. bufpos:=0;
  1440. buflast:=0;
  1441. do_crc:=false;
  1442. iomode:=0;
  1443. tempclosed:=false;
  1444. change_endian:=false;
  1445. clear_crc;
  1446. end;
  1447. destructor tbufferedfile.done;
  1448. begin
  1449. close;
  1450. end;
  1451. procedure tbufferedfile.clear_crc;
  1452. begin
  1453. crc:=crcseed;
  1454. end;
  1455. procedure tbufferedfile.setbuf(p : pchar;s : longint);
  1456. begin
  1457. flush;
  1458. freemem(buf,bufsize);
  1459. bufsize:=s;
  1460. buf:=p;
  1461. end;
  1462. function tbufferedfile.reset:boolean;
  1463. var
  1464. ofm : byte;
  1465. begin
  1466. ofm:=filemode;
  1467. iomode:=1;
  1468. getmem(buf,bufsize);
  1469. filemode:=0;
  1470. {$I-}
  1471. system.reset(f,1);
  1472. {$I+}
  1473. reset:=(ioresult=0);
  1474. filemode:=ofm;
  1475. end;
  1476. procedure tbufferedfile.rewrite;
  1477. begin
  1478. iomode:=2;
  1479. getmem(buf,bufsize);
  1480. system.rewrite(f,1);
  1481. end;
  1482. procedure tbufferedfile.flush;
  1483. var
  1484. {$ifdef FPC}
  1485. count : longint;
  1486. {$else}
  1487. count : integer;
  1488. {$endif}
  1489. begin
  1490. if iomode=2 then
  1491. begin
  1492. if bufpos=0 then
  1493. exit;
  1494. blockwrite(f,buf^,bufpos)
  1495. end
  1496. else if iomode=1 then
  1497. if buflast=bufpos then
  1498. begin
  1499. blockread(f,buf^,bufsize,count);
  1500. buflast:=count;
  1501. end;
  1502. bufpos:=0;
  1503. end;
  1504. function tbufferedfile.getftime : longint;
  1505. var
  1506. l : longint;
  1507. {$ifdef linux}
  1508. Info : Stat;
  1509. {$endif}
  1510. begin
  1511. {$ifndef linux}
  1512. { this only works if the file is open !! }
  1513. dos.getftime(f,l);
  1514. {$else}
  1515. Fstat(f,Info);
  1516. l:=info.mtime;
  1517. {$endif}
  1518. getftime:=l;
  1519. end;
  1520. function tbufferedfile.getsize : longint;
  1521. begin
  1522. getsize:=filesize(f);
  1523. end;
  1524. procedure tbufferedfile.seek(l : longint);
  1525. begin
  1526. if iomode=2 then
  1527. begin
  1528. flush;
  1529. system.seek(f,l);
  1530. end
  1531. else if iomode=1 then
  1532. begin
  1533. { forces a reload }
  1534. bufpos:=buflast;
  1535. system.seek(f,l);
  1536. flush;
  1537. end;
  1538. end;
  1539. type
  1540. {$ifdef tp}
  1541. bytearray1 = array [1..65535] of byte;
  1542. {$else}
  1543. bytearray1 = array [1..10000000] of byte;
  1544. {$endif}
  1545. procedure tbufferedfile.read_data(var data;bytes : longint;var count : longint);
  1546. var
  1547. p : pchar;
  1548. c,i : longint;
  1549. begin
  1550. p:=pchar(@data);
  1551. count:=0;
  1552. while bytes-count>0 do
  1553. begin
  1554. if bytes-count>buflast-bufpos then
  1555. begin
  1556. move((buf+bufpos)^,(p+count)^,buflast-bufpos);
  1557. inc(count,buflast-bufpos);
  1558. bufpos:=buflast;
  1559. flush;
  1560. { can't we read anything ? }
  1561. if bufpos=buflast then
  1562. break;
  1563. end
  1564. else
  1565. begin
  1566. move((buf+bufpos)^,(p+count)^,bytes-count);
  1567. inc(bufpos,bytes-count);
  1568. count:=bytes;
  1569. break;
  1570. end;
  1571. end;
  1572. if do_crc then
  1573. begin
  1574. c:=crc;
  1575. for i:=1 to bytes do
  1576. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1577. crc:=c;
  1578. end;
  1579. end;
  1580. procedure tbufferedfile.write_data(var data;count : longint);
  1581. var
  1582. c,i : longint;
  1583. begin
  1584. if bufpos+count>bufsize then
  1585. flush;
  1586. move(data,(buf+bufpos)^,count);
  1587. inc(bufpos,count);
  1588. if do_crc then
  1589. begin
  1590. c:=crc;
  1591. for i:=1 to count do
  1592. c:=(c shr 8) xor crctable[byte(c) xor (bytearray1(data)[i])];
  1593. crc:=c;
  1594. end;
  1595. end;
  1596. function tbufferedfile.getcrc : longint;
  1597. begin
  1598. getcrc:=crc xor crcseed;
  1599. end;
  1600. procedure tbufferedfile.write_string(const s : string);
  1601. begin
  1602. if bufpos+length(s)>bufsize then
  1603. flush;
  1604. { why is there not CRC here ??? }
  1605. move(s[1],(buf+bufpos)^,length(s));
  1606. inc(bufpos,length(s));
  1607. { should be
  1608. write_data(s[1],length(s)); }
  1609. end;
  1610. procedure tbufferedfile.write_pchar(p : pchar);
  1611. var
  1612. l : longint;
  1613. begin
  1614. l:=strlen(p);
  1615. if l>=bufsize then
  1616. runerror(222);
  1617. { why is there not CRC here ???}
  1618. if bufpos+l>bufsize then
  1619. flush;
  1620. move(p^,(buf+bufpos)^,l);
  1621. inc(bufpos,l);
  1622. { should be
  1623. write_data(p^,l); }
  1624. end;
  1625. procedure tbufferedfile.write_byte(b : byte);
  1626. begin
  1627. write_data(b,sizeof(byte));
  1628. end;
  1629. procedure tbufferedfile.write_long(l : longint);
  1630. var
  1631. w1,w2 : word;
  1632. begin
  1633. if change_endian then
  1634. begin
  1635. w1:=l and $ffff;
  1636. w2:=l shr 16;
  1637. l:=swap(w2)+(longint(swap(w1)) shl 16);
  1638. write_data(l,sizeof(longint))
  1639. end
  1640. else
  1641. write_data(l,sizeof(longint))
  1642. end;
  1643. procedure tbufferedfile.write_word(w : word);
  1644. begin
  1645. if change_endian then
  1646. begin
  1647. w:=swap(w);
  1648. write_data(w,sizeof(word))
  1649. end
  1650. else
  1651. write_data(w,sizeof(word));
  1652. end;
  1653. procedure tbufferedfile.write_double(d : double);
  1654. begin
  1655. write_data(d,sizeof(double));
  1656. end;
  1657. function tbufferedfile.getpath : string;
  1658. begin
  1659. {$ifdef dummy}
  1660. getpath:=strpas(filerec(f).name);
  1661. {$endif}
  1662. getpath:='';
  1663. end;
  1664. procedure tbufferedfile.close;
  1665. begin
  1666. if iomode<>0 then
  1667. begin
  1668. flush;
  1669. system.close(f);
  1670. freemem(buf,bufsize);
  1671. buf:=nil;
  1672. iomode:=0;
  1673. end;
  1674. end;
  1675. procedure tbufferedfile.tempclose;
  1676. begin
  1677. if iomode<>0 then
  1678. begin
  1679. temppos:=system.filepos(f);
  1680. tempmode:=iomode;
  1681. tempclosed:=true;
  1682. system.close(f);
  1683. iomode:=0;
  1684. end
  1685. else
  1686. tempclosed:=false;
  1687. end;
  1688. procedure tbufferedfile.tempreopen;
  1689. var
  1690. ofm : byte;
  1691. begin
  1692. if tempclosed then
  1693. begin
  1694. case tempmode of
  1695. 1 : begin
  1696. ofm:=filemode;
  1697. iomode:=1;
  1698. filemode:=0;
  1699. system.reset(f,1);
  1700. filemode:=ofm;
  1701. end;
  1702. 2 : begin
  1703. iomode:=2;
  1704. system.rewrite(f,1);
  1705. end;
  1706. end;
  1707. system.seek(f,temppos);
  1708. tempclosed:=false;
  1709. end;
  1710. end;
  1711. {$endif BUFFEREDFILE}
  1712. end.
  1713. {
  1714. $Log$
  1715. Revision 1.34 1999-06-15 10:14:19 peter
  1716. * merged
  1717. Revision 1.33.2.1 1999/06/15 10:12:22 peter
  1718. * fixed inserttree which didn't reset left,right
  1719. Revision 1.33 1999/05/31 23:33:21 peter
  1720. * fixed tdictionary rename which didn't reset left,right when
  1721. reinserting
  1722. Revision 1.32 1999/05/27 19:44:23 peter
  1723. * removed oldasm
  1724. * plabel -> pasmlabel
  1725. * -a switches to source writing automaticly
  1726. * assembler readers OOPed
  1727. * asmsymbol automaticly external
  1728. * jumptables and other label fixes for asm readers
  1729. Revision 1.31 1999/05/21 13:54:59 peter
  1730. * NEWLAB for label as symbol
  1731. Revision 1.30 1999/05/21 10:38:59 peter
  1732. * fixed deleteindex which didn't reset indexnr and set first wrong
  1733. Revision 1.29 1999/05/08 19:47:27 peter
  1734. * indexarray.delete resets pointer after dispose
  1735. Revision 1.28 1999/05/05 10:05:48 florian
  1736. * a delphi compiled compiler recompiles ppc
  1737. Revision 1.27 1999/05/05 09:19:03 florian
  1738. * more fixes to get it with delphi running
  1739. Revision 1.26 1999/04/21 09:43:31 peter
  1740. * storenumber works
  1741. * fixed some typos in double_checksum
  1742. + incompatible types type1 and type2 message (with storenumber)
  1743. Revision 1.25 1999/04/15 10:01:44 peter
  1744. * small update for storenumber
  1745. Revision 1.24 1999/04/14 09:14:47 peter
  1746. * first things to store the symbol/def number in the ppu
  1747. Revision 1.23 1999/04/08 20:59:39 florian
  1748. * fixed problem with default properties which are a class
  1749. * case bug (from the mailing list with -O2) fixed, the
  1750. distance of the case labels can be greater than the positive
  1751. range of a longint => it is now a dword for fpc
  1752. Revision 1.22 1999/03/31 13:55:10 peter
  1753. * assembler inlining working for ag386bin
  1754. Revision 1.21 1999/03/19 16:35:29 pierre
  1755. * Tnamedindexobject done also removed left and right
  1756. Revision 1.20 1999/03/18 20:30:45 peter
  1757. + .a writer
  1758. Revision 1.19 1999/03/01 13:32:00 pierre
  1759. * external used before implemented problem fixed
  1760. Revision 1.18 1999/02/24 00:59:13 peter
  1761. * small updates for ag386bin
  1762. Revision 1.17 1999/01/19 11:00:33 daniel
  1763. + Tdictionary object: Tsymtable will become object(TTdictionary) in the
  1764. future
  1765. + Tnamed_item object: Tsym will become object(Tnamed_item) in the future
  1766. Revision 1.16 1998/11/04 10:11:37 peter
  1767. * ansistring fixes
  1768. Revision 1.15 1998/10/19 18:04:40 peter
  1769. + tstringcontainer.init_no_doubles
  1770. Revision 1.14 1998/09/18 16:03:37 florian
  1771. * some changes to compile with Delphi
  1772. Revision 1.13 1998/08/12 19:28:16 peter
  1773. * better libc support
  1774. Revision 1.12 1998/07/14 14:46:47 peter
  1775. * released NEWINPUT
  1776. Revision 1.11 1998/07/07 11:19:54 peter
  1777. + NEWINPUT for a better inputfile and scanner object
  1778. Revision 1.10 1998/07/01 15:26:59 peter
  1779. * better bufferfile.reset error handling
  1780. Revision 1.9 1998/06/03 23:40:37 peter
  1781. + unlimited file support, release tempclose
  1782. Revision 1.8 1998/05/20 09:42:33 pierre
  1783. + UseTokenInfo now default
  1784. * unit in interface uses and implementation uses gives error now
  1785. * only one error for unknown symbol (uses lastsymknown boolean)
  1786. the problem came from the label code !
  1787. + first inlined procedures and function work
  1788. (warning there might be allowed cases were the result is still wrong !!)
  1789. * UseBrower updated gives a global list of all position of all used symbols
  1790. with switch -gb
  1791. Revision 1.7 1998/05/06 18:36:53 peter
  1792. * tai_section extended with code,data,bss sections and enumerated type
  1793. * ident 'compiled by FPC' moved to pmodules
  1794. * small fix for smartlink
  1795. Revision 1.6 1998/05/06 08:38:37 pierre
  1796. * better position info with UseTokenInfo
  1797. UseTokenInfo greatly simplified
  1798. + added check for changed tree after first time firstpass
  1799. (if we could remove all the cases were it happen
  1800. we could skip all firstpass if firstpasscount > 1)
  1801. Only with ExtDebug
  1802. Revision 1.5 1998/04/30 15:59:40 pierre
  1803. * GDB works again better :
  1804. correct type info in one pass
  1805. + UseTokenInfo for better source position
  1806. * fixed one remaining bug in scanner for line counts
  1807. * several little fixes
  1808. Revision 1.4 1998/04/29 10:33:50 pierre
  1809. + added some code for ansistring (not complete nor working yet)
  1810. * corrected operator overloading
  1811. * corrected nasm output
  1812. + started inline procedures
  1813. + added starstarn : use ** for exponentiation (^ gave problems)
  1814. + started UseTokenInfo cond to get accurate positions
  1815. Revision 1.3 1998/04/27 23:10:28 peter
  1816. + new scanner
  1817. * $makelib -> if smartlink
  1818. * small filename fixes pmodule.setfilename
  1819. * moved import from files.pas -> import.pas
  1820. Revision 1.2 1998/04/07 11:09:04 peter
  1821. + filemode is set correct in tbufferedfile.reset
  1822. }