Databaseconnectionclasses  0.5.3
hk_utilities.h
1 // ****************************************************************************
2 // copyright (c) 2000-2005 Horst Knorr <hk_classes@knoda.org>
3 // This file is part of the hk_classes library.
4 // This file may be distributed and/or modified under the terms of the
5 // GNU Library Public License version 2 as published by the Free Software
6 // Foundation and appearing in the file COPYING included in the
7 // packaging of this file.
8 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
9 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
10 // ****************************************************************************
11 #ifndef HK_UTILITIES
12 #define HK_UTILITIES
13 #ifdef HAVE_ARGP
14 #include <argp.h>
15 #endif
16 #include "hk_drivermanager.h"
17 #include "hk_connection.h"
18 #include "hk_database.h"
19 #include "hk_string.h"
20 #include "config.h"
21 #include <cassert>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 
26 #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}
27 
34 {
35  public:
36  hk_string driver,host,port,user,password,database,filter;
37 };
38 #ifdef HAVE_ARGP
39 
42 inline static error_t parse_generaloptions (int key,char* arg, struct argp_state* state)
43 {
44  struct argumentclass* arguments=(argumentclass*)state->input;
45  assert(arguments);
46  switch (key)
47  {
48 
49  case 10 : //version
50  cout <<"Version "<<VERSION<<"\nAuthor: Horst Knorr <hk_classes@knoda.org>\nhttp://hk-classes.sourceforge.net"<<endl;
51  exit(0);
52  break;
53  case 'l' :
54  {
55  vector<hk_string>* l = hk_drivermanager::driverlist();
56  vector<hk_string>::iterator it=l->begin();
57  while (it!=l->end())
58  {
59  cout <<*it<<endl;
60  ++it;
61  }
62  exit(0);
63  }
64 
65  break;
66  case 'd' : arguments->driver=arg;
67  break;
68  case 'h' : arguments->host=arg;
69  break;
70  case 'r' : arguments->port=arg;
71  break;
72  case 'u' : arguments->user=arg;
73  break;
74  case 'p' : arguments->password=arg;
75  break;
76  case 'f' : arguments->filter=arg;
77  break;
78  case 'b' : arguments->database=arg;
79  break;
80  default : return ARGP_ERR_UNKNOWN;
81  }
82  return 0;
83 }
84 
85 
86 bool verify_generalarguments(argumentclass& arguments)
87 {
88  if (arguments.driver.size()==0)
89  {
90  cerr << "No driver selected"<<endl;
91  return false;
92  }
93  if (arguments.database.size()==0)
94  {
95  cerr << "No database selected"<<endl;
96  return false;
97  }
98 
99  return true;
100 }
101 #endif //HAVE_ARGP
102 
103 bool is_localfile(const hk_string& file)
104 {
105  return (file.find('/')!=hk_string::npos);
106 }
107 
108 
109 bool load_file(const hk_string &name,hk_string &file)
110 {
111  if (name.size()==0) return false;
112  ifstream ifs(name.c_str(),ios::in);
113  if (ifs)
114  {
115  char c;
116  file="";
117  while (ifs.get(c))
118  file+=c;
119  }
120  else return false;
121  return true;
122 
123 }
124 #endif
Definition: hk_utilities.h:33