PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Source/DOH/base.c

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