cobjects.pas 62 KB

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