Blender V2.61 - r43446

util_string.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <stdarg.h>
00020 #include <stdio.h>
00021 
00022 #include <boost/algorithm/string.hpp>
00023 
00024 #include "util_foreach.h"
00025 #include "util_string.h"
00026 
00027 #ifdef _WIN32
00028 #ifndef vsnprintf
00029 #define vsnprintf _vsnprintf
00030 #endif
00031 #endif
00032 
00033 CCL_NAMESPACE_BEGIN
00034 
00035 string string_printf(const char *format, ...)
00036 {
00037     vector<char> str(128, 0);
00038 
00039     while(1) {
00040         va_list args;
00041         int result;
00042 
00043         va_start(args, format);
00044         result = vsnprintf(&str[0], str.size(), format, args);
00045         va_end(args);
00046 
00047         if(result == -1) {
00048             /* not enough space or formatting error */
00049             if(str.size() > 65536) {
00050                 assert(0);
00051                 return string("");
00052             }
00053 
00054             str.resize(str.size()*2, 0);
00055             continue;
00056         }
00057         else if(result >= (int)str.size()) {
00058             /* not enough space */
00059             str.resize(result + 1, 0);
00060             continue;
00061         }
00062 
00063         return string(&str[0]);
00064     }
00065 }
00066 
00067 bool string_iequals(const string& a, const string& b)
00068 {
00069     if(a.size() == b.size()) {
00070         for(size_t i = 0; i < a.size(); i++)
00071             if(toupper(a[i]) != toupper(b[i]))
00072                 return false;
00073 
00074         return true;
00075     }
00076 
00077     return false;
00078 }
00079 
00080 void string_split(vector<string>& tokens, const string& str, const string& separators)
00081 {
00082     vector<string> split;
00083 
00084     boost::split(split, str, boost::is_any_of(separators), boost::token_compress_on);
00085 
00086     foreach(const string& token, split)
00087         if(token != "")
00088             tokens.push_back(token);
00089 }
00090 
00091 CCL_NAMESPACE_END
00092