Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/helper_functions.cc @ 10646

Last change on this file since 10646 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 4.5 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[5141]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5141]18#include "helper_functions.h"
19#include "stdlibincl.h"
[1853]20
21
[9406]22
[3245]23/**
[7221]24 * @brief checks if the input was a bool
[5331]25 * @param BOOL a String that holds a bool: must be one of those: 1,0,true,false(case-insensitive)
[5141]26 * @param defaultValue a default value that is set, if BOOL is corrupt
27 * @return returns the bool, if BOOL was correct otherwise defaultValue
28 */
[7221]29bool isBool(const std::string& BOOL, bool defaultValue)
[3365]30{
[7221]31  if (BOOL.empty())
[5329]32    return defaultValue;
[7221]33  if(BOOL[0] == '1' || BOOL == "true" )
[5141]34    return true;
[7221]35  else if (BOOL[0] == '0' || BOOL == "false")
[5141]36    return false;
37  else
38    return defaultValue;
39}
[4320]40
[5331]41
42/**
[7221]43 * @brief checks if the input was a int
[5331]44 * @param INT a String that holds an int.
45 * @param defaultValue a default value that is set, if INT is corrupt
46 * @return returns the contained int, if INT was correct otherwise defaultValue
47 */
[7221]48int isInt(const std::string& INT, int defaultValue)
[5141]49{
[7221]50  if (INT.empty())
[5329]51    return defaultValue;
[5141]52  char* endPtr = NULL;
[5329]53
[7221]54  int result = strtol(INT.c_str(), &endPtr, 10);
[5141]55
[7221]56  if ( endPtr >= INT.c_str() && endPtr < INT.c_str()+ INT.size())
[5141]57    return defaultValue;
58  else
59    return result;
[3365]60}
[1853]61
[5331]62
63/**
[7221]64 * @brief checks if the input was a float
[5331]65 * @param FLOAT a String that holds an float.
66 * @param defaultValue a default value that is set, if FLOAT is corrupt
67 * @return returns the contained float, if FLOAT was correct otherwise defaultValue
68 */
[7221]69float isFloat(const std::string& FLOAT, float defaultValue)
[5141]70{
[7221]71  if (FLOAT.empty())
[5329]72    return defaultValue;
[5141]73  char* endPtr = NULL;
[7221]74  double result = strtod(FLOAT.c_str(), &endPtr);
[1853]75
[7221]76  if ( endPtr >= FLOAT.c_str() && endPtr < FLOAT.c_str() + FLOAT.size())
[5141]77    return defaultValue;
78  else
79    return result;
80}
81
82
[5331]83/**
[7221]84 * @brief checks if the input was a string
[5331]85 * @param STING a String(char-array) that holds an string.
86 * @param defaultValue a default value that is set, if STRING is corrupt
87 * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue
88 */
[7221]89const char* isCString(const std::string& STRING, const char* defaultValue)
[3543]90{
[7221]91  if (STRING.size() > 0)
92    return STRING.c_str();
93  else
94    return defaultValue;
95}
96
97/**
98 * @brief checks if the input was a string
99 * @param STING a String(char-array) that holds an string.
100 * @param defaultValue a default value that is set, if STRING is corrupt
101 * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue
102 */
[7714]103const std::string& isString(const std::string& STRING, const std::string& defaultValue)
[7221]104{
105  if (STRING.size() > 0)
[5141]106    return STRING;
107  else
108    return defaultValue;
[3543]109}
[7221]110
111
112/**
113 * @brief compares two strings without ignoring the case
114 * @param s1 first string
115 * @param s2 second string
116 */
[7371]117int nocaseCmp(const std::string& s1, const std::string& s2)
[7221]118{
119  std::string::const_iterator it1=s1.begin();
120  std::string::const_iterator it2=s2.begin();
121
122  //stop when either string's end has been reached
123  while ( (it1!=s1.end()) && (it2!=s2.end()) )
124  {
125    if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
[7714]126      // return -1 to indicate smaller than, 1 otherwise
[7221]127      return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
128    //proceed to the next character in each string
129    ++it1;
130    ++it2;
131  }
132  size_t size1=s1.size(), size2=s2.size();// cache lengths
[7714]133  //return -1,0 or 1 according to strings' lengths
[7221]134  if (size1==size2)
135    return 0;
136  return (size1<size2) ? -1 : 1;
137}
138
[7371]139
140/**
141 * @brief compares two strings without ignoring the case
142 * @param s1 first string
143 * @param s2 second string
144 * @param len how far from the beginning to start.
145 */
146int nocaseCmp(const std::string& s1, const std::string& s2, unsigned int len)
147{
[7412]148  if (len == 0)
149    return 0;
[7371]150  std::string::const_iterator it1=s1.begin();
151  std::string::const_iterator it2=s2.begin();
152
153  //stop when either string's end has been reached
154  while ( (it1!=s1.end()) && (it2!=s2.end()) && len-- > 0)
155  {
156    if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
[7714]157      // return -1 to indicate smaller than, 1 otherwise
[7371]158      return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
159    //proceed to the next character in each string
160    ++it1;
161    ++it2;
162  }
163  return 0;
164}
165
Note: See TracBrowser for help on using the repository browser.