Xmipp  v3.23.11-Nereus
args.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "args.h"
27 #include "matrix1d.h"
28 
29 // Get parameters from the command line ====================================
30 const char *getParameter(int argc, const char **argv, const char *param, const char *option)
31 {
32  int i = 0;
33 
34  while ((i < argc) && (strcmp(param, argv[i])))
35  i++;
36  if (i < argc - 1)
37  return(argv[i+1]);
38  else
39  if (option == NULL)
41 
42  return((char *) option);
43 }
44 
45 // Get 2 parameters ========================================================
46 bool getTwoDoubleParams(int argc, const char **argv, const char *param,
47  double &v1, double &v2, double v1_def, double v2_def)
48 {
49  bool retval;
50  int i = paremeterPosition(argc, argv, param);
51  if (i != -1)
52  {
53  if (i + 2 >= argc)
55  (std::string)"Not enough arguments after " + *param);
56  v1 = textToFloat(argv[i+1]);
57  v2 = textToFloat(argv[i+2]);
58  retval = true;
59  }
60  else
61  {
62  v1 = v1_def;
63  v2 = v2_def;
64  retval = false;
65  }
66  return retval;
67 }
68 
69 // Get 3 parameters ========================================================
70 bool getThreeDoubleParams(int argc, const char **argv, const char *param,
71  double &v1, double &v2, double &v3,
72  double v1_def, double v2_def, double v3_def)
73 {
74  bool retval;
75  int i = paremeterPosition(argc, argv, param);
76  if (i != -1)
77  {
78  if (i + 3 >= argc)
80  (std::string)"Not enough arguments after " + *param);
81  v1 = textToFloat(argv[i+1]);
82  v2 = textToFloat(argv[i+2]);
83  v3 = textToFloat(argv[i+3]);
84  retval = true;
85  }
86  else
87  {
88  v1 = v1_def;
89  v2 = v2_def;
90  v3 = v3_def;
91  retval = false;
92  }
93  return retval;
94 }
95 
96 // Checks if a boolean parameter was included the command line =============
97 bool checkParameter(int argc, const char **argv, const char *param)
98 {
99  int i = 0;
100 
101  while ((i < argc) && (strcmp(param, argv[i]) != 0))
102  i++;
103 
104  if (i < argc)
105  return(true);
106  else
107  return(false);
108 }
109 
110 // Position of a parameter in the command line =============================
111 int paremeterPosition(int argc, const char **argv, const char *param)
112 {
113  int i = 0;
114 
115  while ((i < argc) && (strcmp(param, argv[i])))
116  i++;
117 
118  if (i < argc - 1)
119  return i;
120  else
121  return -1;
122 }
123 
124 // Number of components ====================================================
125 int numComponents(const std::string &str)
126 {
127  int imax = str.length();
128  int retval = 0;
129  if (str[0] != '[' && str[imax-1] != ']')
130  return retval;
131  for (int i = 0; i < imax; i++)
132  if (str[i] == ',')
133  retval++;
134  return retval + 1;
135 }
136 
137 // Get float vector ========================================================
138 Matrix1D<double> getVectorParameter(int argc, const char **argv, const char *param, int dim)
139 {
140  Matrix1D<double> aux;
141  bool count_dimensionality = (dim == -1);
142 
143  // Find and form vector
144  int pos = paremeterPosition(argc, argv, param);
145  if (pos == -1 || (pos + 1 == argc))
146  {
147  if (count_dimensionality)
148  return aux;
149  else
151  }
152  pos++;
153  if (*(argv[pos]) != '[')
154  {
155  double d = textToFloat(argv[pos]);
156  aux.resize(1);
157  aux(0) = d;
158  return aux;
159  }
160 
161  std::string vector;
162  bool finished = false;
163  while (!finished)
164  {
165  vector += argv[pos];
166  if (vector[vector.length()-1] == ']')
167  finished = true;
168  if (++pos == argc && !finished)
170  }
171 
172  // Remove brackets
173  vector = vector.substr(1, vector.length() - 2);
174 
175  // Count dimensionality
176  int start_copy = 0, end_copy;
177  if (count_dimensionality)
178  {
179  dim = 0;
180  start_copy = 0;
181  do
182  {
183  end_copy = vector.find(',', start_copy);
184  if (end_copy == -1)
185  break;
186  start_copy = end_copy + 1;
187  dim++;
188  }
189  while (1);
190  dim++;
191  }
192 
193  // Read different vector elements
194  int i = 0;
195  start_copy = 0;
196  aux.resize(dim);
197  while (i < dim - 1)
198  {
199  // Find colon
200  end_copy = vector.find(',', start_copy);
201  // Store number
202  aux(i) = textToFloat(vector.substr(start_copy, end_copy));
203 
204  // Prepare for next iteration
205  i++;
206  start_copy = end_copy + 1;
207  }
208 
209  // Copy last element
210  aux(i) = textToFloat(vector.substr(start_copy, vector.length()));
211 
212  return aux;
213 }
214 
215 // Get vector param from file ==============================================
216 Matrix1D<double> getVectorParameter(FILE *fh, const char *param, int dim)
217 {
218  int argcp;
219  char **argvp = NULL;
220  char *copy = NULL;
221  Matrix1D<double> retval;
222  if (!generateCommandLine(fh, param, argcp, argvp, copy))
223  {
224  if (dim != -1)
226  }
227  else
228  {
229  retval = getVectorParameter(argcp, (const char **)argvp, ((std::string)"-" + param).c_str(), dim);
230  delete[] copy;
231  }
232  return retval;
233 }
234 
235 // Generate command line ===================================================
236 #define INSIDE_WORD 1
237 #define OUTSIDE_WORD 2
238 void generateCommandLine(const std::string &command_line, int &argcp,
239  char ** &argvp, char* &copy)
240 {
241  int L = command_line.length();
242 
243  // Some initialization
244  if (L == 0)
245  {
246  argcp = 0;
247  return;
248  }
249  if (command_line[0] == '\n')
250  {
251  argcp = 0;
252  return;
253  }
254 
255  // Check that argvp and copy are empty
256  if (argvp != NULL)
257  delete argvp;
258  if (copy != NULL)
259  delete[] copy;
260 
261  // Copy command line
262  copy = new char[L+1];
263  int i = 0;
264  while (i < L && command_line[i] != '\n')
265  {
266  copy[i] = command_line[i];
267  i++;
268  }
269  L = i;
270  copy[L] = '\0';
271 
272  // Now count how many different words are there
273  int words;
274  int state;
275  if (copy[0] == ' ')
276  {
277  state = OUTSIDE_WORD;
278  words = 0;
279  }
280  else
281  {
282  state = INSIDE_WORD;
283  words = 1;
284  }
285  i = 1;
286  while (i < L)
287  {
288  if (state == OUTSIDE_WORD && copy[i] != ' ')
289  {
290  state = INSIDE_WORD;
291  words++;
292  }
293  if (state == INSIDE_WORD && copy[i] == ' ')
294  state = OUTSIDE_WORD;
295  i++;
296  }
297 
298  // Resize argv and cut words
299  argvp = new char *[words+1];
300  argvp[0] = new char[6];
301  strcpy(argvp[0], "autom");
302  if (copy[0] == ' ')
303  {
304  state = OUTSIDE_WORD;
305  argcp = 1;
306  }
307  else
308  {
309  state = INSIDE_WORD;
310  argvp[1] = &(copy[0]);
311  argcp = 2;
312  }
313  i = 1;
314  while (i < L)
315  {
316  if (state == OUTSIDE_WORD && copy[i] != ' ')
317  {
318  state = INSIDE_WORD;
319  argvp[argcp] = &(copy[i]);
320  argcp++;
321  }
322  if (state == INSIDE_WORD && copy[i] == ' ')
323  {
324  state = OUTSIDE_WORD;
325  copy[i] = '\0';
326  }
327  i++;
328  }
329 }
330 
331 // Generate command line from file =========================================
332 bool generateCommandLine(FILE *fh, const char *param, int &argcp,
333  char ** &argvp, char* &copy)
334 {
335  long actual_pos = ftell(fh);
336  fseek(fh, 0, SEEK_SET);
337 
338  char line[201];
339  char *retval;
340  bool found = false;
341 
342  // Read lines
343  while (fgets(line, 200, fh) != NULL && !found)
344  {
345  if (line[0] == 0)
346  continue;
347  if (line[0] == '#')
348  continue;
349  if (line[0] == ';')
350  continue;
351  if (line[0] == '\n')
352  continue;
353 
354  int i = 0;
355  while (line[i] != 0 && line[i] != '=')
356  i++;
357  if (line[i] == '=')
358  {
359  line[i] = 0;
360  if (strcmp(line, param) == 0)
361  {
362  retval = line + i + 1;
363  found = true;
364  break;
365  }
366  }
367  }
368  fseek(fh, actual_pos, SEEK_SET);
369  if (!found)
370  return false;
371 
372  std::string artificial_line;
373  artificial_line = (std::string)"-" + param + " " + retval;
374  generateCommandLine(artificial_line, argcp, argvp, copy);
375  return true;
376 }
377 
378 // Get "parameter" from file ===============================================
379 std::string getParameter(FILE *fh, const char *param, int skip, const char *option)
380 {
381  long actual_pos = ftell(fh);
382  fseek(fh, 0, SEEK_SET);
383 
384  char line[201];
385  std::string retval;
386  bool found = false;
387  int skipped = 0;
388 
389  // Read lines
390  while (fgets(line, 200, fh) != NULL && !found)
391  {
392  if (line[0] == 0)
393  continue;
394  if (line[0] == '#')
395  continue;
396  if (line[0] == ';')
397  continue;
398  if (line[0] == '\n')
399  continue;
400 
401  int i = 0;
402  char *line_wo_spaces = line;
403  while (*line_wo_spaces == ' ' || *line_wo_spaces == '\t')
404  line_wo_spaces++;
405  while (line_wo_spaces[i] != 0 && line_wo_spaces[i] != '=')
406  i++;
407  if (line_wo_spaces[i] == '=')
408  {
409  line_wo_spaces[i] = 0;
410  if (strcmp(line_wo_spaces, param) == 0)
411  {
412  if (skipped == skip)
413  {
414  retval = line_wo_spaces + i + 1;
415  found = true;
416  break;
417  }
418  else
419  skipped++;
420  }
421  }
422  }
423  fseek(fh, actual_pos, SEEK_SET);
424  if (!found)
425  if (option == NULL)
427  else
428  return option;
429  else
430  return removeSpaces(retval);
431 }
432 
433 // Check "parameter" from file =============================================
434 bool checkParameter(FILE *fh, const char *param)
435 {
436  long actual_pos = ftell(fh);
437  fseek(fh, 0, SEEK_SET);
438 
439  char line[201];
440  bool found = false;
441  std::string retval;
442 
443  // Read lines
444  while (fgets(line, 200, fh) != NULL)
445  {
446  if (line[0] == 0)
447  continue;
448  if (line[0] == '#')
449  continue;
450  if (line[0] == '\n')
451  continue;
452 
453  int i = 0;
454  while (line[i] != 0 && line[i] != '=')
455  i++;
456  if (line[i] == '=')
457  {
458  line[i] = 0;
459  if (strcmp(line, param) == 0)
460  {
461  retval = line + i + 1;
462  found = true;
463  break;
464  }
465  }
466  }
467  fseek(fh, actual_pos, SEEK_SET);
468  return found && retval != "No" && retval != "NO" && retval != "no";
469 }
Matrix1D< double > getVectorParameter(int argc, const char **argv, const char *param, int dim)
Definition: args.cpp:138
Argument missing.
Definition: xmipp_error.h:114
bool checkParameter(int argc, const char **argv, const char *param)
Definition: args.cpp:97
String removeSpaces(const String &_str)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
int paremeterPosition(int argc, const char **argv, const char *param)
Definition: args.cpp:111
#define i
doublereal * d
int numComponents(const std::string &str)
Definition: args.cpp:125
double v1
const char * getParameter(int argc, const char **argv, const char *param, const char *option)
Definition: args.cpp:30
#define INSIDE_WORD
Definition: args.cpp:236
float textToFloat(const char *str)
Incorrect argument received.
Definition: xmipp_error.h:113
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
bool getThreeDoubleParams(int argc, const char **argv, const char *param, double &v1, double &v2, double &v3, double v1_def, double v2_def, double v3_def)
Definition: args.cpp:70
#define OUTSIDE_WORD
Definition: args.cpp:237
struct _parameter * param
bool getTwoDoubleParams(int argc, const char **argv, const char *param, double &v1, double &v2, double v1_def, double v2_def)
Definition: args.cpp:46
void generateCommandLine(const std::string &command_line, int &argcp, char **&argvp, char *&copy)
Definition: args.cpp:238