PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1.3.35/Source/Swig/misc.c

#
C | 1068 lines | 1008 code | 20 blank | 40 comment | 32 complexity | 8dc197cf0263ef785f2c4980945f5880 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * See the LICENSE file for information on copyright, usage and redistribution
  3. * of SWIG, and the README file for authors - http://www.swig.org/release.html.
  4. *
  5. * misc.c
  6. *
  7. * Miscellaneous functions that don't really fit anywhere else.
  8. * ----------------------------------------------------------------------------- */
  9. char cvsroot_misc_c[] = "$Id: misc.c 9991 2007-10-16 21:27:14Z olly $";
  10. #include "swig.h"
  11. #include <errno.h>
  12. #include <ctype.h>
  13. #include <limits.h>
  14. static char *fake_version = 0;
  15. /* -----------------------------------------------------------------------------
  16. * Swig_copy_string()
  17. *
  18. * Duplicate a NULL-terminate string given as a char *.
  19. * ----------------------------------------------------------------------------- */
  20. char *Swig_copy_string(const char *s) {
  21. char *c = 0;
  22. if (s) {
  23. c = (char *) malloc(strlen(s) + 1);
  24. strcpy(c, s);
  25. }
  26. return c;
  27. }
  28. /* -----------------------------------------------------------------------------
  29. * Swig_set_fakeversion()
  30. *
  31. * Version string override
  32. * ----------------------------------------------------------------------------- */
  33. void Swig_set_fakeversion(const char *version) {
  34. fake_version = Swig_copy_string(version);
  35. }
  36. /* -----------------------------------------------------------------------------
  37. * Swig_package_version()
  38. *
  39. * Return the package string containing the version number
  40. * ----------------------------------------------------------------------------- */
  41. const char *Swig_package_version(void) {
  42. return fake_version ? fake_version : PACKAGE_VERSION;
  43. }
  44. /* -----------------------------------------------------------------------------
  45. * Swig_banner()
  46. *
  47. * Emits the SWIG identifying banner.
  48. * ----------------------------------------------------------------------------- */
  49. void Swig_banner(File *f) {
  50. Printf(f, "/* ----------------------------------------------------------------------------\n\
  51. * This file was automatically generated by SWIG (http://www.swig.org).\n\
  52. * Version %s\n\
  53. * \n\
  54. * This file is not intended to be easily readable and contains a number of \n\
  55. * coding conventions designed to improve portability and efficiency. Do not make\n\
  56. * changes to this file unless you know what you are doing--modify the SWIG \n\
  57. * interface file instead. \n", Swig_package_version());
  58. /* String too long for ISO compliance */
  59. Printf(f, " * ----------------------------------------------------------------------------- */\n\n");
  60. }
  61. /* -----------------------------------------------------------------------------
  62. * Swig_strip_c_comments()
  63. *
  64. * Return a new string with C comments stripped from the input string. Null is
  65. * returned if there aren't any.
  66. * ----------------------------------------------------------------------------- */
  67. String *Swig_strip_c_comments(const String *s) {
  68. const char *c = Char(s);
  69. const char *comment_begin = 0;
  70. const char *comment_end = 0;
  71. String *stripped = 0;
  72. while (*c) {
  73. if (!comment_begin && *c == '/') {
  74. ++c;
  75. if (!*c)
  76. break;
  77. if (*c == '*')
  78. comment_begin = c-1;
  79. } else if (comment_begin && !comment_end && *c == '*') {
  80. ++c;
  81. if (*c == '/')
  82. comment_end = c;
  83. break;
  84. }
  85. ++c;
  86. }
  87. if (comment_begin && comment_end) {
  88. int size = comment_begin - Char(s);
  89. String *stripmore = 0;
  90. stripped = NewStringWithSize(s, size);
  91. Printv(stripped, comment_end + 1, NIL);
  92. do {
  93. stripmore = Swig_strip_c_comments(stripped);
  94. if (stripmore) {
  95. Delete(stripped);
  96. stripped = stripmore;
  97. }
  98. } while (stripmore);
  99. }
  100. return stripped;
  101. }
  102. /* -----------------------------------------------------------------------------
  103. * Swig_string_escape()
  104. *
  105. * Takes a string object and produces a string with escape codes added to it.
  106. * ----------------------------------------------------------------------------- */
  107. String *Swig_string_escape(String *s) {
  108. String *ns;
  109. int c;
  110. ns = NewStringEmpty();
  111. while ((c = Getc(s)) != EOF) {
  112. if (c == '\n') {
  113. Printf(ns, "\\n");
  114. } else if (c == '\r') {
  115. Printf(ns, "\\r");
  116. } else if (c == '\t') {
  117. Printf(ns, "\\t");
  118. } else if (c == '\\') {
  119. Printf(ns, "\\\\");
  120. } else if (c == '\'') {
  121. Printf(ns, "\\'");
  122. } else if (c == '\"') {
  123. Printf(ns, "\\\"");
  124. } else if (c == ' ') {
  125. Putc(c, ns);
  126. } else if (!isgraph(c)) {
  127. if (c < 0)
  128. c += UCHAR_MAX + 1;
  129. Printf(ns, "\\%o", c);
  130. } else {
  131. Putc(c, ns);
  132. }
  133. }
  134. return ns;
  135. }
  136. /* -----------------------------------------------------------------------------
  137. * Swig_string_upper()
  138. *
  139. * Takes a string object and returns a copy that is uppercase
  140. * ----------------------------------------------------------------------------- */
  141. String *Swig_string_upper(String *s) {
  142. String *ns;
  143. int c;
  144. ns = NewStringEmpty();
  145. Seek(s, 0, SEEK_SET);
  146. while ((c = Getc(s)) != EOF) {
  147. Putc(toupper(c), ns);
  148. }
  149. return ns;
  150. }
  151. /* -----------------------------------------------------------------------------
  152. * Swig_string_lower()
  153. *
  154. * Takes a string object and returns a copy that is lowercase
  155. * ----------------------------------------------------------------------------- */
  156. String *Swig_string_lower(String *s) {
  157. String *ns;
  158. int c;
  159. ns = NewStringEmpty();
  160. Seek(s, 0, SEEK_SET);
  161. while ((c = Getc(s)) != EOF) {
  162. Putc(tolower(c), ns);
  163. }
  164. return ns;
  165. }
  166. /* -----------------------------------------------------------------------------
  167. * Swig_string_title()
  168. *
  169. * Takes a string object and returns a copy that is lowercase with first letter
  170. * capitalized
  171. * ----------------------------------------------------------------------------- */
  172. String *Swig_string_title(String *s) {
  173. String *ns;
  174. int first = 1;
  175. int c;
  176. ns = NewStringEmpty();
  177. Seek(s, 0, SEEK_SET);
  178. while ((c = Getc(s)) != EOF) {
  179. Putc(first ? toupper(c) : tolower(c), ns);
  180. first = 0;
  181. }
  182. return ns;
  183. }
  184. /* -----------------------------------------------------------------------------
  185. * Swig_string_ccase()
  186. *
  187. * Takes a string object and returns a copy that is lowercase with the first
  188. * letter capitalized and the one following '_', which are removed.
  189. *
  190. * camel_case -> CamelCase
  191. * camelCase -> CamelCase
  192. * ----------------------------------------------------------------------------- */
  193. String *Swig_string_ccase(String *s) {
  194. String *ns;
  195. int first = 1;
  196. int c;
  197. ns = NewStringEmpty();
  198. Seek(s, 0, SEEK_SET);
  199. while ((c = Getc(s)) != EOF) {
  200. if (c == '_') {
  201. first = 1;
  202. continue;
  203. }
  204. Putc(first ? toupper(c) : c, ns);
  205. first = 0;
  206. }
  207. return ns;
  208. }
  209. /* -----------------------------------------------------------------------------
  210. * Swig_string_lccase()
  211. *
  212. * Takes a string object and returns a copy with the character after
  213. * each '_' capitalised, and the '_' removed. The first character is
  214. * also forced to lowercase.
  215. *
  216. * camel_case -> camelCase
  217. * CamelCase -> camelCase
  218. * ----------------------------------------------------------------------------- */
  219. String *Swig_string_lccase(String *s) {
  220. String *ns;
  221. int first = 1;
  222. int after_underscore = 0;
  223. int c;
  224. ns = NewStringEmpty();
  225. Seek(s, 0, SEEK_SET);
  226. while ((c = Getc(s)) != EOF) {
  227. if (c == '_') {
  228. after_underscore = 1;
  229. continue;
  230. }
  231. if (first) {
  232. Putc(tolower(c), ns);
  233. first = 0;
  234. } else {
  235. Putc(after_underscore ? toupper(c) : c, ns);
  236. }
  237. after_underscore = 0;
  238. }
  239. return ns;
  240. }
  241. /* -----------------------------------------------------------------------------
  242. * Swig_string_ucase()
  243. *
  244. * This is the reverse case of ccase, ie
  245. *
  246. * CamelCase -> camel_case
  247. * get2D -> get_2d
  248. * asFloat2 -> as_float2
  249. * ----------------------------------------------------------------------------- */
  250. String *Swig_string_ucase(String *s) {
  251. String *ns;
  252. int c;
  253. int lastC = 0;
  254. int nextC = 0;
  255. int underscore = 0;
  256. ns = NewStringEmpty();
  257. /* We insert a underscore when:
  258. 1. Lower case char followed by upper case char
  259. getFoo > get_foo; getFOo > get_foo; GETFOO > getfoo
  260. 2. Number proceded by char and not end of string
  261. get2D > get_2d; get22D > get_22d; GET2D > get_2d
  262. but:
  263. asFloat2 > as_float2
  264. */
  265. Seek(s, 0, SEEK_SET);
  266. while ((c = Getc(s)) != EOF) {
  267. nextC = Getc(s); Ungetc(nextC, s);
  268. if (isdigit(c) && isalpha(lastC) && nextC != EOF)
  269. underscore = 1;
  270. else if (isupper(c) && isalpha(lastC) && !isupper(lastC))
  271. underscore = 1;
  272. lastC = c;
  273. if (underscore) {
  274. Putc('_', ns);
  275. underscore = 0;
  276. }
  277. Putc(tolower(c), ns);
  278. }
  279. return ns;
  280. }
  281. /* -----------------------------------------------------------------------------
  282. * Swig_string_first_upper()
  283. *
  284. * Make the first character in the string uppercase, leave all the
  285. * rest the same. This is used by the Ruby module to provide backwards
  286. * compatibility with the old way of naming classes and constants. For
  287. * more info see the Ruby documentation.
  288. *
  289. * firstUpper -> FirstUpper
  290. * ----------------------------------------------------------------------------- */
  291. String *Swig_string_first_upper(String *s) {
  292. String *ns = NewStringEmpty();
  293. char *cs = Char(s);
  294. if (cs && cs[0] != 0) {
  295. Putc(toupper((int)cs[0]), ns);
  296. Append(ns, cs + 1);
  297. }
  298. return ns;
  299. }
  300. /* -----------------------------------------------------------------------------
  301. * Swig_string_first_lower()
  302. *
  303. * Make the first character in the string lowercase, leave all the
  304. * rest the same. This is used by the Ruby module to provide backwards
  305. * compatibility with the old way of naming classes and constants. For
  306. * more info see the Ruby documentation.
  307. *
  308. * firstLower -> FirstLower
  309. * ----------------------------------------------------------------------------- */
  310. String *Swig_string_first_lower(String *s) {
  311. String *ns = NewStringEmpty();
  312. char *cs = Char(s);
  313. if (cs && cs[0] != 0) {
  314. Putc(tolower((int)cs[0]), ns);
  315. Append(ns, cs + 1);
  316. }
  317. return ns;
  318. }
  319. /* -----------------------------------------------------------------------------
  320. * Swig_string_schemify()
  321. *
  322. * Replace underscores with dashes, to make identifiers look nice to Schemers.
  323. *
  324. * under_scores -> under-scores
  325. * ----------------------------------------------------------------------------- */
  326. String *Swig_string_schemify(String *s) {
  327. String *ns = NewString(s);
  328. Replaceall(ns, "_", "-");
  329. return ns;
  330. }
  331. /* -----------------------------------------------------------------------------
  332. * Swig_string_typecode()
  333. *
  334. * Takes a string with possible type-escapes in it and replaces them with
  335. * real C datatypes.
  336. * ----------------------------------------------------------------------------- */
  337. String *Swig_string_typecode(String *s) {
  338. String *ns;
  339. int c;
  340. String *tc;
  341. ns = NewStringEmpty();
  342. while ((c = Getc(s)) != EOF) {
  343. if (c == '`') {
  344. String *str = 0;
  345. tc = NewStringEmpty();
  346. while ((c = Getc(s)) != EOF) {
  347. if (c == '`')
  348. break;
  349. Putc(c, tc);
  350. }
  351. str = SwigType_str(tc, 0);
  352. Append(ns, str);
  353. Delete(str);
  354. } else {
  355. Putc(c, ns);
  356. if (c == '\'') {
  357. while ((c = Getc(s)) != EOF) {
  358. Putc(c, ns);
  359. if (c == '\'')
  360. break;
  361. if (c == '\\') {
  362. c = Getc(s);
  363. Putc(c, ns);
  364. }
  365. }
  366. } else if (c == '\"') {
  367. while ((c = Getc(s)) != EOF) {
  368. Putc(c, ns);
  369. if (c == '\"')
  370. break;
  371. if (c == '\\') {
  372. c = Getc(s);
  373. Putc(c, ns);
  374. }
  375. }
  376. }
  377. }
  378. }
  379. return ns;
  380. }
  381. /* -----------------------------------------------------------------------------
  382. * Swig_string_mangle()
  383. *
  384. * Take a string and mangle it by stripping all non-valid C identifier
  385. * characters.
  386. *
  387. * This routine skips unnecessary blank spaces, therefore mangling
  388. * 'char *' and 'char*', 'std::pair<int, int >' and
  389. * 'std::pair<int,int>', produce the same result.
  390. *
  391. * However, note that 'long long' and 'long_long' produce different
  392. * mangled strings.
  393. *
  394. * The mangling method still is not 'perfect', for example std::pair and
  395. * std_pair return the same mangling. This is just a little better
  396. * than before, but it seems to be enough for most of the purposes.
  397. *
  398. * Having a perfect mangling will break some examples and code which
  399. * assume, for example, that A::get_value will be mangled as
  400. * A_get_value.
  401. * ----------------------------------------------------------------------------- */
  402. String *Swig_string_mangle(const String *s) {
  403. #if 0
  404. /* old mangling, not suitable for using in macros */
  405. String *t = Copy(s);
  406. char *c = Char(t);
  407. while (*c) {
  408. if (!isalnum(*c))
  409. *c = '_';
  410. c++;
  411. }
  412. return t;
  413. #else
  414. String *result = NewStringEmpty();
  415. int space = 0;
  416. int state = 0;
  417. char *pc, *cb;
  418. String *b = Copy(s);
  419. if (SwigType_istemplate(b)) {
  420. String *st = Swig_symbol_template_deftype(b, 0);
  421. String *sq = Swig_symbol_type_qualify(st, 0);
  422. String *t = SwigType_namestr(sq);
  423. Delete(st);
  424. Delete(sq);
  425. Delete(b);
  426. b = t;
  427. }
  428. pc = cb = Char(b);
  429. while (*pc) {
  430. char c = *pc;
  431. if (isalnum((int) c) || (c == '_')) {
  432. state = 1;
  433. if (space && (space == state)) {
  434. Append(result, "_SS_");
  435. }
  436. space = 0;
  437. Printf(result, "%c", (int) c);
  438. } else {
  439. if (isspace((int) c)) {
  440. space = state;
  441. ++pc;
  442. continue;
  443. } else {
  444. state = 3;
  445. space = 0;
  446. }
  447. switch (c) {
  448. case '.':
  449. if ((cb != pc) && (*(pc - 1) == 'p')) {
  450. Append(result, "_");
  451. ++pc;
  452. continue;
  453. } else {
  454. c = 'f';
  455. }
  456. break;
  457. case ':':
  458. if (*(pc + 1) == ':') {
  459. Append(result, "_");
  460. ++pc;
  461. ++pc;
  462. continue;
  463. }
  464. break;
  465. case '*':
  466. c = 'm';
  467. break;
  468. case '&':
  469. c = 'A';
  470. break;
  471. case '<':
  472. c = 'l';
  473. break;
  474. case '>':
  475. c = 'g';
  476. break;
  477. case '=':
  478. c = 'e';
  479. break;
  480. case ',':
  481. c = 'c';
  482. break;
  483. case '(':
  484. c = 'p';
  485. break;
  486. case ')':
  487. c = 'P';
  488. break;
  489. case '[':
  490. c = 'b';
  491. break;
  492. case ']':
  493. c = 'B';
  494. break;
  495. case '^':
  496. c = 'x';
  497. break;
  498. case '|':
  499. c = 'o';
  500. break;
  501. case '~':
  502. c = 'n';
  503. break;
  504. case '!':
  505. c = 'N';
  506. break;
  507. case '%':
  508. c = 'M';
  509. break;
  510. case '?':
  511. c = 'q';
  512. break;
  513. case '+':
  514. c = 'a';
  515. break;
  516. case '-':
  517. c = 's';
  518. break;
  519. case '/':
  520. c = 'd';
  521. break;
  522. default:
  523. break;
  524. }
  525. if (isalpha((int) c)) {
  526. Printf(result, "_S%c_", (int) c);
  527. } else {
  528. Printf(result, "_S%02X_", (int) c);
  529. }
  530. }
  531. ++pc;
  532. }
  533. Delete(b);
  534. return result;
  535. #endif
  536. }
  537. String *Swig_string_emangle(String *s) {
  538. return Swig_string_mangle(s);
  539. }
  540. /* -----------------------------------------------------------------------------
  541. * Swig_scopename_prefix()
  542. *
  543. * Take a qualified name like "A::B::C" and return the scope name.
  544. * In this case, "A::B". Returns NULL if there is no base.
  545. * ----------------------------------------------------------------------------- */
  546. void Swig_scopename_split(String *s, String **rprefix, String **rlast) {
  547. char *tmp = Char(s);
  548. char *c = tmp;
  549. char *cc = c;
  550. char *co = 0;
  551. if (!strstr(c, "::")) {
  552. *rprefix = 0;
  553. *rlast = Copy(s);
  554. }
  555. if ((co = strstr(cc, "operator "))) {
  556. if (co == cc) {
  557. *rprefix = 0;
  558. *rlast = Copy(s);
  559. return;
  560. } else {
  561. *rprefix = NewStringWithSize(cc, co - cc - 2);
  562. *rlast = NewString(co);
  563. return;
  564. }
  565. }
  566. while (*c) {
  567. if ((*c == ':') && (*(c + 1) == ':')) {
  568. cc = c;
  569. c += 2;
  570. } else {
  571. if (*c == '<') {
  572. int level = 1;
  573. c++;
  574. while (*c && level) {
  575. if (*c == '<')
  576. level++;
  577. if (*c == '>')
  578. level--;
  579. c++;
  580. }
  581. } else {
  582. c++;
  583. }
  584. }
  585. }
  586. if (cc != tmp) {
  587. *rprefix = NewStringWithSize(tmp, cc - tmp);
  588. *rlast = NewString(cc + 2);
  589. return;
  590. } else {
  591. *rprefix = 0;
  592. *rlast = Copy(s);
  593. }
  594. }
  595. String *Swig_scopename_prefix(String *s) {
  596. char *tmp = Char(s);
  597. char *c = tmp;
  598. char *cc = c;
  599. char *co = 0;
  600. if (!strstr(c, "::"))
  601. return 0;
  602. if ((co = strstr(cc, "operator "))) {
  603. if (co == cc) {
  604. return 0;
  605. } else {
  606. String *prefix = NewStringWithSize(cc, co - cc - 2);
  607. return prefix;
  608. }
  609. }
  610. while (*c) {
  611. if ((*c == ':') && (*(c + 1) == ':')) {
  612. cc = c;
  613. c += 2;
  614. } else {
  615. if (*c == '<') {
  616. int level = 1;
  617. c++;
  618. while (*c && level) {
  619. if (*c == '<')
  620. level++;
  621. if (*c == '>')
  622. level--;
  623. c++;
  624. }
  625. } else {
  626. c++;
  627. }
  628. }
  629. }
  630. if (cc != tmp) {
  631. return NewStringWithSize(tmp, cc - tmp);
  632. } else {
  633. return 0;
  634. }
  635. }
  636. /* -----------------------------------------------------------------------------
  637. * Swig_scopename_last()
  638. *
  639. * Take a qualified name like "A::B::C" and returns the last. In this
  640. * case, "C".
  641. * ----------------------------------------------------------------------------- */
  642. String *Swig_scopename_last(String *s) {
  643. char *tmp = Char(s);
  644. char *c = tmp;
  645. char *cc = c;
  646. char *co = 0;
  647. if (!strstr(c, "::"))
  648. return NewString(s);
  649. if ((co = strstr(cc, "operator "))) {
  650. return NewString(co);
  651. }
  652. while (*c) {
  653. if ((*c == ':') && (*(c + 1) == ':')) {
  654. cc = c;
  655. c += 2;
  656. } else {
  657. if (*c == '<') {
  658. int level = 1;
  659. c++;
  660. while (*c && level) {
  661. if (*c == '<')
  662. level++;
  663. if (*c == '>')
  664. level--;
  665. c++;
  666. }
  667. } else {
  668. c++;
  669. }
  670. }
  671. }
  672. return NewString(cc + 2);
  673. }
  674. /* -----------------------------------------------------------------------------
  675. * Swig_scopename_first()
  676. *
  677. * Take a qualified name like "A::B::C" and returns the first scope name.
  678. * In this case, "A". Returns NULL if there is no base.
  679. * ----------------------------------------------------------------------------- */
  680. String *Swig_scopename_first(String *s) {
  681. char *tmp = Char(s);
  682. char *c = tmp;
  683. char *co = 0;
  684. if (!strstr(c, "::"))
  685. return 0;
  686. if ((co = strstr(c, "operator "))) {
  687. if (co == c) {
  688. return 0;
  689. }
  690. } else {
  691. co = c + Len(s);
  692. }
  693. while (*c && (c != co)) {
  694. if ((*c == ':') && (*(c + 1) == ':')) {
  695. break;
  696. } else {
  697. if (*c == '<') {
  698. int level = 1;
  699. c++;
  700. while (*c && level) {
  701. if (*c == '<')
  702. level++;
  703. if (*c == '>')
  704. level--;
  705. c++;
  706. }
  707. } else {
  708. c++;
  709. }
  710. }
  711. }
  712. if (*c && (c != tmp)) {
  713. return NewStringWithSize(tmp, c - tmp);
  714. } else {
  715. return 0;
  716. }
  717. }
  718. /* -----------------------------------------------------------------------------
  719. * Swig_scopename_suffix()
  720. *
  721. * Take a qualified name like "A::B::C" and returns the suffix.
  722. * In this case, "B::C". Returns NULL if there is no suffix.
  723. * ----------------------------------------------------------------------------- */
  724. String *Swig_scopename_suffix(String *s) {
  725. char *tmp = Char(s);
  726. char *c = tmp;
  727. char *co = 0;
  728. if (!strstr(c, "::"))
  729. return 0;
  730. if ((co = strstr(c, "operator "))) {
  731. if (co == c)
  732. return 0;
  733. }
  734. while (*c) {
  735. if ((*c == ':') && (*(c + 1) == ':')) {
  736. break;
  737. } else {
  738. if (*c == '<') {
  739. int level = 1;
  740. c++;
  741. while (*c && level) {
  742. if (*c == '<')
  743. level++;
  744. if (*c == '>')
  745. level--;
  746. c++;
  747. }
  748. } else {
  749. c++;
  750. }
  751. }
  752. }
  753. if (*c && (c != tmp)) {
  754. return NewString(c + 2);
  755. } else {
  756. return 0;
  757. }
  758. }
  759. /* -----------------------------------------------------------------------------
  760. * Swig_scopename_check()
  761. *
  762. * Checks to see if a name is qualified with a scope name
  763. * ----------------------------------------------------------------------------- */
  764. int Swig_scopename_check(String *s) {
  765. char *c = Char(s);
  766. char *co = 0;
  767. if ((co = strstr(c, "operator "))) {
  768. if (co == c)
  769. return 0;
  770. }
  771. if (!strstr(c, "::"))
  772. return 0;
  773. while (*c) {
  774. if ((*c == ':') && (*(c + 1) == ':')) {
  775. return 1;
  776. } else {
  777. if (*c == '<') {
  778. int level = 1;
  779. c++;
  780. while (*c && level) {
  781. if (*c == '<')
  782. level++;
  783. if (*c == '>')
  784. level--;
  785. c++;
  786. }
  787. } else {
  788. c++;
  789. }
  790. }
  791. }
  792. return 0;
  793. }
  794. /* -----------------------------------------------------------------------------
  795. * Swig_string_command()
  796. *
  797. * Executes a external command via popen with the string as a command
  798. * line parameter. For example:
  799. *
  800. * Printf(stderr,"%(command:sed 's/[a-z]/\U\\1/' <<<)s","hello") -> Hello
  801. * ----------------------------------------------------------------------------- */
  802. #if defined(HAVE_POPEN)
  803. # if defined(_MSC_VER)
  804. # define popen _popen
  805. # define pclose _pclose
  806. # else
  807. extern FILE *popen(const char *command, const char *type);
  808. extern int pclose(FILE *stream);
  809. # endif
  810. #else
  811. # if defined(_MSC_VER)
  812. # define HAVE_POPEN 1
  813. # define popen _popen
  814. # define pclose _pclose
  815. # endif
  816. #endif
  817. String *Swig_string_command(String *s) {
  818. String *res = NewStringEmpty();
  819. #if defined(HAVE_POPEN)
  820. if (Len(s)) {
  821. char *command = Char(s);
  822. FILE *fp = popen(command, "r");
  823. if (fp) {
  824. char buffer[1025];
  825. while (fscanf(fp, "%1024s", buffer) != EOF) {
  826. Append(res, buffer);
  827. }
  828. pclose(fp);
  829. } else {
  830. Swig_error("SWIG", Getline(s), "Command encoder fails attempting '%s'.\n", s);
  831. exit(1);
  832. }
  833. }
  834. #endif
  835. return res;
  836. }
  837. /* -----------------------------------------------------------------------------
  838. * Swig_string_rxspencer()
  839. *
  840. * Executes a regexp substitution via the RxSpencer library. For example:
  841. *
  842. * Printf(stderr,"gsl%(rxspencer:[GSL_.*_][@1])s","GSL_Hello_") -> gslHello
  843. * ----------------------------------------------------------------------------- */
  844. #if defined(HAVE_RXSPENCER)
  845. #include <sys/types.h>
  846. #include <rxspencer/regex.h>
  847. #define USE_RXSPENCER
  848. #endif
  849. const char *skip_delim(char pb, char pe, const char *ce) {
  850. int end = 0;
  851. int lb = 0;
  852. while (!end && *ce != '\0') {
  853. if (*ce == pb) {
  854. ++lb;
  855. }
  856. if (*ce == pe) {
  857. if (!lb) {
  858. end = 1;
  859. --ce;
  860. } else {
  861. --lb;
  862. }
  863. }
  864. ++ce;
  865. }
  866. return end ? ce : 0;
  867. }
  868. #if defined(USE_RXSPENCER)
  869. String *Swig_string_rxspencer(String *s) {
  870. String *res = 0;
  871. if (Len(s)) {
  872. const char *cs = Char(s);
  873. const char *cb;
  874. const char *ce;
  875. if (*cs == '[') {
  876. int retval;
  877. regex_t compiled;
  878. cb = ++cs;
  879. ce = skip_delim('[', ']', cb);
  880. if (ce) {
  881. char bregexp[512];
  882. strncpy(bregexp, cb, ce - cb);
  883. bregexp[ce - cb] = '\0';
  884. ++ce;
  885. retval = regcomp(&compiled, bregexp, REG_EXTENDED);
  886. if (retval == 0) {
  887. cs = ce;
  888. if (*cs == '[') {
  889. cb = ++cs;
  890. ce = skip_delim('[', ']', cb);
  891. if (ce) {
  892. const char *cvalue = ce + 1;
  893. int nsub = (int) compiled.re_nsub + 1;
  894. regmatch_t *pmatch = (regmatch_t *) malloc(sizeof(regmatch_t) * (nsub));
  895. retval = regexec(&compiled, cvalue, nsub, pmatch, 0);
  896. if (retval != REG_NOMATCH) {
  897. char *spos = 0;
  898. res = NewStringWithSize(cb, ce - cb);
  899. spos = Strchr(res, '@');
  900. while (spos) {
  901. char cd = *(++spos);
  902. if (isdigit(cd)) {
  903. char arg[8];
  904. size_t len;
  905. int i = cd - '0';
  906. sprintf(arg, "@%d", i);
  907. if (i < nsub && (len = pmatch[i].rm_eo - pmatch[i].rm_so)) {
  908. char value[256];
  909. strncpy(value, cvalue + pmatch[i].rm_so, len);
  910. value[len] = 0;
  911. Replaceall(res, arg, value);
  912. } else {
  913. Replaceall(res, arg, "");
  914. }
  915. spos = Strchr(res, '@');
  916. } else if (cd == '@') {
  917. spos = strchr(spos + 1, '@');
  918. }
  919. }
  920. }
  921. free(pmatch);
  922. }
  923. }
  924. }
  925. regfree(&compiled);
  926. }
  927. }
  928. }
  929. if (!res)
  930. res = NewStringEmpty();
  931. return res;
  932. }
  933. #else
  934. String *Swig_string_rxspencer(String *s) {
  935. (void) s;
  936. return NewStringEmpty();
  937. }
  938. #endif
  939. /* -----------------------------------------------------------------------------
  940. * Swig_init()
  941. *
  942. * Initialize the SWIG core
  943. * ----------------------------------------------------------------------------- */
  944. void Swig_init() {
  945. /* Set some useful string encoding methods */
  946. DohEncoding("escape", Swig_string_escape);
  947. DohEncoding("upper", Swig_string_upper);
  948. DohEncoding("lower", Swig_string_lower);
  949. DohEncoding("title", Swig_string_title);
  950. DohEncoding("ctitle", Swig_string_ccase);
  951. DohEncoding("lctitle", Swig_string_lccase);
  952. DohEncoding("utitle", Swig_string_ucase);
  953. DohEncoding("typecode", Swig_string_typecode);
  954. DohEncoding("mangle", Swig_string_emangle);
  955. DohEncoding("command", Swig_string_command);
  956. DohEncoding("rxspencer", Swig_string_rxspencer);
  957. DohEncoding("schemify", Swig_string_schemify);
  958. /* aliases for the case encoders */
  959. DohEncoding("uppercase", Swig_string_upper);
  960. DohEncoding("lowercase", Swig_string_lower);
  961. DohEncoding("camelcase", Swig_string_ccase);
  962. DohEncoding("lowercamelcase", Swig_string_lccase);
  963. DohEncoding("undercase", Swig_string_ucase);
  964. DohEncoding("firstuppercase", Swig_string_first_upper);
  965. DohEncoding("firstlowercase", Swig_string_first_lower);
  966. /* Initialize typemaps */
  967. Swig_typemap_init();
  968. /* Initialize symbol table */
  969. Swig_symbol_init();
  970. /* Initialize type system */
  971. SwigType_typesystem_init();
  972. /* Initialize template system */
  973. SwigType_template_init();
  974. }