Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/lib/util/helper_functions.cc @ 7214

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

orxonox/trunk: now the branche works again, as it did before

File size: 3.9 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "helper_functions.h"
19#include "stdlibincl.h"
20
21using namespace std;
22
23/**
24 * @brief checks if the input was a bool
25 * @param BOOL a String that holds a bool: must be one of those: 1,0,true,false(case-insensitive)
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 */
29bool isBool(const std::string& BOOL, bool defaultValue)
30{
31  if (BOOL.empty())
32    return defaultValue;
33  if(BOOL[0] == '1' || BOOL == "true" )
34    return true;
35  else if (BOOL[0] == '0' || BOOL == "false")
36    return false;
37  else
38    return defaultValue;
39}
40
41
42/**
43 * @brief checks if the input was a int
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 */
48int isInt(const std::string& INT, int defaultValue)
49{
50  if (INT.empty())
51    return defaultValue;
52  char* endPtr = NULL;
53
54  int result = strtol(INT.c_str(), &endPtr, 10);
55
56  if ( endPtr >= INT.c_str() && endPtr < INT.c_str()+ INT.size())
57    return defaultValue;
58  else
59    return result;
60}
61
62
63/**
64 * @brief checks if the input was a float
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 */
69float isFloat(const std::string& FLOAT, float defaultValue)
70{
71  if (FLOAT.empty())
72    return defaultValue;
73  char* endPtr = NULL;
74  double result = strtod(FLOAT.c_str(), &endPtr);
75
76  if ( endPtr >= FLOAT.c_str() && endPtr < FLOAT.c_str() + FLOAT.size())
77    return defaultValue;
78  else
79    return result;
80}
81
82
83/**
84 * @brief checks if the input was a string
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 */
89const char* isCString(const std::string& STRING, const char* defaultValue)
90{
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 */
103std::string isString(const std::string& STRING, const std::string& defaultValue)
104{
105  if (STRING.size() > 0)
106  {
107    printf("DECISION1: %s\n", STRING.c_str());
108    return STRING;
109  }
110  else
111  {
112    printf("DECISION2: %s\n", defaultValue.c_str());
113    return defaultValue;
114  }
115}
116
117
118/**
119 * @brief compares two strings without ignoring the case
120 * @param s1 first string
121 * @param s2 second string
122 */
123int nocase_cmp(const std::string& s1, const std::string& s2)
124{
125  std::string::const_iterator it1=s1.begin();
126  std::string::const_iterator it2=s2.begin();
127
128  //stop when either string's end has been reached
129  while ( (it1!=s1.end()) && (it2!=s2.end()) )
130  {
131    if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
132     // return -1 to indicate smaller than, 1 otherwise
133      return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
134    //proceed to the next character in each string
135    ++it1;
136    ++it2;
137  }
138  size_t size1=s1.size(), size2=s2.size();// cache lengths
139   //return -1,0 or 1 according to strings' lengths
140  if (size1==size2)
141    return 0;
142  return (size1<size2) ? -1 : 1;
143}
144
Note: See TracBrowser for help on using the repository browser.