sclang: array primitives - respect mutability when changing object.
[supercollider.git] / common / SC_StringParser.cpp
blobe0931d29d92f4e83bea21d8d55f13392f4728d54
1 // Copyright (c) 2003-2006 stefan kersten
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
16 // USA
18 #include "SC_StringParser.h"
19 #include "SC_BoundsMacros.h"
20 #include <string.h>
21 #include <stdlib.h>
23 SC_StringParser::SC_StringParser()
24 : mSpec(0), mStart(0), mEnd(0), mSep(0)
28 SC_StringParser::SC_StringParser(const char *spec, char sep)
29 : mSpec(spec), mStart(0), mEnd(0), mSep(sep)
31 if (mSpec) {
32 size_t len = strlen(mSpec);
33 if (len > 0) {
34 mStart = mSpec;
35 mEnd = mStart + len;
36 } else {
37 mSpec = 0;
42 bool SC_StringParser::AtEnd() const
44 return mSpec == 0;
47 const char *SC_StringParser::NextToken()
49 if (mSpec) {
50 const char *end = strchr(mStart, mSep);
51 if (end == 0) {
52 end = mEnd;
55 size_t len = sc_min(SC_MAX_TOKEN_LENGTH-1, end-mStart);
56 memcpy(mBuf, mStart, len);
57 mBuf[len] = '\0';
59 if (end == mEnd) {
60 mSpec = 0;
61 } else {
62 mStart = end+1;
65 return mBuf;
68 return 0;
71 // EOF