base.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* -----------------------------------------------------------------------------
  2. * This file is part of SWIG, which is licensed as a whole under version 3
  3. * (or any later version) of the GNU General Public License. Some additional
  4. * terms also apply to certain portions of SWIG. The full details of the SWIG
  5. * license and copyrights can be found in the LICENSE and COPYRIGHT files
  6. * included with the SWIG source code as distributed by the SWIG developers
  7. * and at https://www.swig.org/legal.html.
  8. *
  9. * base.c
  10. *
  11. * This file contains the function entry points for dispatching methods on
  12. * DOH objects. A number of small utility functions are also included.
  13. * ----------------------------------------------------------------------------- */
  14. #include "dohint.h"
  15. /* -----------------------------------------------------------------------------
  16. * DohDelete()
  17. * ----------------------------------------------------------------------------- */
  18. void DohDelete(DOH *obj) {
  19. DohBase *b = (DohBase *) obj;
  20. DohObjInfo *objinfo;
  21. if (!obj)
  22. return;
  23. if (!DohCheck(b)) {
  24. fputs("Fatal internal error: Attempt to delete a non-DOH object.\n", stderr);
  25. Exit(EXIT_FAILURE);
  26. }
  27. if (b->flag_intern)
  28. return;
  29. assert(b->refcount > 0);
  30. b->refcount--;
  31. if (b->refcount <= 0) {
  32. objinfo = b->type;
  33. if (objinfo->doh_del) {
  34. (objinfo->doh_del) (b);
  35. } else {
  36. if (b->data)
  37. DohFree(b->data);
  38. }
  39. DohObjFree(b);
  40. }
  41. }
  42. /* -----------------------------------------------------------------------------
  43. * DohCopy()
  44. * ----------------------------------------------------------------------------- */
  45. DOH *DohCopy(const DOH *obj) {
  46. DohBase *b = (DohBase *) obj;
  47. DohObjInfo *objinfo;
  48. if (!obj)
  49. return 0;
  50. if (!DohCheck(b)) {
  51. fputs("Fatal internal error: Attempt to copy a non-DOH object.\n", stderr);
  52. Exit(EXIT_FAILURE);
  53. }
  54. objinfo = b->type;
  55. if (objinfo->doh_copy) {
  56. DohBase *bc = (DohBase *) (objinfo->doh_copy) (b);
  57. if ((bc) && b->meta) {
  58. bc->meta = Copy(b->meta);
  59. }
  60. return (DOH *) bc;
  61. }
  62. return 0;
  63. }
  64. void DohIncref(DOH *obj) {
  65. Incref(obj);
  66. }
  67. /* -----------------------------------------------------------------------------
  68. * DohClear()
  69. * ----------------------------------------------------------------------------- */
  70. void DohClear(DOH *obj) {
  71. DohBase *b = (DohBase *) obj;
  72. DohObjInfo *objinfo = b->type;
  73. if (objinfo->doh_clear)
  74. (objinfo->doh_clear) (b);
  75. }
  76. /* -----------------------------------------------------------------------------
  77. * DohStr()
  78. * ----------------------------------------------------------------------------- */
  79. DOH *DohStr(const DOH *obj) {
  80. char buffer[512];
  81. DohBase *b = (DohBase *) obj;
  82. DohObjInfo *objinfo;
  83. if (DohCheck(b)) {
  84. objinfo = b->type;
  85. if (objinfo->doh_str) {
  86. return (objinfo->doh_str) (b);
  87. }
  88. sprintf(buffer, "<Object '%s' at %p>", objinfo->objname, (void *) b);
  89. return NewString(buffer);
  90. } else {
  91. return NewString(obj);
  92. }
  93. }
  94. /* -----------------------------------------------------------------------------
  95. * DohDump()
  96. * ----------------------------------------------------------------------------- */
  97. int DohDump(const DOH *obj, DOH *out) {
  98. DohBase *b = (DohBase *) obj;
  99. DohObjInfo *objinfo = b->type;
  100. if (objinfo->doh_dump) {
  101. return (objinfo->doh_dump) (b, out);
  102. }
  103. return 0;
  104. }
  105. /* -----------------------------------------------------------------------------
  106. * DohLen() - Defaults to strlen() if not a DOH object
  107. * ----------------------------------------------------------------------------- */
  108. int DohLen(const DOH *obj) {
  109. DohBase *b = (DohBase *) obj;
  110. DohObjInfo *objinfo;
  111. if (!b)
  112. return 0;
  113. if (DohCheck(b)) {
  114. objinfo = b->type;
  115. if (objinfo->doh_len) {
  116. return (objinfo->doh_len) (b);
  117. }
  118. return 0;
  119. } else {
  120. return (int)strlen((char *) obj);
  121. }
  122. }
  123. /* -----------------------------------------------------------------------------
  124. * DohHashVal()
  125. * ----------------------------------------------------------------------------- */
  126. int DohHashval(const DOH *obj) {
  127. DohBase *b = (DohBase *) obj;
  128. DohObjInfo *objinfo;
  129. /* obj is already checked and/or converted into DohBase* */
  130. /* if (DohCheck(b)) */
  131. {
  132. objinfo = b->type;
  133. if (objinfo->doh_hashval) {
  134. return (objinfo->doh_hashval) (b);
  135. }
  136. }
  137. return 0;
  138. }
  139. /* -----------------------------------------------------------------------------
  140. * DohData()
  141. * ----------------------------------------------------------------------------- */
  142. void *DohData(const DOH *obj) {
  143. DohBase *b = (DohBase *) obj;
  144. DohObjInfo *objinfo;
  145. if (DohCheck(obj)) {
  146. objinfo = b->type;
  147. if (objinfo->doh_data) {
  148. return (objinfo->doh_data) (b);
  149. }
  150. return 0;
  151. }
  152. return (void *) obj;
  153. }
  154. /* -----------------------------------------------------------------------------
  155. * RawData()
  156. * ----------------------------------------------------------------------------- */
  157. static void *RawData(DohBase *b) {
  158. DohObjInfo *objinfo = b->type;
  159. return (objinfo->doh_data) ? (objinfo->doh_data) (b) : 0;
  160. }
  161. /* -----------------------------------------------------------------------------
  162. * DohCmp()
  163. * ----------------------------------------------------------------------------- */
  164. int DohCmp(const DOH *obj1, const DOH *obj2) {
  165. DohBase *b1, *b2;
  166. DohObjInfo *b1info, *b2info;
  167. int c1, c2;
  168. b1 = (DohBase *) obj1;
  169. b2 = (DohBase *) obj2;
  170. c1 = DohCheck(b1);
  171. c2 = DohCheck(b2);
  172. /* most of the times, obj2 is a plain c string */
  173. if (!c1 || !c2) {
  174. if ((b1 == 0) && (b2 == 0))
  175. return 0;
  176. if (b1 && !b2)
  177. return 1;
  178. if (!b1 && b2)
  179. return -1;
  180. return strcmp((char *) (c1 ? RawData(b1) : (void *) obj1), (char *) (c2 ? RawData(b2) : (void *) obj2));
  181. }
  182. b1info = b1->type;
  183. b2info = b2->type;
  184. if ((b1info == b2info) && (b1info->doh_cmp))
  185. return (b1info->doh_cmp) (b1, b2);
  186. return 1;
  187. }
  188. /* -----------------------------------------------------------------------------
  189. * DohEqual()
  190. * ----------------------------------------------------------------------------- */
  191. int DohEqual(const DOH *obj1, const DOH *obj2) {
  192. DohBase *b1 = (DohBase *) obj1;
  193. DohBase *b2 = (DohBase *) obj2;
  194. if (!b1) {
  195. return !b2;
  196. } else if (!b2) {
  197. return 0;
  198. } else {
  199. DohObjInfo *b1info = 0;
  200. DohObjInfo *b2info = 0;
  201. if (DohCheck(b1)) {
  202. b1info = b1->type;
  203. if (DohCheck(b2)) {
  204. b2info = b2->type;
  205. } else {
  206. int len = (b1info->doh_len) (b1);
  207. char *cobj = (char *) obj2;
  208. return len == (int) strlen(cobj) ? (memcmp(RawData(b1), cobj, len) == 0) : 0;
  209. }
  210. } else if (DohCheck(b2)) {
  211. int len = (b2->type->doh_len) (b2);
  212. char *cobj = (char *) obj1;
  213. return len == (int) strlen(cobj) ? (memcmp(RawData(b2), cobj, len) == 0) : 0;
  214. } else {
  215. return strcmp((char *) obj1, (char *) obj2) == 0;
  216. }
  217. if (!b1info) {
  218. return obj1 == obj2;
  219. } else if (b1info == b2info) {
  220. return b1info->doh_equal ? (b1info->doh_equal) (b1, b2) : (b1info->doh_cmp ? (b1info->doh_cmp) (b1, b2) == 0 : (b1 == b2));
  221. } else {
  222. return 0;
  223. }
  224. }
  225. }
  226. /* -----------------------------------------------------------------------------
  227. * DohFirst()
  228. * ----------------------------------------------------------------------------- */
  229. DohIterator DohFirst(DOH *obj) {
  230. DohIterator iter;
  231. DohBase *b;
  232. DohObjInfo *binfo;
  233. b = (DohBase *) obj;
  234. if (DohCheck(b)) {
  235. binfo = b->type;
  236. if (binfo->doh_first) {
  237. return (binfo->doh_first) (b);
  238. }
  239. }
  240. iter.object = 0;
  241. iter.item = 0;
  242. iter.key = 0;
  243. iter._current = 0;
  244. iter._index = 0;
  245. return iter;
  246. }
  247. /* -----------------------------------------------------------------------------
  248. * DohNext()
  249. * ----------------------------------------------------------------------------- */
  250. DohIterator DohNext(DohIterator iter) {
  251. DohIterator niter;
  252. if (iter.object) {
  253. DohBase *b;
  254. DohObjInfo *binfo;
  255. b = (DohBase *) iter.object;
  256. binfo = b->type;
  257. if (binfo->doh_next) {
  258. return (binfo->doh_next) (iter);
  259. }
  260. }
  261. niter = iter;
  262. return niter;
  263. }
  264. /* -----------------------------------------------------------------------------
  265. * DohIsMapping()
  266. * ----------------------------------------------------------------------------- */
  267. int DohIsMapping(const DOH *obj) {
  268. DohBase *b = (DohBase *) obj;
  269. DohObjInfo *objinfo;
  270. if (!DohCheck(b))
  271. return 0;
  272. objinfo = b->type;
  273. if (objinfo->doh_hash)
  274. return 1;
  275. else
  276. return 0;
  277. }
  278. /* -----------------------------------------------------------------------------
  279. * DohGetattr()
  280. * ----------------------------------------------------------------------------- */
  281. DOH *DohGetattr(DOH *obj, const DOH *name) {
  282. DohBase *b = (DohBase *) obj;
  283. DohObjInfo *objinfo = b->type;
  284. if (objinfo->doh_hash && objinfo->doh_hash->doh_getattr) {
  285. DOH *r = (objinfo->doh_hash->doh_getattr) (b, (DOH *) name);
  286. return (r == DohNone) ? 0 : r;
  287. }
  288. return 0;
  289. }
  290. /* -----------------------------------------------------------------------------
  291. * DohSetattr()
  292. * ----------------------------------------------------------------------------- */
  293. int DohSetattr(DOH *obj, const DOH *name, const DOH *value) {
  294. DohBase *b = (DohBase *) obj;
  295. DohObjInfo *objinfo = b->type;
  296. if (objinfo->doh_hash && objinfo->doh_hash->doh_setattr) {
  297. return (objinfo->doh_hash->doh_setattr) (b, (DOH *) name, (DOH *) value);
  298. }
  299. return 0;
  300. }
  301. /* -----------------------------------------------------------------------------
  302. * DohDelattr()
  303. * ----------------------------------------------------------------------------- */
  304. int DohDelattr(DOH *obj, const DOH *name) {
  305. DohBase *b = (DohBase *) obj;
  306. DohObjInfo *objinfo = b->type;
  307. if (objinfo->doh_hash && objinfo->doh_hash->doh_delattr) {
  308. return (objinfo->doh_hash->doh_delattr) (b, (DOH *) name);
  309. }
  310. return 0;
  311. }
  312. /* -----------------------------------------------------------------------------
  313. * DohCheckattr()
  314. * ----------------------------------------------------------------------------- */
  315. int DohCheckattr(DOH *obj, const DOH *name, const DOH *value) {
  316. DOH *attr = Getattr(obj,name);
  317. if (!attr) return 0;
  318. return DohEqual(attr,value);
  319. }
  320. /* -----------------------------------------------------------------------------
  321. * DohKeys()
  322. * ----------------------------------------------------------------------------- */
  323. DOH *DohKeys(DOH *obj) {
  324. DohBase *b = (DohBase *) obj;
  325. DohObjInfo *objinfo = b->type;
  326. if (objinfo && objinfo->doh_hash->doh_keys) {
  327. return (objinfo->doh_hash->doh_keys) (b);
  328. }
  329. return 0;
  330. }
  331. /* -----------------------------------------------------------------------------
  332. * DohSortedKeys()
  333. * ----------------------------------------------------------------------------- */
  334. DOH *DohSortedKeys(DOH *obj, int (*cmp) (const DOH *, const DOH *)) {
  335. DOHList *keys = DohKeys(obj);
  336. if (keys) {
  337. DohSortList(keys, cmp);
  338. }
  339. return keys;
  340. }
  341. /* -----------------------------------------------------------------------------
  342. * DohGetInt()
  343. * ----------------------------------------------------------------------------- */
  344. int DohGetInt(DOH *obj, const DOH *name) {
  345. DOH *val;
  346. val = Getattr(obj, (DOH *) name);
  347. if (!val)
  348. return 0;
  349. if (DohIsString(val)) {
  350. return atoi((char *) Data(val));
  351. }
  352. return 0;
  353. }
  354. /* -----------------------------------------------------------------------------
  355. * DohGetDouble()
  356. * ----------------------------------------------------------------------------- */
  357. double DohGetDouble(DOH *obj, const DOH *name) {
  358. DOH *val;
  359. val = Getattr(obj, (DOH *) name);
  360. if (!val)
  361. return 0;
  362. if (DohIsString(val)) {
  363. return atof((char *) Data(val));
  364. }
  365. return 0;
  366. }
  367. /* -----------------------------------------------------------------------------
  368. * DohGetChar()
  369. * ----------------------------------------------------------------------------- */
  370. char *DohGetChar(DOH *obj, const DOH *name) {
  371. DOH *val;
  372. val = Getattr(obj, (DOH *) name);
  373. if (!val)
  374. return 0;
  375. if (DohIsString(val)) {
  376. return (char *) Data(val);
  377. }
  378. return 0;
  379. }
  380. /* -----------------------------------------------------------------------------
  381. * DohGetFlagAttr() / DohGetFlag()
  382. * A flag is unset if the attribute (name) does not exist on the node (obj),
  383. * or it is set to "0". If the attribute is set to any other value,
  384. * the flag is set.
  385. *
  386. * DohGetFlag() returns if the flag is set or not
  387. * DohGetFlagAttr() returns the flag value if is set, NULL otherwise
  388. * ----------------------------------------------------------------------------- */
  389. DOH *DohGetFlagAttr(DOH *obj, const DOH *name) {
  390. DOH *val = Getattr(obj, (DOH *) name);
  391. if (!val) {
  392. return NULL;
  393. } else {
  394. const char *cval = Char(val);
  395. if (!cval)
  396. return val;
  397. return (strcmp(cval, "0") != 0) ? val : NULL;
  398. }
  399. }
  400. int DohGetFlag(DOH *obj, const DOH *name) {
  401. return DohGetFlagAttr(obj, name) ? 1 : 0;
  402. }
  403. /* -----------------------------------------------------------------------------
  404. * DohGetVoid()
  405. * ----------------------------------------------------------------------------- */
  406. void *DohGetVoid(DOH *obj, const DOH *name) {
  407. DOH *val;
  408. val = Getattr(obj, (DOH *) name);
  409. if (!val)
  410. return 0;
  411. return (void *) Data(val);
  412. }
  413. /* -----------------------------------------------------------------------------
  414. * DohSetInt()
  415. * ----------------------------------------------------------------------------- */
  416. void DohSetInt(DOH *obj, const DOH *name, int value) {
  417. DOH *temp;
  418. temp = NewStringEmpty();
  419. Printf(temp, "%d", value);
  420. Setattr(obj, (DOH *) name, temp);
  421. }
  422. /* -----------------------------------------------------------------------------
  423. * DohSetDouble()
  424. * ----------------------------------------------------------------------------- */
  425. void DohSetDouble(DOH *obj, const DOH *name, double value) {
  426. DOH *temp;
  427. temp = NewStringEmpty();
  428. Printf(temp, "%0.17f", value);
  429. Setattr(obj, (DOH *) name, temp);
  430. }
  431. /* -----------------------------------------------------------------------------
  432. * DohSetChar()
  433. * ----------------------------------------------------------------------------- */
  434. void DohSetChar(DOH *obj, const DOH *name, char *value) {
  435. Setattr(obj, (DOH *) name, NewString(value));
  436. }
  437. /* -----------------------------------------------------------------------------
  438. * DohSetFlag()
  439. * ----------------------------------------------------------------------------- */
  440. void DohSetFlagAttr(DOH *obj, const DOH *name, const DOH *attr) {
  441. Setattr(obj, (DOH *) name, attr ? attr : NewString("0"));
  442. }
  443. void DohSetFlag(DOH *obj, const DOH *name) {
  444. Setattr(obj, (DOH *) name, NewString("1"));
  445. }
  446. /* -----------------------------------------------------------------------------
  447. * DohSetVoid()
  448. * ----------------------------------------------------------------------------- */
  449. void DohSetVoid(DOH *obj, const DOH *name, void *value) {
  450. Setattr(obj, (DOH *) name, NewVoid(value, 0));
  451. }
  452. /* -----------------------------------------------------------------------------
  453. * DohIsSequence()
  454. * ----------------------------------------------------------------------------- */
  455. int DohIsSequence(const DOH *obj) {
  456. DohBase *b = (DohBase *) obj;
  457. DohObjInfo *objinfo;
  458. if (!DohCheck(b))
  459. return 0;
  460. objinfo = b->type;
  461. if (objinfo->doh_list)
  462. return 1;
  463. else
  464. return 0;
  465. }
  466. /* -----------------------------------------------------------------------------
  467. * DohGetitem()
  468. * ----------------------------------------------------------------------------- */
  469. DOH *DohGetitem(DOH *obj, int index) {
  470. DohBase *b = (DohBase *) obj;
  471. DohObjInfo *objinfo = b->type;
  472. if (objinfo->doh_list && objinfo->doh_list->doh_getitem) {
  473. return (objinfo->doh_list->doh_getitem) (b, index);
  474. }
  475. return 0;
  476. }
  477. /* -----------------------------------------------------------------------------
  478. * DohSetitem()
  479. * ----------------------------------------------------------------------------- */
  480. int DohSetitem(DOH *obj, int index, const DOH *value) {
  481. DohBase *b = (DohBase *) obj;
  482. DohObjInfo *objinfo = b->type;
  483. if (objinfo->doh_list && objinfo->doh_list->doh_setitem) {
  484. return (objinfo->doh_list->doh_setitem) (b, index, (DOH *) value);
  485. }
  486. return -1;
  487. }
  488. /* -----------------------------------------------------------------------------
  489. * DohDelitem()
  490. * ----------------------------------------------------------------------------- */
  491. int DohDelitem(DOH *obj, int index) {
  492. DohBase *b = (DohBase *) obj;
  493. DohObjInfo *objinfo = b->type;
  494. if (objinfo->doh_list && objinfo->doh_list->doh_delitem) {
  495. return (objinfo->doh_list->doh_delitem) (b, index);
  496. }
  497. return -1;
  498. }
  499. /* -----------------------------------------------------------------------------
  500. * DohInsertitem()
  501. * ----------------------------------------------------------------------------- */
  502. int DohInsertitem(DOH *obj, int index, const DOH *value) {
  503. DohBase *b = (DohBase *) obj;
  504. DohObjInfo *objinfo = b->type;
  505. if (objinfo->doh_list && objinfo->doh_list->doh_insitem) {
  506. return (objinfo->doh_list->doh_insitem) (b, index, (DOH *) value);
  507. }
  508. return -1;
  509. }
  510. /* -----------------------------------------------------------------------------
  511. * DohDelslice()
  512. * ----------------------------------------------------------------------------- */
  513. int DohDelslice(DOH *obj, int sindex, int eindex) {
  514. DohBase *b = (DohBase *) obj;
  515. DohObjInfo *objinfo = b->type;
  516. if (objinfo->doh_list && objinfo->doh_list->doh_delslice) {
  517. return (objinfo->doh_list->doh_delslice) (b, sindex, eindex);
  518. }
  519. return -1;
  520. }
  521. /* -----------------------------------------------------------------------------
  522. * DohIsFile()
  523. * ----------------------------------------------------------------------------- */
  524. int DohIsFile(const DOH *obj) {
  525. DohBase *b = (DohBase *) obj;
  526. DohObjInfo *objinfo;
  527. if (!DohCheck(b))
  528. return 0;
  529. objinfo = b->type;
  530. if (objinfo->doh_file)
  531. return 1;
  532. else
  533. return 0;
  534. }
  535. /* -----------------------------------------------------------------------------
  536. * DohRead()
  537. * ----------------------------------------------------------------------------- */
  538. int DohRead(DOH *obj, void *buffer, int length) {
  539. DohBase *b = (DohBase *) obj;
  540. DohObjInfo *objinfo;
  541. if (DohCheck(obj)) {
  542. objinfo = b->type;
  543. if ((objinfo->doh_file) && (objinfo->doh_file->doh_read)) {
  544. return (objinfo->doh_file->doh_read) (b, buffer, length);
  545. }
  546. return -1;
  547. }
  548. /* Hmmm. Not a file. Maybe it's a real FILE */
  549. return (int)fread(buffer, 1, length, (FILE *) b);
  550. }
  551. /* -----------------------------------------------------------------------------
  552. * DohWrite()
  553. * ----------------------------------------------------------------------------- */
  554. int DohWrite(DOH *obj, const void *buffer, int length) {
  555. DohBase *b = (DohBase *) obj;
  556. DohObjInfo *objinfo;
  557. if (DohCheck(obj)) {
  558. objinfo = b->type;
  559. if ((objinfo->doh_file) && (objinfo->doh_file->doh_write)) {
  560. return (objinfo->doh_file->doh_write) (b, buffer, length);
  561. }
  562. return -1;
  563. }
  564. /* Hmmm. Not a file. Maybe it's a real FILE */
  565. return (int)fwrite(buffer, 1, length, (FILE *) b);
  566. }
  567. /* -----------------------------------------------------------------------------
  568. * DohSeek()
  569. * ----------------------------------------------------------------------------- */
  570. int DohSeek(DOH *obj, long offset, int whence) {
  571. DohBase *b = (DohBase *) obj;
  572. DohObjInfo *objinfo;
  573. if (DohCheck(obj)) {
  574. objinfo = b->type;
  575. if ((objinfo->doh_file) && (objinfo->doh_file->doh_seek)) {
  576. return (objinfo->doh_file->doh_seek) (b, offset, whence);
  577. }
  578. return -1;
  579. }
  580. return fseek((FILE *) b, offset, whence);
  581. }
  582. /* -----------------------------------------------------------------------------
  583. * DohTell()
  584. * ----------------------------------------------------------------------------- */
  585. long DohTell(DOH *obj) {
  586. DohBase *b = (DohBase *) obj;
  587. DohObjInfo *objinfo;
  588. if (DohCheck(obj)) {
  589. objinfo = b->type;
  590. if ((objinfo->doh_file) && (objinfo->doh_file->doh_tell)) {
  591. return (objinfo->doh_file->doh_tell) (b);
  592. }
  593. return -1;
  594. }
  595. return ftell((FILE *) b);
  596. }
  597. /* -----------------------------------------------------------------------------
  598. * DohGetc()
  599. * ----------------------------------------------------------------------------- */
  600. int DohGetc(DOH *obj) {
  601. static DOH *lastdoh = 0;
  602. DohBase *b = (DohBase *) obj;
  603. DohObjInfo *objinfo;
  604. if (obj == lastdoh) {
  605. objinfo = b->type;
  606. return (objinfo->doh_file->doh_getc) (b);
  607. }
  608. if (DohCheck(obj)) {
  609. objinfo = b->type;
  610. if (objinfo->doh_file->doh_getc) {
  611. lastdoh = obj;
  612. return (objinfo->doh_file->doh_getc) (b);
  613. }
  614. return EOF;
  615. }
  616. return fgetc((FILE *) b);
  617. }
  618. /* -----------------------------------------------------------------------------
  619. * DohPutc()
  620. * ----------------------------------------------------------------------------- */
  621. int DohPutc(int ch, DOH *obj) {
  622. static DOH *lastdoh = 0;
  623. DohBase *b = (DohBase *) obj;
  624. DohObjInfo *objinfo;
  625. if (obj == lastdoh) {
  626. objinfo = b->type;
  627. return (objinfo->doh_file->doh_putc) (b, ch);
  628. }
  629. if (DohCheck(obj)) {
  630. objinfo = b->type;
  631. if (objinfo->doh_file->doh_putc) {
  632. lastdoh = obj;
  633. return (objinfo->doh_file->doh_putc) (b, ch);
  634. }
  635. return EOF;
  636. }
  637. return fputc(ch, (FILE *) b);
  638. }
  639. /* -----------------------------------------------------------------------------
  640. * DohUngetc()
  641. * ----------------------------------------------------------------------------- */
  642. int DohUngetc(int ch, DOH *obj) {
  643. DohBase *b = (DohBase *) obj;
  644. DohObjInfo *objinfo;
  645. if (DohCheck(obj)) {
  646. objinfo = b->type;
  647. if (objinfo->doh_file->doh_ungetc) {
  648. return (objinfo->doh_file->doh_ungetc) (b, ch);
  649. }
  650. return EOF;
  651. }
  652. return ungetc(ch, (FILE *) b);
  653. }
  654. /* -----------------------------------------------------------------------------
  655. * DohIsString()
  656. * ----------------------------------------------------------------------------- */
  657. int DohIsString(const DOH *obj) {
  658. DohBase *b = (DohBase *) obj;
  659. DohObjInfo *objinfo;
  660. if (!DohCheck(b))
  661. return 0;
  662. objinfo = b->type;
  663. if (objinfo->doh_string)
  664. return 1;
  665. else
  666. return 0;
  667. }
  668. /* -----------------------------------------------------------------------------
  669. * DohReplace()
  670. * ----------------------------------------------------------------------------- */
  671. int DohReplace(DOH *src, const DOH *token, const DOH *rep, int flags) {
  672. DohBase *b = (DohBase *) src;
  673. DohObjInfo *objinfo;
  674. if (!token)
  675. return 0;
  676. if (!rep)
  677. rep = "";
  678. if (DohIsString(src)) {
  679. objinfo = b->type;
  680. if (objinfo->doh_string->doh_replace) {
  681. return (objinfo->doh_string->doh_replace) (b, (DOH *) token, (DOH *) rep, flags);
  682. }
  683. }
  684. return 0;
  685. }
  686. /* -----------------------------------------------------------------------------
  687. * DohChop()
  688. * ----------------------------------------------------------------------------- */
  689. void DohChop(DOH *src) {
  690. DohBase *b = (DohBase *) src;
  691. DohObjInfo *objinfo;
  692. if (DohIsString(src)) {
  693. objinfo = b->type;
  694. if (objinfo->doh_string->doh_chop) {
  695. (objinfo->doh_string->doh_chop) (b);
  696. }
  697. }
  698. }
  699. /* -----------------------------------------------------------------------------
  700. * DohSetFile()
  701. * ----------------------------------------------------------------------------- */
  702. void DohSetfile(DOH *ho, DOH *file) {
  703. DohBase *h = (DohBase *) ho;
  704. DohObjInfo *objinfo;
  705. if (!h)
  706. return;
  707. objinfo = h->type;
  708. if (objinfo->doh_setfile)
  709. (objinfo->doh_setfile) (h, file);
  710. }
  711. /* -----------------------------------------------------------------------------
  712. * DohGetFile()
  713. * ----------------------------------------------------------------------------- */
  714. DOH *DohGetfile(const DOH *ho) {
  715. DohBase *h = (DohBase *) ho;
  716. DohObjInfo *objinfo;
  717. if (!h)
  718. return 0;
  719. objinfo = h->type;
  720. if (objinfo->doh_getfile)
  721. return (objinfo->doh_getfile) (h);
  722. return 0;
  723. }
  724. /* -----------------------------------------------------------------------------
  725. * DohSetLine()
  726. * ----------------------------------------------------------------------------- */
  727. void DohSetline(DOH *ho, int l) {
  728. DohBase *h = (DohBase *) ho;
  729. DohObjInfo *objinfo;
  730. if (!h)
  731. return;
  732. objinfo = h->type;
  733. if (objinfo->doh_setline)
  734. (objinfo->doh_setline) (h, l);
  735. }
  736. /* -----------------------------------------------------------------------------
  737. * DohGetLine()
  738. * ----------------------------------------------------------------------------- */
  739. int DohGetline(const DOH *ho) {
  740. DohBase *h = (DohBase *) ho;
  741. DohObjInfo *objinfo;
  742. if (!h)
  743. return 0;
  744. objinfo = h->type;
  745. if (objinfo->doh_getline)
  746. return (objinfo->doh_getline) (h);
  747. return 0;
  748. }
  749. /* -----------------------------------------------------------------------------
  750. * DohGetmeta()
  751. * ----------------------------------------------------------------------------- */
  752. DOH *DohGetmeta(DOH *ho, const DOH *name) {
  753. DohBase *h = (DohBase *) ho;
  754. if (!DohCheck(ho))
  755. return 0;
  756. if (!h->meta)
  757. return 0;
  758. return DohGetattr(h->meta, name);
  759. }
  760. /* -----------------------------------------------------------------------------
  761. * DohGetmeta()
  762. * ----------------------------------------------------------------------------- */
  763. int DohSetmeta(DOH *ho, const DOH *name, const DOH *value) {
  764. DohBase *h = (DohBase *) ho;
  765. if (!DohCheck(ho))
  766. return 0;
  767. if (!h->meta)
  768. h->meta = NewHash();
  769. return DohSetattr(h->meta, name, value);
  770. }
  771. /* -----------------------------------------------------------------------------
  772. * DohDelmeta()
  773. * ----------------------------------------------------------------------------- */
  774. int DohDelmeta(DOH *ho, const DOH *name) {
  775. DohBase *h = (DohBase *) ho;
  776. if (!DohCheck(ho))
  777. return 0;
  778. if (!h->meta)
  779. return 0;
  780. return DohDelattr(h->meta, name);
  781. }
  782. /* -----------------------------------------------------------------------------
  783. * DohSetmark()
  784. * ----------------------------------------------------------------------------- */
  785. void DohSetmark(DOH *ho, int x) {
  786. DohBase *h = (DohBase *) ho;
  787. h->flag_usermark = x;
  788. }
  789. int DohGetmark(DOH *ho) {
  790. DohBase *h = (DohBase *) ho;
  791. return h->flag_usermark;
  792. }
  793. /* -----------------------------------------------------------------------------
  794. * DohCall()
  795. *
  796. * Invokes a function via DOH. A Function is represented by a hash table with
  797. * the following attributes:
  798. *
  799. * "builtin" - Pointer to built-in function (if any)
  800. *
  801. * (Additional attributes may be added later)
  802. *
  803. * Returns a DOH object with result on success. Returns NULL on error
  804. * ----------------------------------------------------------------------------- */
  805. DOH *DohCall(DOH *func, DOH *args) {
  806. DOH *result;
  807. DohFuncPtr_t builtin;
  808. builtin.p = GetVoid(func, "builtin");
  809. if (!builtin.p)
  810. return 0;
  811. result = (*builtin.func) (args);
  812. return result;
  813. }