~ubuntu-branches/ubuntu/feisty/openbabel/feisty

« back to all changes in this revision

Viewing changes to src/tokenst.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2006-05-14 19:46:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060514194601-h3j1wovvc42cigxl
Tags: 2.0.1-1
* New upstream release. (Closes: #341628)
* debian/patches/04_zipstream_fix.diff: Removed, applied upstream.
* debian/rules (DEB_MAKE_CHECK_TARGET): Readded.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**********************************************************************
2
2
tokenst.cpp - Tokenize a string.
3
 
 
 
3
 
4
4
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5
 
Some portions Copyright (c) 2001-2003 by Geoffrey R. Hutchison
6
 
 
 
5
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
 
6
 
7
7
This file is part of the Open Babel project.
8
8
For more information, see <http://openbabel.sourceforge.net/>
9
 
 
 
9
 
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.
13
 
 
 
13
 
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
25
25
#include <vector>
26
26
#include <string>
27
27
 
 
28
#include "babelconfig.h"
 
29
 
28
30
using namespace std;
29
 
 
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);
33
 
 
34
 
namespace OpenBabel {
35
 
 
36
 
bool tokenize(vector<string> &vcr, const char *buf, const char *delimstr)
 
31
/*
 
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);
 
35
*/
 
36
namespace OpenBabel
 
37
{
 
38
 
 
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)
37
43
{
38
44
    vcr.clear();
39
45
    string s = buf;
40
46
    s += "\n";
41
47
    size_t startpos=0,endpos=0;
42
 
    
 
48
 
43
49
    for (;;)
44
50
    {
45
 
        startpos = s.find_first_not_of(delimstr,startpos);
46
 
        endpos = s.find_first_of(delimstr,startpos);
47
 
 
48
 
        if (endpos <= s.size() && startpos <= s.size())
49
 
            vcr.push_back(s.substr(startpos,endpos-startpos));
50
 
        else
51
 
            break;
52
 
        
53
 
        startpos = endpos+1;
 
51
        startpos = s.find_first_not_of(delimstr,startpos);
 
52
        endpos = s.find_first_of(delimstr,startpos);
 
53
 
 
54
        if (endpos <= s.size() && startpos <= s.size())
 
55
            vcr.push_back(s.substr(startpos,endpos-startpos));
 
56
        else
 
57
            break;
 
58
 
 
59
        startpos = endpos+1;
54
60
    }
55
61
 
56
62
    return(true);
57
63
}
58
64
 
59
 
 
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)
61
67
{
62
 
  int length;
63
 
 
64
 
  length = strlen(string);
65
 
  if (length == 0)
66
 
    return string;
67
 
 
68
 
  while ((length > 0) && (string[0] == ' '))
69
 
  {
70
 
    string++;
71
 
    --length;
72
 
  }
73
 
 
74
 
  if (length > 0)
75
 
  {
76
 
    while ((length > 0) && (string[length-1] == ' '))
77
 
    {
78
 
      string[length-1] = '\0';
79
 
      --length;
80
 
    }
81
 
  }
82
 
 
83
 
  return(string);
 
68
    int length;
 
69
 
 
70
    length = strlen(string);
 
71
    if (length == 0)
 
72
        return string;
 
73
 
 
74
    while ((length > 0) && (string[0] == ' '))
 
75
    {
 
76
        string++;
 
77
        --length;
 
78
    }
 
79
 
 
80
    if (length > 0)
 
81
    {
 
82
        while ((length > 0) && (string[length-1] == ' '))
 
83
        {
 
84
            string[length-1] = '\0';
 
85
            --length;
 
86
        }
 
87
    }
 
88
 
 
89
    return(string);
84
90
}
85
91
 
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)
87
98
{
88
 
  vcr.clear();
89
 
  size_t startpos=0,endpos=0;
 
99
    vcr.clear();
 
100
    size_t startpos=0,endpos=0;
90
101
 
91
 
  int matched=0;
92
 
  unsigned int s_size = s.size();
93
 
  for (;;)
 
102
    int matched=0;
 
103
    unsigned int s_size = s.size();
 
104
    for (;;)
94
105
    {
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)
98
 
          {
 
109
        {
99
110
            vcr.push_back(s.substr(startpos,endpos-startpos));
100
111
 
101
112
            matched++;
102
113
            if (matched == limit)
103
 
              {
 
114
            {
104
115
                startpos = endpos+1;
105
116
                vcr.push_back(s.substr(startpos,s_size));
106
117
                break;
107
 
              }
108
 
          }
 
118
            }
 
119
        }
109
120
        else
110
 
          {
 
121
        {
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));
113
124
            break;
114
 
          }
 
125
        }
115
126
 
116
127
        startpos = endpos+1;
117
128
    }
118
129
    return(true);
119
130
}
120
131
 
 
132
OBAPI void Trim(string& txt)
 
133
{
 
134
        string::size_type pos = txt.find_last_not_of(" \t\n\r");
 
135
        if(pos!=string::npos)
 
136
                txt.erase(pos+1);
 
137
        else
 
138
                txt.erase();
 
139
 
 
140
        pos = txt.find_first_not_of(" \t\n\r");
 
141
        if(pos!=string::npos)
 
142
                txt.erase(0, pos);
 
143
        else
 
144
                txt.erase();
121
145
}
122
146
 
123
 
 
124
 
 
 
147
} // end namespace OpenBabel
 
148
 
 
149
//! \file tokenst.cpp
 
150
//! \brief Tokenize a string.