stringutils.cpp

00001 /***************************************************************************
00002 *   Copyright (C) 2005-2011 by the FIFE team                              *
00003 *   http://www.fifengine.net                                              *
00004 *   This file is part of FIFE.                                            *
00005 *                                                                         *
00006 *   FIFE is free software; you can redistribute it and/or                 *
00007 *   modify it under the terms of the GNU Lesser General Public            *
00008 *   License as published by the Free Software Foundation; either          *
00009 *   version 2.1 of the License, or (at your option) any later version.    *
00010 *                                                                         *
00011 *   This library is distributed in the hope that it will be useful,       *
00012 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014 *   Lesser General Public License for more details.                       *
00015 *                                                                         *
00016 *   You should have received a copy of the GNU Lesser General Public      *
00017 *   License along with this library; if not, write to the                 *
00018 *   Free Software Foundation, Inc.,                                       *
00019 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020 ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <cstdio>
00024 
00025 // FIFE includes
00026 // These includes are split up in two parts, separated by one empty line
00027 // First block: files included from the FIFE root src directory
00028 // Second block: files included from the same folder
00029 #include "stringutils.h"
00030 
00031 namespace FIFE {
00032     int32_t makeInt32(const std::string& str) {
00033         int32_t ret;
00034         sscanf(str.c_str(), "%d", &ret);
00035         return ret;
00036     }
00037 
00038     IntVector tokenize(const std::string& str, char delim, char group) {
00039         IntVector tokens;
00040         if(str.empty()) {
00041             return tokens;
00042         }
00043 
00044         int curr = 0;
00045         int start = 0;
00046 
00047         start = curr = static_cast<int>(str.find_first_not_of(delim));
00048 
00049         while(str[curr]) {
00050             if(str[curr] == group) {
00051                 curr = static_cast<int>(str.find_first_of(group, curr+1));
00052                 if((size_t)curr == std::string::npos) {
00053                     return IntVector();
00054                 }
00055 
00056                 std::string token = str.substr(start+1, curr-start-1);
00057                 tokens.push_back(makeInt32(token));
00058                 start = curr + 1;
00059             } else if(str[curr] == delim) {
00060                 if(str[curr-1] != delim && str[curr-1] != group) {
00061                     std::string token = str.substr(start, curr-start);
00062                     tokens.push_back(makeInt32(token));
00063                 }
00064                 start = curr + 1;
00065             }
00066             ++curr;
00067         }
00068 
00069         if(tokens.size() == 0) {
00070             tokens.push_back(makeInt32(str));
00071             return tokens;
00072         }
00073 
00074         if(str[curr-1] != delim && str[curr-1] != group) {
00075             std::string token = str.substr(start, curr - 1);
00076             tokens.push_back(makeInt32(token));
00077         }
00078 
00079         return tokens;
00080     }
00081 }