"Fossies" - the Fresh Open Source Software Archive

Member "qdiff-0.9.1/tappconfig.h" (21 Oct 2008, 8892 Bytes) of package /linux/privat/old/qdiff-0.9.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*GPL*START*
    2  *
    3  * tappconfig.h - console application framework header file
    4  * 
    5  * Copyright (C) 1998 by Johannes Overmann <Johannes.Overmann@gmx.de>
    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., 675 Mass Ave, Cambridge, MA 02139, USA.
   20  * *GPL*END*/  
   21 
   22 #ifndef _ngw_tappconfig_h_
   23 #define _ngw_tappconfig_h_
   24 
   25 #include "tmap.h"
   26 #include "tvector.h"
   27 #include "tstring.h"
   28 
   29 
   30 // global application pointer
   31 class TAppConfig;
   32 extern TAppConfig *tApp;
   33 
   34 // should be a private subclass of TAppConfig
   35 class TAppConfigItem {
   36  public:
   37    // types
   38 enum TACO_TYPE {TACO_TYPE_NONE, STRING, INT, DOUBLE, BOOL, SWITCH};
   39 enum TACO_STRING_MODE {OVERRIDE, APPEND, ONCE};
   40 enum TACO_SET_IN {NEVER, DEFAULT, COMMAND_LINE, RC_FILE, ENVIRONMENT, APPLICATION};
   41 
   42    // cons
   43    TAppConfigItem();
   44    TAppConfigItem(const char *confitem, const char *context, bool privat);
   45    
   46    // interface
   47    void printItemToFile(FILE *f) const; // print default
   48    void printCurItemToFile(FILE *f, bool simple) const; // print current
   49    void printValue(const tstring& env, const tstring& rcfile) const;
   50    void printHelp(int maxoptlen, bool globalonlycl) const;
   51    int getOptLen() const;
   52    tstring getTypeStr() const;
   53    void setValue(const tstring& par, TACO_SET_IN setin, 
   54          bool verbose=false, const tstring& env="", 
   55          const tstring& rcfile="",
   56          const tstring& context="command line");
   57    
   58    // set from application methods:
   59    
   60    // returns true if value is valid, else false
   61    // sets value from string according to any type (switch == bool here)
   62    bool setValueFromAppFromStr(const tstring& parameter);
   63    void setValueFromApp(const tstring& str);
   64    // returns true if value is valid, else false
   65    bool setValueFromApp(double d);
   66    // returns true if value is valid, else false
   67    bool setValueFromApp(int i);
   68    void setValueFromApp(bool b);
   69    
   70 
   71 
   72 
   73  private: 
   74    // private methods
   75    void setComp(const tvector<tstring>& a, bool privat);
   76    void validate(const char *context);
   77    void privateInit();
   78    tstring getParamStr() const;
   79    tstring getWasSetStr(const tstring& env, const tstring& rcfile) const;
   80    tstring getWasSetStr(TACO_SET_IN setin, const tstring& env, const tstring& rcfile) const;
   81    tstring getFlagsStr(const tstring& optprefix, bool globalonlycl) const;
   82    
   83 
   84  public:   
   85    // data
   86    bool must_have;
   87    bool should_have;
   88    bool only_cl;
   89    bool configopt;
   90    bool only_app;
   91    bool save;
   92    bool optional_param;
   93    bool hide;
   94    
   95    TACO_TYPE type;
   96    TACO_SET_IN set_in;
   97    TACO_STRING_MODE string_mode;
   98    tstring string_sep;
   99    double double_value, double_upper, double_lower, double_default;
  100    int int_value, int_upper, int_lower, int_default;
  101    bool bool_value, bool_default;
  102    
  103    // temp flag
  104    bool printed;
  105 
  106    tstring name;
  107    tstring context;
  108    tstring help;
  109    tstring headline;
  110    tstring char_name;
  111    tstring par;
  112    tvector<tstring> alias;
  113    tstring type_str;
  114    tstring upper, lower, def;
  115    tstring string_value, string_default;
  116 };
  117 
  118 
  119 // this is the main class of this h file
  120 class TAppConfig {
  121  public:
  122    // ctor & dtor
  123    TAppConfig(const char *conflist[], const char *listname,
  124           int argc, char *av[],
  125           const char *envstrname,
  126           const char *rcname,
  127           const tstring& version);
  128    ~TAppConfig();
  129       
  130    // main interface
  131    void printHelp(bool show_hidden = false) const;
  132    void printValues() const;
  133    bool save(tstring *rc_name_out = 0); // save items with item.save==true
  134    
  135    // typed options:
  136    const tstring& getString(const tstring& par) const;
  137    double getDouble(const tstring& par) const;
  138    int getInt(const tstring& par) const;
  139    bool getBool(const tstring& par) const;        // bool + switch
  140    bool getSwitch(const tstring& par) const;      // bool + switch
  141    bool operator() (const tstring& par) const; // bool + switch
  142    
  143    // untyped parameters:
  144    tstring param(int i) const { return _params[i]; }
  145    size_t numParam() const { return _params.size(); }
  146    const tvector<tstring>& params() const { return _params; }
  147    
  148    // set values: 
  149    // returns true if value is valid, else false
  150    // sets value from string according to any type (switch == bool here)
  151    bool setValueFromStr(const tstring &n, const tstring& str);
  152    void setValue(const tstring &n, const tstring& str);
  153    bool setValue(const tstring &n, double d);
  154    bool setValue(const tstring &n, int i);
  155    void setValue(const tstring &n, bool b);
  156 
  157    // return the upper and lower bounds and defaults for the type
  158    int intUpper(const tstring &n) const;
  159    int intLower(const tstring &n) const;
  160    int intDefault(const tstring &n) const;
  161    double doubleUpper(const tstring &n) const;
  162    double doubleLower(const tstring &n) const;
  163    double doubleDefault(const tstring &n) const;
  164    tstring stringDefault(const tstring &n) const;
  165    bool   boolDefault(const tstring &n) const;
  166 
  167    // return location where parameter was set
  168    TAppConfigItem::TACO_SET_IN wasSetIn(const tstring& n) const;
  169    bool wasSetByUser(const tstring& n) const;
  170    
  171  private:
  172    // readonly public data
  173    tvector<tstring> _params;
  174    
  175    // private data
  176    tmap<tstring,int> name;  // get index of long name
  177    int char2index[256];           // get index of short name
  178    tvector<TAppConfigItem> opt;    // all options in the order of conflist
  179    tmap<tstring,int> alias; // aliases for options
  180    tstring envname;    // name of env var
  181    tstring rc_name;    // name of loaded rc file
  182    tstring rc_str;     // namestr of rc file
  183    bool verbose_conf; // verbose configuration: warnings and values
  184    bool onlycl;       // true== dont use rcfile and envvar, only command line
  185    bool stopatdd;     // stop option scanning after --
  186    bool removedd;     // remove first -- param
  187    bool ignore_negnum;// something like -20 or -.57 is not trated as options
  188    tstring usage;      // usage string: printed before help
  189    tstring trailer;    // trailer string: printed after help
  190    tstring commonhead; // headline for common options
  191    
  192    int getMaxOptLen(bool show_hidden) const;
  193    void doMetaChar(const tstring& str, const tstring& context);
  194    void setComp(const tvector<tstring>& a, const tstring& context);
  195    void addConfigItems(const char **list, const char *listname, bool privat);
  196    void doCommandLine(int ac, char *av[], const tstring& version);
  197    void doEnvironVar(const char *envvar);
  198    void doRCFile(const tstring& rcfile, const tstring& clrcfile);
  199    void setFromStr(const tstring& str, const tstring& context, TAppConfigItem::TACO_SET_IN setin);
  200    tstring getName(const tstring& str, const tstring& context, const tstring& optprefix="", bool errorIfNotFound = true) const;
  201    void createRCFile(const tstring& fname, const tstring& rcname) const;
  202 };
  203 
  204 
  205 
  206 // global helper functions neede by tappconfig and other applications
  207 
  208 // file tools
  209 
  210 // return true if file is a directory
  211 bool fisdir(const char *fname);
  212 
  213 // return true if file is a regular file
  214 bool fisregular(const char *fname);
  215 
  216 // return true if file is a symbolic link
  217 bool fissymlink(const char *fname);
  218 
  219 // return false if nothing with this name exists
  220 bool fexists(const char *fname);
  221 
  222 // return length of file or -1 if file does not exist
  223 off_t flen(const char *fname);
  224 off_t flen(int fdes);
  225 off_t flen(FILE *file);
  226 
  227 
  228 // errors
  229 
  230 void userWarning(const char *message, ...)
  231 #ifdef __GNUC__
  232   __attribute__ ((format(printf,1,2)))
  233 #endif
  234     ;
  235 void userError(const char *message, ...) 
  236 #ifdef __GNUC__
  237   __attribute__ ((noreturn,format(printf,1,2)))
  238 #endif
  239     ;
  240 int setUserErrorExitStatus(int status);
  241 
  242 // checkpoint macros
  243 
  244 //#define CHECKPOINTS
  245 #define KEYWORD_CHECKPOINTS
  246 
  247 #ifdef CHECKPOINTS
  248 
  249 # ifdef __GNUC__
  250 #  define CP() addCheckpoint(__FILE__,__LINE__,__PRETTY_FUNCTION__)
  251 # else
  252 #  define CP() addCheckpoint(__FILE__,__LINE__)
  253 # endif
  254 int addCheckpoint(const char *file, int line, const char *func = 0);
  255 
  256 // keyword checkpoint
  257 # ifdef KEYWORD_CHECKPOINTS
  258 #  define for if(CP())for
  259 // #define while if(0);else while
  260 #  define do if(CP())do
  261 // #define if if(0);else if
  262 #  define else else if(CP())
  263 #  define switch if(CP())switch
  264 #  define break if(CP())break
  265 #  define return if(CP())return
  266 # endif
  267 
  268 // internal checkpoint data
  269 extern const char *checkpoint_file[];
  270 extern const char *checkpoint_func[];
  271 extern int checkpoint_line[];
  272 
  273 #endif
  274 
  275 
  276 #endif /* tappconfig.h */