1
1
/**********************************************************************
2
2
tokenst.cpp - Tokenize a string.
4
4
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5
Some portions Copyright (c) 2001-2003 by Geoffrey R. Hutchison
5
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
7
7
This file is part of the Open Babel project.
8
8
For more information, see <http://openbabel.sourceforge.net/>
10
10
This program is free software; you can redistribute it and/or modify
11
11
it under the terms of the GNU General Public License as published by
12
12
the Free Software Foundation version 2 of the License.
14
14
This program is distributed in the hope that it will be useful,
15
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
#include "babelconfig.h"
28
30
using namespace std;
30
bool tokenize(vector<string> &, const char *, const char *);
31
char *trim_spaces(char *string);
32
bool tokenize(vector<string> &vcr, string &s, const char *delimstr,int limit=-1);
36
bool tokenize(vector<string> &vcr, const char *buf, const char *delimstr)
32
OBAPI bool tokenize(vector<string> &, const char *, const char *);
33
OBAPI char *trim_spaces(char *string);
34
OBAPI bool tokenize(vector<string> &vcr, string &s, const char *delimstr,int limit=-1);
39
//! Break a string (supplied as the second argument) into tokens, returned
40
//! in the first argument. Tokens are determined by the delimiters supplied
41
//! (defaults to whitespace (i.e., spaces, tabs, newlines)
42
OBAPI bool tokenize(vector<string> &vcr, const char *buf, const char *delimstr)
41
47
size_t startpos=0,endpos=0;
45
startpos = s.find_first_not_of(delimstr,startpos);
46
endpos = s.find_first_of(delimstr,startpos);
48
if (endpos <= s.size() && startpos <= s.size())
49
vcr.push_back(s.substr(startpos,endpos-startpos));
51
startpos = s.find_first_not_of(delimstr,startpos);
52
endpos = s.find_first_of(delimstr,startpos);
54
if (endpos <= s.size() && startpos <= s.size())
55
vcr.push_back(s.substr(startpos,endpos-startpos));
60
char *trim_spaces(char *string)
65
//! Trim any trailing spaces at the end of the supplied string.
66
OBAPI char *trim_spaces(char *string)
64
length = strlen(string);
68
while ((length > 0) && (string[0] == ' '))
76
while ((length > 0) && (string[length-1] == ' '))
78
string[length-1] = '\0';
70
length = strlen(string);
74
while ((length > 0) && (string[0] == ' '))
82
while ((length > 0) && (string[length-1] == ' '))
84
string[length-1] = '\0';
86
bool tokenize(vector<string> &vcr, string &s, const char *delimstr,int limit)
92
//! Break a string (supplied as the second argument) into tokens, returned
93
//! in the first argument. Tokens are determined by the delimiters supplied
94
//! (defaults to whitespace (i.e., spaces, tabs, newlines)
95
//! Only breaks at most 'limit' tokens and the last item in the vector may
96
//! include un-parsed tokens.
97
OBAPI bool tokenize(vector<string> &vcr, string &s, const char *delimstr, int limit)
89
size_t startpos=0,endpos=0;
100
size_t startpos=0,endpos=0;
92
unsigned int s_size = s.size();
103
unsigned int s_size = s.size();
95
106
startpos = s.find_first_not_of(delimstr,startpos);
96
107
endpos = s.find_first_of(delimstr,startpos);
97
108
if (endpos <= s_size && startpos <= s_size)
99
110
vcr.push_back(s.substr(startpos,endpos-startpos));
102
113
if (matched == limit)
104
115
startpos = endpos+1;
105
116
vcr.push_back(s.substr(startpos,s_size));
111
122
if (startpos < s_size)
112
vcr.push_back(s.substr(startpos,s_size-startpos));
123
vcr.push_back(s.substr(startpos,s_size-startpos));
116
127
startpos = endpos+1;
132
OBAPI void Trim(string& txt)
134
string::size_type pos = txt.find_last_not_of(" \t\n\r");
135
if(pos!=string::npos)
140
pos = txt.find_first_not_of(" \t\n\r");
141
if(pos!=string::npos)
147
} // end namespace OpenBabel
149
//! \file tokenst.cpp
150
//! \brief Tokenize a string.