hk_utilities.h

00001 // ****************************************************************************
00002 // copyright (c) 2000-2005 Horst Knorr <hk_classes@knoda.org>  
00003 // This file is part of the hk_classes library.
00004 // This file may be distributed and/or modified under the terms of the
00005 // GNU Library Public License version 2 as published by the Free Software
00006 // Foundation and appearing in the file COPYING included in the
00007 // packaging of this file.
00008 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00009 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00010 // ****************************************************************************
00011 #ifndef HK_UTILITIES
00012 #define HK_UTILITIES
00013 #ifdef HAVE_ARGP
00014 #include <argp.h>
00015 #endif
00016 #include "hk_drivermanager.h"
00017 #include "hk_connection.h"
00018 #include "hk_database.h"
00019 #include "hk_string.h"
00020 #include "config.h"
00021 #include <cassert>
00022 #include <unistd.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 
00026 #define DEFAULTPARAMETER {"driver",'d',"DRIVERNAME",0,"Name of the database driver",0},{"list",'l',0,1,"List available database drivers",0},{"host",'h',"HOST",0,"IP-number or name of hosts",0},{"port",'r',"PORT",0,"TCP-port on host",0},{"user",'u',"USERNAME",0,"user name",0},{"password",'p',"PASSWORD",0,"user password",0},{"database",'b',"DATABASE",0,"database name",0},{"version",10,0,1,"version and author of hk_classes",0}
00027 
00033 class argumentclass
00034 {
00035     public:
00036         hk_string driver,host,port,user,password,database,filter;
00037 };
00038 #ifdef HAVE_ARGP
00039 
00042 inline static  error_t parse_generaloptions (int key,char* arg, struct argp_state* state)
00043 {
00044     struct argumentclass* arguments=(argumentclass*)state->input;
00045     assert(arguments);
00046     switch (key)
00047     {
00048 
00049         case 10     :                             //version
00050             cout <<"Version "<<VERSION<<"\nAuthor: Horst Knorr <hk_classes@knoda.org>\nhttp://hk-classes.sourceforge.net"<<endl;
00051             exit(0);
00052             break;
00053         case 'l'        :
00054         {
00055             vector<hk_string>* l = hk_drivermanager::driverlist();
00056             vector<hk_string>::iterator it=l->begin();
00057             while (it!=l->end())
00058             {
00059                 cout <<*it<<endl;
00060                 ++it;
00061             }
00062             exit(0);
00063         }
00064 
00065         break;
00066         case 'd'    : arguments->driver=arg;
00067         break;
00068         case 'h'    : arguments->host=arg;
00069         break;
00070         case 'r'    : arguments->port=arg;
00071         break;
00072         case 'u'    : arguments->user=arg;
00073         break;
00074         case 'p'    : arguments->password=arg;
00075         break;
00076         case 'f'    : arguments->filter=arg;
00077         break;
00078         case 'b'    : arguments->database=arg;
00079         break;
00080         default :  return ARGP_ERR_UNKNOWN;
00081     }
00082     return 0;
00083 }
00084 
00085 
00086 bool verify_generalarguments(argumentclass& arguments)
00087 {
00088     if (arguments.driver.size()==0)
00089     {
00090         cerr << "No driver selected"<<endl;
00091         return false;
00092     }
00093     if (arguments.database.size()==0)
00094     {
00095         cerr << "No database selected"<<endl;
00096         return false;
00097     }
00098 
00099     return true;
00100 }
00101 #endif //HAVE_ARGP
00102 
00103 bool is_localfile(const hk_string& file)
00104 {
00105     return (file.find('/')!=hk_string::npos);
00106 }
00107 
00108 
00109 bool load_file(const hk_string &name,hk_string &file)
00110 {
00111     if (name.size()==0) return false;
00112     ifstream ifs(name.c_str(),ios::in);
00113     if (ifs)
00114     {
00115         char c;
00116         file="";
00117         while (ifs.get(c))
00118             file+=c;
00119     }
00120     else return false;
00121     return true;
00122 
00123 }
00124 #endif

Generated on Tue Nov 28 02:48:08 2006 for Databaseconnectionclasses by  doxygen 1.5.0