iRoCS Toolbox  1.1.0
Compilers.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2005-2015 Olaf Ronneberger, Jörg Mechnich, Florian Pigorsch,
4  * Mario Emmenlauer, Thorsten Schmidt
5  *
6  * Image Analysis Lab, University of Freiburg, Germany
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  **************************************************************************/
23 
24 /**************************************************************************
25 ** Title: compilers for various datatypes
26 ** $RCSfile$
27 ** $Revision: 16 $$Name$
28 ** $Date: 2002-03-26 08:36:28 +0100 (Tue, 26 Mar 2002) $
29 ** Copyright: GPL $Author: ronneber $
30 ** Description:
31 **
32 **
33 **
34 **-------------------------------------------------------------------------
35 **
36 ** $Log$
37 ** Revision 1.1 2002/03/26 07:36:28 ronneber
38 ** restructuring for autoconf
39 **
40 ** Revision 1.1.1.1 2002/03/22 13:45:07 pigorsch
41 ** moved from polsoft repository
42 **
43 **
44 **
45 **************************************************************************/
46 
47 #ifndef COMPILERS_HH
48 #define COMPILERS_HH
49 
50 #ifdef HAVE_CONFIG_H
51 #include <config.hh>
52 #endif
53 
54 #include <cctype>
55 #include <cstdlib>
56 #include <iostream>
57 
58 #include "Compiler.hh"
59 
60 template<>
61 bool
63  const char* arg,
64  const char*& endptr,
65  int& value)
66 {
67  if (arg==0) // null string - nothing to do
68  {
69  return true;
70  }
71  else if (*arg==0) // empty string
72  {
73  setErrorMsg("empty integer value specified");
74  return false;
75  }
76 
77  // compile the string into an integer
78  char* ptr;
79  int result=strtol(arg, &ptr, 0);
80 
81  if (ptr==arg) // first char is invalid
82  {
83  setErrorMsg("invalid integer value \""+std::string(arg)+"\"");
84  return false;
85  }
86 
87  endptr=ptr;
88  value=(int)result;
89  return true;
90 };
91 
92 template<>
93 bool
95  const char* arg,
96  const char*& endptr,
97  double& value)
98 {
99  if (arg==0) // null string - nothing to do
100  {
101  return true;
102  }
103  else if (*arg==0) // empty string
104  {
105  setErrorMsg("empty integer value specified");
106  return false;
107  }
108 
109  // compile the string into an integer
110  char* ptr;
111  double result=strtod(arg, &ptr);
112 
113  if (ptr==arg) // first char is invalid
114  {
115  setErrorMsg("invalid double value \""+std::string(arg)+"\"");
116  return false;
117  }
118 
119  endptr=ptr;
120  value=result;
121  return true;
122 };
123 
124 template<>
125 bool
127  const char* arg,
128  const char*& endptr,
129  std::string& value)
130 {
131  if (arg!=0)
132  {
133  endptr=0;
134  value=arg;
135  }
136 
137  return true;
138 };
139 
140 template<>
141 bool
143  const char* arg,
144  const char*& endptr,
145  bool& value)
146 {
147  if (arg==0) // no argument -> true
148  {
149  value=true;
150  return true;
151  }
152  else if (*arg==0) // empty string -> true
153  {
154  value=true;
155  return true;
156  }
157 
158  std::string s(arg);
159  for (std::string::iterator p=s.begin();
160  p!=s.end();
161  ++p)
162  {
163  *p=tolower(*p);
164  }
165 
166  if (s=="1" || s=="on" || s=="true")
167  {
168  endptr=0;
169  value=true;
170  return true;
171  }
172  else if (s=="0" || s=="off" || s=="false")
173  {
174  endptr=0;
175  value=false;
176  return true;
177  }
178 
179  return false;
180 };
181 
182 #endif
bool compile(const char *arg, const char *&enptr, T &value)
Convert the given string into the desired datatype T.