PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/gsoc2008-jezabek/Source/Swig/misc.c

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