4 Copyright (C) 1999 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 GInputFunction function;
36 static int irssi_io_invoke(GIOChannel *source, GIOCondition condition,
39 IRSSI_INPUT_REC *rec = data;
42 if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
43 /* error, we have to call the function.. */
44 if (rec->condition & G_IO_IN)
45 icond |= G_INPUT_READ;
47 icond |= G_INPUT_WRITE;
50 if (condition & (G_IO_IN | G_IO_PRI))
51 icond |= G_INPUT_READ;
52 if (condition & G_IO_OUT)
53 icond |= G_INPUT_WRITE;
55 if (rec->condition & icond)
56 rec->function(rec->data, source, icond);
61 int g_input_add_full(GIOChannel *source, int priority, int condition,
62 GInputFunction function, void *data)
68 rec = g_new(IRSSI_INPUT_REC, 1);
69 rec->condition = condition;
70 rec->function = function;
73 cond = (GIOCondition) (G_IO_ERR|G_IO_HUP|G_IO_NVAL);
74 if (condition & G_INPUT_READ)
75 cond |= G_IO_IN|G_IO_PRI;
76 if (condition & G_INPUT_WRITE)
79 result = g_io_add_watch_full(source, priority, cond,
80 irssi_io_invoke, rec, g_free);
85 int g_input_add(GIOChannel *source, int condition,
86 GInputFunction function, void *data)
88 return g_input_add_full(source, G_PRIORITY_DEFAULT, condition,
92 int g_timeval_cmp(const GTimeVal *tv1, const GTimeVal *tv2)
94 if (tv1->tv_sec < tv2->tv_sec)
96 if (tv1->tv_sec > tv2->tv_sec)
99 return tv1->tv_usec < tv2->tv_usec ? -1 :
100 tv1->tv_usec > tv2->tv_usec ? 1 : 0;
103 long get_timeval_diff(const GTimeVal *tv1, const GTimeVal *tv2)
107 secs = tv1->tv_sec - tv2->tv_sec;
108 usecs = tv1->tv_usec - tv2->tv_usec;
113 usecs = usecs/1000 + secs * 1000;
118 int find_substr(const char *list, const char *item)
122 g_return_val_if_fail(list != NULL, FALSE);
123 g_return_val_if_fail(item != NULL, FALSE);
129 while (i_isspace(*list)) list++;
130 if (*list == '\0') break;
132 ptr = strchr(list, ' ');
133 if (ptr == NULL) ptr = list+strlen(list);
135 if (g_strncasecmp(list, item, ptr-list) == 0 &&
136 item[ptr-list] == '\0')
145 int strarray_length(char **array)
149 g_return_val_if_fail(array != NULL, 0);
159 int strarray_find(char **array, const char *item)
164 g_return_val_if_fail(array != NULL, 0);
165 g_return_val_if_fail(item != NULL, 0);
168 for (tmp = array; *tmp != NULL; tmp++, index++) {
169 if (g_strcasecmp(*tmp, item) == 0)
176 GSList *gslist_find_string(GSList *list, const char *key)
178 for (list = list; list != NULL; list = list->next)
179 if (strcmp(list->data, key) == 0) return list;
184 GSList *gslist_find_icase_string(GSList *list, const char *key)
186 for (list = list; list != NULL; list = list->next)
187 if (g_strcasecmp(list->data, key) == 0) return list;
192 void *gslist_foreach_find(GSList *list, FOREACH_FIND_FUNC func, const void *data)
196 while (list != NULL) {
197 ret = func(list->data, (void *) data);
198 if (ret != NULL) return ret;
206 /* `list' contains pointer to structure with a char* to string. */
207 char *gslistptr_to_string(GSList *list, int offset, const char *delimiter)
212 str = g_string_new(NULL);
213 while (list != NULL) {
214 data = G_STRUCT_MEMBER_P(list->data, offset);
216 if (str->len != 0) g_string_append(str, delimiter);
217 g_string_append(str, *data);
222 g_string_free(str, FALSE);
226 /* `list' contains char* */
227 char *gslist_to_string(GSList *list, const char *delimiter)
232 str = g_string_new(NULL);
233 while (list != NULL) {
234 if (str->len != 0) g_string_append(str, delimiter);
235 g_string_append(str, list->data);
241 g_string_free(str, FALSE);
245 void hash_save_key(char *key, void *value, GSList **list)
247 *list = g_slist_append(*list, key);
250 /* save all keys in hash table to linked list - you shouldn't remove any
251 items while using this list, use g_slist_free() after you're done with it */
252 GSList *hashtable_get_keys(GHashTable *hash)
257 g_hash_table_foreach(hash, (GHFunc) hash_save_key, &list);
261 GList *glist_find_string(GList *list, const char *key)
263 for (list = list; list != NULL; list = list->next)
264 if (strcmp(list->data, key) == 0) return list;
269 GList *glist_find_icase_string(GList *list, const char *key)
271 for (list = list; list != NULL; list = list->next)
272 if (g_strcasecmp(list->data, key) == 0) return list;
277 char *stristr(const char *data, const char *key)
280 int keylen, datalen, pos;
282 keylen = strlen(key);
283 datalen = strlen(data);
285 if (keylen > datalen)
288 return (char *) data;
290 max = data+datalen-keylen;
292 while (data <= max) {
293 if (key[pos] == '\0')
294 return (char *) data;
296 if (i_toupper(data[pos]) == i_toupper(key[pos]))
308 ((unsigned char) (c) < 128 && \
309 (i_isspace(c) || i_ispunct(c)))
311 char *strstr_full_case(const char *data, const char *key, int icase)
313 const char *start, *max;
314 int keylen, datalen, pos, match;
316 keylen = strlen(key);
317 datalen = strlen(data);
319 if (keylen > datalen)
322 return (char *) data;
324 max = data+datalen-keylen;
325 start = data; pos = 0;
326 while (data <= max) {
327 if (key[pos] == '\0') {
328 if (data[pos] != '\0' && !isbound(data[pos])) {
333 return (char *) data;
336 match = icase ? (i_toupper(data[pos]) == i_toupper(key[pos])) :
337 data[pos] == key[pos];
339 if (match && (pos != 0 || data == start || isbound(data[-1])))
350 char *strstr_full(const char *data, const char *key)
352 return strstr_full_case(data, key, FALSE);
355 char *stristr_full(const char *data, const char *key)
357 return strstr_full_case(data, key, TRUE);
360 int regexp_match(const char *str, const char *regexp)
366 if (regcomp(&preg, regexp, REG_EXTENDED|REG_ICASE|REG_NOSUB) != 0)
369 ret = regexec(&preg, str, 0, NULL, 0);
378 /* Create the directory and all it's parent directories */
379 int mkpath(const char *path, int mode)
385 g_return_val_if_fail(path != NULL, -1);
387 p = g_path_skip_root((char *) path);
389 /* not a full path, maybe not what we wanted
390 but continue anyway.. */
394 if (*p != G_DIR_SEPARATOR && *p != '\0') {
399 dir = g_strndup(path, (int) (p-path));
400 if (stat(dir, &statbuf) != 0) {
402 if (mkdir(dir, mode) == -1)
404 if (_mkdir(dir) == -1)
420 /* convert ~/ to $HOME */
421 char *convert_home(const char *path)
425 if (*path == '~' && (*(path+1) == '/' || *(path+1) == '\0')) {
426 home = g_get_home_dir();
430 return g_strconcat(home, path+1, NULL);
432 return g_strdup(path);
436 int g_istr_equal(gconstpointer v, gconstpointer v2)
438 return g_strcasecmp((const char *) v, (const char *) v2) == 0;
441 int g_istr_cmp(gconstpointer v, gconstpointer v2)
443 return g_strcasecmp((const char *) v, (const char *) v2);
446 /* a char* hash function from ASU */
447 unsigned int g_istr_hash(gconstpointer v)
449 const char *s = (const char *) v;
450 unsigned int h = 0, g;
453 h = (h << 4) + i_toupper(*s);
454 if ((g = h & 0xf0000000UL)) {
464 /* Find `mask' from `data', you can use * and ? wildcards. */
465 int match_wildcards(const char *cmask, const char *data)
467 char *mask, *newmask, *p1, *p2;
470 newmask = mask = g_strdup(cmask);
471 for (; *mask != '\0' && *data != '\0'; mask++) {
473 if (*mask != '?' && i_toupper(*mask) != i_toupper(*data))
480 while (*mask == '?' || *mask == '*') mask++;
482 data += strlen(data);
486 p1 = strchr(mask, '*');
487 p2 = strchr(mask, '?');
488 if (p1 == NULL || (p2 < p1 && p2 != NULL)) p1 = p2;
490 if (p1 != NULL) *p1 = '\0';
492 data = stristr(data, mask);
493 if (data == NULL) break;
495 data += strlen(mask);
496 mask += strlen(mask)-1;
498 if (p1 != NULL) *p1 = p1 == p2 ? '?' : '*';
501 while (*mask == '*') mask++;
503 ret = data != NULL && *data == '\0' && *mask == '\0';
509 /* Return TRUE if all characters in `str' are numbers.
510 Stop when `end_char' is found from string. */
511 int is_numeric(const char *str, char end_char)
513 g_return_val_if_fail(str != NULL, FALSE);
515 if (*str == '\0' || *str == end_char)
518 while (*str != '\0' && *str != end_char) {
519 if (!i_isdigit(*str)) return FALSE;
526 /* replace all `from' chars in string to `to' chars. returns `str' */
527 char *replace_chars(char *str, char from, char to)
531 for (p = str; *p != '\0'; p++) {
532 if (*p == from) *p = to;
537 int octal2dec(int octal)
550 int dec2octal(int decimal)
555 while (decimal > 0) {
556 octal += (decimal & 7)*(pos == 0 ? 1 : pos);
564 /* string -> uoff_t */
565 uoff_t str_to_uofft(const char *str)
570 while (*str != '\0') {
571 ret = ret*10 + (*str - '0');
578 /* convert all low-ascii (<32) to ^<A..> combinations */
579 char *show_lowascii(const char *channel)
583 str = p = g_malloc(strlen(channel)*2+1);
584 while (*channel != '\0') {
585 if ((unsigned char) *channel >= 32)
589 *p++ = *channel + 'A'-1;
598 /* Get time in human readable form with localtime() + asctime() */
599 char *my_asctime(time_t t)
606 str = g_strdup(asctime(tm));
609 if (len > 0) str[len-1] = '\0';
613 /* Returns number of columns needed to print items.
614 save_column_widths is filled with length of each column. */
615 int get_max_column_count(GSList *items, COLUMN_LEN_FUNC len_func,
616 int max_width, int max_columns,
617 int item_extra, int item_min_size,
618 int **save_column_widths, int *rows)
621 int **columns, *columns_width, *columns_rows;
622 int item_pos, items_count;
623 int ret, len, max_len, n, col;
625 items_count = g_slist_length(items);
626 if (items_count == 0) {
627 *save_column_widths = NULL;
632 len = max_width/(item_extra+item_min_size);
633 if (len <= 0) len = 1;
634 if (max_columns <= 0 || len < max_columns)
637 columns = g_new0(int *, max_columns);
638 columns_width = g_new0(int, max_columns);
639 columns_rows = g_new0(int, max_columns);
641 for (n = 1; n < max_columns; n++) {
642 columns[n] = g_new0(int, n+1);
643 columns_rows[n] = items_count <= n+1 ? 1 :
644 (items_count+n)/(n+1);
647 /* for each possible column count, save the column widths and
648 find the biggest column count that fits to screen. */
649 item_pos = 0; max_len = 0;
650 for (tmp = items; tmp != NULL; tmp = tmp->next) {
651 len = item_extra+len_func(tmp->data);
655 for (n = 1; n < max_columns; n++) {
656 if (columns_width[n] > max_width)
657 continue; /* too wide */
659 col = item_pos/columns_rows[n];
660 if (columns[n][col] < len) {
661 columns_width[n] += len-columns[n][col];
662 columns[n][col] = len;
669 for (n = max_columns-1; n >= 1; n--) {
670 if (columns_width[n] <= max_width &&
676 *save_column_widths = g_new(int, ret);
678 **save_column_widths = max_len;
681 memcpy(*save_column_widths, columns[ret-1], sizeof(int)*ret);
682 *rows = columns_rows[ret-1];
685 for (n = 1; n < max_columns; n++)
687 g_free(columns_width);
688 g_free(columns_rows);
694 /* Return a column sorted copy of a list. */
695 GSList *columns_sort_list(GSList *list, int rows)
697 GSList *tmp, *sorted;
700 if (list == NULL || rows == 0)
705 for (row = 0; row < rows; row++) {
706 tmp = g_slist_nth(list, row);
708 for (; tmp != NULL; tmp = tmp->next) {
711 sorted = g_slist_append(sorted, tmp->data);
716 g_return_val_if_fail(g_slist_length(sorted) ==
717 g_slist_length(list), sorted);
721 /* Expand escape string, the first character in data should be the
722 one after '\'. Returns the expanded character or -1 if error. */
723 int expand_escape(const char **data)
739 if (!i_isxdigit((*data)[1]) || !i_isxdigit((*data)[2]))
742 digit[0] = (*data)[1];
743 digit[1] = (*data)[2];
746 return strtol(digit, NULL, 16);
748 /* control character (\cA = ^A) */
750 return i_toupper(**data) - 64;
752 if (!i_isdigit(**data))
756 digit[0] = (*data)[0];
757 digit[1] = (*data)[1];
758 digit[2] = (*data)[2];
761 return strtol(digit, NULL, 8);
765 /* Escape all '"', "'" and '\' chars with '\' */
766 char *escape_string(const char *str)
770 p = ret = g_malloc(strlen(str)*2+1);
771 while (*str != '\0') {
772 if (*str == '"' || *str == '\'' || *str == '\\')
781 int strocpy(char *dest, const char *src, size_t dstsize)
786 while (*src != '\0' && dstsize > 1) {
792 return *src == '\0' ? 0 : -1;
795 int nearest_power(int num)
799 while (n < num) n <<= 1;
803 int parse_time_interval(const char *time, int *msecs)
806 int number, sign, len, ret;
810 /* max. return value is around 24 days */
811 number = 0; sign = 1; ret = TRUE;
819 if (i_isdigit(*time)) {
820 number = number*10 + (*time - '0');
825 /* skip punctuation */
826 while (*time != '\0' && i_ispunct(*time))
829 /* get description */
830 for (len = 0, desc = time; i_isalpha(*time); time++)
834 *msecs += number * 1000; /* assume seconds */
839 if (g_strncasecmp(desc, "days", len) == 0) {
844 *msecs += number * 1000*3600*24;
845 } else if (g_strncasecmp(desc, "hours", len) == 0)
846 *msecs += number * 1000*3600;
847 else if (g_strncasecmp(desc, "minutes", len) == 0 ||
848 g_strncasecmp(desc, "mins", len) == 0)
849 *msecs += number * 1000*60;
850 else if (g_strncasecmp(desc, "seconds", len) == 0 ||
851 g_strncasecmp(desc, "secs", len) == 0)
852 *msecs += number * 1000;
853 else if (g_strncasecmp(desc, "milliseconds", len) == 0 ||
854 g_strncasecmp(desc, "millisecs", len) == 0 ||
855 g_strncasecmp(desc, "mseconds", len) == 0 ||
856 g_strncasecmp(desc, "msecs", len) == 0)
862 /* skip punctuation */
863 while (*time != '\0' && i_ispunct(*time))
876 int parse_size(const char *size, int *bytes)
883 /* max. return value is about 1.6 years */
885 while (*size != '\0') {
886 if (i_isdigit(*size)) {
887 number = number*10 + (*size - '0');
892 /* skip punctuation */
893 while (*size != '\0' && i_ispunct(*size))
896 /* get description */
897 for (len = 0, desc = size; i_isalpha(*size); size++)
906 *bytes += number*1024; /* assume kilobytes */
910 if (g_strncasecmp(desc, "gbytes", len) == 0)
911 *bytes += number * 1024*1024*1024;
912 if (g_strncasecmp(desc, "mbytes", len) == 0)
913 *bytes += number * 1024*1024;
914 if (g_strncasecmp(desc, "kbytes", len) == 0)
915 *bytes += number * 1024;
916 if (g_strncasecmp(desc, "bytes", len) == 0)
919 /* skip punctuation */
920 while (*size != '\0' && i_ispunct(*size))