string.cc 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include <stdarg.h>
  9. #include <math.h>
  10. #include "string/string.hh"
  11. #include "file/file.hh"
  12. #include "memory/malloc.hh"
  13. #include "arch.hh"
  14. #include "error/error.hh"
  15. #include "memory/lalloc.hh"
  16. #include "memory/growheap.hh"
  17. i4_string_manager_class i4_string_man;
  18. i4_string_manager_class::node::node_allocator *i4_string_manager_class::node::nodes=0;
  19. w32 i4_string_manager_class::node::nodes_ref=0; // number of string managers using 'nodes'
  20. i4_string_manager_class::array_node::node_allocator *i4_string_manager_class::array_node::nodes=0;
  21. static i4_const_str *null_string;
  22. ////////////////////////////////////////////////////////////////////////
  23. //
  24. // Constant String Methods
  25. //
  26. i4_str::~i4_str()
  27. {
  28. if (ptr)
  29. i4_free((char *)ptr);
  30. }
  31. #ifndef i4_NEW_CHECK
  32. void *i4_string_manager_class::node::operator new(size_t size)
  33. {
  34. return nodes->alloc();
  35. }
  36. void i4_string_manager_class::node::operator delete(void *ptr)
  37. {
  38. nodes->free((node *)ptr);
  39. }
  40. #endif
  41. i4_string_manager_class::node::~node()
  42. {
  43. if (left)
  44. delete left;
  45. if (right)
  46. delete right;
  47. }
  48. #ifndef i4_NEW_CHECK
  49. void *i4_string_manager_class::array_node::operator new(size_t size)
  50. {
  51. return nodes->alloc();
  52. }
  53. void i4_string_manager_class::array_node::operator delete(void *ptr)
  54. {
  55. nodes->free((array_node *)ptr);
  56. }
  57. #endif
  58. i4_string_manager_class::array_node::~array_node()
  59. {
  60. if (left)
  61. delete left;
  62. if (right)
  63. delete right;
  64. }
  65. i4_const_str::iterator i4_const_str::strstr(const i4_const_str &needle_to_find) const
  66. {
  67. iterator j=needle_to_find.begin();
  68. for (iterator i=begin(); i!=end(); ++i)
  69. {
  70. iterator k=i;
  71. j=needle_to_find.begin();
  72. while (j.get()==k.get())
  73. {
  74. ++j;
  75. if (j==needle_to_find.end())
  76. return i;
  77. ++k;
  78. }
  79. }
  80. return end();
  81. }
  82. i4_str *i4_const_str::vsprintf(w32 max_length, va_list &ap) const
  83. {
  84. i4_str *ns=new i4_str(*this,max_length);
  85. char *fmt=ptr,*out=ns->ptr;
  86. int l=length();
  87. while (*fmt && l)
  88. {
  89. if (*fmt=='%')
  90. {
  91. fmt++;
  92. if (*fmt=='S')
  93. {
  94. l--;
  95. i4_const_str *cs=va_arg(ap, i4_const_str *);
  96. if (cs)
  97. {
  98. i4_const_str::iterator it=cs->begin();
  99. while (it!=cs->end())
  100. {
  101. *out=(char)it.get().value();
  102. out++;
  103. ++it;
  104. }
  105. }
  106. } else
  107. {
  108. char fmt_str[10],*fs;
  109. fs=fmt_str+1;
  110. fmt_str[0]='%';
  111. do
  112. {
  113. *fs=*fmt;
  114. fs++;
  115. fmt++;
  116. l--;
  117. } while (fmt[-1]!='s' &&
  118. fmt[-1]!='d' &&
  119. fmt[-1]!='f' &&
  120. fmt[-1]!='p' &&
  121. fmt[-1]!='g' &&
  122. fmt[-1]!='c' &&
  123. fmt[-1]!='i' &&
  124. fmt[-1]!='x' &&
  125. fmt[-1]!='u' &&
  126. fmt[-1]!='X' &&
  127. fmt[-1]!='o');
  128. fmt--;
  129. *fs=0;
  130. switch (*fmt)
  131. {
  132. case 'd' :
  133. case 'i' :
  134. case 'x' :
  135. case 'X' :
  136. case 'o' :
  137. ::sprintf(out,fmt_str,va_arg(ap,int));
  138. break;
  139. case 'f' :
  140. {
  141. float f=va_arg(ap, double);
  142. ::sprintf(out,fmt_str,f);
  143. } break;
  144. case 'g' :
  145. ::sprintf(out,fmt_str,va_arg(ap,double));
  146. break;
  147. default :
  148. ::sprintf(out,fmt_str,va_arg(ap,void *));
  149. break;
  150. }
  151. while (*out)
  152. out++;
  153. }
  154. } else
  155. {
  156. *out=*fmt;
  157. out++;
  158. }
  159. fmt++;
  160. l--;
  161. }
  162. *out=0;
  163. ns->len=strlen(ns->ptr);
  164. return ns;
  165. }
  166. i4_str *i4_const_str::sprintf(w32 max_length, ...) const
  167. {
  168. va_list ap;
  169. va_start(ap, max_length);
  170. i4_str *ret=vsprintf(max_length,ap);
  171. va_end(ap);
  172. return ret;
  173. }
  174. w32 i4_const_str::iterator::read_ascii(char *buffer, w32 buffer_size)
  175. {
  176. w32 count=0;
  177. iterator cur(*this);
  178. while (cur.get().is_space())
  179. {
  180. ++cur;
  181. ++node;
  182. }
  183. while (buffer_size>0 && cur.get().value() && !cur.get().is_space())
  184. {
  185. *(buffer++)=cur.get().value();
  186. buffer_size--;
  187. ++cur;
  188. ++node;
  189. ++count;
  190. }
  191. *buffer=0;
  192. return count;
  193. }
  194. i4_str *i4_const_str::iterator::read_string()
  195. {
  196. iterator start(*this);
  197. while (start.get().value() && start.get().is_space())
  198. {
  199. ++start;
  200. ++node;
  201. }
  202. if (!start.get().value())
  203. return 0;
  204. w32 len=0;
  205. iterator cur(start);
  206. while (cur.get().value() && !cur.get().is_space())
  207. {
  208. ++len;
  209. ++cur;
  210. ++node;
  211. }
  212. i4_str *ret=new i4_str(start,cur,len+1);
  213. return ret;
  214. }
  215. sw32 i4_const_str::iterator::read_number()
  216. {
  217. iterator cur(*this);
  218. while (cur.get().is_space())
  219. {
  220. ++cur;
  221. ++node;
  222. }
  223. sw32 x=0;
  224. i4_bool error=i4_F;
  225. i4_bool neg;
  226. if (cur.get() == i4_char('-'))
  227. {
  228. neg=i4_T;
  229. ++cur;
  230. ++node;
  231. }
  232. else neg=i4_F;
  233. while (cur.get().value() && !cur.get().is_space() && !error)
  234. {
  235. char_type c=cur.get().value();
  236. if (c<'0' || c>'9')
  237. error=i4_T;
  238. else
  239. {
  240. x=(x*10)+c-'0';
  241. ++node;
  242. ++cur;
  243. }
  244. }
  245. if (neg)
  246. x=-x;
  247. if (error)
  248. return 0;
  249. else return x;
  250. }
  251. double i4_const_str::iterator::read_float()
  252. {
  253. while (get().is_space())
  254. ++(*this);
  255. double x=0;
  256. sw32 exp=0;
  257. i4_bool
  258. error=i4_F;
  259. i4_bool neg = i4_F;
  260. if (get() == i4_char('-'))
  261. {
  262. neg=i4_T;
  263. ++(*this);
  264. }
  265. else if (get() == i4_char('+'))
  266. ++(*this);
  267. while (get().value())
  268. {
  269. if (get()<i4_char('0') || get()>i4_char('9'))
  270. break;
  271. x = (x*10) + (double)(get().value()-'0');
  272. ++(*this);
  273. }
  274. if (get() == i4_char('.'))
  275. {
  276. ++(*this);
  277. while (get().value())
  278. {
  279. if (get()<i4_char('0') || get()>i4_char('9'))
  280. break;
  281. x = (x*10) + (double)(get().value()-'0');
  282. exp--;
  283. ++(*this);
  284. }
  285. }
  286. if (get() == i4_char('e') || get() == i4_char('E'))
  287. {
  288. ++(*this);
  289. exp += read_number();
  290. }
  291. if (neg)
  292. x=-x;
  293. if (error)
  294. return 0;
  295. return x*pow(10,exp);
  296. }
  297. ////////////////////////////////////////////////////////////////////////
  298. //
  299. // String methods
  300. //
  301. void i4_str::alloc(w16 _buf_len)
  302. {
  303. buf_len = _buf_len;
  304. if (buf_len==0)
  305. ptr=0;
  306. else
  307. ptr=(char_type *)i4_malloc(sizeof(char_type)*(buf_len+1),"i4_str");
  308. }
  309. void i4_str::init_from_string(const i4_const_str &str, w16 _buf_len)
  310. {
  311. if (!_buf_len)
  312. {
  313. ptr=0;
  314. return ;
  315. }
  316. alloc(_buf_len);
  317. len = (str.length() >= buf_len) ? buf_len : str.length();
  318. i4_const_str::iterator p=str.begin();
  319. char_type *c=ptr;
  320. for (w16 x=0 ;x<len; ++x)
  321. {
  322. *c = (char_type)( p.get().value() );
  323. ++p;
  324. ++c;
  325. }
  326. *c=0;
  327. }
  328. i4_str::i4_str(const i4_const_str::iterator start,
  329. const i4_const_str::iterator last, w16 _buf_len)
  330. : i4_const_str(0)
  331. {
  332. alloc(_buf_len);
  333. len=0;
  334. char_type *c=ptr;
  335. w16 count=buf_len;
  336. i4_const_str::iterator s(start);
  337. while (s!=last && count)
  338. {
  339. *c=(char_type)( s.get().value() );
  340. ++c;
  341. ++s;
  342. ++len;
  343. count--;
  344. }
  345. *c=0;
  346. }
  347. void i4_str::insert(i4_str::iterator p, const i4_char ch)
  348. // insert ch before p
  349. {
  350. if (len+1<buf_len)
  351. {
  352. if (p==end())
  353. {
  354. p.set(ch);
  355. ++p;
  356. p.set(0);
  357. ++len;
  358. }
  359. else
  360. {
  361. iterator c(ptr+len+1);
  362. *c.node=0;
  363. while (c!=p)
  364. {
  365. iterator q(c);
  366. --c;
  367. q.set(c.get());
  368. --q;
  369. }
  370. c.set(ch);
  371. ++len;
  372. }
  373. }
  374. }
  375. void i4_str::insert(i4_str::iterator p, const i4_const_str &other)
  376. // insert other before p
  377. {
  378. sw16 move_length=ptr_diff(p,end());
  379. sw16 max_move=buf_len-(ptr_diff(begin(),p)+other.length()+1);
  380. if (max_move>0) // see if anything can be moved
  381. {
  382. iterator p2(0);
  383. if (move_length>max_move)
  384. {
  385. move_length=max_move;
  386. p2=iterator(ptr+buf_len-1);
  387. len=buf_len-1;
  388. } else
  389. {
  390. p2=iterator(ptr+ptr_diff(begin(),p)+other.length()+move_length);
  391. len+=other.length();
  392. }
  393. p2.set(0);
  394. --p2;
  395. i4_const_str::iterator p1(p);
  396. for (w16 x=1;x<move_length;++x) ++p1;
  397. while (move_length)
  398. {
  399. p2.set(p1.get());
  400. --p2;
  401. --p1;
  402. move_length--;
  403. }
  404. } else
  405. {
  406. len=buf_len-1;
  407. ptr[buf_len-1]=0;
  408. }
  409. // now see how much we can insert
  410. sw16 insert_len=other.length();
  411. if (insert_len+ptr_diff(begin(),p)>=buf_len)
  412. insert_len=buf_len-ptr_diff(begin(),p)-1;
  413. if (insert_len>0)
  414. {
  415. i4_const_str::iterator ip=other.begin();
  416. while (insert_len)
  417. {
  418. p.set(ip.get());
  419. ++p;
  420. ++ip;
  421. insert_len--;
  422. }
  423. }
  424. }
  425. void i4_str::remove(i4_str::iterator start, i4_str::iterator last)
  426. {
  427. sw32 rm_len=ptr_diff(last,end());
  428. len-=ptr_diff(start,last);
  429. while (rm_len>0)
  430. {
  431. start.set(last.get());
  432. ++start;
  433. ++last;
  434. rm_len--;
  435. }
  436. start.set(i4_char(0));
  437. }
  438. void i4_str::to_lower()
  439. {
  440. for (i4_str::iterator p=begin(); p!=end(); ++p)
  441. p.set(p.get().to_lower());
  442. }
  443. void i4_str::to_upper()
  444. {
  445. for (i4_str::iterator p=begin(); p!=end(); ++p)
  446. p.set(p.get().to_upper());
  447. }
  448. ////////////////////////////////////////////////////////////////////////
  449. //
  450. // String Manager Methods
  451. //
  452. const i4_const_str &i4_string_manager_class::get(const char *internal_name)
  453. {
  454. if (!internal_name)
  455. return *null_string;
  456. node *n=root;
  457. while (n)
  458. {
  459. int c=strcmp(internal_name,n->str_token);
  460. if (c<0)
  461. n=n->left;
  462. else if (c>0)
  463. n=n->right;
  464. else return n->value;
  465. }
  466. i4_warning("symbol not found %s\n",internal_name);
  467. return *null_string;
  468. }
  469. void i4_string_manager_class::show_node(node *who)
  470. {
  471. if (who)
  472. {
  473. show_node(who->left);
  474. i4_warning("%s\n",who->str_token);
  475. show_node(who->right);
  476. }
  477. }
  478. void i4_string_manager_class::show_nodes()
  479. {
  480. if (root)
  481. show_node(root);
  482. }
  483. i4_const_str *i4_string_manager_class::get_array(const char *internal_name)
  484. {
  485. array_node *n=array_root;
  486. while (n)
  487. {
  488. int c=strcmp(internal_name,n->str_token);
  489. if (c<0)
  490. n=n->left;
  491. else if (c>0)
  492. n=n->right;
  493. else
  494. {
  495. w32 count=0,x;
  496. char **c=n->value;
  497. i4_const_str *ret;
  498. while (*c)
  499. {
  500. c++;
  501. count++;
  502. }
  503. ret=(i4_const_str *)i4_malloc(sizeof(i4_const_str)*(count+1),"array list");
  504. for (x=0; x<count; x++)
  505. {
  506. ret[x].ptr=n->value[x];
  507. ret[x].len=strlen(ret[x].ptr);
  508. }
  509. ret[count].ptr=0;
  510. ret[count].len=0;
  511. return ret;
  512. }
  513. }
  514. i4_error("array not found %s\n",internal_name);
  515. return 0;
  516. }
  517. void i4_string_manager_class::init()
  518. {
  519. string_heap=new i4_grow_heap_class(0x1000,0x1000);
  520. if (!node::nodes)
  521. {
  522. node::nodes=new i4_linear_allocator(sizeof(node),0,128,"strings");
  523. array_node::nodes=new i4_linear_allocator(sizeof(array_node),
  524. 0,128,"arrays");
  525. null_string=new i4_const_str(0);
  526. }
  527. i4_string_manager_class::node::nodes_ref++;
  528. root=0;
  529. array_root=0;
  530. }
  531. i4_string_manager_class::~i4_string_manager_class()
  532. {
  533. if (string_heap)
  534. delete string_heap;
  535. }
  536. void i4_string_manager_class::uninit()
  537. {
  538. if (root)
  539. {
  540. delete_node(root);
  541. root=0;
  542. }
  543. if (array_root)
  544. {
  545. delete_array_node(array_root);
  546. array_root=0;
  547. }
  548. if (string_heap)
  549. {
  550. delete string_heap;
  551. string_heap=0;
  552. }
  553. i4_string_manager_class::node::nodes_ref--;
  554. if (i4_string_manager_class::node::nodes_ref==0)
  555. {
  556. delete node::nodes; // if there are multiple string managers we can only do this
  557. delete array_node::nodes; // once, so for now we won't do it
  558. node::nodes=0;
  559. delete null_string;
  560. null_string=0;
  561. }
  562. }
  563. i4_string_manager_class::i4_string_manager_class()
  564. {
  565. root=0;
  566. array_root=0;
  567. string_heap=0;
  568. }
  569. inline i4_bool space_char(char ch)
  570. {
  571. return (i4_bool)(ch==' ' || ch=='\n' || ch=='\t' || ch=='\r');
  572. }
  573. char *i4_string_manager_class::alloc_str(char *string)
  574. {
  575. w16 len=strlen(string)+1;
  576. void *storage=string_heap->malloc(len,"string");
  577. memcpy(storage,string,len);
  578. return (char *)storage;
  579. }
  580. void i4_string_manager_class::add_node(char *token, char *string)
  581. {
  582. node **p=&root;
  583. while (*p)
  584. {
  585. if (strcmp(token,(*p)->str_token)<0)
  586. p=&(*p)->left;
  587. else
  588. p=&(*p)->right;
  589. }
  590. *p=new_node(alloc_str(token),i4_const_str(alloc_str(string)));
  591. }
  592. void i4_string_manager_class::add_array_node(char *token, char **array, w32 total)
  593. {
  594. array_node **p=&array_root;
  595. while (*p)
  596. {
  597. if (strcmp(token,(*p)->str_token)<0)
  598. p=&(*p)->left;
  599. else
  600. p=&(*p)->right;
  601. }
  602. char **new_array;
  603. new_array = (char**)ALIGN_FORWARD( string_heap->malloc((total+1)*sizeof(char*) + sizeof(int) - 1,
  604. "string_array") );
  605. for (int i=0; i<total; i++)
  606. new_array[i]=array[i];
  607. new_array[total] = 0;
  608. *p=new_array_node(alloc_str(token),new_array);
  609. }
  610. static void skip_white(char *&s, w32 &line_on)
  611. {
  612. char *sl=s; // use local pointer so a register can be substituted
  613. while (1)
  614. {
  615. switch (*sl)
  616. {
  617. case ' ' :
  618. case '\t' :
  619. case '\r' :
  620. sl++;
  621. break;
  622. case '\n' :
  623. sl++;
  624. line_on++;
  625. break;
  626. case '/' :
  627. {
  628. if (sl[1]=='/')
  629. {
  630. while (*sl && *sl!='\n' && *sl!='\r')
  631. sl++;
  632. } else
  633. {
  634. s=sl;
  635. return ;
  636. }
  637. } break;
  638. default :
  639. s=sl;
  640. return;
  641. }
  642. }
  643. }
  644. static void *dump_file_buf;
  645. static int dump_buf_len;
  646. void i4_string_manager_class::expand_macro(char *&s, char *&buf, w32 &line_on, char *error_prefix)
  647. {
  648. char *sl=s+1,*start;
  649. w32 len;
  650. skip_white(sl,line_on);
  651. if (*sl!='{')
  652. {
  653. i4_file_class *fp=i4_open("dump.txt", I4_WRITE);
  654. fp->write(dump_file_buf, dump_buf_len);
  655. delete fp;
  656. i4_error("%s:%d:expected { after $",error_prefix,line_on);
  657. }
  658. sl++;
  659. start=buf;
  660. get_token(sl,start,line_on,error_prefix);
  661. *start=0;
  662. i4_const_str str=get(buf);
  663. if (str.null())
  664. i4_error("%s:%d: macro symbol has no value '%s'",error_prefix, line_on, buf);
  665. start=str.ptr;
  666. len=str.length();
  667. while (len)
  668. {
  669. len--;
  670. *buf=*start;
  671. start++;
  672. buf++;
  673. }
  674. skip_white(sl,line_on);
  675. if (*sl!='}')
  676. i4_error("%s:%d: expected }",error_prefix,line_on);
  677. sl++;
  678. s=sl;
  679. }
  680. void i4_string_manager_class::get_char(char *&s, char *&buf, w32 &line_on, char *error_prefix)
  681. {
  682. if (*s=='$')
  683. expand_macro(s,buf,line_on,error_prefix);
  684. else if (*s=='\\')
  685. {
  686. s++;
  687. if (*s=='n') *buf='\n';
  688. else if (*s=='r') *buf='\r';
  689. else if (*s=='t') *buf='\t';
  690. else *buf=*s;
  691. s++;
  692. buf++;
  693. }
  694. else
  695. {
  696. if (*s=='\n')
  697. line_on++;
  698. *buf=*s;
  699. buf++;
  700. s++;
  701. }
  702. }
  703. void i4_string_manager_class::read_array(char *&s,
  704. char **array,
  705. w32 &total,
  706. w32 &line_on,
  707. char *error_prefix,
  708. char *token_buf)
  709. {
  710. char *t1;
  711. total=0;
  712. while (*s)
  713. {
  714. t1=token_buf;
  715. get_token(s,t1,line_on,error_prefix);
  716. *t1=0;
  717. if (token_buf[0]=='}' && token_buf[1]==0)
  718. return ;
  719. array[total]=alloc_str(token_buf);
  720. total++;
  721. }
  722. i4_error("%s:%d: expecting }",error_prefix,line_on);
  723. }
  724. void i4_string_manager_class::get_token(char *&s, char *&buf, w32 &line_on, char *error_prefix)
  725. {
  726. char *sl=s;
  727. skip_white(sl,line_on);
  728. if (*sl=='"')
  729. {
  730. sl++;
  731. while (*sl && *sl!='"')
  732. get_char(sl,buf,line_on,error_prefix);
  733. if (*sl==0)
  734. i4_error("%s:%d:expected %c",error_prefix,line_on,'"');
  735. else
  736. sl++;
  737. }
  738. else
  739. {
  740. if (*sl=='=' || *sl=='{' || *sl=='}' || *sl=='#')
  741. {
  742. *buf=*sl;
  743. sl++;
  744. buf++;
  745. }
  746. else
  747. {
  748. while (*sl &&
  749. !space_char(*sl) &&
  750. *sl!='=' && *sl!='}' && *sl!='{')
  751. get_char(sl,buf,line_on,error_prefix);
  752. }
  753. }
  754. s=sl;
  755. }
  756. i4_bool i4_string_manager_class::load_buffer(void *internal_buffer, char *error_prefix)
  757. {
  758. char *s=(char *)internal_buffer;
  759. char *token1,*token2,*t1,*t2;
  760. char **array;
  761. w32 line_on=1;
  762. token1 = (char *)i4_malloc(4000,"token1 buf");
  763. token2 = (char *)i4_malloc(4000,"token2 buf");
  764. array = (char **)i4_malloc(1000*sizeof(char *),"array buf");
  765. dump_file_buf=internal_buffer;
  766. dump_buf_len=strlen((char *)internal_buffer);
  767. while (*s)
  768. {
  769. t1=token1;
  770. get_token(s,t1,line_on,error_prefix);
  771. *t1=0;
  772. if (token1[0]=='=' && token1[1]==0)
  773. i4_error("%s:%d: expected token not =", error_prefix, line_on);
  774. else if (token1[0]=='{')
  775. i4_error("%s:%d: expected token not {", error_prefix, line_on);
  776. else if (token1[0]=='#')
  777. {
  778. t1=token1;
  779. get_token(s,t1,line_on,error_prefix);
  780. *t1=0;
  781. if (strcmp(token1,"include"))
  782. i4_error("%s:%d: expected include", error_prefix, line_on);
  783. t1=token1;
  784. get_token(s,t1,line_on,error_prefix);
  785. *t1=0;
  786. load(token1);
  787. }
  788. else
  789. {
  790. t2=token2;
  791. get_token(s,t2,line_on,error_prefix);
  792. *t2=0;
  793. if (token2[0]=='=' && token2[1]==0) // it's ok to have an equal here
  794. {
  795. t2=token2;
  796. get_token(s,t2,line_on,error_prefix);
  797. *t2=0;
  798. }
  799. if (token2[0]=='{' && token2[1]==0)
  800. {
  801. w32 total;
  802. read_array(s, array, total, line_on, error_prefix, token2);
  803. add_array_node(token1,array, total);
  804. }
  805. else
  806. {
  807. // printf("'%s' : '%s'\n",token1,token2);
  808. add_node(token1,token2);
  809. }
  810. }
  811. skip_white(s,line_on);
  812. }
  813. i4_free(token1);
  814. i4_free(token2);
  815. i4_free(array);
  816. return i4_T;
  817. }
  818. i4_bool i4_string_manager_class::load(const i4_const_str &filename)
  819. {
  820. return load(filename.ptr);
  821. }
  822. i4_bool i4_string_manager_class::load(char *filename)
  823. {
  824. i4_file_class *fp;
  825. i4_const_str tmp(filename);
  826. fp=i4_open(tmp);
  827. if (!fp)
  828. {
  829. i4_warning("include file %s missing",filename);
  830. return i4_F;
  831. }
  832. else
  833. {
  834. w32 size=fp->size();
  835. char *mem=(char *)i4_malloc(size+1,"tmp");
  836. if (fp->read(mem,size)!=size)
  837. {
  838. delete fp;
  839. return i4_F;
  840. }
  841. mem[size]=0;
  842. load_buffer(mem,filename);
  843. i4_free(mem);
  844. delete fp;
  845. }
  846. return i4_T;
  847. }
  848. char *i4_os_string(const i4_const_str &name, char *tmp_buf, int buflen)
  849. {
  850. i4_const_str::iterator p=name.begin();
  851. char *s=tmp_buf;
  852. while (p!=name.end() && buflen>1)
  853. {
  854. *s=(char)p.get().value();
  855. ++s;
  856. ++p;
  857. buflen--;
  858. }
  859. *s=0;
  860. return tmp_buf;
  861. }
  862. i4_str *i4_from_ascii(const char *buf)
  863. {
  864. int l=strlen(buf);
  865. i4_str *ret=new i4_str(l);
  866. ret->len=l;
  867. memcpy(ret->ptr, buf, l);
  868. return ret;
  869. }
  870. const i4_const_str &i4gets(char *str, i4_bool barf_on_error)
  871. {
  872. const i4_const_str *s=&i4_string_man.get(str);
  873. if (barf_on_error && s->null())
  874. i4_error("Resource missing %s",str);
  875. return *s;
  876. }
  877. int i4getn(char *str, i4_bool barf_on_error)
  878. {
  879. i4_const_str::iterator i=i4gets(str, barf_on_error).begin();
  880. return i.read_number();
  881. }