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 int execute(const char *cmd)
183 g_return_val_if_fail(cmd != NULL, -1);
187 if (pid == -1) return FALSE;
193 args = g_strsplit(cmd, " ", -1);
194 execvp(args[0], args);
200 args = g_strsplit(cmd, " ", -1);
201 _spawnvp(_P_DETACH, args[0], args);
207 GSList *gslist_find_string(GSList *list, const char *key)
209 for (list = list; list != NULL; list = list->next)
210 if (strcmp(list->data, key) == 0) return list;
215 GSList *gslist_find_icase_string(GSList *list, const char *key)
217 for (list = list; list != NULL; list = list->next)
218 if (g_strcasecmp(list->data, key) == 0) return list;
223 void *gslist_foreach_find(GSList *list, FOREACH_FIND_FUNC func, const void *data)
227 while (list != NULL) {
228 ret = func(list->data, (void *) data);
229 if (ret != NULL) return ret;
237 /* `list' contains pointer to structure with a char* to string. */
238 char *gslistptr_to_string(GSList *list, int offset, const char *delimiter)
243 str = g_string_new(NULL);
244 while (list != NULL) {
245 data = G_STRUCT_MEMBER_P(list->data, offset);
247 if (str->len != 0) g_string_append(str, delimiter);
248 g_string_append(str, *data);
253 g_string_free(str, FALSE);
257 /* `list' contains char* */
258 char *gslist_to_string(GSList *list, const char *delimiter)
263 str = g_string_new(NULL);
264 while (list != NULL) {
265 if (str->len != 0) g_string_append(str, delimiter);
266 g_string_append(str, list->data);
272 g_string_free(str, FALSE);
276 void hash_save_key(char *key, void *value, GSList **list)
278 *list = g_slist_append(*list, key);
281 /* save all keys in hash table to linked list - you shouldn't remove any
282 items while using this list, use g_slist_free() after you're done with it */
283 GSList *hashtable_get_keys(GHashTable *hash)
288 g_hash_table_foreach(hash, (GHFunc) hash_save_key, &list);
292 GList *glist_find_string(GList *list, const char *key)
294 for (list = list; list != NULL; list = list->next)
295 if (strcmp(list->data, key) == 0) return list;
300 GList *glist_find_icase_string(GList *list, const char *key)
302 for (list = list; list != NULL; list = list->next)
303 if (g_strcasecmp(list->data, key) == 0) return list;
308 char *stristr(const char *data, const char *key)
311 int keylen, datalen, pos;
313 keylen = strlen(key);
314 datalen = strlen(data);
316 if (keylen > datalen)
319 return (char *) data;
321 max = data+datalen-keylen;
323 while (data <= max) {
324 if (key[pos] == '\0')
325 return (char *) data;
327 if (i_toupper(data[pos]) == i_toupper(key[pos]))
339 ((unsigned char) (c) < 128 && \
340 (i_isspace(c) || i_ispunct(c)))
342 char *strstr_full_case(const char *data, const char *key, int icase)
344 const char *start, *max;
345 int keylen, datalen, pos, match;
347 keylen = strlen(key);
348 datalen = strlen(data);
350 if (keylen > datalen)
353 return (char *) data;
355 max = data+datalen-keylen;
356 start = data; pos = 0;
357 while (data <= max) {
358 if (key[pos] == '\0') {
359 if (data[pos] != '\0' && !isbound(data[pos])) {
364 return (char *) data;
367 match = icase ? (i_toupper(data[pos]) == i_toupper(key[pos])) :
368 data[pos] == key[pos];
370 if (match && (pos != 0 || data == start || isbound(data[-1])))
381 char *strstr_full(const char *data, const char *key)
383 return strstr_full_case(data, key, FALSE);
386 char *stristr_full(const char *data, const char *key)
388 return strstr_full_case(data, key, TRUE);
391 int regexp_match(const char *str, const char *regexp)
397 if (regcomp(&preg, regexp, REG_EXTENDED|REG_ICASE|REG_NOSUB) != 0)
400 ret = regexec(&preg, str, 0, NULL, 0);
409 /* Create the directory and all it's parent directories */
410 int mkpath(const char *path, int mode)
416 g_return_val_if_fail(path != NULL, -1);
418 p = g_path_skip_root((char *) path);
420 /* not a full path, maybe not what we wanted
421 but continue anyway.. */
425 if (*p != G_DIR_SEPARATOR && *p != '\0') {
430 dir = g_strndup(path, (int) (p-path));
431 if (stat(dir, &statbuf) != 0) {
433 if (mkdir(dir, mode) == -1)
435 if (_mkdir(dir) == -1)
451 /* convert ~/ to $HOME */
452 char *convert_home(const char *path)
454 return *path == '~' && (*(path+1) == '/' || *(path+1) == '\0') ?
455 g_strconcat(g_get_home_dir(), path+1, NULL) :
459 int g_istr_equal(gconstpointer v, gconstpointer v2)
461 return g_strcasecmp((const char *) v, (const char *) v2) == 0;
464 int g_istr_cmp(gconstpointer v, gconstpointer v2)
466 return g_strcasecmp((const char *) v, (const char *) v2);
469 /* a char* hash function from ASU */
470 unsigned int g_istr_hash(gconstpointer v)
472 const char *s = (const char *) v;
473 unsigned int h = 0, g;
476 h = (h << 4) + i_toupper(*s);
477 if ((g = h & 0xf0000000UL)) {
487 /* Find `mask' from `data', you can use * and ? wildcards. */
488 int match_wildcards(const char *cmask, const char *data)
490 char *mask, *newmask, *p1, *p2;
493 newmask = mask = g_strdup(cmask);
494 for (; *mask != '\0' && *data != '\0'; mask++) {
496 if (*mask != '?' && i_toupper(*mask) != i_toupper(*data))
503 while (*mask == '?' || *mask == '*') mask++;
505 data += strlen(data);
509 p1 = strchr(mask, '*');
510 p2 = strchr(mask, '?');
511 if (p1 == NULL || (p2 < p1 && p2 != NULL)) p1 = p2;
513 if (p1 != NULL) *p1 = '\0';
515 data = stristr(data, mask);
516 if (data == NULL) break;
518 data += strlen(mask);
519 mask += strlen(mask)-1;
521 if (p1 != NULL) *p1 = p1 == p2 ? '?' : '*';
524 while (*mask == '*') mask++;
526 ret = data != NULL && *data == '\0' && *mask == '\0';
532 /* Return TRUE if all characters in `str' are numbers.
533 Stop when `end_char' is found from string. */
534 int is_numeric(const char *str, char end_char)
536 g_return_val_if_fail(str != NULL, FALSE);
538 if (*str == '\0' || *str == end_char)
541 while (*str != '\0' && *str != end_char) {
542 if (!i_isdigit(*str)) return FALSE;
549 /* replace all `from' chars in string to `to' chars. returns `str' */
550 char *replace_chars(char *str, char from, char to)
554 for (p = str; *p != '\0'; p++) {
555 if (*p == from) *p = to;
560 int octal2dec(int octal)
573 int dec2octal(int decimal)
578 while (decimal > 0) {
579 octal += (decimal & 7)*(pos == 0 ? 1 : pos);
587 /* convert all low-ascii (<32) to ^<A..> combinations */
588 char *show_lowascii(const char *channel)
592 str = p = g_malloc(strlen(channel)*2+1);
593 while (*channel != '\0') {
594 if ((unsigned char) *channel >= 32)
598 *p++ = *channel + 'A'-1;
607 /* Get time in human readable form with localtime() + asctime() */
608 char *my_asctime(time_t t)
615 str = g_strdup(asctime(tm));
618 if (len > 0) str[len-1] = '\0';
622 /* Returns number of columns needed to print items.
623 save_column_widths is filled with length of each column. */
624 int get_max_column_count(GSList *items, COLUMN_LEN_FUNC len_func,
625 int max_width, int max_columns,
626 int item_extra, int item_min_size,
627 int **save_column_widths, int *rows)
630 int **columns, *columns_width, *columns_rows;
631 int item_pos, items_count;
632 int ret, len, max_len, n, col;
634 items_count = g_slist_length(items);
635 if (items_count == 0) {
636 *save_column_widths = NULL;
641 len = max_width/(item_extra+item_min_size);
642 if (len <= 0) len = 1;
643 if (max_columns <= 0 || len < max_columns)
646 columns = g_new0(int *, max_columns);
647 columns_width = g_new0(int, max_columns);
648 columns_rows = g_new0(int, max_columns);
650 for (n = 1; n < max_columns; n++) {
651 columns[n] = g_new0(int, n+1);
652 columns_rows[n] = items_count <= n+1 ? 1 :
653 (items_count+n)/(n+1);
656 /* for each possible column count, save the column widths and
657 find the biggest column count that fits to screen. */
658 item_pos = 0; max_len = 0;
659 for (tmp = items; tmp != NULL; tmp = tmp->next) {
660 len = item_extra+len_func(tmp->data);
664 for (n = 1; n < max_columns; n++) {
665 if (columns_width[n] > max_width)
666 continue; /* too wide */
668 col = item_pos/columns_rows[n];
669 if (columns[n][col] < len) {
670 columns_width[n] += len-columns[n][col];
671 columns[n][col] = len;
678 for (n = max_columns-1; n >= 1; n--) {
679 if (columns_width[n] <= max_width &&
685 *save_column_widths = g_new(int, ret);
687 **save_column_widths = max_len;
690 memcpy(*save_column_widths, columns[ret-1], sizeof(int)*ret);
691 *rows = columns_rows[ret-1];
694 for (n = 1; n < max_columns; n++)
696 g_free(columns_width);
697 g_free(columns_rows);
703 /* Return a column sorted copy of a list. */
704 GSList *columns_sort_list(GSList *list, int rows)
706 GSList *tmp, *sorted;
709 if (list == NULL || rows == 0)
714 for (row = 0; row < rows; row++) {
715 tmp = g_slist_nth(list, row);
717 for (; tmp != NULL; tmp = tmp->next) {
720 sorted = g_slist_append(sorted, tmp->data);
725 g_return_val_if_fail(g_slist_length(sorted) ==
726 g_slist_length(list), sorted);
730 /* Expand escape string, the first character in data should be the
731 one after '\'. Returns the expanded character or -1 if error. */
732 int expand_escape(const char **data)
748 if (!i_isxdigit((*data)[1]) || !i_isxdigit((*data)[2]))
751 digit[0] = (*data)[1];
752 digit[1] = (*data)[2];
755 return strtol(digit, NULL, 16);
757 /* control character (\cA = ^A) */
759 return i_toupper(**data) - 64;
761 if (!i_isdigit(**data))
765 digit[0] = (*data)[0];
766 digit[1] = (*data)[1];
767 digit[2] = (*data)[2];
770 return strtol(digit, NULL, 8);