summaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authorAdam <Adam@drink-coca-cola.info>2010-05-17 20:33:28 -0400
committerAdam <Adam@anope.org>2010-06-18 21:03:41 -0400
commit17ab4104005cc5098d9152e6c90d54977222dd06 (patch)
treed8ffffedcf5537292a4f19faeb5f1e7796969132 /src/misc.c
parent2a22d9c2fdf6978858055f1143af0ec40e417424 (diff)
Removed process_numlist and having to mark everything as "in use" and constantly checking it. Replaced with a better system.
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c132
1 files changed, 66 insertions, 66 deletions
diff --git a/src/misc.c b/src/misc.c
index 443dec298..d7060660e 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -223,83 +223,83 @@ const char *merge_args(int argc, char **argv)
}
/*************************************************************************/
-/**
- * Process a string containing a number/range list in the form
- * "n1[-n2][,n3[-n4]]...", calling a caller-specified routine for each
- * number in the list. If the callback returns -1, stop immediately.
- * Returns the sum of all nonnegative return values from the callback.
- * If `count' is non-NULL, it will be set to the total number of times the
- * callback was called.
- *
- * The callback should be of type range_callback_t, which is defined as:
- * int (*range_callback_t)(User *u, int num, va_list args)
- * @param numstr
- * @param count_ret
- * @param callback Call back function
- * @param u User Struct
- * @param ... various args
- * @return int
- */
-int process_numlist(const char *numstr, int *count_ret,
- range_callback_t callback, User * u, ...)
+
+NumberList::NumberList(const std::string &list)
{
- int n1, n2, i;
- int res = 0, retval = 0, count = 0;
- va_list args, preserve;
+ commasepstream sep(list);
+ std::string token;
- if (!numstr || !*numstr) {
- return -1;
- }
+ sep.GetToken(token);
+ if (token.empty())
+ token = list;
+ do
+ {
+ char *h = strchr(token.c_str(), '-');
- va_start(args, u);
-
- /*
- * This algorithm ignores invalid characters, ignores a dash
- * when it precedes a comma, and ignores everything from the
- * end of a valid number or range to the next comma or null.
- */
- for (;;) {
- n1 = n2 = strtol(numstr, const_cast<char **>(&numstr), 10);
- numstr += strcspn(numstr, "0123456789,-");
- if (*numstr == '-') {
- numstr++;
- numstr += strcspn(numstr, "0123456789,");
- if (isdigit(*numstr)) {
- n2 = strtol(numstr, const_cast<char **>(&numstr), 10);
- numstr += strcspn(numstr, "0123456789,-");
+ if (!h)
+ {
+ errno = 0;
+ unsigned num = strtol(token.c_str(), NULL, 10);
+ if (!errno)
+ {
+ numbers.insert(num);
}
- }
- for (i = n1; i <= n2 && i >= 0; i++) {
- VA_COPY(preserve, args);
- res = callback(u, i, preserve);
- va_end(preserve);
- count++;
- if (res < 0)
- break;
- retval += res;
- if (count >= 32767) {
- if (count_ret)
- *count_ret = count;
- return retval;
+ else
+ {
+ if (!this->InvalidRange(list))
+ {
+ delete this;
+ return;
+ }
}
}
- if (res < -1)
- break;
- numstr += strcspn(numstr, ",");
- if (*numstr)
- numstr++;
else
- break;
- }
- if (count_ret)
- *count_ret = count;
+ {
+ *h++ = '\0';
+ errno = 0;
+ unsigned num1 = strtol(token.c_str(), NULL, 10);
+ unsigned num2 = strtol(h, NULL, 10);
+ if (!errno)
+ {
+ for (unsigned i = num1; i <= num2; ++i)
+ {
+ numbers.insert(i);
+ }
+ }
+ else
+ {
+ if (!this->InvalidRange(list))
+ {
+ delete this;
+ return;
+ }
+ }
+ }
+ } while (sep.GetToken(token));
+}
- va_end(args);
+NumberList::~NumberList()
+{
+}
- return retval;
+void NumberList::Process()
+{
+ for (std::set<unsigned>::reverse_iterator it = numbers.rbegin(); it != numbers.rend(); ++it)
+ {
+ this->HandleNumber(*it);
+ }
+
+ delete this;
}
-/*************************************************************************/
+void NumberList::HandleNumber(unsigned)
+{
+}
+
+bool NumberList::InvalidRange(const std::string &)
+{
+ return true;
+}
/**
* dotime: Return the number of seconds corresponding to the given time