Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

arguments.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- 00002 @COPYRIGHT : 00003 Copyright 1993,1994,1995 David MacDonald, 00004 McConnell Brain Imaging Centre, 00005 Montreal Neurological Institute, McGill University. 00006 Permission to use, copy, modify, and distribute this 00007 software and its documentation for any purpose and without 00008 fee is hereby granted, provided that the above copyright 00009 notice appear in all copies. The author and McGill University 00010 make no representations about the suitability of this 00011 software for any purpose. It is provided "as is" without 00012 express or implied warranty. 00013 ---------------------------------------------------------------------------- */ 00014 00015 #include <volume_io/internal_volume_io.h> 00016 #include <bicpl/prog_utils.h> 00017 00018 #ifndef lint 00019 static char rcsid[] = "$Header: /software/source//libraries/bicpl/Prog_utils/arguments.c,v 1.13 2000/02/06 15:30:47 stever Exp $"; 00020 #endif 00021 00022 typedef struct 00023 { 00024 int argc; 00025 char **argv; 00026 int current_arg; 00027 int current_offset_within_arg; 00028 } arguments_struct; 00029 00030 private arguments_struct arguments; 00031 00032 /* ----------------------------- MNI Header ----------------------------------- 00033 @NAME : arguments_remaining 00034 @INPUT : 00035 @OUTPUT : 00036 @RETURNS : TRUE if some arguments remain to be parsed 00037 @DESCRIPTION: Checks if all arguments have been parsed. 00038 @METHOD : 00039 @GLOBALS : 00040 @CALLS : 00041 @CREATED : David MacDonald 00042 @MODIFIED : 00043 ---------------------------------------------------------------------------- */ 00044 00045 private BOOLEAN arguments_remaining( void ) 00046 { 00047 return( arguments.current_arg < arguments.argc ); 00048 } 00049 00050 public int get_n_arguments_remaining( void ) 00051 { 00052 return( arguments.argc - arguments.current_arg ); 00053 } 00054 00055 /* ----------------------------- MNI Header ----------------------------------- 00056 @NAME : get_current_argument_string 00057 @INPUT : 00058 @OUTPUT : 00059 @RETURNS : a pointer to the argument string. 00060 @DESCRIPTION: Returns a point to an argument string. This may be one 00061 : of the argv[i] parameters, or may point within one of these, 00062 : for instance, if the -n part of -nfilename has been parsed. 00063 @METHOD : 00064 @GLOBALS : 00065 @CALLS : 00066 @CREATED : David MacDonald 00067 @MODIFIED : 00068 ---------------------------------------------------------------------------- */ 00069 00070 private STRING get_current_argument_string( void ) 00071 { 00072 STRING arg; 00073 00074 arg = arguments.argv[arguments.current_arg]; 00075 return( &arg[arguments.current_offset_within_arg] ); 00076 } 00077 00078 /* ----------------------------- MNI Header ----------------------------------- 00079 @NAME : advance_argument 00080 @INPUT : 00081 @OUTPUT : 00082 @RETURNS : 00083 @DESCRIPTION: Advances the current arg pointer to the next argument. 00084 @METHOD : 00085 @GLOBALS : 00086 @CALLS : 00087 @CREATED : David MacDonald 00088 @MODIFIED : 00089 ---------------------------------------------------------------------------- */ 00090 00091 private void advance_argument( void ) 00092 { 00093 ++arguments.current_arg; 00094 arguments.current_offset_within_arg = 0; 00095 } 00096 00097 /* ----------------------------- MNI Header ----------------------------------- 00098 @NAME : initialize_argument_processing 00099 @INPUT : argc 00100 : argv 00101 @OUTPUT : 00102 @RETURNS : 00103 @DESCRIPTION: Records the arguments from the main program, and initializes 00104 : parsing to the first argument. 00105 @METHOD : 00106 @GLOBALS : 00107 @CALLS : 00108 @CREATED : David MacDonald 00109 @MODIFIED : 00110 ---------------------------------------------------------------------------- */ 00111 00112 public void initialize_argument_processing( 00113 int argc, 00114 char *argv[] ) 00115 { 00116 arguments.argc = argc; 00117 arguments.argv = argv; 00118 arguments.current_arg = 1; 00119 arguments.current_offset_within_arg = 0; 00120 } 00121 00122 /* ----------------------------- MNI Header ----------------------------------- 00123 @NAME : get_int_argument 00124 @INPUT : default_value 00125 @OUTPUT : value 00126 @RETURNS : TRUE if next argument exists and is an integer 00127 @DESCRIPTION: Checks if arguments remain, and if the next one is an integer, 00128 : stores it in "value", and advances the current pointer to the 00129 : next argument. Otherwise, value is assigned the default_value. 00130 @METHOD : 00131 @GLOBALS : 00132 @CALLS : 00133 @CREATED : David MacDonald 00134 @MODIFIED : 00135 ---------------------------------------------------------------------------- */ 00136 00137 public BOOLEAN get_int_argument( 00138 int default_value, 00139 int *value ) 00140 { 00141 BOOLEAN found; 00142 00143 if( arguments_remaining() && 00144 sscanf( get_current_argument_string(), "%d", value ) == 1 ) 00145 { 00146 advance_argument(); 00147 found = TRUE; 00148 } 00149 else 00150 { 00151 *value = default_value; 00152 found = FALSE; 00153 } 00154 00155 return( found ); 00156 } 00157 00158 /* ----------------------------- MNI Header ----------------------------------- 00159 @NAME : get_real_argument 00160 @INPUT : default_value 00161 @OUTPUT : value 00162 @RETURNS : TRUE if next argument exists and is a real 00163 @DESCRIPTION: Checks if arguments remain, and if the next one is a real, 00164 : stores it in "value", and advances the current pointer to the 00165 : next argument. Otherwise, value is assigned the default_value. 00166 @METHOD : 00167 @GLOBALS : 00168 @CALLS : 00169 @CREATED : David MacDonald 00170 @MODIFIED : 00171 ---------------------------------------------------------------------------- */ 00172 00173 public BOOLEAN get_real_argument( 00174 Real default_value, 00175 Real *value ) 00176 { 00177 BOOLEAN found; 00178 00179 if( arguments_remaining() && 00180 ((real_is_double() && 00181 sscanf( get_current_argument_string(), "%lf", (double *) value ) == 1) || 00182 (!real_is_double() && 00183 sscanf( get_current_argument_string(), "%f", (float *) value ) == 1)) 00184 ) 00185 { 00186 advance_argument(); 00187 found = TRUE; 00188 } 00189 else 00190 { 00191 *value = default_value; 00192 found = FALSE; 00193 } 00194 00195 return( found ); 00196 } 00197 00198 /* ----------------------------- MNI Header ----------------------------------- 00199 @NAME : get_string_argument 00200 @INPUT : default_value 00201 @OUTPUT : value 00202 @RETURNS : TRUE if next argument exists 00203 @DESCRIPTION: Checks if arguments remain, stores the pointer to the next one 00204 : in "value", and advances the current pointer to the 00205 : next argument. Otherwise, value is assigned the default_value. 00206 @METHOD : 00207 @GLOBALS : 00208 @CALLS : 00209 @CREATED : David MacDonald 00210 @MODIFIED : 00211 ---------------------------------------------------------------------------- */ 00212 00213 public BOOLEAN get_string_argument( 00214 STRING default_value, 00215 STRING *value ) 00216 { 00217 BOOLEAN found; 00218 00219 if( arguments_remaining() ) 00220 { 00221 *value = get_current_argument_string(); 00222 advance_argument(); 00223 found = TRUE; 00224 } 00225 else 00226 { 00227 *value = default_value; 00228 found = FALSE; 00229 } 00230 00231 return( found ); 00232 } 00233 00234 /* ----------------------------- MNI Header ----------------------------------- 00235 @NAME : get_prefix_argument 00236 @INPUT : prefix - string to look for, e.g., "-n" 00237 @OUTPUT : 00238 @RETURNS : TRUE if next argument begins with the prefix 00239 @DESCRIPTION: Checks if arguments remain, and if the next one starts with 00240 : the prefix, advances the current pointer to just after the 00241 : prefix, which may be within the same argument. 00242 @METHOD : 00243 @GLOBALS : 00244 @CALLS : 00245 @CREATED : David MacDonald 00246 @MODIFIED : 00247 ---------------------------------------------------------------------------- */ 00248 00249 public BOOLEAN get_prefix_argument( 00250 STRING prefix ) 00251 { 00252 char *next_str; 00253 BOOLEAN found; 00254 00255 if( arguments_remaining() && 00256 string_length(get_current_argument_string()) >= string_length(prefix) && 00257 strncmp( get_current_argument_string(), prefix, 00258 (size_t) string_length(prefix) ) == 0 ) 00259 { 00260 arguments.current_offset_within_arg += string_length(prefix); 00261 00262 next_str = get_current_argument_string(); 00263 while( *next_str == ' ' || *next_str == '\t' ) 00264 ++next_str; 00265 00266 if( *next_str == END_OF_STRING ) 00267 advance_argument(); 00268 00269 found = TRUE; 00270 } 00271 else 00272 { 00273 found = FALSE; 00274 } 00275 00276 return( found ); 00277 }

Generated on Wed Jul 28 09:10:56 2004 for BICPL by doxygen 1.3.7