TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
read_command_line.c
Go to the documentation of this file.
1 
2 /* KeyPalette MANAGES THE I/O FILES OF A MODEL
3 KeyPalette Version 0.9375 KMackenzie
4 
5 file read_command_line.c
6 
7 Copyright, 2009 Emanuele Cordano and Riccardo Rigon
8 
9 This file is part of KeyPalette.
10  KeyPalette is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  KeyPalette is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 
25 
26 //#include <cstdio>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "read_command_line.h"
31 
32 #define SUCCESS 1
33 #define NO_SUCCESS 0
34 
35 
36 
37 
38 char *read_option_string(int argc,char *argv[], char *option_f,char *no_option_argument,short print)
39 {
40  /*
41  * \author Emanuele Cordano
42  * \date July 2008
43  *
44  *\param (int) - argc
45  *\param (char *) - argv
46  *\param (char *) - option_f - a string related to the flag which can appear in the command
47  *\param (char *) - no_option_f - a string returned by the routine in case of MISSING option_field
48  *\param (short) - print
49  *
50  *\return Returns the string followed by the string option_f in the command line
51  *
52  */
53  int i,s;
54 
55 
56 
57  s=NO_SUCCESS;
58 
59  for (i = 1; i < argc; i++) /* Skip argv[0] (program name). */
60  {
61  /*
62  * Use the 'strcmp' function to compare the argv values
63  * to a string of your choice (here, it's the optional
64  * argument "-q"). When strcmp returns 0, it means that the
65  * two strings are identical.
66  */
67 
68  if (!strcmp(argv[i],option_f)) {
69  if (print==1) printf(" \n READ ARGUMENT of %s : %s \n",argv[i],argv[i+1]);
70  return argv[i+1];
71  }
72  }
73  /* warning message */
74  if (print==1) printf("\nWARNING: Option %s is set to default value %s",option_f,no_option_argument);
75 
76 
77 
78 
79  return no_option_argument;
80 }
81 
82 
83 double read_option_double(int argc,char *argv[], char *option_f, char *no_option_argument, double default_value,short print)
84 {
85  /*
86  * \author Emanuele Cordano
87  * \date July 2008
88  *
89  *\param (int) - argc
90  *\param (char *) - argv
91  *\param (char *) - option_f - a string related to the flag which can appear in the command
92  *\param (char *) - no_option_f - a string returned by the routine in case of MISSING option_field
93  *\param (double)- default_value - the DOUBKLE value applied if option_f is missing
94  *\param (short) - print
95  *
96  *\return Return the double value followed by the the string option_f in the command line
97  *
98  */
99  int s;
100  double d;
101  char *argument;
102 
103 
104  argument=read_option_string(argc,argv,option_f,no_option_argument,print);
105 
106  s=sscanf(argument,"%lf",&d);
107  if (s==0 && print==1) {
108  printf("\nWARNING: No real value found for %s option, the default value %lf is set ",option_f,default_value);
109  free(argument);
110  return default_value;
111  }
112 // d=default_value;
113  free(argument);
114  return d;
115 }
116 
117 int read_flag(int argc,char *argv[],char *flag,short print)
118 /*
119  * \author Emanuele Cordano
120  * \date July 2008
121  *
122  */
123 {
124  int s,i;
125  s=NO_SUCCESS;
126  for (i = 1; i < argc; i++) /* Skip argv[0] (program name). */
127  {
128  /*
129  * Use the 'strcmp' function to compare the argv values
130  * to a string of your choice (here, it's the optional
131  * argument "-q"). When strcmp returns 0, it means that the
132  * two strings are identical.
133  */
134 
135  if (strcmp(argv[i],flag) == 0) s=SUCCESS;
136 
137  }
138  if (print==1) {
139  if (s==NO_SUCCESS) printf("\nWARNING: flag %s is missing and not activated \n ",flag);
140  if (s==SUCCESS) printf("\nWARNING: flag %s is activated \n ",flag);
141 
142  }
143  return s;
144 }
145 
146 
147 
148 
149 
150 
151 
152 
153 
154