diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2007-06-05 16:49:06 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2007-06-05 16:49:06 +0000 |
commit | d396355d3e594544a4760f3e7d4e7c75a02430a8 (patch) | |
tree | 133d64d9dc155d5227483ede13bd398b51039160 | |
parent | 1b93cf63e3bcfbb8168fe40fc116fe01fc306a5f (diff) |
Refactored InspIRCd::Duration(). The leetcode now lives!
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@7237 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | include/command_parse.h | 22 | ||||
-rw-r--r-- | include/inspircd.h | 20 | ||||
-rw-r--r-- | src/command_parse.cpp | 61 | ||||
-rw-r--r-- | src/inspircd.cpp | 2 |
4 files changed, 38 insertions, 67 deletions
diff --git a/include/command_parse.h b/include/command_parse.h index 2ffb30ee6..6d98af97c 100644 --- a/include/command_parse.h +++ b/include/command_parse.h @@ -205,6 +205,26 @@ class cmd_reload : public command_t CmdResult Handle(const char** parameters, int pcnt, userrec *user); }; - +const int duration_multi[] = +{ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 86400, 1, 1, 1, 3600, + 1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 604800, 1, 31536000, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31536000, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 +}; #endif diff --git a/include/inspircd.h b/include/inspircd.h index 505fa1034..de6e93c0c 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -396,26 +396,6 @@ class CoreExport InspIRCd : public classbase */ char ReadBuffer[65535]; - /** Number of seconds in a minute - */ - const long duration_m; - - /** Number of seconds in an hour - */ - const long duration_h; - - /** Number of seconds in a day - */ - const long duration_d; - - /** Number of seconds in a week - */ - const long duration_w; - - /** Number of seconds in a year - */ - const long duration_y; - /** Used when connecting clients */ insp_sockaddr client, server; diff --git a/src/command_parse.cpp b/src/command_parse.cpp index ab935559d..598585315 100644 --- a/src/command_parse.cpp +++ b/src/command_parse.cpp @@ -65,59 +65,30 @@ std::string InspIRCd::TimeString(time_t curtime) long InspIRCd::Duration(const char* str) { char n_field[MAXBUF]; + const char* maxsize = n_field + MAXBUF; long total = 0; - n_field[0] = 0; + char* field_ptr = n_field; + *n_field = 0; - if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y'))) + for (const char* i = str; *i; i++) { - std::string n = str; - n += 's'; - return Duration(n.c_str()); - } - - for (char* i = (char*)str; *i; i++) - { - // if we have digits, build up a string for the value in n_field, - // up to 10 digits in size. - if ((*i >= '0') && (*i <= '9')) - { - strlcat(n_field,i,10); - } + if ((*i >= '0') && (*i <= '9') && (field_ptr < maxsize)) + *field_ptr++ = *i; else { - // we dont have a digit, check for numeric tokens - switch (tolower(*i)) - { - case 's': - total += atoi(n_field); - break; - - case 'm': - total += (atoi(n_field)*duration_m); - break; - - case 'h': - total += (atoi(n_field)*duration_h); - break; - - case 'd': - total += (atoi(n_field)*duration_d); - break; - - case 'w': - total += (atoi(n_field)*duration_w); - break; - - case 'y': - total += (atoi(n_field)*duration_y); - break; - } - n_field[0] = 0; + *field_ptr = 0; + field_ptr = n_field; + total += atoi(n_field) * duration_multi[(const unsigned char)*i]; + *n_field = 0; } } // add trailing seconds - total += atoi(n_field); - + if (*n_field) + { + *field_ptr = 0; + total += atoi(n_field); + } + return total; } diff --git a/src/inspircd.cpp b/src/inspircd.cpp index bcfc92c48..52aeddf04 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -352,7 +352,7 @@ std::string InspIRCd::GetRevision() } InspIRCd::InspIRCd(int argc, char** argv) - : ModCount(-1), duration_m(60), duration_h(60*60), duration_d(60*60*24), duration_w(60*60*24*7), duration_y(60*60*24*365), GlobalCulls(this) + : ModCount(-1), GlobalCulls(this) { #ifdef WINDOWS ClearConsole(); |