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"
25 s2str::explode (std::string
&str
, std::string
*buf
, const char *token
, size_t count
)
27 std::vector
<size_t> tokenPos (1, 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)
53 warnFlag
|= EXPLODE_NO_TOKEN
;
57 for (int i
=0; i
< (int) count
; i
++)
59 buf
[i
] = str
.substr (tokenPos
[i
], (tokenPos
[i
+1] - tokenPos
[i
]));
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
) )
79 else if (*c
== ' ' || *c
== '\t')
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')
107 else if (*c
== ' ' || *c
== '\t')