gutil.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file gutil.cpp
  3. /// \brief GUTIL LIBRARY (Fully portable version)
  4. ///
  5. /// This library is provided as a component of the ecosystem modelling platform
  6. /// LPJ-GUESS. It combines the functionality of the XTRING and BENUTIL libraries
  7. ///
  8. /// FULL PORTABILITY VERSION: tested and should work in any Unix, Linux or Windows
  9. /// environment.
  10. ///
  11. /// Enquiries to: Joe Siltberg, Lund University: joe.siltberg@nateko.lu.se
  12. /// All rights reserved, copyright retained by the author.
  13. ///
  14. /// \author Ben Smith, University of Lund
  15. /// $Date: 2014-02-05 10:30:16 +0100 (Wed, 05 Feb 2014) $
  16. ///
  17. ///////////////////////////////////////////////////////////////////////////////////////
  18. #include "gutil.h"
  19. #include <stdarg.h>
  20. void fail() {
  21. ::printf("Error in GUTIL library: out of memory\n");
  22. fprintf(stderr,"Error in GUTIL library: out of memory\n");
  23. exit(99);
  24. }
  25. const unsigned long XEGMENT=32;
  26. void xtring::init() {
  27. buf=NULL;
  28. ptext=new char[XEGMENT];
  29. if (!ptext) fail();
  30. nxegment=1;
  31. *ptext='\0';
  32. }
  33. void xtring::resize(unsigned long nchar) {
  34. // Resizes character buffer pointed to by ptext if necessary to
  35. // accomodate nchar characters, plus a \0
  36. if (nchar<0) nchar=0;
  37. nchar++;
  38. if (nchar>nxegment*XEGMENT || (nxegment-1)*XEGMENT>=nchar) {
  39. ptext[nxegment*XEGMENT-1]='\0';
  40. unsigned long nxegment_old=nxegment;
  41. nxegment=nchar/XEGMENT+!!(nchar%XEGMENT);
  42. char* pnew=new char[nxegment*XEGMENT];
  43. if (!pnew) fail();
  44. if (nxegment>nxegment_old || strlen(ptext)<nxegment*XEGMENT) strcpy(pnew,ptext);
  45. else pnew[nxegment*XEGMENT-1]='\0';
  46. delete[] ptext;
  47. ptext=pnew;
  48. }
  49. }
  50. void xtring::expand(unsigned long nchar) {
  51. if (nchar<0) nchar=0;
  52. if (nchar>=nxegment*XEGMENT) {
  53. nxegment=nchar/XEGMENT+1;
  54. char* pnew=new char[nxegment*XEGMENT];
  55. if (!pnew) fail();
  56. unsigned long i;
  57. for (i=0;i<nchar;i++)
  58. pnew[i]=ptext[i];
  59. pnew[i]='\0';
  60. delete[] ptext;
  61. ptext=pnew;
  62. }
  63. }
  64. xtring::xtring(const xtring& s) {
  65. // COPY CONSTRUCTOR
  66. init();
  67. resize(strlen(s.ptext));
  68. strcpy(ptext,s.ptext);
  69. }
  70. xtring::xtring() {
  71. // CONSTRUCTOR: xtring s;
  72. init();
  73. *ptext='\0';
  74. }
  75. xtring::xtring(char* inittext) {
  76. // CONSTRUCTOR: xtring s="text";
  77. init();
  78. resize(strlen(inittext));
  79. strcpy(ptext,inittext);
  80. }
  81. xtring::xtring(const char* inittext) {
  82. // CONSTRUCTOR: xtring s="text";
  83. init();
  84. resize(strlen(inittext));
  85. strcpy(ptext,inittext);
  86. }
  87. xtring::xtring(char c) {
  88. // CONSTRUCTOR: xtring s='c';
  89. init();
  90. ptext[0]=c;
  91. ptext[1]='\0';
  92. }
  93. xtring::xtring(unsigned long n) {
  94. // CONSTRUCTOR: xtring s[n]
  95. init();
  96. resize(n);
  97. *ptext='\0';
  98. }
  99. xtring::xtring(int n) {
  100. // CONSTRUCTOR: xtring s[n]
  101. init();
  102. resize(n);
  103. *ptext='\0';
  104. }
  105. xtring::xtring(unsigned int n) {
  106. // CONSTRUCTOR: xtring s[n]
  107. init();
  108. resize(n);
  109. *ptext='\0';
  110. }
  111. xtring::xtring(long n) {
  112. // CONSTRUCTOR: xtring s[n]
  113. init();
  114. resize(n);
  115. *ptext='\0';
  116. }
  117. xtring::~xtring() {
  118. // DESTRUCTOR
  119. if (buf) delete[] buf;
  120. delete[] ptext;
  121. }
  122. xtring::operator char*() {
  123. // Conversion function (cast operator) to char*
  124. return ptext;
  125. }
  126. xtring::operator const char*() const {
  127. // Conversion function (cast operator) to const char*
  128. return ptext;
  129. }
  130. void xtring::reserve(unsigned long n) {
  131. // Set string buffer length to at least n (not including trailing \0)
  132. resize(n);
  133. }
  134. unsigned long xtring::len() {
  135. // String length not including trailing \0
  136. return strlen(ptext);
  137. }
  138. xtring xtring::upper() {
  139. // Upper case
  140. if (buf) delete[] buf;
  141. buf=new char[len()+1];
  142. if (!buf) fail();
  143. char* pold=ptext,*pnew=buf;
  144. do {
  145. if (*pold>='a' && *pold<='z') *pnew=*pold-32;
  146. else *pnew=*pold;
  147. pnew++;
  148. } while (*pold++);
  149. return (xtring)buf;
  150. }
  151. xtring xtring::lower() {
  152. // Lower case
  153. if (buf) delete[] buf;
  154. buf=new char[len()+1];
  155. if (!buf) fail();
  156. char* pold=ptext,*pnew=buf;
  157. do {
  158. if (*pold>='A' && *pold<='Z') *pnew=*pold+32;
  159. else *pnew=*pold;
  160. pnew++;
  161. } while (*pold++);
  162. return (xtring)buf;
  163. }
  164. xtring xtring::printable() {
  165. // Printable characters (ASCII code >=32)
  166. if (buf) delete[] buf;
  167. buf=new char[len()+1];
  168. if (!buf) fail();
  169. char* pold=ptext,*pnew=buf;
  170. while (*pold) {
  171. if (*pold>=' ') *pnew++=*pold;
  172. pold++;
  173. }
  174. *pnew='\0';
  175. return (xtring)buf;
  176. }
  177. xtring xtring::left(unsigned long n) {
  178. // Leftmost n characters
  179. if (buf) delete[] buf;
  180. if (n<=0) {
  181. buf=new char[1];
  182. if (!buf) fail();
  183. *buf='\0';
  184. }
  185. else {
  186. buf=new char[n+1];
  187. if (!buf) fail();
  188. char* pold=ptext,*pnew=buf;
  189. unsigned long i=0;
  190. while (i<n && *pold) {
  191. *pnew=*pold;
  192. pold++;
  193. pnew++;
  194. i++;
  195. }
  196. *pnew='\0';
  197. }
  198. return (xtring)buf;
  199. }
  200. xtring xtring::mid(unsigned long s) {
  201. // Rightmost portion of xtring, starting at character s
  202. if (buf) delete[] buf;
  203. if (s>=len()) {
  204. buf=new char[1];
  205. if (!buf) fail();
  206. *buf='\0';
  207. }
  208. else {
  209. buf=new char[len()-s+1];
  210. if (!buf) fail();
  211. strcpy(buf,ptext+s);
  212. }
  213. return (xtring)buf;
  214. }
  215. xtring xtring::mid(unsigned long s,unsigned long n) {
  216. // Middle n characters starting at character s
  217. if (buf) delete[] buf;
  218. if (s>=len() || n<=0) {
  219. buf=new char[1];
  220. if (!buf) fail();
  221. *buf='\0';
  222. }
  223. else {
  224. buf=new char[n+1];
  225. if (!buf) fail();
  226. char* pold=ptext+s,*pnew=buf;
  227. unsigned long i=0;
  228. while (i++<n && *pold) *pnew++=*pold++;
  229. *pnew='\0';
  230. }
  231. return (xtring)buf;
  232. }
  233. xtring xtring::right(unsigned long n) {
  234. // Rightmost n characters
  235. unsigned long s=len()-n;
  236. if (s<0) s=0;
  237. return mid(s);
  238. }
  239. long xtring::find(const char* s) {
  240. // Find string s in this xtring
  241. // (returns -1 if string does not occur)
  242. long length=strlen(s);
  243. long bound=len()-length;
  244. long i=0;
  245. while (i<=bound) {
  246. xtring sub=mid(i,length);
  247. if (sub==s) return i;
  248. i++;
  249. }
  250. return -1;
  251. }
  252. long xtring::find(char c) {
  253. // Find character in this xtring
  254. long i;
  255. long bound=len();
  256. for (i=0;i<bound;i++)
  257. if (ptext[i]==c) return i;
  258. return -1;
  259. }
  260. long xtring::findoneof(const char* s) {
  261. // Find one of several characters in this xtring
  262. long i,j;
  263. long bound=len();
  264. long nchar=strlen(s);
  265. for (i=0;i<bound;i++)
  266. for (j=0;j<nchar;j++)
  267. if (ptext[i]==s[j]) return i;
  268. return -1;
  269. }
  270. long xtring::findnotoneof(const char* s) {
  271. // Find first character NOT matching characters in s
  272. long i,j;
  273. long bound=len();
  274. long nchar=strlen(s);
  275. char found;
  276. for (i=0;i<bound;i++) {
  277. found=0;
  278. for (j=0;j<nchar && !found;j++)
  279. if (ptext[i]==s[j]) found=1;
  280. if (!found) return i;
  281. }
  282. return -1;
  283. }
  284. double xtring::num() {
  285. // Conversion to number
  286. char* endptr;
  287. double dval;
  288. dval=strtod(ptext,&endptr);
  289. if (*endptr) return 0;
  290. return dval;
  291. }
  292. char xtring::isnum() {
  293. // Whether xtring can be converted to a number
  294. char* endptr;
  295. strtod(ptext,&endptr);
  296. if (*endptr) return 0;
  297. return 1;
  298. }
  299. void xtring::printf(const char* fmt,...) {
  300. va_list v;
  301. va_start(v,fmt);
  302. const int MINBUF=100;
  303. xtring sect;
  304. xtring output="";
  305. xtring fspec;
  306. xtring wspec;
  307. xtring buffer(MINBUF);
  308. const char* pfmt=fmt;
  309. char* pchar;
  310. int i;
  311. char waitspec=0,readspec=0,readwidth,havewidth;
  312. i=0;
  313. while (*pfmt) {
  314. if (waitspec) {
  315. if ((*pfmt>='0' && *pfmt<='9') || *pfmt=='-' || *pfmt=='+' || *pfmt==' ' ||
  316. *pfmt=='#' || *pfmt=='h' || *pfmt=='l' || *pfmt=='I' || *pfmt=='L' ||
  317. *pfmt=='c' || *pfmt=='C' || *pfmt=='d' || *pfmt=='i' || *pfmt=='o' ||
  318. *pfmt=='u' || *pfmt=='x' || *pfmt=='X' || *pfmt=='e' || *pfmt=='E' ||
  319. *pfmt=='f' || *pfmt=='g' || *pfmt=='G' || *pfmt=='n' || *pfmt=='p' ||
  320. *pfmt=='s' || *pfmt=='S' || *pfmt=='.') {
  321. readspec=1;
  322. havewidth=0;
  323. readwidth=0;
  324. wspec="0";
  325. fspec="%";
  326. pfmt--;
  327. }
  328. else output+=*pfmt;
  329. waitspec=0;
  330. }
  331. else if (readspec) {
  332. if (*pfmt!='*') fspec+=*pfmt;
  333. if (readwidth) {
  334. if (*pfmt>='0' && *pfmt<='9') wspec+=*pfmt;
  335. else {
  336. readwidth=0;
  337. havewidth=1;
  338. }
  339. }
  340. else if (*pfmt>='0' && *pfmt<='9' && !havewidth) {
  341. wspec=*pfmt;
  342. readwidth=1;
  343. }
  344. else if (*pfmt=='*' && !havewidth) {
  345. wspec.reserve(MINBUF);
  346. sprintf(wspec,"%d",va_arg(v,int));
  347. fspec+=wspec;
  348. havewidth=1;
  349. }
  350. else if (*pfmt!='-' && *pfmt!='+' && *pfmt!=' ' && *pfmt!='#') {
  351. havewidth=1;
  352. }
  353. if (*pfmt=='c' || *pfmt=='C' || *pfmt=='d' || *pfmt=='i' || *pfmt=='o' ||
  354. *pfmt=='u' || *pfmt=='x' || *pfmt=='X') {
  355. buffer.reserve((unsigned long)(MINBUF+wspec.num()));
  356. sprintf(buffer,fspec,va_arg(v,int));
  357. output+=buffer;
  358. readspec=0;
  359. }
  360. else if (*pfmt=='e' || *pfmt=='E' || *pfmt=='f' || *pfmt=='g' ||
  361. *pfmt=='G') {
  362. buffer.reserve((unsigned long)(MINBUF+wspec.num()));
  363. sprintf(buffer,fspec,va_arg(v,double));
  364. output+=buffer;
  365. readspec=0;
  366. }
  367. else if (*pfmt=='p') {
  368. buffer.reserve((unsigned long)(MINBUF+wspec.num()));
  369. sprintf(buffer,fspec,va_arg(v,void*));
  370. output+=buffer;
  371. readspec=0;
  372. }
  373. else if (*pfmt=='s' || *pfmt=='S') {
  374. pchar=va_arg(v,char*);
  375. buffer.reserve((unsigned long)(strlen(pchar)+wspec.num()));
  376. sprintf(buffer,fspec,pchar);
  377. output+=buffer;
  378. readspec=0;
  379. }
  380. }
  381. else if (*pfmt=='%') waitspec=1;
  382. else {
  383. sect[i++]=*pfmt;
  384. if (*(pfmt+1)=='%' || !*(pfmt+1)) {
  385. sect[i]='\0';
  386. output+=sect;
  387. i=0;
  388. }
  389. }
  390. pfmt++;
  391. }
  392. resize(output.len());
  393. strcpy(ptext,output);
  394. }
  395. xtring& xtring::operator=(xtring& s) {
  396. // Assignment: s1=s2;
  397. resize(s.len());
  398. strcpy(ptext,s.ptext);
  399. return *this;
  400. }
  401. xtring& xtring::operator=(const char* s) {
  402. // Assignment: s="text"
  403. resize(strlen(s));
  404. strcpy(ptext,s);
  405. return *this;
  406. }
  407. xtring& xtring::operator=(char c) {
  408. // Assignment: s='c'
  409. resize(1);
  410. ptext[0]=c;
  411. ptext[1]='\0';
  412. return *this;
  413. }
  414. xtring xtring::operator+(xtring& s2) {
  415. // Concatenate: s1+s2
  416. if (buf) delete[] buf;
  417. buf=new char[len()+s2.len()+1];
  418. if (!buf) fail();
  419. strcpy(buf,ptext);
  420. strcat(buf,s2.ptext);
  421. return (xtring)buf;
  422. }
  423. xtring xtring::operator+(const char* s2) {
  424. // Concatenate: s1+"text"
  425. if (buf) delete[] buf;
  426. buf=new char[len()+strlen(s2)+1];
  427. if (!buf) fail();
  428. strcpy(buf,ptext);
  429. strcat(buf,s2);
  430. return xtring(buf);
  431. }
  432. xtring xtring::operator+(char c) {
  433. // Concatenate s1+'c'
  434. return (xtring)ptext+(xtring)c;
  435. }
  436. xtring& xtring::operator+=(xtring& s2) {
  437. // Concatenate s1+=s2;
  438. resize(len()+s2.len());
  439. strcat(ptext,s2.ptext);
  440. return *this;
  441. }
  442. xtring& xtring::operator+=(const char* s2) {
  443. // Concatenate s1+="text";
  444. resize(len()+strlen(s2));
  445. strcat(ptext,s2);
  446. return *this;
  447. }
  448. xtring& xtring::operator+=(char c) {
  449. // Concatenate s1+='c';
  450. resize(len()+1);
  451. strcat(ptext,(xtring)c);
  452. return *this;
  453. }
  454. char& xtring::operator[](unsigned long n) {
  455. // Reference s[n] (NB: resizes xtring if necessary)
  456. expand(n);
  457. return ptext[n];
  458. }
  459. char& xtring::operator[](int n) {
  460. expand(n);
  461. return ptext[(unsigned long)n];
  462. }
  463. char& xtring::operator[](unsigned int n) {
  464. expand(n);
  465. return ptext[(unsigned long)n];
  466. }
  467. char& xtring::operator[](long n) {
  468. expand(n);
  469. return ptext[(unsigned long)n];
  470. }
  471. // Comparison operators
  472. bool xtring::operator==(xtring& s2) const {
  473. if (strcmp(ptext,s2.ptext)) return false;
  474. return true;
  475. }
  476. bool xtring::operator==(char* s2) const {
  477. if (strcmp(ptext,s2)) return false;
  478. return true;
  479. }
  480. bool xtring::operator==(const char* s2) const {
  481. if (strcmp(ptext,s2)) return false;
  482. return true;
  483. }
  484. bool xtring::operator==(char c) const {
  485. if (strcmp(ptext,(xtring)c)) return false;
  486. return true;
  487. }
  488. bool xtring::operator!=(xtring& s2) const {
  489. if (strcmp(ptext,s2.ptext)) return true;
  490. return false;
  491. }
  492. bool xtring::operator!=(char* s2) const {
  493. if (strcmp(ptext,s2)) return true;
  494. return false;
  495. }
  496. bool xtring::operator!=(const char* s2) const {
  497. if (strcmp(ptext,s2)) return true;
  498. return false;
  499. }
  500. bool xtring::operator!=(char c) const {
  501. if (strcmp(ptext,(xtring)c)) return true;
  502. return false;
  503. }
  504. bool xtring::operator<(xtring& s2) const {
  505. if (strcmp(ptext,s2.ptext)<0) return true;
  506. return false;
  507. }
  508. bool xtring::operator<(char* s2) const {
  509. if (strcmp(ptext,s2)<0) return true;
  510. return false;
  511. }
  512. bool xtring::operator<(const char* s2) const {
  513. if (strcmp(ptext,s2)<0) return true;
  514. return false;
  515. }
  516. bool xtring::operator<(char c) const {
  517. if (strcmp(ptext,(xtring)c)<0) return true;
  518. return false;
  519. }
  520. bool xtring::operator>(xtring& s2) const {
  521. if (strcmp(ptext,s2.ptext)>0) return true;
  522. return false;
  523. }
  524. bool xtring::operator>(char* s2) const {
  525. if (strcmp(ptext,s2)>0) return true;
  526. return false;
  527. }
  528. bool xtring::operator>(const char* s2) const {
  529. if (strcmp(ptext,s2)>0) return true;
  530. return false;
  531. }
  532. bool xtring::operator>(char c) const {
  533. if (strcmp(ptext,(xtring)c)>0) return true;
  534. return false;
  535. }
  536. bool xtring::operator<=(xtring& s2) const {
  537. if (strcmp(ptext,s2.ptext)<=0) return true;
  538. return false;
  539. }
  540. bool xtring::operator<=(char* s2) const {
  541. if (strcmp(ptext,s2)<=0) return true;
  542. return false;
  543. }
  544. bool xtring::operator<=(const char* s2) const {
  545. if (strcmp(ptext,s2)<=0) return true;
  546. return false;
  547. }
  548. bool xtring::operator<=(char c) const {
  549. if (strcmp(ptext,(xtring)c)<=0) return true;
  550. return false;
  551. }
  552. bool xtring::operator>=(xtring& s2) const {
  553. if (strcmp(ptext,s2.ptext)>=0) return true;
  554. return false;
  555. }
  556. bool xtring::operator>=(char* s2) const {
  557. if (strcmp(ptext,s2)>=0) return true;
  558. return false;
  559. }
  560. bool xtring::operator>=(const char* s2) const {
  561. if (strcmp(ptext,s2)>=0) return true;
  562. return false;
  563. }
  564. bool xtring::operator>=(char c) const {
  565. if (strcmp(ptext,(xtring)c)>=0) return true;
  566. return false;
  567. }
  568. void unixtime(xtring& result) {
  569. time_t t;
  570. time(&t);
  571. result=ctime(&t);
  572. result=result.left(result.findoneof("\n"));
  573. }
  574. FILE** pin;
  575. xtring inxtr;
  576. bool iseol=false;
  577. char cbuf;
  578. inline char xgetc() {
  579. // Equivalent to getc(*pin), but returns character stored in cbuf (global)
  580. // if there is one (see xungetc below)
  581. char copy;
  582. if (cbuf) {
  583. copy=cbuf;
  584. cbuf=0;
  585. return copy;
  586. }
  587. return getc(*pin);
  588. }
  589. inline void xungetc(char& ch) {
  590. // Equivalent to ungetc(ch,*pin), but assigns ch to cbuf instead of trying
  591. // to push it back onto the stream (which may not be possible)
  592. cbuf=ch;
  593. }
  594. inline bool xfeof() {
  595. // Equivalent to feof(*pin), but returns false if character stored in cbuf
  596. // even if feof(*pin)==true
  597. if (feof(*pin) && !cbuf) return true;
  598. return false;
  599. }
  600. inline int readfixedwidth(int& width) {
  601. // Reads the next 'width' characters from stream in
  602. // Returns the number of characters successfully read (may be <width on eof)
  603. int cread=0;
  604. char ch;
  605. iseol=false;
  606. inxtr.reserve(width);
  607. if (xfeof()) {
  608. inxtr[cread]='\0';
  609. iseol=1;
  610. return 0;
  611. }
  612. while (cread<width) {
  613. ch=xgetc();
  614. if (xfeof() || ch=='\n') {
  615. inxtr[cread]='\0';
  616. iseol=1;
  617. return cread;
  618. }
  619. inxtr[cread++]=ch;
  620. }
  621. inxtr[cread]='\0';
  622. return cread;
  623. }
  624. inline int readtoeol(bool breakcomma) {
  625. // Reads to end of the current line or eof from stream in
  626. // Returns number of characters read
  627. int cread=0;
  628. char ch;
  629. iseol=false;
  630. do {
  631. ch=xgetc();
  632. if (xfeof() || ch=='\n') {
  633. inxtr[cread]='\0';
  634. iseol=true;
  635. return cread;
  636. }
  637. else if (breakcomma && ch==',') {
  638. inxtr[cread]='\0';
  639. return cread;
  640. }
  641. inxtr[cread++]=ch;
  642. } while (true);
  643. }
  644. inline void readtocomma() {
  645. // Reads to next comma or next non-white-space character,
  646. // whichever comes first (newline or eof always terminates input)
  647. char ch;
  648. iseol=false;
  649. do {
  650. ch=xgetc();
  651. if (xfeof() || ch=='\n') {
  652. iseol=true;
  653. return;
  654. }
  655. if (ch==',') return;
  656. if (ch!=' ' && ch!='\t') {
  657. xungetc(ch);
  658. return;
  659. }
  660. } while (true);
  661. }
  662. inline int readtowhitespace(bool& breakcomma) {
  663. // Reads to next space, tab, newline or eof from stream in
  664. // Space or tab is not read in (returned to stream)
  665. // Returns number of characters read
  666. int cread=0;
  667. char ch;
  668. iseol=false;
  669. do {
  670. ch=xgetc();
  671. if (xfeof() || ch=='\n') {
  672. iseol=true;
  673. inxtr[cread]='\0';
  674. return cread;
  675. }
  676. if (breakcomma) {
  677. if (ch==' ' || ch=='\t') {
  678. inxtr[cread]='\0';
  679. readtocomma();
  680. return cread;
  681. }
  682. if (ch==',') {
  683. inxtr[cread]='\0';
  684. return cread;
  685. }
  686. }
  687. else {
  688. if (ch==' ' || ch=='\t') {
  689. xungetc(ch);
  690. inxtr[cread]='\0';
  691. return cread;
  692. }
  693. }
  694. inxtr[cread++]=ch;
  695. } while (true);
  696. }
  697. inline int readtochar(char sep) {
  698. // Reads to next instance of character 'sep'
  699. // Character sep is lost to the stream, but not stored as part of the input string
  700. // Newline or eof also terminate input
  701. // Returns number of characters read
  702. int cread=0;
  703. char ch;
  704. iseol=false;
  705. do {
  706. ch=xgetc();
  707. if (xfeof() || ch=='\n') {
  708. iseol=true;
  709. inxtr[cread]='\0';
  710. return cread;
  711. }
  712. if (ch==sep) {
  713. inxtr[cread]='\0';
  714. return cread;
  715. }
  716. inxtr[cread++]=ch;
  717. } while (true);
  718. }
  719. inline void skipwhitespace() {
  720. // Reads to next non-white-space character
  721. // Newline or eof also terminate input
  722. iseol=false;
  723. char ch;
  724. do {
  725. ch=xgetc();
  726. if (xfeof() || ch=='\n') {
  727. iseol=true;
  728. return;
  729. }
  730. if (ch!=' ' && ch!='\t') {
  731. xungetc(ch);
  732. return;
  733. }
  734. } while (true);
  735. }
  736. bool readfloat(int nitem,int& width,int dec,int exp,char termch,double* parg) {
  737. int i,j,len;
  738. char ch;
  739. bool havesign;
  740. bool negcard;
  741. bool haveexpsign;
  742. bool havedec;
  743. bool haveexp;
  744. bool negexp;
  745. double cardpart;
  746. double decpart;
  747. double decmul;
  748. double value;
  749. int exppart;
  750. int posdec;
  751. int posexp;
  752. bool breakcomma=termch==',';
  753. if (termch==',' || termch=='$') termch=0;
  754. do {
  755. if (xfeof()) return false;
  756. havesign=false;
  757. negcard=false;
  758. haveexpsign=false;
  759. negexp=false;
  760. havedec=false;
  761. haveexp=false;
  762. cardpart=0;
  763. decpart=0;
  764. decmul=1.0;
  765. exppart=0;
  766. if (width) {
  767. readfixedwidth(width);
  768. }
  769. else { // no width specified, read to next white space or end of line
  770. if (!iseol) skipwhitespace();
  771. if (iseol) {
  772. *parg++=0.0;
  773. goto next;
  774. }
  775. if (termch) readtochar(termch);
  776. else readtowhitespace(breakcomma);
  777. }
  778. len=inxtr.len();
  779. if (dec>=0 || exp>=0) { // strip non-numeric characters from string
  780. i=j=0;
  781. ch=inxtr[j];
  782. posdec=posexp=-1;
  783. while (ch) {
  784. if (ch=='e' || ch=='E') {
  785. if (posexp<0) {
  786. inxtr[i++]=inxtr[j++];
  787. posexp=i;
  788. }
  789. else j++;
  790. }
  791. else if (ch=='.') {
  792. if (posexp<0 && posdec<0) {
  793. inxtr[i++]=inxtr[j++];
  794. posdec=i;
  795. }
  796. else j++;
  797. }
  798. else if (ch=='-' || (ch>='0' && ch<='9'))
  799. inxtr[i++]=inxtr[j++];
  800. else j++;
  801. ch=inxtr[j];
  802. }
  803. inxtr[i]='\0';
  804. len=inxtr.len();
  805. if (exp>=0 && posexp<0 && posdec<len-exp) posexp=len-exp;
  806. else posexp=len;
  807. if (dec>=0 && posdec<0) posdec=posexp-dec;
  808. else posdec=posexp;
  809. }
  810. else posdec=posexp=len;
  811. len=inxtr.len();
  812. for (i=0;i<len;i++) {
  813. ch=inxtr[i];
  814. if (i<posdec) { // cardinal part
  815. if (ch=='-' && !havesign) negcard=true;
  816. else if (ch=='.' && !havedec && !haveexp) {
  817. posdec=i+1;
  818. if (posexp<posdec) posexp=posdec;
  819. havedec=true;
  820. }
  821. else if ((ch=='E' || ch=='e') && !haveexp) {
  822. posexp=i+1;
  823. posdec=i+1;
  824. haveexp=true;
  825. }
  826. else if (ch>='0' && ch<='9') {
  827. havesign=true;
  828. cardpart=cardpart*10.0+(double)(ch-'0');
  829. }
  830. }
  831. else if (i<posexp) { // decimal part
  832. if ((ch=='E' || ch=='e') && !haveexp) {
  833. posexp=i+1;
  834. posdec=i+1;
  835. haveexp=true;
  836. }
  837. else if (ch>='0' && ch<='9') {
  838. decmul/=10.0;
  839. decpart+=(double)(ch-'0')*decmul;
  840. }
  841. }
  842. else { // exponent part
  843. if (ch=='-' && !haveexpsign) negexp=true;
  844. else if (ch>='0' && ch<='9') {
  845. haveexpsign=true;
  846. exppart=exppart*10+(ch-'0');
  847. }
  848. }
  849. }
  850. if (negcard)
  851. value=-cardpart-decpart;
  852. else
  853. value=cardpart+decpart;
  854. if (exppart) {
  855. if (negexp)
  856. inxtr.printf("1E-%d",exppart);
  857. else
  858. inxtr.printf("1E%d",exppart);
  859. value*=inxtr.num();
  860. }
  861. *parg++=value;
  862. next:
  863. nitem--;
  864. } while (nitem>0);
  865. return true;
  866. }
  867. bool readint(int& nitem,int& width,char termch,int* parg) {
  868. bool havesign;
  869. bool neg;
  870. int value;
  871. int len,i;
  872. char ch;
  873. bool breakcomma=termch==',';
  874. if (termch==',' || termch=='$') termch=0;
  875. do {
  876. if (xfeof()) return false;
  877. havesign=false;
  878. neg=false;
  879. value=0;
  880. if (width) {
  881. readfixedwidth(width);
  882. }
  883. else { // no width specified, read to next white space or end of line
  884. if (!iseol) skipwhitespace();
  885. if (iseol) {
  886. *parg++=0;
  887. goto next;
  888. }
  889. if (termch) readtochar(termch);
  890. else readtowhitespace(breakcomma);
  891. }
  892. len=inxtr.len();
  893. for (i=0;i<len;i++) {
  894. ch=inxtr[i];
  895. if (ch=='-' && !havesign) {
  896. havesign=true;
  897. neg=true;
  898. }
  899. else if (ch>='0' && ch<='9') {
  900. havesign=true;
  901. value=value*10+ch-'0';
  902. }
  903. }
  904. if (neg) value=-value;
  905. *parg++=value;
  906. next:
  907. nitem--;
  908. } while (nitem>0);
  909. return true;
  910. }
  911. bool readchar(int nitem,int& width,char termch,xtring* parg,bool read_to_eol) {
  912. bool nomulti=nitem>1 && width<0;
  913. bool breakcomma=termch==',';
  914. if (termch==',' || termch=='$') termch=0;
  915. do {
  916. if (xfeof()) return false;
  917. if (width) {
  918. readfixedwidth(width);
  919. }
  920. else {
  921. if (!iseol) skipwhitespace();
  922. if (iseol) {
  923. *parg++="";
  924. goto next;
  925. }
  926. if (termch) readtochar(termch);
  927. else if (read_to_eol) readtoeol(breakcomma);
  928. else readtowhitespace(breakcomma);
  929. }
  930. *parg++=inxtr;
  931. next:
  932. nitem--;
  933. while (nomulti && nitem>0) {
  934. // Disallow multiple strings if width not specified
  935. // Set extra arguments to blank
  936. *parg++="";
  937. }
  938. } while (nitem>0);
  939. return true;
  940. }
  941. bool readfor(FILE* in, const char* fmt, ...) {
  942. va_list v;
  943. va_start(v,fmt);
  944. bool waitwidth=false;
  945. bool waitfloat=false;
  946. bool waitdec=false;
  947. bool waitexp=false;
  948. bool waitint=false;
  949. bool waitchar=false;
  950. bool waitcomma=false;
  951. bool croff=false;
  952. bool read_to_eol=false;
  953. const char* pchar = fmt;
  954. int nitem=0;
  955. int width=0;
  956. int dec=0;
  957. int exp=0;
  958. iseol=false;
  959. pin=&in;
  960. cbuf=0;
  961. while (*pchar) {
  962. if (*pchar!=' ') {
  963. if (*pchar!='$' && *pchar!='#') croff=read_to_eol=false;
  964. else if (*pchar=='$') croff=true;
  965. else if (*pchar=='#') read_to_eol=true;
  966. if (waitwidth) {
  967. if (*pchar>='0' && *pchar<='9') { // width specifier
  968. width=width*10+*pchar-'0';
  969. }
  970. else if (*pchar=='.' && waitfloat) {
  971. waitdec=true;
  972. waitwidth=false;
  973. }
  974. else if ((*pchar=='e' || *pchar=='E') && waitfloat) {
  975. dec=0;
  976. waitexp=true;
  977. waitwidth=false;
  978. }
  979. else if (*pchar=='/') {
  980. if (waitfloat) {
  981. if (!readfloat(nitem,width,-1,-1,0,va_arg(v,double*))) return false;
  982. waitfloat=false;
  983. nitem=0;
  984. width=0;
  985. }
  986. else if (waitint) {
  987. if (!readint(nitem,width,0,va_arg(v,int*))) return false;
  988. waitint=false;
  989. nitem=0;
  990. width=0;
  991. }
  992. else {
  993. if (!readchar(nitem,width,0,va_arg(v,xtring*),read_to_eol)) return false;
  994. waitchar=false;
  995. nitem=0;
  996. width=0;
  997. }
  998. if (!iseol) readtoeol(false);
  999. iseol=false;
  1000. waitwidth=false;
  1001. }
  1002. else if (waitfloat) {
  1003. if (!readfloat(nitem,width,-1,-1,*pchar,va_arg(v,double*))) return false;
  1004. waitfloat=false;
  1005. waitwidth=false;
  1006. nitem=0;
  1007. width=0;
  1008. }
  1009. else if (waitint) {
  1010. if (!readint(nitem,width,*pchar,va_arg(v,int*))) return false;
  1011. waitint=false;
  1012. waitwidth=false;
  1013. nitem=0;
  1014. width=0;
  1015. }
  1016. else {
  1017. if (!readchar(nitem,width,*pchar,va_arg(v,xtring*),read_to_eol)) return false;
  1018. waitchar=false;
  1019. waitwidth=false;
  1020. nitem=0;
  1021. width=0;
  1022. }
  1023. }
  1024. else if (waitdec) {
  1025. if (*pchar>='0' && *pchar<='9') { // decimal places specifier
  1026. dec=dec*10+*pchar-'0';
  1027. }
  1028. else if (*pchar=='e' || *pchar=='E') {
  1029. waitexp=true;
  1030. waitdec=false;
  1031. }
  1032. else if (*pchar=='/') {
  1033. if (!readfloat(nitem,width,dec,-1,0,va_arg(v,double*))) return false;
  1034. if (!iseol) readtoeol(false);
  1035. iseol=false;
  1036. waitdec=false;
  1037. waitfloat=false;
  1038. nitem=0;
  1039. width=0;
  1040. dec=0;
  1041. }
  1042. else {
  1043. if (!readfloat(nitem,width,dec,-1,*pchar,va_arg(v,double*))) return false;
  1044. waitdec=false;
  1045. waitfloat=false;
  1046. nitem=0;
  1047. width=0;
  1048. dec=0;
  1049. }
  1050. }
  1051. else if (waitexp) {
  1052. if (*pchar>='0' && *pchar<='9') { // exponent width specifier
  1053. exp=exp*10+*pchar-'0';
  1054. }
  1055. else if (*pchar=='/') {
  1056. if (!readfloat(nitem,width,dec,exp,0,va_arg(v,double*))) return false;
  1057. if (!iseol) readtoeol(false);
  1058. iseol=false;
  1059. waitexp=false;
  1060. waitfloat=false;
  1061. nitem=0;
  1062. width=0;
  1063. dec=0;
  1064. exp=0;
  1065. }
  1066. else {
  1067. if (!readfloat(nitem,width,dec,exp,*pchar,va_arg(v,double*))) return false;
  1068. waitexp=false;
  1069. waitfloat=false;
  1070. nitem=0;
  1071. width=0;
  1072. dec=0;
  1073. exp=0;
  1074. }
  1075. }
  1076. else if (waitcomma) {
  1077. waitcomma=false;
  1078. }
  1079. else {
  1080. if (*pchar>='0' && *pchar<='9') { // number of items specifier
  1081. nitem=nitem*10+*pchar-'0';
  1082. }
  1083. else if (*pchar=='f' || *pchar=='F') { // floating point number
  1084. waitfloat=true;
  1085. waitwidth=true;
  1086. }
  1087. else if (*pchar=='i' || *pchar=='I') { // integer
  1088. waitint=true;
  1089. waitwidth=true;
  1090. }
  1091. else if (*pchar=='A' || *pchar=='a') { // character string
  1092. waitchar=true;
  1093. waitwidth=true;
  1094. }
  1095. else if (*pchar=='/') { // to end of line
  1096. readtoeol(false);
  1097. iseol=false;
  1098. }
  1099. else if (*pchar=='X' || *pchar=='x') { // skip characters
  1100. readfixedwidth(nitem);
  1101. nitem=0;
  1102. waitcomma=true;
  1103. }
  1104. else if (*pchar!='$') { // take any other character (including a comma) as a separator specifier
  1105. readtochar(*pchar);
  1106. }
  1107. }
  1108. }
  1109. pchar++;
  1110. }
  1111. if (waitfloat) {
  1112. if (waitexp) {
  1113. if (!readfloat(nitem,width,dec,exp,0,va_arg(v,double*))) return false;
  1114. }
  1115. else if (waitdec) {
  1116. if (!readfloat(nitem,width,dec,-1,0,va_arg(v,double*))) return false;
  1117. }
  1118. else {
  1119. if (!readfloat(nitem,width,-1,-1,0,va_arg(v,double*))) return false;
  1120. }
  1121. }
  1122. else if (waitint) {
  1123. if (!readint(nitem,width,0,va_arg(v,int*))) return false;
  1124. }
  1125. else if (waitchar) {
  1126. if (!readchar(nitem,width,0,va_arg(v,xtring*),read_to_eol)) return false;
  1127. }
  1128. if (!iseol && !croff) readtoeol(false);
  1129. return true;
  1130. }
  1131. void formatf(xtring& output,char* format,va_list& v) {
  1132. const int MINBUF=100;
  1133. char* pchar;
  1134. xtring sect;
  1135. xtring fspec;
  1136. xtring wspec;
  1137. xtring buffer(MINBUF);
  1138. char* pfmt=(char*)format;
  1139. int i;
  1140. char waitspec=0,readspec=0,readwidth,havewidth;
  1141. output="";
  1142. i=0;
  1143. while (*pfmt) {
  1144. if (waitspec) {
  1145. if ((*pfmt>='0' && *pfmt<='9') || *pfmt=='-' || *pfmt=='+' || *pfmt==' ' ||
  1146. *pfmt=='#' || *pfmt=='h' || *pfmt=='l' || *pfmt=='I' || *pfmt=='L' ||
  1147. *pfmt=='c' || *pfmt=='C' || *pfmt=='d' || *pfmt=='i' || *pfmt=='o' ||
  1148. *pfmt=='u' || *pfmt=='x' || *pfmt=='X' || *pfmt=='e' || *pfmt=='E' ||
  1149. *pfmt=='f' || *pfmt=='g' || *pfmt=='G' || *pfmt=='n' || *pfmt=='p' ||
  1150. *pfmt=='s' || *pfmt=='S' || *pfmt=='.') {
  1151. readspec=1;
  1152. havewidth=0;
  1153. readwidth=0;
  1154. wspec="0";
  1155. fspec="%";
  1156. pfmt--;
  1157. }
  1158. else output+=*pfmt;
  1159. waitspec=0;
  1160. }
  1161. else if (readspec) {
  1162. if (*pfmt!='*') fspec+=*pfmt;
  1163. if (readwidth) {
  1164. if (*pfmt>='0' && *pfmt<='9') wspec+=*pfmt;
  1165. else {
  1166. readwidth=0;
  1167. havewidth=1;
  1168. }
  1169. }
  1170. else if (*pfmt>='0' && *pfmt<='9' && !havewidth) {
  1171. wspec=*pfmt;
  1172. readwidth=1;
  1173. }
  1174. else if (*pfmt=='*' && !havewidth) {
  1175. wspec.reserve(MINBUF);
  1176. sprintf(wspec,"%d",va_arg(v,int));
  1177. fspec+=wspec;
  1178. havewidth=1;
  1179. }
  1180. else if (*pfmt!='-' && *pfmt!='+' && *pfmt!=' ' && *pfmt!='#') {
  1181. havewidth=1;
  1182. }
  1183. if (*pfmt=='c' || *pfmt=='C' || *pfmt=='d' || *pfmt=='i' || *pfmt=='o' ||
  1184. *pfmt=='u' || *pfmt=='x' || *pfmt=='X') {
  1185. buffer.reserve((unsigned long)(MINBUF+wspec.num()));
  1186. sprintf(buffer,fspec,va_arg(v,int));
  1187. output+=buffer;
  1188. readspec=0;
  1189. }
  1190. else if (*pfmt=='e' || *pfmt=='E' || *pfmt=='f' || *pfmt=='g' ||
  1191. *pfmt=='G') {
  1192. buffer.reserve((unsigned long)(MINBUF+wspec.num()));
  1193. sprintf(buffer,fspec,va_arg(v,double));
  1194. output+=buffer;
  1195. readspec=0;
  1196. }
  1197. else if (*pfmt=='p') {
  1198. buffer.reserve((unsigned long)(MINBUF+wspec.num()));
  1199. sprintf(buffer,fspec,va_arg(v,void*));
  1200. output+=buffer;
  1201. readspec=0;
  1202. }
  1203. else if (*pfmt=='s' || *pfmt=='S') {
  1204. pchar=va_arg(v,char*);
  1205. buffer.reserve((unsigned long)(strlen(pchar)+wspec.num()));
  1206. sprintf(buffer,fspec,pchar);
  1207. output+=buffer;
  1208. readspec=0;
  1209. }
  1210. }
  1211. else if (*pfmt=='%') waitspec=1;
  1212. else {
  1213. sect[i++]=*pfmt;
  1214. if (*(pfmt+1)=='%' || !*(pfmt+1)) {
  1215. sect[i]='\0';
  1216. output+=sect;
  1217. i=0;
  1218. }
  1219. }
  1220. pfmt++;
  1221. }
  1222. }
  1223. bool fileexists(const xtring& filename) {
  1224. // Returns true if filename exists
  1225. FILE* handle;
  1226. handle=fopen(filename,"r+t");
  1227. if (!handle) return false;
  1228. fclose(handle);
  1229. return true;
  1230. }