Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7204 was 7203, checked in by bensch, 19 years ago

orxonox/trunk: compiles again, BUT well…. i do not expect it to run anymore

File size: 3.8 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 char* BOOL, bool defaultValue)
30{
31  if (BOOL == NULL)
32    return defaultValue;
33  if(!strcmp(BOOL, "1") || !strcasecmp( BOOL, "true") )
34    return true;
35  else if (!strcmp(BOOL, "0") || !strcasecmp( BOOL, "false"))
36    return false;
37  else
38    return defaultValue;
39
40}
41
42
43/**
44 * @brief checks if the input was a int
45 * @param INT a String that holds an int.
46 * @param defaultValue a default value that is set, if INT is corrupt
47 * @return returns the contained int, if INT was correct otherwise defaultValue
48 */
49int isInt(const char* INT, int defaultValue)
50{
51  if (INT == NULL)
52    return defaultValue;
53  char* endPtr = NULL;
54
55  int result = strtol(INT, &endPtr, 10);
56
57  if ( endPtr >= INT && endPtr < INT + strlen(INT))
58    return defaultValue;
59  else
60    return result;
61}
62
63
64/**
65 * @brief checks if the input was a float
66 * @param FLOAT a String that holds an float.
67 * @param defaultValue a default value that is set, if FLOAT is corrupt
68 * @return returns the contained float, if FLOAT was correct otherwise defaultValue
69 */
70float isFloat(const char* FLOAT, float defaultValue)
71{
72  if (FLOAT == NULL)
73    return defaultValue;
74  char* endPtr = NULL;
75  double result = strtod(FLOAT, &endPtr);
76
77  if ( endPtr >= FLOAT && endPtr < FLOAT + strlen(FLOAT))
78    return defaultValue;
79  else
80    return result;
81}
82
83
84/**
85 * @brief checks if the input was a string
86 * @param STING a String(char-array) that holds an string.
87 * @param defaultValue a default value that is set, if STRING is corrupt
88 * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue
89 */
90const char* isCString(const char* STRING, const char* defaultValue)
91{
92  if (STRING != NULL && strlen(STRING) > 0)
93    return STRING;
94  else
95    return defaultValue;
96}
97
98/**
99 * @brief checks if the input was a string
100 * @param STING a String(char-array) that holds an string.
101 * @param defaultValue a default value that is set, if STRING is corrupt
102 * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue
103 */
104std::string isString(const char* STRING, const std::string& defaultValue)
105{
106  if (STRING != NULL && strlen(STRING) > 0)
107    return STRING;
108  else
109    return defaultValue;
110}
111
112
113/**
114 * @brief compares two strings without ignoring the case
115 * @param s1 first string
116 * @param s2 second string
117 */
118int nocase_cmp(const std::string& s1, const std::string& s2)
119{
120  std::string::const_iterator it1=s1.begin();
121  std::string::const_iterator it2=s2.begin();
122
123  //stop when either string's end has been reached
124  while ( (it1!=s1.end()) && (it2!=s2.end()) )
125  {
126    if(::toupper(*it1) != ::toupper(*it2)) //letters differ?
127     // return -1 to indicate smaller than, 1 otherwise
128      return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;
129    //proceed to the next character in each string
130    ++it1;
131    ++it2;
132  }
133  size_t size1=s1.size(), size2=s2.size();// cache lengths
134   //return -1,0 or 1 according to strings' lengths
135  if (size1==size2)
136    return 0;
137  return (size1<size2) ? -1 : 1;
138}
139
Note: See TracBrowser for help on using the repository browser.