~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/libdrizzle/libdrizzle.hpp

  • Committer: Olaf van der Spek
  • Date: 2011-08-20 14:28:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2409.
  • Revision ID: olafvdspek@gmail.com-20110820142825-ij88ywkdhpfm8ecp
Read conf files

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 
34
34
#pragma once
35
35
 
 
36
#include <boost/algorithm/string.hpp>
 
37
#include <boost/foreach.hpp>
36
38
#include <boost/shared_ptr.hpp>
37
39
#include <cstring>
 
40
#include <fstream>
38
41
#include <libdrizzle/libdrizzle.h>
 
42
#include <map>
39
43
#include <sstream>
40
44
#include <stdexcept>
41
45
 
145
149
  explicit connection_c(drizzle_c& drizzle)
146
150
  {
147
151
    drizzle_con_create(drizzle, *this);
 
152
    read_conf_files();
148
153
  }
149
154
 
150
155
  ~connection_c()
214
219
    return query(str, strlen(str));
215
220
  }
216
221
private:
 
222
  void read_conf_files();
 
223
 
217
224
  drizzle_con_st b_;
218
225
};
219
226
 
302
309
  std::string out_;
303
310
};
304
311
 
 
312
template<class T>
 
313
const char* get_conf(const T& c, const std::string& v)
 
314
{
 
315
  typename T::const_iterator i = c.find(v);
 
316
  return i == c.end() ? NULL : i->second.c_str();
 
317
}
 
318
 
 
319
void connection_c::read_conf_files()
 
320
{
 
321
  using namespace std;
 
322
 
 
323
  vector<string> conf_files;
 
324
#ifdef WIN32
 
325
  {
 
326
    boost::array<char, MAX_PATH> d;
 
327
    GetWindowsDirectoryA(d.data(), d.size());
 
328
    conf_files.push_back(string(d.data()) + "/my.cnf");
 
329
    conf_files.push_back(string(d.data()) + "/drizzle.cnf");
 
330
    conf_files.push_back(string(d.data()) + "/drizzle.conf");
 
331
  }
 
332
#else
 
333
  conf_files.push_back("/etc/mysql/my.cnf");
 
334
  conf_files.push_back("/etc/drizzle/drizzle.cnf");
 
335
  conf_files.push_back("/etc/drizzle/drizzle.conf");
 
336
#endif
 
337
  if (const char* d = getenv("HOME"))
 
338
  {
 
339
    conf_files.push_back(string(d) + "/.my.cnf");  
 
340
    conf_files.push_back(string(d) + "/.drizzle.conf");
 
341
  }
 
342
  
 
343
  typedef map<string, string> conf_t;
 
344
  conf_t conf;
 
345
  BOOST_FOREACH(string& it, conf_files)
 
346
  {
 
347
    ifstream is(it.c_str());
 
348
    bool client_section = false;
 
349
    for (string s; getline(is, s); )
 
350
    {
 
351
      size_t i = s.find('#');
 
352
      if (i != string::npos)
 
353
        s.erase(i);
 
354
      boost::trim(s);
 
355
      if (boost::starts_with(s, "["))
 
356
      {
 
357
        client_section = s == "[client]";
 
358
        continue;
 
359
      }
 
360
      else if (!client_section)
 
361
        continue;
 
362
      i = s.find('=');
 
363
      if (i == string::npos)
 
364
        continue;
 
365
      conf[boost::trim_copy(s.substr(0, i))] = boost::trim_copy(s.substr(i + 1));
 
366
      if (s.empty() || !client_section)
 
367
        continue;
 
368
    }
 
369
  }
 
370
  if (conf.count("host") || conf.count("port"))
 
371
    set_tcp(get_conf(conf, "host"), atoi(get_conf(conf, "port")));
 
372
  if (conf.count("user") || conf.count("password"))
 
373
    set_auth(get_conf(conf, "user"), get_conf(conf, "password"));
 
374
}
 
375
 
305
376
}