From bd6fe052bce57849a7a850c0c5bee0ad896b9460 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Sat, 28 Dec 2002 17:54:13 +0000 Subject: Added time, size and level setting types. Breaks some settings - I'll add automatic converter to these settings later. Meanwhile you CVS users can fix your config files yourself :) Time settings allow using "days", "hours", "minutes", "seconds" and "milliseconds" or several of their abbreviations. For example "5d 4h 5msecs". Size settings allow using "gbytes", "mbytes", "kbytes" and "bytes" or their abbrevations. For example "5MB". Level settings are currently handled pretty much the way they were before. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@3080 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/misc.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) (limited to 'src/core/misc.c') diff --git a/src/core/misc.c b/src/core/misc.c index 89f564d3..46720d62 100644 --- a/src/core/misc.c +++ b/src/core/misc.c @@ -816,3 +816,117 @@ int nearest_power(int num) while (n < num) n <<= 1; return n; } + +int parse_time_interval(const char *time, int *msecs) +{ + const char *desc; + int number, len; + + *msecs = 0; + + /* max. return value is about 1.6 years */ + number = 0; + for (;;) { + if (i_isdigit(*time)) { + number = number*10 + (*time - '0'); + time++; + continue; + } + + /* skip punctuation */ + while (*time != '\0' && i_ispunct(*time)) + time++; + + /* get description */ + for (len = 0, desc = time; i_isalpha(*time); time++) + len++; + + if (len == 0) { + if (number == 0) { + /* "0" - allow it */ + return TRUE; + } + + *msecs += number; /* assume seconds */ + return FALSE; + } + + if (g_strncasecmp(desc, "weeks", len) == 0) + *msecs += number * 1000*3600*7; + if (g_strncasecmp(desc, "days", len) == 0) + *msecs += number * 1000*3600; + else if (g_strncasecmp(desc, "minutes", len) == 0 || + g_strncasecmp(desc, "mins", len) == 0) + *msecs += number * 1000*60; + else if (g_strncasecmp(desc, "seconds", len) == 0 || + g_strncasecmp(desc, "secs", len) == 0) + *msecs += number * 1000; + else if (g_strncasecmp(desc, "milliseconds", len) == 0 || + g_strncasecmp(desc, "millisecs", len) == 0 || + g_strncasecmp(desc, "mseconds", len) == 0 || + g_strncasecmp(desc, "msecs", len) == 0) + *msecs += number; + + /* skip punctuation */ + while (*time != '\0' && i_ispunct(*time)) + time++; + + if (*time == '\0') + break; + + number = 0; + } + + return TRUE; +} + +int parse_size(const char *size, int *bytes) +{ + const char *desc; + int number, len; + + *bytes = 0; + + /* max. return value is about 1.6 years */ + number = 0; + while (*size != '\0') { + if (i_isdigit(*size)) { + number = number*10 + (*size - '0'); + size++; + continue; + } + + /* skip punctuation */ + while (*size != '\0' && i_ispunct(*size)) + size++; + + /* get description */ + for (len = 0, desc = size; i_isalpha(*size); size++) + len++; + + if (len == 0) { + if (number == 0) { + /* "0" - allow it */ + return TRUE; + } + + *bytes += number*1024; /* assume kilobytes */ + return FALSE; + } + + if (g_strncasecmp(desc, "gbytes", len) == 0) + *bytes += number * 1024*1024*1024; + if (g_strncasecmp(desc, "mbytes", len) == 0) + *bytes += number * 1024*1024; + if (g_strncasecmp(desc, "kbytes", len) == 0) + *bytes += number * 1024; + if (g_strncasecmp(desc, "bytes", len) == 0) + *bytes += number; + + /* skip punctuation */ + while (*size != '\0' && i_ispunct(*size)) + size++; + } + + return TRUE; +} -- cgit v1.2.3