Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cd_old/src/lib/util/helper_functions.cc @ 7451

Last change on this file since 7451 was 5331, checked in by bensch, 19 years ago

orxonox/trunk: inition is our motto, so now the debug function of the ClassList really looks greate and is easy to use.

File size: 2.5 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 * 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 * 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 * 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 * 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* isString(const char* STRING, const char* defaultValue)
91{
92  if (STRING != NULL && strlen(STRING) > 0)
93    return STRING;
94  else
95    return defaultValue;
96}
Note: See TracBrowser for help on using the repository browser.