token.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*!
  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(1, "%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;
  68. char **tokens;
  69. char *p;
  70. /* do not modify buf, make a copy */
  71. p = G_store(buf);
  72. i = 0;
  73. tokens = (char **)G_malloc(2 * sizeof(char *));
  74. /* always one token */
  75. tokens[i++] = p;
  76. while (TRUE) {
  77. /* find next delimiter */
  78. while (!strchr(delim, *p)) {
  79. /* opening border ? */
  80. if (inchar && *p == *inchar) {
  81. p++;
  82. /* find closing border */
  83. while (*p != *inchar && *p != 0)
  84. p++;
  85. if (*p == 0)
  86. break;
  87. }
  88. p++;
  89. }
  90. if (*p == 0)
  91. break;
  92. /* replace delim with '\0' */
  93. *p++ = 0;
  94. /* set next token */
  95. tokens[i++] = p;
  96. tokens = (char **)G_realloc((char *)tokens, (i + 1) * sizeof(char *));
  97. }
  98. tokens[i] = NULL;
  99. return tokens;
  100. }
  101. /*!
  102. \brief Return number of tokens
  103. \param tokens
  104. \return number of tokens
  105. */
  106. int G_number_of_tokens(char **tokens)
  107. {
  108. int n;
  109. n = 0;
  110. for (n = 0; tokens[n] != NULL; n++)
  111. ;
  112. return n;
  113. }
  114. /*!
  115. \brief Free memory allocated to tokens.
  116. <b>Note:</b> <i>G_free_tokens()</i> must be called when finished with
  117. tokens to release memory.
  118. \param[out] tokens
  119. */
  120. void G_free_tokens(char **tokens)
  121. {
  122. if (tokens[0] != NULL)
  123. G_free(tokens[0]);
  124. G_free(tokens);
  125. }