Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

getparam.C

Go to the documentation of this file.
00001 //
00002 // this is a small library of classless access functions to the
00003 // command line interface. The Program class uses a static
00004 // pointer to itself to find itself back here! 
00005 // Each routine MUST call init_main() to double check if Program
00006 // has been loaded.
00007 //
00008 // The alternative is to define your own main() and instantiate a
00009 // Program and do all the work. This set of routines is merely provided
00010 // for those 99% of the programs which suffice with the simple default
00011 // fixed set of program parameters and don't need any dynamic behavior.
00012 // (Note: we have not quite defined how dynamic we allow the CLUI to be)
00013 //
00014 // History:
00015 //      summer 1996     Created in Amsterdam            Peter Teuben
00016 //      10-apr-97       minor doc fixes                 PJT
00017 //      5-jun-97        fixed up Error/Debug/Dprintf    PJT
00018 //
00019 // Todo:
00020 //      - expression parser for getrparam, getiparam
00021 //
00022 #include "program.h"
00023 #include "stdlib.h"
00024 #include "stdarg.h"
00025 
00026 static Program dummy, *main = 0;     // Program
00027 
00028 static void init_main(void) {        // check if everything initialized is here
00029     if (main == 0) {
00030         main = dummy.main();
00031         if (main == 0) 
00032             Error("getparam.C: main could not be initialized");
00033     }
00034 }
00035 
00036 int hasvalue(string key) 
00037 {
00038     init_main();
00039     string val = main->get(key);
00040     if (val == 0 || *val == 0) return FALSE;
00041     return TRUE;
00042 }
00043 
00044 int isdefault(string key)
00045 {
00046     init_main();
00047     return main->count(key);
00048 }
00049     
00050 int getflag(string key) 
00051 {
00052     init_main();
00053     char *sp = main->get(key);
00054     if (strchr("tTyY1",*sp)) return 1;
00055     if (strchr("fFnN0",*sp)) return 0;
00056     Error("Syntax error for flag %s=%s",key,sp);
00057     return 0;
00058 }
00059 
00060 string getparam(string key)
00061 {
00062     init_main();
00063     return main->get(key);
00064 }
00065 
00066 int getiparam(string key)
00067 {
00068     init_main();
00069     // Note: no syntax checking yet
00070     return atoi(main->get(key));
00071 }
00072 
00073 real getrparam(string key)
00074 {
00075     init_main();
00076     // Note: no syntax checking yet
00077     return atof(main->get(key));
00078 }
00079 
00080 int get_argc(void)
00081 {
00082     init_main();
00083     return main->get_argc();
00084 }
00085 
00086 char **get_argv(void)
00087 {
00088     init_main();
00089     return main->get_argv();
00090 }
00091 
00092 //  using printf-style output 
00093 
00094 void Error(char *fmt ...)
00095 {
00096     va_list ap;
00097 
00098     init_main();
00099 
00100     fprintf(stderr,"### Fatal error [%s]: ",main->get("argv0"));  
00101     
00102     va_start(ap, fmt);              // ap starts with string 'fmt'
00103     vfprintf(stderr, fmt, ap);      // print out on stderr
00104 
00105     if (fmt[strlen(fmt)-1] != '\n') // be nice if no newline supplied
00106         fprintf(stderr,"\n");       // and print it anyhow
00107     fflush(stderr);                 // flush it NOW 
00108     va_end(ap);                     // end varargs 
00109 
00110 
00111     int e = main->dec_error();
00112     if (e > 0) return;
00113    
00114     if (main->get_debug()>5) abort(); // produce coredump if requested
00115       exit(-1);                       // close the shop and pass this to parent
00116 }
00117 
00118 
00119 void Warning(char *fmt ...)
00120 {
00121     va_list ap;
00122 
00123     init_main();
00124 
00125     fprintf(stderr,"### Warning [%s]: ",main->get("argv0"));  
00126     
00127     va_start(ap, fmt);              // ap starts with string 'fmt'
00128     vfprintf(stderr, fmt, ap);      // print out on stderr
00129     if (fmt[strlen(fmt)-1] != '\n') // be nice if no newline supplied
00130         fprintf(stderr,"\n");       // and print it anyhow
00131     fflush(stderr);                 // flush it NOW 
00132 
00133 #if 0
00134     //  using strings only, not very useful
00135     for (;;) {
00136         char *p = va_arg(ap, char *);
00137         if (p == 0) break;
00138         cerr << p ;
00139     }
00140     cerr << endl;
00141 #endif
00142     va_end(ap);                     // end varargs 
00143 
00144 }
00145 
00146 void Dprintf(int level, char *fmt ...)
00147 {
00148     va_list ap;
00149 
00150     init_main();
00151 
00152     if (level > main->get_debug()) return;
00153 
00154     va_start(ap, fmt);              // ap starts with string 'fmt'
00155     vfprintf(stderr, fmt, ap);      // print out on stderr
00156     fflush(stderr);                 // flush it NOW 
00157     va_end(ap);                     // end varargs 
00158 
00159 }
00160 
00161 
00162 
00163 

Generated at Sun Feb 24 09:57:01 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001