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 #pragma once
41 
42 #define command_usage(usage) command_line::option((const char*)0,argc,argv,(const char*)0,usage)
43 #define command_option(name,defaut,usage) command_line::option(name,argc,argv,defaut,usage)
44 
45 #ifdef WIN32
46 #define command_line_OS 2
47 #pragma warning( disable : 4530) //MSVC standard library can't be inlined
48 #pragma warning( disable : 4996) //MSVC warning C4996: declared deprecated
49 #pragma warning( disable : 4290) //MSVC warning C4290: C++ exception specification
50 #else
51 #define use_color_terminal
52 #define command_line_OS 1
53 #endif
54 
55 #include <cmath>
56 #include <cstdlib>
57 #include <cstdio>
58 #include <iostream>
59 
60 namespace OpenMEEG {
61 
62  namespace command_line {
63 
64  #ifdef use_color_terminal
65  const char t_normal[9] = {0x1b,'[','0',';','0',';','0','m','\0'};
66  const char t_red[11] = {0x1b,'[','4',';','3','1',';','5','9','m','\0'};
67  const char t_bold[5] = {0x1b,'[','1','m','\0'};
68  const char t_purple[11] = {0x1b,'[','0',';','3','5',';','5','9','m','\0'};
69  #else
70  const char t_normal[1] = {'\0'};
72  #endif
73 
74  inline char uncase(const char x) { return (char)( ( x < 'A' || x > 'Z' )?x:x-'A'+'a'); }
75 
76  inline float atof(const char *str)
77  {
78  float x = 0, y = 1;
79  if ( !str ) {
80  return 0;
81  } else {
82  std::sscanf(str, "%g/%g", &x,&y);
83  return x/y;
84  }
85  }
86 
87  inline int strlen(const char *s)
88  {
89  if ( s ) {
90  int k;
91  for ( k = 0; s[k]; k++) { };
92  return k;
93  }
94  return -1;
95  }
96 
97  inline int strncmp(const char *s1, const char *s2, const int l) {
98  if ( s1 && s2 ) {
99  int n = 0;
100  for ( int k = 0; k < l; k++) {
101  n += abs(s1[k]- s2[k]);
102  }
103  return n;
104  }
105  return 0;
106  }
107 
108  inline int strfind(const char *s, const char c)
109  {
110  if ( s ) {
111  int l;
112  for ( l = command_line::strlen(s); l >= 0 && s[l] != c; l--);
113  return l;
114  }
115  return -1;
116  }
117 
118  inline int strncasecmp(const char *s1, const char *s2, const int l) {
119  if ( s1 && s2 ) {
120  int n = 0;
121  for ( int k = 0; k < l; k++) {
122  n += abs(uncase(s1[k])-uncase(s2[k]));
123  }
124  return n;
125  }
126  return 0;
127  }
128 
129  inline int strcmp(const char *s1, const char *s2)
130  {
131  const int l1 = command_line::strlen(s1), l2 = command_line::strlen(s2);
132  return command_line::strncmp(s1, s2, 1+(l1<l2?l1:l2));
133  }
134 
135  inline int strcasecmp(const char *s1, const char *s2)
136  {
137  const int l1 = command_line::strlen(s1), l2 = command_line::strlen(s2);
138  return command_line::strncasecmp(s1, s2, 1 + (( l1 < l2 )?l1:l2));
139  }
140 
141  inline const char* basename(const char *s)
142  {
143  return ( command_line_OS != 2 )?(s?s+1+command_line::strfind(s, '/'):NULL):(s?s+1+command_line::strfind(s, '\\'):NULL);
144  }
145 
146  inline const char* option(const char *const name, const int argc, char **argv,
147  const char *defaut, const char *const usage=NULL)
148  {
149  static bool first = true, visu = false;
150  const char *res = NULL;
151  if ( first ) {
152  first = false;
153  visu = command_line::option("-h", argc, argv, (const char*)NULL)!=NULL;
154  }
155  if ( !name && visu ) {
156  std::fprintf(stderr, "\n %s%s%s", command_line::t_red, command_line::basename(argv[0]), command_line::t_normal);
157  if ( usage ) {
158  std::fprintf(stderr, " : %s", usage);
159  }
160  std::fprintf(stderr," (%s, %s)\n\n",__DATE__,__TIME__);
161  }
162  if ( name ) {
163  if ( argc > 0 ) {
164  int k = 0;
165  while ( k < argc && command_line::strcmp(argv[k], name)) {
166  k++;
167  }
168  res = (( k++ == argc )?defaut:(( k == argc )?argv[--k]:argv[k]));
169  } else {
170  res = defaut;
171  }
172  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);
173  }
174  return res;
175  }
176 
177  inline bool option(const char *const name, const int argc, char **argv,
178  const bool defaut, const char *const usage=NULL)
179  {
180  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
181  const bool res = s?(command_line::strcasecmp(s,"false") && command_line::strcasecmp(s,"off") && command_line::strcasecmp(s,"0")):defaut;
182  command_line::option(name, 0, NULL, res?"true":"false", usage);
183  return res;
184  }
185 
186  inline int option(const char *const name, const int argc, char **argv,
187  const int defaut, const char *const usage=NULL)
188  {
189  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
190  const int res = s?std::atoi(s):defaut;
191  char tmp[256];
192  std::sprintf(tmp, "%d", res);
193  command_line::option(name, 0, NULL, tmp, usage);
194  return res;
195  }
196 
197  inline char option(const char *const name, const int argc, char **argv,
198  const char defaut, const char *const usage=NULL)
199  {
200  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
201  const char res = s?s[0]:defaut;
202  char tmp[8];
203  tmp[0] = res;
204  tmp[1] ='\0';
205  command_line::option(name, 0, NULL, tmp, usage);
206  return res;
207  }
208 
209  inline double option(const char *const name, const int argc, char **argv,
210  const double defaut, const char *const usage=NULL)
211  {
212  const char *s = command_line::option(name, argc, argv, (const char*)NULL);
213  const double res = s?command_line::atof(s):defaut;
214  char tmp[256];
215  std::sprintf(tmp, "%g", res);
216  command_line::option(name, 0, NULL, tmp, usage);
217  return res;
218  }
219  }
220 }
OpenMEEG::command_line::uncase
char uncase(const char x)
Definition: options.h:74
OpenMEEG::command_line::strncasecmp
int strncasecmp(const char *s1, const char *s2, const int l)
Definition: options.h:118
OpenMEEG::command_line::t_normal
const char t_normal[9]
Definition: options.h:65
OpenMEEG::command_line::atof
float atof(const char *str)
Definition: options.h:76
OpenMEEG::command_line::option
const char * option(const char *const name, const int argc, char **argv, const char *defaut, const char *const usage=NULL)
Definition: options.h:146
OpenMEEG::command_line::strlen
int strlen(const char *s)
Definition: options.h:87
OpenMEEG::command_line::strfind
int strfind(const char *s, const char c)
Definition: options.h:108
command_line_OS
#define command_line_OS
Definition: options.h:52
OpenMEEG::command_line::t_bold
const char t_bold[5]
Definition: options.h:67
OpenMEEG
Definition: analytics.h:45
OpenMEEG::command_line::strcmp
int strcmp(const char *s1, const char *s2)
Definition: options.h:129
OpenMEEG::command_line::basename
const char * basename(const char *s)
Definition: options.h:141
OpenMEEG::command_line::strcasecmp
int strcasecmp(const char *s1, const char *s2)
Definition: options.h:135
OpenMEEG::command_line::t_purple
const char t_purple[11]
Definition: options.h:68
OpenMEEG::command_line::t_red
const char t_red[11]
Definition: options.h:66
OpenMEEG::command_line::strncmp
int strncmp(const char *s1, const char *s2, const int l)
Definition: options.h:97