/src/pipewash/pipe_framework/token.c

https://bitbucket.org/becoggins/nmr_wash · C · 944 lines · 767 code · 121 blank · 56 comment · 77 complexity · 9a9210b5a6ab4f3b1410d6afb5e04e6c MD5 · raw file

  1. /******************************************************************************/
  2. /* */
  3. /* ---- NIH NMR Software System ---- */
  4. /* Copyright 1992 and 1993 */
  5. /* Frank Delaglio */
  6. /* NIH Laboratory of Chemical Physics */
  7. /* */
  8. /* This software is not for distribution without */
  9. /* the written permission of the author. */
  10. /* */
  11. /******************************************************************************/
  12. /***/
  13. /* token: tools for tokenizing blank-delimited tokens.
  14. /* Defined in "token.h", along with global
  15. /* variable "badToken"; badToken will be set
  16. /* to zero after a successful call to any
  17. /* of these procedures; or non-zero otheriwse.
  18. /* Non-zero "badToken" indicates that the
  19. /* returned value is undefined.
  20. /*
  21. /* In the procedures listed, "string" is the character
  22. /* string to be tokenized, "n" is token number to extract,
  23. /* starting at 1.
  24. /*
  25. /* The following extract the Nth token from string as
  26. /* a string, float, and integer, respectively. The string versions
  27. /* store their return value in a static space which will be over-written
  28. /* by other calls to these token utilities.
  29. /*
  30. /* char *getToken( n, string )
  31. /* char *getTokenS( n, string ) (for quoted strings)
  32. /* float getTokenR( n, string )
  33. /* int getTokenI( n, string )
  34. /*
  35. /* The following also extract the Nth token
  36. /*
  37. /* The following extract the leftmost and rightmost tokens
  38. /* in string, compressing multiple spaces, and starting from
  39. /* the Nth token:
  40. /*
  41. /* char *leftToken( n, string )
  42. /* char *rightToken( n, string )
  43. /*
  44. /* The following returns the number of tokens in string:
  45. /*
  46. /* int cntToken( string )
  47. /* int cntTokenS( string ) (for quoted strings).
  48. /*
  49. /* NOTA BENE: char * token functions above will return
  50. /* pointers to character arrays that will be
  51. /* overwritten in successive calls; return
  52. /* strings should be copied as needed.
  53. /***/
  54. /***/
  55. /* For experimentation, compile this stand-alone test:
  56. /***/
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <ctype.h>
  60. #include "stralloc.h"
  61. #include "memory.h"
  62. #include "minmax.h"
  63. #include "token.h"
  64. #include "prec.h"
  65. static char *lValue = (char *)NULL;
  66. static char *rValue = (char *)NULL;
  67. static char *tValue = (char *)NULL;
  68. static char *cValue = (char *)NULL;
  69. static char lBuff[NAMELEN+1];
  70. static char rBuff[NAMELEN+1];
  71. static char tBuff[NAMELEN+1];
  72. static char cBuff[NAMELEN+1];
  73. static int lBuffLen = 0, rBuffLen = 0, tBuffLen = 0, cBuffLen = 0;
  74. #define FPR (void)fprintf
  75. #define PR (void)printf
  76. #ifdef STANDALONE
  77. int main( argc, argv )
  78. int argc;
  79. char **argv;
  80. {
  81. char string[NAMELEN+1], *token;
  82. int i, loc1, locN, count, n, ival;
  83. float rval;
  84. loc1 = -1;
  85. locN = -1;
  86. PR( "Line: " );
  87. (void) gets( string );
  88. PR( "Token: " );
  89. (void) scanf( "%d", &n );
  90. PR( "Your line: %s\n", string );
  91. count = strlen( string );
  92. PR( "Locations: ", string );
  93. for( i = 0; i < count; i++ ) PR( "%d", i % 10 );
  94. PR( "\n" );
  95. count = cntToken( string );
  96. PR( "The line has %d regular token(s).\n", count );
  97. count = cntTokenS( string );
  98. PR( "The line has %d quoted token(s).\n", count );
  99. token = getToken( n, string );
  100. PR( "Token(%d) Status(%d): %s\n", n, badToken, token );
  101. token = getTokenS( n, string );
  102. PR( "Quoted Token(%d) Status(%d): %s\n", n, badToken, token );
  103. n = getTokenLoc( n, string, &loc1, &locN );
  104. PR( "Quoted Token(%d) Status(%d) Loc %d to %d\n", n, badToken, loc1, locN );
  105. ival = getTokenI( n, string );
  106. PR( "TokenI(%d) Status(%d): %d\n", n, badToken, ival );
  107. rval = getTokenR( n, string );
  108. PR( "TokenR(%d) Status(%d): %f\n", n, badToken, rval );
  109. token = leftToken( n, string );
  110. PR( "LeftToken(%d) Status(%d): %s\n", n, badToken, token );
  111. token = rightToken( n, string );
  112. PR( "RightToken(%d) Status(%d): %s\n", n, badToken, token );
  113. return( 0 );
  114. }
  115. #endif
  116. int cntToken( string )
  117. char *string;
  118. {
  119. int wasSpace, count;
  120. count = 0; wasSpace = 1; badToken = 0;
  121. if (!string) return( 0 );
  122. while( *string )
  123. {
  124. if (wasSpace)
  125. {
  126. if (!isspace( (int) *string ))
  127. {
  128. count++;
  129. wasSpace = 0;
  130. }
  131. }
  132. else
  133. {
  134. if (isspace( (int) *string ))
  135. {
  136. wasSpace = 1;
  137. }
  138. }
  139. string++;
  140. }
  141. return( count );
  142. }
  143. int cntTokenS( string )
  144. char *string;
  145. {
  146. int wasSpace, count;
  147. char qc;
  148. count = 0; wasSpace = 1; badToken = 0;
  149. if (!string) return( 0 );
  150. while( *string )
  151. {
  152. if (wasSpace)
  153. {
  154. if (!isspace( (int) *string ))
  155. {
  156. count++;
  157. wasSpace = 0;
  158. if (*string == '"' || *string == '\'')
  159. {
  160. qc = *string++;
  161. while( *string && *string != qc) string++;
  162. }
  163. }
  164. }
  165. else
  166. {
  167. if (isspace( (int) *string ))
  168. {
  169. wasSpace = 1;
  170. }
  171. }
  172. string++;
  173. }
  174. return( count );
  175. }
  176. int cntTokenC( string, tokChars )
  177. char *string, *tokChars;
  178. {
  179. int wasSpace, count;
  180. char qc;
  181. count = 0; wasSpace = 1; badToken = 0;
  182. if (!string) return( 0 );
  183. while( *string )
  184. {
  185. if (wasSpace)
  186. {
  187. if (!strchr( tokChars, (int) *string ))
  188. {
  189. count++;
  190. wasSpace = 0;
  191. if (*string == '"' || *string == '\'')
  192. {
  193. qc = *string++;
  194. while( *string && *string != qc) string++;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. if (strchr( tokChars, (int) *string ))
  201. {
  202. wasSpace = 1;
  203. }
  204. }
  205. string++;
  206. }
  207. return( count );
  208. }
  209. float getTokenR( n, string )
  210. int n;
  211. char *string;
  212. {
  213. float value;
  214. value = 0.0;
  215. badToken = 0;
  216. if (!string)
  217. {
  218. badToken = 1;
  219. return( value );
  220. }
  221. if (!sscanf( getToken( n, string ), "%f", &value ))
  222. {
  223. badToken = 1;
  224. }
  225. return( value );
  226. }
  227. int getTokenI( n, string )
  228. char *string;
  229. int n;
  230. {
  231. int value;
  232. value = 0;
  233. badToken = 0;
  234. if (!string)
  235. {
  236. badToken = 1;
  237. return( value );
  238. }
  239. if (!sscanf( getToken( n, string ), "%d", &value ))
  240. {
  241. badToken = 1;
  242. }
  243. return( value );
  244. }
  245. char *leftToken( n, string )
  246. int n;
  247. char *string;
  248. {
  249. int oldN, i, count;
  250. badToken = 0;
  251. if (!lValue)
  252. {
  253. lBuffLen = NAMELEN + 1;
  254. lValue = lBuff;
  255. lBuff[0] = '\0';
  256. }
  257. lValue[0] = '\0';
  258. if (n < 1)
  259. {
  260. return( lValue );
  261. }
  262. if (!string)
  263. {
  264. badToken = 1;
  265. return( lValue );
  266. }
  267. count = cntToken( string );
  268. oldN = n;
  269. n = n > count ? count : n;
  270. n = n < 1 ? 1 : n;
  271. for( i = 1; i <= n; i++ )
  272. {
  273. (void) strcatAlloc( &lValue,
  274. getToken( i, string ),
  275. lBuff, NAMELEN+1,
  276. &lBuffLen );
  277. if (i != n)
  278. {
  279. (void) strcatAlloc( &lValue, " ", lBuff, NAMELEN+1, &lBuffLen );
  280. }
  281. }
  282. if (oldN > count) badToken = 1;
  283. return( lValue );
  284. }
  285. char *rightToken( n, string )
  286. char *string;
  287. int n;
  288. {
  289. int i, oldN, count;
  290. badToken = 0;
  291. if (!rValue)
  292. {
  293. rBuffLen = NAMELEN + 1;
  294. rValue = rBuff;
  295. rBuff[0] = '\0';
  296. }
  297. rValue[0] = '\0';
  298. if (!string)
  299. {
  300. badToken = 1;
  301. return( rValue );
  302. }
  303. count = cntToken( string );
  304. oldN = n;
  305. if (n > count)
  306. {
  307. return( rValue );
  308. }
  309. n = n > count ? count : n;
  310. n = n < 1 ? 1 : n;
  311. for( i = n; i <= count; i++ )
  312. {
  313. (void) strcatAlloc( &rValue,
  314. getToken( i, string ),
  315. rBuff, NAMELEN+1,
  316. &rBuffLen );
  317. if (i != count)
  318. {
  319. (void) strcatAlloc( &rValue, " ", rBuff, NAMELEN+1, &rBuffLen );
  320. }
  321. }
  322. if (oldN > count) badToken = 1;
  323. return( rValue );
  324. }
  325. char *getToken( n, string )
  326. int n;
  327. char *string;
  328. {
  329. int i, count, skip, nn;
  330. char *sPtr;
  331. if (!tValue)
  332. {
  333. tBuffLen = NAMELEN + 1;
  334. tValue = tBuff;
  335. }
  336. badToken = 0;
  337. tValue[0] = '\0';
  338. if (!string)
  339. {
  340. badToken = 1;
  341. return( tValue );
  342. }
  343. count = cntToken( string );
  344. if ((n < 1) || (n > count))
  345. {
  346. badToken = 1;
  347. return( tValue );
  348. }
  349. nn = strlen( string ) + 1;
  350. if (nn > tBuffLen)
  351. {
  352. if (tValue != tBuff)
  353. {
  354. (void) deAlloc( "token", tValue, sizeof(char)*tBuffLen );
  355. }
  356. tBuffLen = NAMELEN + 1;
  357. tValue = tBuff;
  358. if (sPtr = charAlloc( "token", nn ))
  359. {
  360. tBuffLen = nn;
  361. tValue = sPtr;
  362. }
  363. else
  364. {
  365. FPR( stderr, "GetToken: Memory Allocation Failure.\n" );
  366. }
  367. tValue[0] = '\0';
  368. }
  369. sPtr = string;
  370. for( i = 0; i < n; i++ )
  371. {
  372. (void) sscanf( sPtr, "%s%n", tValue, &skip );
  373. sPtr += skip;
  374. }
  375. return( tValue );
  376. }
  377. int getTokenLoc( n, strOrig, loc1, locN )
  378. char *strOrig;
  379. int n, *loc1, *locN;
  380. {
  381. int loc, wasSpace, count, nn;
  382. char *string, *sPtr, qc;
  383. badToken = 0;
  384. count = 0;
  385. wasSpace = 1;
  386. *loc1 = -1;
  387. *locN = -1;
  388. if (!strOrig)
  389. {
  390. badToken = 1;
  391. return( 0 );
  392. }
  393. string = strOrig;
  394. while( *string )
  395. {
  396. if (wasSpace)
  397. {
  398. if (!isspace( (int)*string ))
  399. {
  400. count++;
  401. wasSpace = 0;
  402. if (*string == '"' || *string == '\'')
  403. {
  404. qc = *string++;
  405. if (n == count)
  406. {
  407. while( *string && *string != qc)
  408. {
  409. loc = string - strOrig;
  410. if (*loc1 < 0) *loc1 = loc;
  411. *locN = loc;
  412. string++;
  413. }
  414. return( 1 + *locN - *loc1 );
  415. }
  416. else
  417. {
  418. while( *string && *string != qc) string++;
  419. }
  420. }
  421. else
  422. {
  423. if (n == count)
  424. {
  425. while( *string && !isspace((int)*string) )
  426. {
  427. loc = string - strOrig;
  428. if (*loc1 < 0) *loc1 = loc;
  429. *locN = loc;
  430. string++;
  431. }
  432. return( 1 + *locN - *loc1 );
  433. }
  434. }
  435. }
  436. }
  437. else
  438. {
  439. if (isspace( (int) *string ))
  440. {
  441. wasSpace = 1;
  442. }
  443. }
  444. string++;
  445. }
  446. badToken = 1;
  447. return( 0 );
  448. }
  449. char *getTokenS( n, string )
  450. char *string;
  451. int n;
  452. {
  453. int wasSpace, count, nn;
  454. char *sPtr, qc;
  455. if (!tValue)
  456. {
  457. tBuffLen = NAMELEN + 1;
  458. tValue = tBuff;
  459. }
  460. badToken = 0;
  461. count = 0;
  462. wasSpace = 1;
  463. tValue[0] = '\0';
  464. if (!string)
  465. {
  466. badToken = 1;
  467. return( tValue );
  468. }
  469. nn = strlen( string ) + 1;
  470. if (nn > tBuffLen)
  471. {
  472. if (tValue != tBuff)
  473. {
  474. (void) deAlloc( "token", tValue, sizeof(char)*tBuffLen );
  475. }
  476. tBuffLen = NAMELEN + 1;
  477. tValue = tBuff;
  478. if (sPtr = charAlloc( "token", nn ))
  479. {
  480. tBuffLen = nn;
  481. tValue = sPtr;
  482. }
  483. else
  484. {
  485. FPR( stderr, "GetToken: Memory Allocation Failure.\n" );
  486. }
  487. tValue[0] = '\0';
  488. }
  489. sPtr = tValue;
  490. while( *string )
  491. {
  492. if (wasSpace)
  493. {
  494. if (!isspace( (int)*string ))
  495. {
  496. count++;
  497. wasSpace = 0;
  498. if (*string == '"' || *string == '\'')
  499. {
  500. qc = *string++;
  501. if (n == count)
  502. {
  503. while( *string && *string != qc)
  504. {
  505. *sPtr++ = *string++;
  506. }
  507. *sPtr++ = '\0';
  508. return( tValue );
  509. }
  510. else
  511. {
  512. while( *string && *string != qc) string++;
  513. }
  514. }
  515. else
  516. {
  517. if (n == count)
  518. {
  519. while( *string && !isspace((int)*string) )
  520. {
  521. *sPtr++ = *string++;
  522. }
  523. *sPtr++ = '\0';
  524. return( tValue );
  525. }
  526. }
  527. }
  528. }
  529. else
  530. {
  531. if (isspace( (int) *string ))
  532. {
  533. wasSpace = 1;
  534. }
  535. }
  536. string++;
  537. }
  538. badToken = 1;
  539. return( tValue );
  540. }
  541. char *getTokenC( n, string, tokChars )
  542. char *string, *tokChars;
  543. int n;
  544. {
  545. int wasSpace, count, nn;
  546. char *sPtr, qc;
  547. if (!cValue)
  548. {
  549. cBuffLen = NAMELEN + 1;
  550. cValue = cBuff;
  551. }
  552. badToken = 0;
  553. count = 0;
  554. wasSpace = 1;
  555. cValue[0] = '\0';
  556. if (!string)
  557. {
  558. badToken = 1;
  559. return( tValue );
  560. }
  561. nn = strlen( string ) + 1;
  562. if (nn > cBuffLen)
  563. {
  564. if (cValue != cBuff)
  565. {
  566. (void) deAlloc( "token", cValue, sizeof(char)*cBuffLen );
  567. }
  568. cBuffLen = NAMELEN + 1;
  569. cValue = cBuff;
  570. if (sPtr = charAlloc( "token", nn ))
  571. {
  572. cBuffLen = nn;
  573. cValue = sPtr;
  574. }
  575. else
  576. {
  577. FPR( stderr, "GetToken: Memory Allocation Failure.\n" );
  578. }
  579. cValue[0] = '\0';
  580. }
  581. sPtr = cValue;
  582. while( *string )
  583. {
  584. if (wasSpace)
  585. {
  586. if (!strchr( tokChars, (int) *string ))
  587. {
  588. count++;
  589. wasSpace = 0;
  590. if (*string == '"' || *string == '\'')
  591. {
  592. qc = *string++;
  593. if (n == count)
  594. {
  595. while( *string && *string != qc)
  596. {
  597. *sPtr++ = *string++;
  598. }
  599. *sPtr++ = '\0';
  600. return( cValue );
  601. }
  602. else
  603. {
  604. while( *string && *string != qc) string++;
  605. }
  606. }
  607. else
  608. {
  609. if (n == count)
  610. {
  611. while( *string && !strchr( tokChars, (int) *string ))
  612. {
  613. *sPtr++ = *string++;
  614. }
  615. *sPtr++ = '\0';
  616. return( cValue );
  617. }
  618. }
  619. }
  620. }
  621. else
  622. {
  623. if (strchr( tokChars, (int) *string ))
  624. {
  625. wasSpace = 1;
  626. }
  627. }
  628. string++;
  629. }
  630. badToken = 1;
  631. return( cValue );
  632. }
  633. int strTrimLoc( s, loc1, locN )
  634. char *s;
  635. int *loc1, *locN;
  636. {
  637. int i, n;
  638. char *sPtr;
  639. *loc1 = -1;
  640. *locN = -1;
  641. if (!s) return( 0 );
  642. n = strlen( s );
  643. if (!n) return( 0 );
  644. *loc1 = 0;
  645. *locN = n - 1;
  646. for( sPtr = s; *sPtr; sPtr++ )
  647. {
  648. if (!isspace( *sPtr )) break;
  649. (*loc1)++;
  650. }
  651. for( sPtr = s + n - 1; *sPtr; sPtr-- )
  652. {
  653. if (!isspace( *sPtr )) break;
  654. (*locN)--;
  655. }
  656. return( 1 + *locN - *loc1 );
  657. }
  658. int isInteger( s )
  659. char *s;
  660. {
  661. int i, loc1, locN, nC, nD;
  662. if (!s) return( 0 );
  663. loc1 = -1;
  664. locN = -1;
  665. nC = strTrimLoc( s, &loc1, &locN );
  666. if (nC < 1) return( 0 );
  667. nD = 0;
  668. for( i = loc1; i <= locN; i++ )
  669. {
  670. if (s[i] == '-' || s[i] == '+')
  671. {
  672. if (i > loc1) return( 0 );
  673. }
  674. else if (s[i] >= '0' && s[i] <= '9')
  675. {
  676. nD++;
  677. }
  678. else
  679. {
  680. return( 0 );
  681. }
  682. }
  683. if (nD) return( 1 );
  684. return( 0 );
  685. }
  686. int isFloat( s )
  687. char *s;
  688. {
  689. int i, loc1, locN, locE, nC, nD, nE, nS, nP, nX;
  690. if (!s) return( 0 );
  691. loc1 = -1;
  692. locN = -1;
  693. locE = -1;
  694. nC = strTrimLoc( s, &loc1, &locN );
  695. if (nC < 1) return( 0 );
  696. nD = 0;
  697. nE = 0;
  698. nS = 0;
  699. nP = 0;
  700. nX = 0;
  701. for( i = loc1; i <= locN; i++ )
  702. {
  703. if (s[i] == 'e' || s[i] == 'E')
  704. {
  705. if (nE || !nD) return( 0 );
  706. locE = i;
  707. nE++;
  708. }
  709. else if (s[i] == '-' || s[i] == '+')
  710. {
  711. if (nS > 1) return( 0 );
  712. nS++;
  713. if (i == loc1) continue;
  714. if (!nE) return( 0 );
  715. if (i != locE + 1) return( 0 );
  716. }
  717. else if (s[i] == '.')
  718. {
  719. if (nP || nE) return( 0 );
  720. nP++;
  721. }
  722. else if (s[i] >= '0' && s[i] <= '9')
  723. {
  724. if (nE)
  725. nX++;
  726. else
  727. nD++;
  728. }
  729. else
  730. {
  731. return( 0 );
  732. }
  733. }
  734. if (nD < 1) return( 0 );
  735. if (nE && !nX) return( 0 );
  736. return( 1 );
  737. }
  738. int freeToken()
  739. {
  740. (void) strAllocFree( &lValue, lBuff, NAMELEN+1, &lBuffLen );
  741. (void) strAllocFree( &rValue, rBuff, NAMELEN+1, &rBuffLen );
  742. (void) strAllocFree( &tValue, tBuff, NAMELEN+1, &tBuffLen );
  743. lValue = (char *)NULL; lBuffLen = 0;
  744. rValue = (char *)NULL; rBuffLen = 0;
  745. tValue = (char *)NULL; tBuffLen = 0;
  746. return( 0 );
  747. }
  748. /***/
  749. /* Bottom.
  750. /***/