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

/tags/rel-1-3-28/SWIG/Source/Swig/misc.c

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