FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
stringutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2005-2017 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cstdio>
24 
25 // FIFE includes
26 // These includes are split up in two parts, separated by one empty line
27 // First block: files included from the FIFE root src directory
28 // Second block: files included from the same folder
29 #include "stringutils.h"
30 
31 namespace FIFE {
32  int32_t makeInt32(const std::string& str) {
33  int32_t ret;
34  sscanf(str.c_str(), "%d", &ret);
35  return ret;
36  }
37 
38  IntVector tokenize(const std::string& str, char delim, char group) {
39  IntVector tokens;
40  if(str.empty()) {
41  return tokens;
42  }
43 
44  int curr = 0;
45  int start = 0;
46 
47  start = curr = static_cast<int>(str.find_first_not_of(delim));
48 
49  while(str[curr]) {
50  if(str[curr] == group) {
51  curr = static_cast<int>(str.find_first_of(group, curr+1));
52  if((size_t)curr == std::string::npos) {
53  return IntVector();
54  }
55 
56  std::string token = str.substr(start+1, curr-start-1);
57  tokens.push_back(makeInt32(token));
58  start = curr + 1;
59  } else if(str[curr] == delim) {
60  if(str[curr-1] != delim && str[curr-1] != group) {
61  std::string token = str.substr(start, curr-start);
62  tokens.push_back(makeInt32(token));
63  }
64  start = curr + 1;
65  }
66  ++curr;
67  }
68 
69  if(tokens.size() == 0) {
70  tokens.push_back(makeInt32(str));
71  return tokens;
72  }
73 
74  if(str[curr-1] != delim && str[curr-1] != group) {
75  std::string token = str.substr(start, curr - 1);
76  tokens.push_back(makeInt32(token));
77  }
78 
79  return tokens;
80  }
81 }
int32_t makeInt32(const std::string &str)
Definition: stringutils.cpp:32
std::vector< int32_t > IntVector
Definition: stringutils.h:32
IntVector tokenize(const std::string &str, char delim, char group)
Definition: stringutils.cpp:38