Included first (incomplete) doxygen documentation
[openstranded.git] / src / s2string.cc
blob763be9f27e441144ad6569dfd7f0d62a8b471b01
1 /*
2 * See s2string.hh for more information about this file.
4 * Copyright (C) 2008 David Kolossa
6 * This file is part of OpenStranded.
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
22 #include "s2string.hh"
24 int
25 s2str::explode (std::string &str, std::string *buf, const char *token, size_t count)
27 std::vector<size_t> tokenPos (1, 0);
28 int warnFlag = 0;
30 while (str.find (token, tokenPos.back () ) != std::string::npos)
32 tokenPos.push_back (str.find (token, tokenPos.back () ));
33 str.erase (tokenPos.back (), 1);
36 // the number of commas in str has to be one lower than count
37 // but because of the additional entry for position 0, the size of the
38 // vector has to be count
39 if (tokenPos.size () < count)
41 warnFlag |= EXPLODE_LOWER; // number of substrings lower than count
44 if (tokenPos.size () > count)
46 warnFlag |= EXPLODE_GREATER; // number of substrings greater than count
49 // Catch the special case that no token occured
50 if (tokenPos.size () == 1)
52 buf[0] = str;
53 warnFlag |= EXPLODE_NO_TOKEN;
55 else
57 for (int i=0; i < (int) count; i++)
59 buf[i] = str.substr (tokenPos[i], (tokenPos[i+1] - tokenPos[i]));
63 return warnFlag;
67 bool
68 s2str::stof (std::string &str, float &buf)
70 std::istringstream stream;
71 bool pointOccured = false;
73 for (std::string::iterator c = str.begin (); c != str.end (); c++)
75 if ( (*c >= '0' && *c <= '9') || (*c == '.' && !pointOccured) )
77 continue;
79 else if (*c == ' ' || *c == '\t')
81 str.erase (c--);
82 continue;
84 else
86 buf = 0.0;
87 return false;
90 stream.str (str);
91 stream >> buf;
92 return true;
96 bool
97 s2str::stoi (std::string &str, int &buf)
99 std::istringstream stream;
101 for (std::string::iterator c = str.begin (); c != str.end (); c++)
103 if (*c >= '0' && *c <= '9')
105 continue;
107 else if (*c == ' ' || *c == '\t')
109 str.erase (c--);
110 continue;
112 else
114 buf = 0;
115 return false;
118 stream.str (str);
119 stream >> buf;
120 return true;