Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 1999-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception, you may use this file as part of a free software |
| 19 | // library without restriction. Specifically, if other files instantiate |
| 20 | // templates or use macros or inline functions from this file, or you compile |
| 21 | // this file and link it with other files to produce an executable, this |
| 22 | // file does not by itself cause the resulting executable to be covered by |
| 23 | // the GNU General Public License. This exception does not however |
| 24 | // invalidate any other reasons why the executable file might be covered by |
| 25 | // the GNU General Public License. |
| 26 | // |
| 27 | // This exception applies only to the code released under the name GNU |
| 28 | // Common C++. If you copy code from other releases into a copy of GNU |
| 29 | // Common C++, as the General Public License permits, the exception does |
| 30 | // not apply to the code that you add in this way. To avoid misleading |
| 31 | // anyone as to the status of such modified files, you must delete |
| 32 | // this exception notice from them. |
| 33 | // |
| 34 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 35 | // whether to permit this exception to apply to your modifications. |
| 36 | // If you do not wish that, delete this exception notice. |
| 37 | // |
| 38 | |
| 39 | #include <cc++/config.h> |
| 40 | #include <cc++/string.h> |
| 41 | #include <cc++/exception.h> |
| 42 | #include <cc++/thread.h> |
| 43 | #include <cc++/export.h> |
| 44 | #include <cc++/tokenizer.h> |
| 45 | #include "private.h" |
| 46 | #include <cstdlib> |
| 47 | #include <cstdio> |
| 48 | |
| 49 | #ifdef CCXX_NAMESPACES |
| 50 | namespace ost { |
| 51 | using namespace std; |
| 52 | #endif |
| 53 | |
| 54 | // sorted by the usual probability of occurence |
| 55 | // see also: manpage of isspace() |
| 56 | const char * const StringTokenizer::SPACE=" \t\n\r\f\v"; |
| 57 | |
| 58 | StringTokenizer::StringTokenizer (const char *_str, const char *_delim, bool _skipAll, bool _trim) : |
| 59 | str(_str),delim(_delim),skipAll(_skipAll),trim(_trim) |
| 60 | { |
| 61 | if (str == 0) |
| 62 | itEnd = iterator(*this, 0); |
| 63 | else |
| 64 | itEnd = iterator(*this,strchr(str, '\0')+1); |
| 65 | } |
| 66 | |
| 67 | StringTokenizer::StringTokenizer (const char *s) : |
| 68 | str(s), delim(SPACE), skipAll(false),trim(true) |
| 69 | { |
| 70 | if (str == 0) |
| 71 | itEnd = iterator(*this, 0); |
| 72 | else |
| 73 | itEnd = iterator(*this,strchr(str, '\0')+1); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | StringTokenizer::iterator& StringTokenizer::iterator::operator ++ () THROWS (StringTokenizer::NoSuchElementException) |
| 78 | { |
| 79 | |
| 80 | // someone requested to read beyond the end .. tsts |
| 81 | if (endp == myTok->itEnd.endp) |
| 82 | THROW (NoSuchElementException()); |
| 83 | |
| 84 | if (token) { |
| 85 | // this is to help people find their bugs, if they |
| 86 | // still maintain a pointer to this invalidated |
| 87 | // area :-) |
| 88 | *token = '\0'; |
| 89 | delete[] token; |
| 90 | token = 0; |
| 91 | } |
| 92 | |
| 93 | start = ++endp; |
| 94 | if (endp == myTok->itEnd.endp) return *this; // done |
| 95 | |
| 96 | // search for next delimiter |
| 97 | while (*endp && strchr(myTok->delim, *endp)==NULL) |
| 98 | ++endp; |
| 99 | |
| 100 | tokEnd = endp; |
| 101 | |
| 102 | if (*endp && myTok->skipAll) { // skip all delimiters |
| 103 | while (*(endp+1) && strchr(myTok->delim, *(endp+1))) |
| 104 | ++endp; |
| 105 | } |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * if no one requests the token, no time is spent skipping the whitespaces |
| 111 | * or allocating memory. |
| 112 | */ |
| 113 | const char * StringTokenizer::iterator::operator * () THROWS (StringTokenizer::NoSuchElementException) |
| 114 | { |
| 115 | // someone requested to read beyond the end .. tsts |
| 116 | if (endp == myTok->itEnd.endp) |
| 117 | THROW (NoSuchElementException()); |
| 118 | |
| 119 | if (!token) { |
| 120 | /* |
| 121 | * someone requests this token; return a copy to provide |
| 122 | * a NULL terminated string. |
| 123 | */ |
| 124 | |
| 125 | /* don't clobber tokEnd, it is used in nextDelimiter() */ |
| 126 | const char *wsTokEnd = tokEnd; |
| 127 | if (myTok->trim) { |
| 128 | while (wsTokEnd > start && strchr(SPACE, *start)) |
| 129 | ++start; |
| 130 | while (wsTokEnd > start && strchr(SPACE,*(wsTokEnd-1))) |
| 131 | --wsTokEnd; |
| 132 | } |
| 133 | size_t tokLen = wsTokEnd - start; |
| 134 | if (start > wsTokEnd) { |
| 135 | tokLen = 0; |
| 136 | } |
| 137 | token = newString(start, tokLen + 1); |
| 138 | } |
| 139 | return token; |
| 140 | } |
| 141 | |
| 142 | #ifdef CCXX_NAMESPACES |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | /** EMACS ** |
| 147 | * Local variables: |
| 148 | * mode: c++ |
| 149 | * c-basic-offset: 4 |
| 150 | * End: |
| 151 | */ |