/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "helper_functions.h" #include "stdlibincl.h" using namespace std; /** * @brief checks if the input was a bool * @param BOOL a String that holds a bool: must be one of those: 1,0,true,false(case-insensitive) * @param defaultValue a default value that is set, if BOOL is corrupt * @return returns the bool, if BOOL was correct otherwise defaultValue */ bool isBool(const std::string& BOOL, bool defaultValue) { if (BOOL.empty()) return defaultValue; if(BOOL[0] == '1' || BOOL == "true" ) return true; else if (BOOL[0] == '0' || BOOL == "false") return false; else return defaultValue; } /** * @brief checks if the input was a int * @param INT a String that holds an int. * @param defaultValue a default value that is set, if INT is corrupt * @return returns the contained int, if INT was correct otherwise defaultValue */ int isInt(const std::string& INT, int defaultValue) { if (INT.empty()) return defaultValue; char* endPtr = NULL; int result = strtol(INT.c_str(), &endPtr, 10); if ( endPtr >= INT.c_str() && endPtr < INT.c_str()+ INT.size()) return defaultValue; else return result; } /** * @brief checks if the input was a float * @param FLOAT a String that holds an float. * @param defaultValue a default value that is set, if FLOAT is corrupt * @return returns the contained float, if FLOAT was correct otherwise defaultValue */ float isFloat(const std::string& FLOAT, float defaultValue) { if (FLOAT.empty()) return defaultValue; char* endPtr = NULL; double result = strtod(FLOAT.c_str(), &endPtr); if ( endPtr >= FLOAT.c_str() && endPtr < FLOAT.c_str() + FLOAT.size()) return defaultValue; else return result; } /** * @brief checks if the input was a string * @param STING a String(char-array) that holds an string. * @param defaultValue a default value that is set, if STRING is corrupt * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue */ const char* isCString(const std::string& STRING, const char* defaultValue) { if (STRING.size() > 0) return STRING.c_str(); else return defaultValue; } /** * @brief checks if the input was a string * @param STING a String(char-array) that holds an string. * @param defaultValue a default value that is set, if STRING is corrupt * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue */ std::string isString(const std::string& STRING, const std::string& defaultValue) { if (STRING.size() > 0) return STRING; else return defaultValue; } /** * @brief compares two strings without ignoring the case * @param s1 first string * @param s2 second string */ int nocaseCmp(const std::string& s1, const std::string& s2) { std::string::const_iterator it1=s1.begin(); std::string::const_iterator it2=s2.begin(); //stop when either string's end has been reached while ( (it1!=s1.end()) && (it2!=s2.end()) ) { if(::toupper(*it1) != ::toupper(*it2)) //letters differ? // return -1 to indicate smaller than, 1 otherwise return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; //proceed to the next character in each string ++it1; ++it2; } size_t size1=s1.size(), size2=s2.size();// cache lengths //return -1,0 or 1 according to strings' lengths if (size1==size2) return 0; return (size1 0) { if(::toupper(*it1) != ::toupper(*it2)) //letters differ? // return -1 to indicate smaller than, 1 otherwise return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; //proceed to the next character in each string ++it1; ++it2; } return 0; }