token.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*!
  2. \file lib/gis/token.c
  3. \brief GIS Library - Tokenize strings
  4. (C) 2001-2008, 2011-2013 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. static char **tokenize_strtok(const char *, const char *);
  14. /*!
  15. \brief Tokenize string
  16. Given a string, <em>buf</em>, turn delimiter, <em>delim</em>, into
  17. '\0' (NULL) and place pointers to tokens in tokens. <em>buf</em>
  18. must not contain a new line (\n). <em>delim</em> may consist of more
  19. than one character. G_free_tokens() must be called when finished
  20. with tokens to release memory.
  21. Example:
  22. \code
  23. char **tokens;
  24. int ntok, i;
  25. tokens = G_tokenize(buf, " |:,");
  26. ntok = G_number_of_tokens(tokens);
  27. for (i=0; i < ntok; i++) {
  28. G_debug(1, "%d=[%s]", i, tokens[i]);
  29. }
  30. G_free_tokens(tokens);
  31. \endcode
  32. \param buf input string
  33. \param delim string delimiter
  34. \return pointer to string token
  35. */
  36. char **G_tokenize(const char *buf, const char *delim)
  37. {
  38. return tokenize_strtok(buf, delim);
  39. }
  40. /*!
  41. \brief Tokenize string
  42. This fuction behaves similarly to G_tokenize().
  43. It introduces <em>valchar</em> which defines borders of token. Within
  44. token <em>delim</em> is ignored.
  45. Example:
  46. \code
  47. char *str = "a,'b,c',d";
  48. char **tokens1, **tokens2;
  49. int ntok1, ntok2;
  50. tokens1 = G_tokenize(str, ",");
  51. ntok1 = G_number_of_tokens(tokens1);
  52. tokens1 = G_tokenize2(str, ",", "'");
  53. ntok2 = G_number_of_tokens(tokens2);
  54. \endcode
  55. In this example <em>ntok1</em> will be 4, <em>ntok2</em> only 3,
  56. i.e. { "a", "'b, c'", "d"}
  57. \param buf input string
  58. \param delim string delimiter
  59. \param valchar character defining border of token
  60. \return pointer to string token
  61. */
  62. char **G_tokenize2(const char *buf, const char *delim, const char *valchar)
  63. {
  64. return tokenize(buf, delim, valchar);
  65. }
  66. /* strtok-based version of tokenize subroutine */
  67. char **tokenize_strtok(const char *buf, const char *delim)
  68. {
  69. int i;
  70. char **tokens;
  71. char *p, *ptr;
  72. p = G_store(buf);
  73. i = 0;
  74. tokens = (char **) G_malloc (sizeof (char *));
  75. ptr = strtok(p, delim);
  76. tokens[i] = ptr;
  77. i++;
  78. while(ptr != NULL) {
  79. tokens = (char **) G_realloc(tokens, sizeof(char*) * (i + 1));
  80. ptr = strtok(NULL, delim);
  81. tokens[i] = ptr;
  82. i++;
  83. }
  84. return tokens;
  85. }
  86. /* own version of tokenize subroutine */
  87. char **tokenize(const char *buf, const char *delim, const char *inchar)
  88. {
  89. int i, invalue;
  90. char **tokens;
  91. char *p;
  92. /* needed for G_free () */
  93. while (!strchr(delim, *buf) && (*buf == ' ' || *buf == '\t'))
  94. buf++;
  95. p = G_store(buf);
  96. tokens = (char **)G_malloc(sizeof(char *));
  97. i = 0;
  98. invalue = FALSE;
  99. while (TRUE) {
  100. while (!(strchr(delim, *p) && !invalue) && (*p == ' ' || *p == '\t')) {
  101. if (inchar && *p == *inchar)
  102. invalue = invalue ? FALSE : TRUE;
  103. p++;
  104. }
  105. if (*p == 0)
  106. break;
  107. tokens[i++] = p;
  108. tokens = (char **)G_realloc((char *)tokens, (i + 1) * sizeof(char *));
  109. while (*p && !(strchr(delim, *p) && !invalue)) {
  110. if (inchar && *p == *inchar)
  111. invalue = invalue ? FALSE : TRUE;
  112. p++;
  113. }
  114. if (*p == 0)
  115. break;
  116. *p++ = 0;
  117. }
  118. tokens[i] = NULL;
  119. return tokens;
  120. }
  121. /*!
  122. \brief Return number of tokens
  123. \param tokens
  124. \return number of tokens
  125. */
  126. int G_number_of_tokens(char **tokens)
  127. {
  128. int n;
  129. n = 0;
  130. for (n = 0; tokens[n] != NULL; n++)
  131. ;
  132. return n;
  133. }
  134. /*!
  135. \brief Free memory allocated to tokens.
  136. <b>Note:</b> <i>G_free_tokens()</i> must be called when finished with
  137. tokens to release memory.
  138. \param[out] tokens
  139. */
  140. void G_free_tokens(char **tokens)
  141. {
  142. if (tokens[0] != NULL)
  143. G_free(tokens[0]);
  144. G_free(tokens);
  145. }