token.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. \file lib/gis/token.c
  3. \brief GIS Library - Tokenize strings
  4. (C) 2001-2008, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author USA CERL and others
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <grass/gis.h>
  12. static char **tokenize(const char *, const char *, const char *);
  13. /*!
  14. \brief Tokenize string
  15. Given a string, <em>buf</em>, turn delimiter, <em>delim</em>, into
  16. '\0' (NULL) and place pointers to tokens in tokens. <em>buf</em>
  17. must not contain a new line (\n). <em>delim</em> may consist of more
  18. than one character. G_free_tokens() must be called when finished
  19. with tokens to release memory.
  20. Example:
  21. \code
  22. char **tokens;
  23. int ntok, i;
  24. tokens = G_tokenize(buf, " |:,");
  25. ntok = G_number_of_tokens(tokens);
  26. for (i=0; i < ntok; i++) {
  27. G_debug(0, "%d=[%s]", i, tokens[i]);
  28. }
  29. G_free_tokens(tokens);
  30. \endcode
  31. \param buf input string
  32. \param delim string delimiter
  33. \return pointer to string token
  34. */
  35. char **G_tokenize(const char *buf, const char *delim)
  36. {
  37. return tokenize(buf, delim, NULL);
  38. }
  39. /*!
  40. \brief Tokenize string
  41. This fuction behaves similarly to G_tokenize().
  42. It introduces <em>valchar</em> which defines borders of token. Within
  43. token <em>delim</em> is ignored.
  44. Example:
  45. \code
  46. char *str = "a,'b,c',d";
  47. char **tokens1, **tokens2;
  48. int ntok1, ntok2;
  49. tokens1 = G_tokenize(str, ",");
  50. ntok1 = G_number_of_tokens(tokens1);
  51. tokens1 = G_tokenize2(str, ",", "'");
  52. ntok2 = G_number_of_tokens(tokens2);
  53. \endcode
  54. In this example <em>ntok1</em> will be 4, <em>ntok2</em> only 3,
  55. i.e. { "a", "'b, c'", "d"}
  56. \param buf input string
  57. \param delim string delimiter
  58. \param valchar character defining border of token
  59. \return pointer to string token
  60. */
  61. char **G_tokenize2(const char *buf, const char *delim, const char *valchar)
  62. {
  63. return tokenize(buf, delim, valchar);
  64. }
  65. char **tokenize(const char *buf, const char *delim, const char *inchar)
  66. {
  67. int i, invalue;
  68. char **tokens;
  69. char *p;
  70. /* needed for G_free () */
  71. while (!strchr(delim, *buf) && (*buf == ' ' || *buf == '\t'))
  72. buf++;
  73. p = G_store(buf);
  74. tokens = (char **)G_malloc(sizeof(char *));
  75. i = 0;
  76. invalue = FALSE;
  77. while (TRUE) {
  78. while (!(strchr(delim, *p) && !invalue) && (*p == ' ' || *p == '\t')) {
  79. if (inchar && *p == *inchar)
  80. invalue = invalue ? FALSE : TRUE;
  81. p++;
  82. }
  83. if (*p == 0)
  84. break;
  85. tokens[i++] = p;
  86. tokens = (char **)G_realloc((char *)tokens, (i + 1) * sizeof(char *));
  87. while (*p && !(strchr(delim, *p) && !invalue)) {
  88. if (inchar && *p == *inchar)
  89. invalue = invalue ? FALSE : TRUE;
  90. p++;
  91. }
  92. if (*p == 0)
  93. break;
  94. *p++ = 0;
  95. }
  96. tokens[i] = NULL;
  97. return tokens;
  98. }
  99. /*!
  100. \brief Return number of tokens
  101. \param tokens
  102. \return number of tokens
  103. */
  104. int G_number_of_tokens(char **tokens)
  105. {
  106. int n;
  107. n = 0;
  108. for (n = 0; tokens[n] != NULL; n++)
  109. ;
  110. return n;
  111. }
  112. /*!
  113. \brief Free memory allocated to tokens.
  114. <b>Note:</b> <i>G_free_tokens()</i> must be called when finished with
  115. tokens to release memory.
  116. \param[out] tokens
  117. */
  118. void G_free_tokens(char **tokens)
  119. {
  120. if (tokens[0] != NULL)
  121. G_free(tokens[0]);
  122. G_free(tokens);
  123. }