diff options
author | pixel <pixel> | 2003-08-22 16:26:21 +0000 |
---|---|---|
committer | pixel <pixel> | 2003-08-22 16:26:21 +0000 |
commit | 366d123c3a01da8fd48444794f03e89ac8e248c7 (patch) | |
tree | fa4d4f594731396ae1eb82dc86b7a1d2d5aeefcd /lib | |
parent | 23974eb093c83a5e6f7b1109e0f739ad7a870aac (diff) |
Latest changes in Baltisot...
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ConfigFile.cc | 43 | ||||
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/SQL.cc | 61 | ||||
-rw-r--r-- | lib/String.cc | 28 |
4 files changed, 122 insertions, 12 deletions
diff --git a/lib/ConfigFile.cc b/lib/ConfigFile.cc new file mode 100644 index 0000000..85cbb00 --- /dev/null +++ b/lib/ConfigFile.cc @@ -0,0 +1,43 @@ +#include "ConfigFile.h" +#include "Regex.h" + +ConfigFile::ConfigFile(Handle * f) throw (GeneralException) { + Regex r("^\\[.*\\]$"), comment("^#.*$"), empty("^ *$"), line("^.*=.*$"); + bool started = false; + String s, section, key, value; + ConfigSectionContents contents; + int pos; + + while (!f->IsClosed()) { + (*f) >> s; + if (comment.Match(s) || empty.Match(s)) + continue; + if (r.Match(s)) { + if (!started) { + started = true; + } else { + c[section] = contents; + contents.clear(); + } + section = s.extract(1, s.strlen() - 2); + continue; + } + if (line.Match(s)) { + if (!started) { + throw GeneralException("Config file must begin with a section"); + } + pos = s.strchr('='); + key = s.extract(0, pos - 1).trim(); + value = s.extract(pos + 1).trim(); + contents[key] = value; + continue; + } + throw GeneralException("Invalid line: " + s); + } + + c[section] = contents; +} + +ConfigSectionContents & ConfigFile::operator[](String s) { + return c[s]; +} diff --git a/lib/Makefile.am b/lib/Makefile.am index c843753..29edcf1 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,4 +9,4 @@ libBaltisot_la_SOURCES = Action.cc Buffer.cc checkargs.c Confirm.cc CopyJob.cc \ datecalc.c Exceptions.cc Form.cc GMPString.cc Handle.cc HttpServ.cc Image.cc \ InPipe.cc Input.cc IRC.cc Main.cc Menu.cc Message.cc OutPipe.cc Output.cc \ ReadJob.cc Regex.cc Socket.cc String.cc Table.cc Task.cc TaskMan.cc \ -Variables.cc generic.cc fileutils.cc SQL.cc +Variables.cc generic.cc fileutils.cc SQL.cc ConfigFile.cc @@ -5,15 +5,17 @@ SQLConnection::SQLConnection(String host, String user, String passwd, unsigned long cflags) throw (GeneralException) : res(0) { mysql_init(&con); - char * p; - - if (!mysql_real_connect(&con, - p = ((ugly_string)(host)).p ? p : 0, - ((ugly_string)(user)).p, - ((ugly_string)(passwd)).p, - ((ugly_string)(db)).p, port, - ((ugly_string)(socket)).p, cflags)) { - throw GeneralException(String("Could not connect to MySQL host ") + p); + const char * phost = ((ugly_string) host).p; + const char * puser = ((ugly_string) user).p; + const char * ppasswd = ((ugly_string) passwd).p; + const char * pdb = ((ugly_string) db).p; + const char * psocket = ((ugly_string) socket).p; + + phost = *phost ? phost : 0; + psocket = *psocket ? psocket : 0; + + if (!mysql_real_connect(&con, phost, puser, ppasswd, pdb, port, psocket, cflags)) { + throw GeneralException("Could not connect to MySQL host " + host); } } @@ -22,4 +24,45 @@ SQLConnection::~SQLConnection() { } void SQLConnection::query(String q) throw(GeneralException) { + if (res) { + mysql_free_result(res); + } + + if (mysql_real_query(&con, ((ugly_string)q).p, q.strlen())) { + throw GeneralException(String("Couldn't run query ") + q); + } + + res = mysql_store_result(&con); + + if (res) { + nr = mysql_num_rows(res); + nf = mysql_num_fields(res); + fields = mysql_fetch_fields(res); + } else { + nr = 0; + nf = 0; + fields = 0; + } +} + +int SQLConnection::numrows() { + return nr; +} + +int SQLConnection::numfields() { + return nf; +} + +AssocArray SQLConnection::fetchrow() { + AssocArray r; + MYSQL_ROW row; + int i; + + row = mysql_fetch_row(res); + + for (i = 0; i < nf; i++) { + r[fields[i].name] = row[i]; + } + + return r; } diff --git a/lib/String.cc b/lib/String.cc index 402424c..f24dcca 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -475,6 +475,30 @@ String & String::tolower() { return *this; } -bool compare(String a, String b) { - return a < b; +String String::ltrim() const { + char * d = (char *) malloc(strlen() + 1), * p, * r; + int s; + + r = d; + + for (p = str; *p && *p == ' '; p++); + for (s = 0; *p; *(d++) = *(p++), s++); + *d = 0; + + return String(s, r); +} + +String String::rtrim() const { + char * d = strdup(), * r; + int s = strlen(); + + r = d; + + for (d += s - 1; s && (*d == ' '); *(d--) = 0, s--); + + return String(s, r); +} + +String String::trim() const { + return rtrim().ltrim(); } |