Merged with Irssi 0.8.6.
[silc.git] / apps / irssi / src / core / misc.c
index 1af327afb8ff5152eac9ac3396b006481c97be37..00f9d096b394fbf12cef0c9e0277fad1f53857af 100644 (file)
@@ -126,7 +126,7 @@ int find_substr(const char *list, const char *item)
                return FALSE;
 
        for (;;) {
-               while (isspace((gint) *list)) list++;
+               while (i_isspace(*list)) list++;
                if (*list == '\0') break;
 
                ptr = strchr(list, ' ');
@@ -324,7 +324,7 @@ char *stristr(const char *data, const char *key)
                if (key[pos] == '\0')
                         return (char *) data;
 
-               if (toupper(data[pos]) == toupper(key[pos]))
+               if (i_toupper(data[pos]) == i_toupper(key[pos]))
                        pos++;
                else {
                        data++;
@@ -337,7 +337,7 @@ char *stristr(const char *data, const char *key)
 
 #define isbound(c) \
        ((unsigned char) (c) < 128 && \
-       (isspace((int) (c)) || ispunct((int) (c))))
+       (i_isspace(c) || i_ispunct(c)))
 
 char *strstr_full_case(const char *data, const char *key, int icase)
 {
@@ -364,7 +364,7 @@ char *strstr_full_case(const char *data, const char *key, int icase)
                        return (char *) data;
                }
 
-               match = icase ? (toupper(data[pos]) == toupper(key[pos])) :
+               match = icase ? (i_toupper(data[pos]) == i_toupper(key[pos])) :
                                 data[pos] == key[pos];
 
                if (match && (pos != 0 || data == start || isbound(data[-1])))
@@ -430,10 +430,11 @@ int mkpath(const char *path, int mode)
                dir = g_strndup(path, (int) (p-path));
                if (stat(dir, &statbuf) != 0) {
 #ifndef WIN32
-                       if (mkdir(dir, mode) == -1) {
+                       if (mkdir(dir, mode) == -1)
 #else
-                       if (_mkdir(dir) == -1) {
+                       if (_mkdir(dir) == -1)
 #endif
+                       {
                                g_free(dir);
                                return -1;
                        }
@@ -450,9 +451,17 @@ int mkpath(const char *path, int mode)
 /* convert ~/ to $HOME */
 char *convert_home(const char *path)
 {
-       return *path == '~' && (*(path+1) == '/' || *(path+1) == '\0') ?
-               g_strconcat(g_get_home_dir(), path+1, NULL) :
-               g_strdup(path);
+       const char *home;
+
+       if (*path == '~' && (*(path+1) == '/' || *(path+1) == '\0')) {
+               home = g_get_home_dir();
+               if (home == NULL)
+                       home = ".";
+
+               return g_strconcat(home, path+1, NULL);
+       } else {
+               return g_strdup(path);
+       }
 }
 
 int g_istr_equal(gconstpointer v, gconstpointer v2)
@@ -472,7 +481,7 @@ unsigned int g_istr_hash(gconstpointer v)
        unsigned int h = 0, g;
 
        while (*s != '\0') {
-               h = (h << 4) + toupper(*s);
+               h = (h << 4) + i_toupper(*s);
                if ((g = h & 0xf0000000UL)) {
                        h = h ^ (g >> 24);
                        h = h ^ g;
@@ -492,7 +501,7 @@ int match_wildcards(const char *cmask, const char *data)
        newmask = mask = g_strdup(cmask);
        for (; *mask != '\0' && *data != '\0'; mask++) {
                if (*mask != '*') {
-                       if (*mask != '?' && toupper(*mask) != toupper(*data))
+                       if (*mask != '?' && i_toupper(*mask) != i_toupper(*data))
                                break;
 
                        data++;
@@ -538,7 +547,7 @@ int is_numeric(const char *str, char end_char)
                return FALSE;
 
        while (*str != '\0' && *str != end_char) {
-               if (!isdigit(*str)) return FALSE;
+               if (!i_isdigit(*str)) return FALSE;
                str++;
        }
 
@@ -583,6 +592,20 @@ int dec2octal(int decimal)
        return octal;
 }
 
+/* string -> uoff_t */
+uoff_t str_to_uofft(const char *str)
+{
+       uoff_t ret;
+
+       ret = 0;
+       while (*str != '\0') {
+               ret = ret*10 + (*str - '0');
+               str++;
+       }
+
+       return ret;
+}
+
 /* convert all low-ascii (<32) to ^<A..> combinations */
 char *show_lowascii(const char *channel)
 {
@@ -725,3 +748,63 @@ GSList *columns_sort_list(GSList *list, int rows)
                             g_slist_length(list), sorted);
         return sorted;
 }
+
+/* Expand escape string, the first character in data should be the
+   one after '\'. Returns the expanded character or -1 if error. */
+int expand_escape(const char **data)
+{
+        char digit[4];
+
+       switch (**data) {
+       case 't':
+               return '\t';
+       case 'r':
+               return '\r';
+       case 'n':
+               return '\n';
+       case 'e':
+               return 27; /* ESC */
+
+       case 'x':
+                /* hex digit */
+               if (!i_isxdigit((*data)[1]) || !i_isxdigit((*data)[2]))
+                       return -1;
+
+               digit[0] = (*data)[1];
+               digit[1] = (*data)[2];
+                digit[2] = '\0';
+               *data += 2;
+               return strtol(digit, NULL, 16);
+       case 'c':
+                /* control character (\cA = ^A) */
+                (*data)++;
+               return i_toupper(**data) - 64;
+       default:
+               if (!i_isdigit(**data))
+                       return -1;
+
+                /* octal */
+                digit[0] = (*data)[0];
+                digit[1] = (*data)[1];
+               digit[2] = (*data)[2];
+                digit[3] = '\0';
+               *data += 2;
+               return strtol(digit, NULL, 8);
+       }
+}
+
+/* Escape all '"', "'" and '\' chars with '\' */
+char *escape_string(const char *str)
+{
+       char *ret, *p;
+
+       p = ret = g_malloc(strlen(str)*2+1);
+       while (*str != '\0') {
+               if (*str == '"' || *str == '\'' || *str == '\\')
+                       *p++ = '\\';
+               *p++ = *str++;
+       }
+       *p = '\0';
+
+       return ret;
+}