properties_base.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 The FLWOR Foundation.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef ZORBA_PROPERTIES_BASE_H
00017 #define ZORBA_PROPERTIES_BASE_H
00018 
00019 #include <string>
00020 #include <sstream>
00021 #include <iostream>
00022 #include <vector>
00023 #include <cctype>
00024 #include <zorba/config.h>
00025 
00026 namespace zorba {
00027 
00028 class PropertiesBase {
00029 protected:
00030   std::vector<std::string > __thePositionalArgs;
00031 
00032 public:
00033   virtual ~PropertiesBase () {}
00034 
00035   virtual const char **get_all_options () const = 0;
00036   virtual std::string load_argv (int argc, const char **argv) = 0;
00037   virtual std::string check_args () { return ""; }
00038 
00039   virtual std::string load_all (const char *cfgname, const std::string &env_pfx, int argc, const char **argv) {
00040     std::string result;
00041     if (! (result = load_env (env_pfx)).empty ())
00042       return result;
00043     if (! (result = load_file (cfgname)).empty ())
00044       return result;
00045     return load_argv (argc, argv);
00046   }
00047 
00048   virtual std::string load_env (const std::string &env_pfx, const char **options);
00049   virtual std::string load_env (const std::string &env_pfx) { return load_env (env_pfx, get_all_options ()); }
00050   std::string load_file (const char *fname);
00051 
00052   const std::vector<std::string > &getPositionalArgs () const { return __thePositionalArgs; }
00053 
00054   void copy_args (const char **argv) {
00055     for (; *argv != NULL; ++argv) {
00056       __thePositionalArgs.push_back (*argv);
00057     }
00058   }
00059 
00060   template<class T> void init_val (const char *str, T &val, unsigned delta = 0) {
00061     std::istringstream is (str + delta);
00062     is >> val;
00063   }
00064 
00065 };
00066 
00067 template<> void PropertiesBase::init_val (const char *str, std::string &val, unsigned delta);
00068 template<> void PropertiesBase::init_val (const char *str, std::vector<std::string> &val, unsigned delta);
00069 
00070 }
00071 
00072 #endif  // ZORBA_PROPERTIES_BASE_H
00073 /*
00074  * Local variables:
00075  * mode: c++
00076  * End:
00077  */