JWS C Library
C language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00055 #ifndef OPTION_H 00056 #define OPTION_H 00057 00058 00059 #include <jwsc/config.h> 00060 00061 #include <stdlib.h> 00062 #include <stdio.h> 00063 00064 #include "jwsc/base/error.h" 00065 00066 00067 #ifdef __cplusplus 00068 namespace jwsc { 00069 extern "C" { 00070 #endif 00071 00072 00074 typedef const char* Option_string; 00075 00077 typedef char Option_flag; 00078 00080 typedef const char* Option_desc; 00081 00083 typedef const char* Option_arg; 00084 00086 typedef Error* (*Option_func_with_arg)(Option_arg); 00087 00089 typedef Error* (*Option_func_no_arg)(void); 00090 00091 00117 typedef struct 00118 { 00124 Option_string string; 00125 00131 Option_flag flag; 00132 00138 Option_desc desc; 00139 00141 Option_func_with_arg func; 00142 } 00143 Option_with_arg; 00144 00145 00166 typedef struct 00167 { 00173 Option_string string; 00174 00180 Option_flag flag; 00181 00187 Option_desc desc; 00188 00190 Option_func_no_arg func; 00191 } 00192 Option_no_arg; 00193 00194 00198 void init_option_with_arg 00199 ( 00200 Option_with_arg* opt, 00201 Option_string str, 00202 Option_flag flag, 00203 Option_desc desc, 00204 Option_func_with_arg func 00205 ); 00206 00207 00211 void init_option_no_arg 00212 ( 00213 Option_no_arg* opt, 00214 Option_string str, 00215 Option_flag flag, 00216 Option_desc desc, 00217 Option_func_no_arg func 00218 ); 00219 00220 00224 Error* process_option_with_arg 00225 ( 00226 int argc, 00227 const char** argv, 00228 int* argi, 00229 Option_with_arg* opt 00230 ); 00231 00232 00236 Error* process_option_no_arg 00237 ( 00238 int argc, 00239 const char** argv, 00240 int* argi, 00241 Option_no_arg* opt 00242 ); 00243 00244 00249 Error* process_options 00250 ( 00251 int argc, 00252 const char** argv, 00253 int* argi, 00254 int num_opts_no_arg, 00255 Option_no_arg* opts_no_arg, 00256 int num_opts_with_arg, 00257 Option_with_arg* opts_with_arg 00258 ); 00259 00260 00265 Error* process_options_from_file 00266 ( 00267 const char* fname, 00268 int num_opts_no_arg, 00269 Option_no_arg* opts_no_arg, 00270 int num_opts_with_arg, 00271 Option_with_arg* opts_with_arg 00272 ); 00273 00274 00278 void print_option_with_arg 00279 ( 00280 FILE* fp, 00281 int opt_width, 00282 Option_with_arg* opt 00283 ); 00284 00285 00289 void print_option_no_arg 00290 ( 00291 FILE* fp, 00292 int opt_width, 00293 Option_no_arg* opt 00294 ); 00295 00296 00300 void print_options 00301 ( 00302 FILE* fp, 00303 int opt_width, 00304 int num_opts_no_arg, 00305 Option_no_arg* opts_no_arg, 00306 int num_opts_with_arg, 00307 Option_with_arg* opts_with_arg 00308 ); 00309 00310 00311 #ifdef __cplusplus 00312 } 00313 } 00314 #endif 00315 00316 00317 #endif