- /* +------------------------------------+
+/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
return std::string(ctime(&curtime),24);
}
-long InspIRCd::Duration(const char* str)
+/** Refactored by Brain, Jun 2007. Much faster with some clever O(1) array
+ * lookups and pointer maths.
+ */
+long InspIRCd::Duration(const std::string &str)
{
- char n_field[MAXBUF];
+ unsigned char multiplier = 0;
long total = 0;
- n_field[0] = 0;
+ long times = 1;
+ long subtotal = 0;
- if ((!strchr(str,'s')) && (!strchr(str,'m')) && (!strchr(str,'h')) && (!strchr(str,'d')) && (!strchr(str,'w')) && (!strchr(str,'y')))
- {
- std::string n = str;
- n += 's';
- return Duration(n.c_str());
- }
-
- for (char* i = (char*)str; *i; i++)
+ /* Iterate each item in the string, looking for number or multiplier */
+ for (std::string::const_reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
{
- // if we have digits, build up a string for the value in n_field,
- // up to 10 digits in size.
+ /* Found a number, queue it onto the current number */
if ((*i >= '0') && (*i <= '9'))
{
- strlcat(n_field,i,10);
+ subtotal = subtotal + ((*i - '0') * times);
+ times = times * 10;
}
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;
+ /* Found something thats not a number, find out how much
+ * it multiplies the built up number by, multiply the total
+ * and reset the built up number.
+ */
+ if (subtotal)
+ total += subtotal * duration_multi[multiplier];
- case 'y':
- total += (atoi(n_field)*duration_y);
- break;
- }
- n_field[0] = 0;
+ /* Next subtotal please */
+ subtotal = 0;
+ multiplier = *i;
+ times = 1;
}
}
- // add trailing seconds
- total += atoi(n_field);
-
- return total;
+ if (multiplier)
+ {
+ total += subtotal * duration_multi[multiplier];
+ subtotal = 0;
+ }
+ /* Any trailing values built up are treated as raw seconds */
+ return total + subtotal;
}
/* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
*/
irc::commasepstream items1(parameters[splithere]);
irc::commasepstream items2(parameters[extra]);
- std::string item = "*";
+ std::string item("*");
unsigned int max = 0;
/* Attempt to iterate these lists and call the command objech
/* Only one commasepstream here */
irc::commasepstream items1(parameters[splithere]);
- std::string item = "*";
+ std::string item("*");
unsigned int max = 0;
/* Parse the commasepstream until there are no tokens remaining.
{
if (!user->IsModeSet(cm->second->flags_needed))
{
- user->WriteServ("481 %s :Permission Denied- You do not have the required operator privileges",user->nick);
+ user->WriteServ("481 %s :Permission Denied - You do not have the required operator privileges",user->nick);
return;
}
if (!user->HasPermission(command))
{
- user->WriteServ("481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
+ user->WriteServ("481 %s :Permission Denied - Oper type %s does not have access to command %s",user->nick,user->oper,command.c_str());
return;
}
}
if (x->source == std::string(source))
{
cmdlist.erase(safei);
+ delete x;
}
}