PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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