OpenMEEG
options.h
Go to the documentation of this file.
1 /*
2 Project Name : OpenMEEG
3 
4 © INRIA and ENPC (contributors: Geoffray ADDE, Maureen CLERC, Alexandre
5 GRAMFORT, Renaud KERIVEN, Jan KYBIC, Perrine LANDREAU, Théodore PAPADOPOULO,
6 Emmanuel OLIVI
7 Maureen.Clerc.AT.inria.fr, keriven.AT.certis.enpc.fr,
8 kybic.AT.fel.cvut.cz, papadop.AT.inria.fr)
9 
10 The OpenMEEG software is a C++ package for solving the forward/inverse
11 problems of electroencephalography and magnetoencephalography.
12 
13 This software is governed by the CeCILL-B license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL-B
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's authors, the holders of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL-B license and that you accept its terms.
38 */
39 
40 #define command_usage(usage) command_line::option((const char*)0,argc,argv,(const char*)0,usage)
41 #define command_option(name,defaut,usage) command_line::option(name,argc,argv,defaut,usage)
42 
43 #ifdef WIN32
44 #define command_line_OS 2
45 #pragma warning( disable : 4530) //MSVC standard library can't be inlined
46 #pragma warning( disable : 4996) //MSVC warning C4996: declared deprecated
47 #pragma warning( disable : 4290) //MSVC warning C4290: C++ exception specification
48 #else
49 #define use_color_terminal
50 #define command_line_OS 1
51 #endif
52 
53 #include <cmath>
54 #include <cstdlib>
55 #include <cstdio>
56 #include <iostream>
57 
58 namespace OpenMEEG {
59 
60  namespace command_line {
61 
62  #ifdef use_color_terminal
63  const char t_normal[9] = {0x1b,'[','0',';','0',';','0','m','\0'};
64  const char t_red[11] = {0x1b,'[','4',';','3','1',';','5','9','m','\0'};
65  const char t_bold[5] = {0x1b,'[','1','m','\0'};
66  const char t_purple[11] = {0x1b,'[','0',';','3','5',';','5','9','m','\0'};
67  #else
68  const char t_normal[1] = {'\0'};
70  #endif
71 
72  inline char uncase(const char x) { return (char)( ( x < 'A' || x > 'Z' )?x:x-'A'+'a'); }
73 
74  inline float atof(const char *str)
75  {
76  float x = 0, y = 1;
77  if ( !str ) {
78  return 0;
79  } else {
80  std::sscanf(str, "%g/%g", &x,&y);
81  return x/y;
82  }
83  }
84 
85  inline int strlen(const char *s)
86  {
87  if ( s ) {
88  int k;
89  for ( k = 0; s[k]; k++) { };
90  return k;
91  }
92  return -1;
93  }
94 
95  inline int strncmp(const char *s1, const char *s2, const int l) {
96  if ( s1 && s2 ) {
97  int n = 0;
98  for ( int k = 0; k < l; k++) {
99  n += abs(s1[k]- s2[k]);
100  }
101  return n;
102  }
103  return 0;
104  }
105 
106  inline int strfind(const char *s, const char c)
107  {
108  if ( s ) {
109  int l;
110  for ( l = command_line::strlen(s); l >= 0 && s[l] != c; l--);
111  return l;
112  }
113  return -1;
114  }
115 
116  inline int strncasecmp(const char *s1, const char *s2, const int l) {
117  if ( s1 && s2 ) {
118  int n = 0;
119  for ( int k = 0; k < l; k++) {
120  n += abs(uncase(s1[k])-uncase(s2[k]));
121  }
122  return n;
123  }
124  return 0;
125  }
126 
127  inline int strcmp(const char *s1, const char *s2)
128  {
129  const int l1 = command_line::strlen(s1), l2 = command_line::strlen(s2);
130  return command_line::strncmp(s1, s2, 1+(l1<l2?l1:l2));
131  }
132 
133  inline int strcasecmp(const char *s1, const char *s2)
134  {
135  const int l1 = command_line::strlen(s1), l2 = command_line::strlen(s2);
136  return command_line::strncasecmp(s1, s2, 1 + (( l1 < l2 )?l1:l2));
137  }
138 
139  inline const char* basename(const char *s)
140  {
141  return ( command_line_OS != 2 )?(s?s+1+command_line::strfind(s, '/'):NULL):(s?s+1+command_line::strfind(s, '\\'):NULL);
142  }
143 
144  inline const char* option(const char *const name, const int argc, char **argv,
145  const char *defaut, const char *const usage=NULL)
146  {
147  static bool first = true, visu = false;
148  const char *res = NULL;
149  if ( first ) {
150  first = false;
151  visu = command_line::option("-h", argc, argv, (const char*)NULL)!=NULL;
152  }
153  if ( !name && visu ) {
154  std::fprintf(stderr, "\n %s%s%s", command_line::t_red, command_line::basename(argv[0]), command_line::t_normal);
155  if ( usage ) {
156  std::fprintf(stderr, " : %s", usage);
157  }
158  std::fprintf(stderr," (%s, %s)\n\n",__DATE__,__TIME__);
159  }
160  if ( name ) {
161  if ( argc > 0 ) {
162  int k = 0;
163  while ( k < argc && command_line::strcmp(argv[k], name)) {
164  k++;
165  }
166  res = (( k++ == argc )?defaut:(( k == argc )?argv[--k]:argv[k]));
167  } else {
168  res = defaut;
169  }
170  if ( visu && usage ) std::fprintf(stderr, " %s%-8s%s = %-12s : %s%s%s\n", command_line::t_bold, name, command_line::t_normal, res?res:"NULL", command_line::t_purple, usage, command_line::t_normal);
171  }
172  return res;
173  }
174 
175  inline bool option(const char *const name, const int argc, char **argv,
176  const bool defaut, const char *const usage=NULL)
177  {
178  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
179  const bool res = s?(command_line::strcasecmp(s,"false") && command_line::strcasecmp(s,"off") && command_line::strcasecmp(s,"0")):defaut;
180  command_line::option(name, 0, NULL, res?"true":"false", usage);
181  return res;
182  }
183 
184  inline int option(const char *const name, const int argc, char **argv,
185  const int defaut, const char *const usage=NULL)
186  {
187  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
188  const int res = s?std::atoi(s):defaut;
189  char tmp[256];
190  std::sprintf(tmp, "%d", res);
191  command_line::option(name, 0, NULL, tmp, usage);
192  return res;
193  }
194 
195  inline char option(const char *const name, const int argc, char **argv,
196  const char defaut, const char *const usage=NULL)
197  {
198  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
199  const char res = s?s[0]:defaut;
200  char tmp[8];
201  tmp[0] = res;
202  tmp[1] ='\0';
203  command_line::option(name, 0, NULL, tmp, usage);
204  return res;
205  }
206 
207  inline double option(const char *const name, const int argc, char **argv,
208  const double defaut, const char *const usage=NULL)
209  {
210  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
211  const double res = s?command_line::atof(s):defaut;
212  char tmp[256];
213  std::sprintf(tmp, "%g", res);
214  command_line::option(name, 0, NULL, tmp, usage);
215  return res;
216  }
217  }
218 }
int strfind(const char *s, const char c)
Definition: options.h:106
const char t_bold[5]
Definition: options.h:65
float atof(const char *str)
Definition: options.h:74
#define command_line_OS
Definition: options.h:50
const char * option(const char *const name, const int argc, char **argv, const char *defaut, const char *const usage=NULL)
Definition: options.h:144
int strlen(const char *s)
Definition: options.h:85
int strcasecmp(const char *s1, const char *s2)
Definition: options.h:133
const char t_purple[11]
Definition: options.h:66
const char t_red[11]
Definition: options.h:64
int strcmp(const char *s1, const char *s2)
Definition: options.h:127
int strncmp(const char *s1, const char *s2, const int l)
Definition: options.h:95
const char * basename(const char *s)
Definition: options.h:139
char uncase(const char x)
Definition: options.h:72
int strncasecmp(const char *s1, const char *s2, const int l)
Definition: options.h:116
const char t_normal[9]
Definition: options.h:63