cobjects.pas 55 KB

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