From 54e62a566c003363078aa5273a728c0a0657b3a0 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 27 Oct 2001 11:15:57 +0000 Subject: Reworking on it... --- lib/Exceptions.cc | 2 +- lib/Handle.cc | 30 +++++++++++++++----- lib/HttpServ.cc | 14 ++++------ lib/IRC.cc | 10 +++---- lib/Makefile.am | 2 +- lib/String.cc | 83 ++++++++++++++++++++++++++++++------------------------- lib/Task.cc | 0 lib/Variables.cc | 35 +++++++++++++++++------ lib/datecalc.c | 6 ---- 9 files changed, 107 insertions(+), 75 deletions(-) create mode 100644 lib/Task.cc (limited to 'lib') diff --git a/lib/Exceptions.cc b/lib/Exceptions.cc index 734e5e9..0376fee 100644 --- a/lib/Exceptions.cc +++ b/lib/Exceptions.cc @@ -27,7 +27,7 @@ void * Base::operator new(size_t s, void * p) { return p; } -GeneralException::GeneralException(String emsg) : msg(strdup(emsg.to_charp())) { } +GeneralException::GeneralException(String emsg) : msg(emsg.strdup()) { } GeneralException::GeneralException() : msg(0) { } GeneralException::GeneralException(const GeneralException & e) : msg(strdup(e.msg)) { } diff --git a/lib/Handle.cc b/lib/Handle.cc index 8108285..4824b43 100644 --- a/lib/Handle.cc +++ b/lib/Handle.cc @@ -2,14 +2,15 @@ #include #include #include +#include #include "Handle.h" #include "config.h" -Handle::Handle(const Handle & nh) : h(dup(nh.h)), closed(false) { } +Handle::Handle(const Handle & nh) : h(dup(nh.h)), closed(false), nonblock(false) { } Handle::~Handle() { if (h != -1) { - ::close(h); + close(h); } } @@ -25,6 +26,7 @@ ssize_t Handle::write(const void *buf, size_t count) throw (IOException) { do { done = true; + errno = 0; if ((r = ::write(h, buf, count)) < 0) { if ((!errno) || (errno = EAGAIN)) { // Avant de déclarer une erreur, on vérifie si ce n'est pas un @@ -35,10 +37,13 @@ ssize_t Handle::write(const void *buf, size_t count) throw (IOException) { if (full) { throw IOException(GetName(), IO_WRITE, count); } else { - sleep(1); done = false; full = true; - tr += r; + if (nonblock) { + return 0; + } else { + sleep(1); + } } } else { throw IOException(GetName(), IO_WRITE, count); @@ -56,6 +61,7 @@ ssize_t Handle::write(const void *buf, size_t count) throw (IOException) { ssize_t Handle::read(void *buf, size_t count) throw (IOException) { ssize_t r; + errno = 0; if ((r = ::read(h, buf, count)) < 0) { if ((!errno) || (errno = EAGAIN)) { // Avant de déclarer une erreur, on vérifie si ce n'est pas un @@ -67,6 +73,7 @@ ssize_t Handle::read(void *buf, size_t count) throw (IOException) { } if (!r) { + close(h); closed = true; } @@ -77,6 +84,15 @@ bool Handle::IsClosed(void) { return closed; } +bool Handle::IsNonBlock(void) { + return nonblock; +} + +void Handle::SetNonBlock(void) { + nonblock = true; + fcntl(h, F_SETFL, O_NONBLOCK); +} + Handle & operator<<(Handle & h, const String & s) { char * p; @@ -91,9 +107,9 @@ Handle & operator>>(Handle & h, String & s) { int i = 0, r; while ((r = h.read(&(t[i]), 1)) && (i != (BUFSIZ - 1))) { - // Il y a souvent des \r\n dans les sockets par exemple. - // On ignore le \r pour ne garder que le \n, standard sous - // Unix. + // Il y a souvent des \r\n dans les sockets par exemple, + // ou bien en lisant des fichiers au format MS-DOS. On + // ignore le \r pour ne garder que le \n, standard sous Unix. if (t[i] == '\r') { continue; } diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc index dfead28..97152a9 100644 --- a/lib/HttpServ.cc +++ b/lib/HttpServ.cc @@ -37,8 +37,8 @@ void HttpServ::ProcessRequest(Action * p) { s >> t; cerr << t << endl; if ((t.strstr("Content-Length: ") == 0) || (t.strstr("Content-length: ") == 0)) { - cerr << "Saw 'Content-Lenght:', reading length from '" << t.to_charp(16) << "'\n"; - len = String(t.to_charp(16)).to_int(); + cerr << "Saw 'Content-Lenght:', reading length from '" << t.extract(16) << "'\n"; + len = t.extract(16).to_int(); } } while (t.strlen()); @@ -173,7 +173,7 @@ void HttpServ::ParseVars(Socket & s, int len) { bool HttpServ::ParseUri(String & file, String & domain, Socket & s) { String t, Uri; bool post = false; - char * p; + char * p = NULL; ssize_t sppos; s >> t; @@ -184,15 +184,13 @@ bool HttpServ::ParseUri(String & file, String & domain, Socket & s) { // p nous indiquera la position de la chaîne URL. switch (t[0]) { case 'P': /* POST? */ - p = t.to_charp(1, 4); - if (!strcmp(p, "OST ")) { + if (t.extract(1, 4) == "OST ") { p = t.to_charp(5); post = true; break; } case 'G': /* GET? */ - p = t.to_charp(1, 3); - if (!strcmp(p, "ET ")) { + if (t.extract(1, 3) == "ET ") { p = t.to_charp(4); break; } @@ -284,7 +282,7 @@ String HttpServ::GetMime(const String & f) { ppos = f.strrchr('.'); if (ppos >= 0) { - ext = f.to_charp(ppos + 1); + ext = f.extract(ppos + 1); if (ext == "jpg") return "image/jpeg"; if (ext == "jpeg") return "image/jpeg"; if (ext == "htm") return "text/html"; diff --git a/lib/IRC.cc b/lib/IRC.cc index 8ba15cc..3dfa1d1 100644 --- a/lib/IRC.cc +++ b/lib/IRC.cc @@ -196,9 +196,9 @@ bool IRC::Connect() { } int IRC::Parse(const String & line) { - if (!strcmp(line.to_charp(0, 3), "PING")) { - sock << "PONG" << line.to_charp(4) << endhl; - cerr << "PING/PONG" << line.to_charp(4) << endhl; + if (line.extract(0, 3) == "PING") { + sock << "PONG" << line.extract(4) << endhl; + cerr << "PING/PONG" << line.extract(4) << endhl; return 0; } @@ -215,7 +215,7 @@ void IRC::MainLoop() { cerr << "Trying the login sequence..." << endl; sock << "NICK " << nick << endhl; sock << "USER " << user << " 8 * :" << name << endhl; - sock << "JOIN #phear.org dede" << endhl; +// sock << "JOIN #linuxdjeunz" << endhl; loginsequence = 0; } sock >> line; @@ -225,7 +225,7 @@ void IRC::MainLoop() { } if (count == 20) { - sock << "JOIN #phear.org dede" << endhl; + sock << "JOIN #linuxdjeunz" << endhl; } count++; diff --git a/lib/Makefile.am b/lib/Makefile.am index edb449d..91076a8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -7,6 +7,6 @@ lib_LTLIBRARIES = libBaltisot.la libBaltisot_la_SOURCES = Exceptions.cc Handle.cc Output.cc String.cc\ Socket.cc Input.cc HttpServ.cc Variables.cc Action.cc Menu.cc Message.cc\ - Form.cc Confirm.cc Table.cc checkargs.c datecalc.c IRC.cc + Form.cc Confirm.cc Table.cc checkargs.c datecalc.c IRC.cc Task.cc libBaltisot_la_LDFLAGS = -release $(Baltisot_VERSION_INFO) diff --git a/lib/String.cc b/lib/String.cc index 16c7db3..0e22de0 100644 --- a/lib/String.cc +++ b/lib/String.cc @@ -12,29 +12,35 @@ extern "C" { char String::t[BUFSIZ]; -String::String(const String & s) : str(strdup(s.str)) { } +String::String(const String & s) : str(::strdup(s.str)), siz(s.siz) { } -String::String(char c) { - char t[2]; +String::String(char c) : siz(1) { + static char t[2]; sprintf(t, "%c", c); - str = strdup(t); + str = ::strdup(t); } -String::String(const char * s) : str(s ? strdup(s) : strdup("")) { } +String::String(const char * s) : str(s ? ::strdup(s) : ::strdup("")) { + siz = ::strlen(str); +} + +String::String(int hs, const char * s) : str(s ? ::strdup(s) : ::strdup("")), siz(hs) { } String::String(int i) { char t[20]; sprintf(t, "%i", i); - str = strdup(t); + str = ::strdup(t); + siz = ::strlen(str); } String::String(double d) { char t[30]; sprintf(t, "%g", d); - str = strdup(t); + str = ::strdup(t); + siz = ::strlen(str); } String::~String() { @@ -47,8 +53,9 @@ char * String::set(char * s, ...) { va_start(ap, s); vsprintf(t, s, ap); free(str); - str = strdup(t); + str = ::strdup(t); va_end(ap); + siz = ::strlen(str); return t; } @@ -56,8 +63,8 @@ char * String::to_charp(size_t from, ssize_t to) const { if (to < 0) { strcpy(t, &(str[from])); } else { - if (to >= strlen()) { - to = strlen() - 1; + if (to >= siz) { + to = siz - 1; } if (to >= from) { int i; @@ -72,6 +79,14 @@ char * String::to_charp(size_t from, ssize_t to) const { return t; } +String String::extract(size_t from, ssize_t to) const { + return String(to_charp(from, to)); +} + +char * String::strdup(size_t from, ssize_t to) const { + return ::strdup(to_charp(from, to)); +} + int String::to_int(void) const { int r; @@ -90,7 +105,8 @@ String & String::operator=(const String & s) { if (str != s.str) { // On évite l'autodestruction... free(str); - str = strdup(s.str); + str = s.strdup(); + siz = s.siz; } return *this; } @@ -98,14 +114,15 @@ String & String::operator=(const String & s) { String String::operator+(const String & s) const { strcpy(t, str); strcat(t, s.str); - return String(t); + return String(siz + s.siz, t); } String & String::operator+=(const String & s) { strcpy(t, str); strcat(t, s.str); free(str); - str = strdup(t); + str = ::strdup(t); + siz += s.siz; return (*this); } @@ -151,11 +168,11 @@ bool String::operator>(const String & s) const { } size_t String::strlen() const { - return (str ? ::strlen(str) : 0); + return (siz); } char String::operator[](size_t i) const { - if (i >= strlen()) { + if (i >= siz) { return 0; } else { return str[i]; @@ -163,9 +180,7 @@ char String::operator[](size_t i) const { } ssize_t String::strchr(char c, size_t from) const { - size_t s = strlen(); - - for (size_t i = from; i < s; i++) { + for (size_t i = from; i < siz; i++) { if (str[i] == c) return i; } @@ -173,9 +188,7 @@ ssize_t String::strchr(char c, size_t from) const { } ssize_t String::strrchr(char c) const { - size_t s = strlen(); - - for (size_t i = s - 1; i >= 0; i--) { + for (size_t i = siz - 1; i >= 0; i--) { if (str[i] == c) return i; } @@ -194,9 +207,7 @@ ssize_t String::strstr(const String & s) const { int String::strchrcnt(char c) const { size_t i, cnt = 0; - size_t s = strlen(); - - for (i = 0; i < s; i++) { + for (i = 0; i < siz; i++) { if (str[i] == c) cnt++; } @@ -205,18 +216,18 @@ int String::strchrcnt(char c) const { String String::to_sqldate(void) const { /* DD/MM/YYYY ==> YYYYMMMDD */ - return (is_date() ? String(to_charp(6, 9)) + to_charp(3, 4) + to_charp(0, 1) : ""); + return (is_date() ? extract(6, 9) + extract(3, 4) + extract(0, 1) : ""); } String String::to_sqltime(void) const { /* h:m ==> h * 60 + m */ int p = strchr(':'); - return (is_time() ? String(String(to_charp(0, p - 1)).to_int() * 60 + String(to_charp(p + 1)).to_int()) : ""); + return (is_time() ? String(extract(0, p - 1).to_int() * 60 + extract(p + 1).to_int()) : ""); } String String::from_sqldate(void) const { /* YYYYMMDD ==> DD/MM/YYYY */ - return ((strlen() == 8) && is_number() ? String(to_charp(6, 7)) + '/' + to_charp(4, 5) + '/' + to_charp(0, 3) : ""); + return ((strlen() == 8) && is_number() ? extract(6, 7) + '/' + extract(4, 5) + '/' + extract(0, 3) : ""); } String String::from_sqltime(void) const { @@ -231,9 +242,9 @@ bool String::is_date(void) const { if (strlen() != 10) return false; if ((str[2] != '/') || (str[5] != '/') || - (!String(to_charp(0, 1)).is_number()) || - (!String(to_charp(3, 4)).is_number()) || - (!String(to_charp(6, 9)).is_number())) { + (!extract(0, 1).is_number()) || + (!extract(3, 4).is_number()) || + (!extract(6, 9).is_number())) { return (isDateArgument(to_sqldate().str)); } @@ -251,20 +262,16 @@ double String::datedif(const String & s) const { } bool String::is_number(void) const { - size_t s = strlen(); - - - for (size_t i = ((str[i] == '-') ? 1 : 0); i < s; i++) { + for (size_t i = ((str[i] == '-') ? 1 : 0); i < siz; i++) { if ((str[i] > '9') || (str[i] < '0')) return false; } return true; } bool String::is_float(void) const { - size_t s = strlen(); bool seendot = false; - for (size_t i = ((str[i] == '-') ? 1 : 0); i < s; i++) { + for (size_t i = ((str[i] == '-') ? 1 : 0); i < siz; i++) { if ((str[i] > '9') || (str[i] < '0')) { if ((str[i] == '.') && !seendot) { seendot = true; @@ -284,8 +291,8 @@ bool String::is_time(void) const { // On accepte les heures sous le format xxxxxx:yy pour pouvoir indiquer des durées. - if ((!String(to_charp(0, p - 1)).is_number()) || (!String(to_charp(p + 1)).is_number())) + if ((!extract(0, p - 1).is_number()) || (!extract(p + 1).is_number())) return false; - return (String(to_charp(p + 1)).to_int() < 60) ? true : false; + return (extract(p + 1).to_int() < 60) ? true : false; } diff --git a/lib/Task.cc b/lib/Task.cc new file mode 100644 index 0000000..e69de29 diff --git a/lib/Variables.cc b/lib/Variables.cc index 15de09d..0bb8c01 100644 --- a/lib/Variables.cc +++ b/lib/Variables.cc @@ -4,13 +4,9 @@ #include "String.h" #include "config.h" -Variables::Variables(int nb) : Vars(nb ? new String[nb] : 0), nbvars(nb) { } +Variables::Variables(int nb) : Vars(nb), nbvars(nb) { } -Variables::~Variables() { - if (Vars) { - delete[] Vars; - } -} +Variables::~Variables() { } void Variables::SetTo(int i, const String & s) { Vars[i] = s; @@ -27,7 +23,7 @@ String Variables::operator[](const String & name) { if (i == nbvars) { r = ""; } else { - r = Vars[i].to_charp(Vars[i].strchr('=') + 1); + r = Vars[i].extract(Vars[i].strchr('=') + 1); } return r; @@ -47,9 +43,30 @@ void Variables::Dump(Handle * h) { for (i = 0; i < nbvars; i++) { eqp = Vars[i].strchr('='); - Vn = Vars[i].to_charp(0, eqp - 1); - Vv = Vars[i].to_charp(eqp + 1); + Vn = Vars[i].extract(0, eqp - 1); + Vv = Vars[i].extract(eqp + 1); (*h) << "" << endnl; } } +void Variables::Add(const String & s) { + nbvars++; + Vars.push_back(s); +} + +void Variables::Del(int i) { + nbvars--; + Vars.erase(&Vars[i], &Vars[i]); +} + +void Variables::Del(const String & name) { + int i; + + for (i = 0; i < nbvars; i++) { + if (Vars[i].strstr(name) == 0) break; + } + + if (i != nbvars) { + Del(i); + } +} diff --git a/lib/datecalc.c b/lib/datecalc.c index 46e7179..ad6684a 100644 --- a/lib/datecalc.c +++ b/lib/datecalc.c @@ -76,9 +76,3 @@ double dateCalc(char date1[], char date2[]){ return dateDifference; } - - - - - - -- cgit v1.2.3