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
33 GInputFunction function;
37 static int irssi_io_invoke(GIOChannel *source, GIOCondition condition,
40 IRSSI_INPUT_REC *rec = data;
43 if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
44 /* error, we have to call the function.. */
45 if (rec->condition & G_IO_IN)
46 icond |= G_INPUT_READ;
48 icond |= G_INPUT_WRITE;
51 if (condition & (G_IO_IN | G_IO_PRI))
52 icond |= G_INPUT_READ;
53 if (condition & G_IO_OUT)
54 icond |= G_INPUT_WRITE;
56 if (rec->condition & icond)
57 rec->function(rec->data, source, icond);
62 int g_input_add_full(GIOChannel *source, int priority, int condition,
63 GInputFunction function, void *data)
69 rec = g_new(IRSSI_INPUT_REC, 1);
70 rec->condition = condition;
71 rec->function = function;
74 cond = (GIOCondition) (G_IO_ERR|G_IO_HUP|G_IO_NVAL);
75 if (condition & G_INPUT_READ)
76 cond |= G_IO_IN|G_IO_PRI;
77 if (condition & G_INPUT_WRITE)
80 result = g_io_add_watch_full(source, priority, cond,
81 irssi_io_invoke, rec, g_free);
86 int g_input_add(GIOChannel *source, int condition,
87 GInputFunction function, void *data)
89 return g_input_add_full(source, G_PRIORITY_DEFAULT, condition,
93 int g_timeval_cmp(const GTimeVal *tv1, const GTimeVal *tv2)
95 if (tv1->tv_sec < tv2->tv_sec)
97 if (tv1->tv_sec > tv2->tv_sec)
100 return tv1->tv_usec < tv2->tv_usec ? -1 :
101 tv1->tv_usec > tv2->tv_usec ? 1 : 0;
104 long get_timeval_diff(const GTimeVal *tv1, const GTimeVal *tv2)
108 secs = tv1->tv_sec - tv2->tv_sec;
109 usecs = tv1->tv_usec - tv2->tv_usec;
114 usecs = usecs/1000 + secs * 1000;
119 int find_substr(const char *list, const char *item)
123 g_return_val_if_fail(list != NULL, FALSE);
124 g_return_val_if_fail(item != NULL, FALSE);
130 while (i_isspace(*list)) list++;
131 if (*list == '\0') break;
133 ptr = strchr(list, ' ');
134 if (ptr == NULL) ptr = list+strlen(list);
136 if (g_strncasecmp(list, item, ptr-list) == 0 &&
137 item[ptr-list] == '\0')
146 int strarray_length(char **array)
150 g_return_val_if_fail(array != NULL, 0);
160 int strarray_find(char **array, const char *item)
165 g_return_val_if_fail(array != NULL, 0);
166 g_return_val_if_fail(item != NULL, 0);
169 for (tmp = array; *tmp != NULL; tmp++, index++) {
170 if (g_strcasecmp(*tmp, item) == 0)
177 int execute(const char *cmd)
184 g_return_val_if_fail(cmd != NULL, -1);
188 if (pid == -1) return FALSE;
194 args = g_strsplit(cmd, " ", -1);
195 execvp(args[0], args);
201 args = g_strsplit(cmd, " ", -1);
202 _spawnvp(_P_DETACH, args[0], args);
208 GSList *gslist_find_string(GSList *list, const char *key)
210 for (list = list; list != NULL; list = list->next)
211 if (strcmp(list->data, key) == 0) return list;
216 GSList *gslist_find_icase_string(GSList *list, const char *key)
218 for (list = list; list != NULL; list = list->next)
219 if (g_strcasecmp(list->data, key) == 0) return list;
224 void *gslist_foreach_find(GSList *list, FOREACH_FIND_FUNC func, const void *data)
228 while (list != NULL) {
229 ret = func(list->data, (void *) data);
230 if (ret != NULL) return ret;
238 /* `list' contains pointer to structure with a char* to string. */
239 char *gslistptr_to_string(GSList *list, int offset, const char *delimiter)
244 str = g_string_new(NULL);
245 while (list != NULL) {
246 data = G_STRUCT_MEMBER_P(list->data, offset);
248 if (str->len != 0) g_string_append(str, delimiter);
249 g_string_append(str, *data);
254 g_string_free(str, FALSE);
258 /* `list' contains char* */
259 char *gslist_to_string(GSList *list, const char *delimiter)
264 str = g_string_new(NULL);
265 while (list != NULL) {
266 if (str->len != 0) g_string_append(str, delimiter);
267 g_string_append(str, list->data);
273 g_string_free(str, FALSE);
277 void hash_save_key(char *key, void *value, GSList **list)
279 *list = g_slist_append(*list, key);
282 /* save all keys in hash table to linked list - you shouldn't remove any
283 items while using this list, use g_slist_free() after you're done with it */
284 GSList *hashtable_get_keys(GHashTable *hash)
289 g_hash_table_foreach(hash, (GHFunc) hash_save_key, &list);
293 GList *glist_find_string(GList *list, const char *key)
295 for (list = list; list != NULL; list = list->next)
296 if (strcmp(list->data, key) == 0) return list;
301 GList *glist_find_icase_string(GList *list, const char *key)
303 for (list = list; list != NULL; list = list->next)
304 if (g_strcasecmp(list->data, key) == 0) return list;
309 char *stristr(const char *data, const char *key)
312 int keylen, datalen, pos;
314 keylen = strlen(key);
315 datalen = strlen(data);
317 if (keylen > datalen)
320 return (char *) data;
322 max = data+datalen-keylen;
324 while (data <= max) {
325 if (key[pos] == '\0')
326 return (char *) data;
328 if (i_toupper(data[pos]) == i_toupper(key[pos]))
340 ((unsigned char) (c) < 128 && \
341 (i_isspace(c) || i_ispunct(c)))
343 char *strstr_full_case(const char *data, const char *key, int icase)
345 const char *start, *max;
346 int keylen, datalen, pos, match;
348 keylen = strlen(key);
349 datalen = strlen(data);
351 if (keylen > datalen)
354 return (char *) data;
356 max = data+datalen-keylen;
357 start = data; pos = 0;
358 while (data <= max) {
359 if (key[pos] == '\0') {
360 if (data[pos] != '\0' && !isbound(data[pos])) {
365 return (char *) data;
368 match = icase ? (i_toupper(data[pos]) == i_toupper(key[pos])) :
369 data[pos] == key[pos];
371 if (match && (pos != 0 || data == start || isbound(data[-1])))
382 char *strstr_full(const char *data, const char *key)
384 return strstr_full_case(data, key, FALSE);
387 char *stristr_full(const char *data, const char *key)
389 return strstr_full_case(data, key, TRUE);
392 int regexp_match(const char *str, const char *regexp)
398 if (regcomp(&preg, regexp, REG_EXTENDED|REG_ICASE|REG_NOSUB) != 0)
401 ret = regexec(&preg, str, 0, NULL, 0);
410 /* Create the directory and all it's parent directories */
411 int mkpath(const char *path, int mode)
417 g_return_val_if_fail(path != NULL, -1);
419 p = g_path_skip_root((char *) path);
421 /* not a full path, maybe not what we wanted
422 but continue anyway.. */
426 if (*p != G_DIR_SEPARATOR && *p != '\0') {
431 dir = g_strndup(path, (int) (p-path));
432 if (stat(dir, &statbuf) != 0) {
434 if (mkdir(dir, mode) == -1)
436 if (_mkdir(dir) == -1)
452 /* Get home directory */
453 const char *get_home_dir(void)
455 struct passwd *pw = getpwuid(getuid());
457 if (g_getenv("HOME"))
458 return g_getenv("HOME");
465 /* convert ~/ to $HOME */
466 char *convert_home(const char *path)
468 return *path == '~' && (*(path+1) == '/' || *(path+1) == '\0') ?
469 g_strconcat((char *)get_home_dir, path+1, NULL) :
473 int g_istr_equal(gconstpointer v, gconstpointer v2)
475 return g_strcasecmp((const char *) v, (const char *) v2) == 0;
478 int g_istr_cmp(gconstpointer v, gconstpointer v2)
480 return g_strcasecmp((const char *) v, (const char *) v2);
483 /* a char* hash function from ASU */
484 unsigned int g_istr_hash(gconstpointer v)
486 const char *s = (const char *) v;
487 unsigned int h = 0, g;
490 h = (h << 4) + i_toupper(*s);
491 if ((g = h & 0xf0000000UL)) {
501 /* Find `mask' from `data', you can use * and ? wildcards. */
502 int match_wildcards(const char *cmask, const char *data)
504 char *mask, *newmask, *p1, *p2;
507 newmask = mask = g_strdup(cmask);
508 for (; *mask != '\0' && *data != '\0'; mask++) {
510 if (*mask != '?' && i_toupper(*mask) != i_toupper(*data))
517 while (*mask == '?' || *mask == '*') mask++;
519 data += strlen(data);
523 p1 = strchr(mask, '*');
524 p2 = strchr(mask, '?');
525 if (p1 == NULL || (p2 < p1 && p2 != NULL)) p1 = p2;
527 if (p1 != NULL) *p1 = '\0';
529 data = stristr(data, mask);
530 if (data == NULL) break;
532 data += strlen(mask);
533 mask += strlen(mask)-1;
535 if (p1 != NULL) *p1 = p1 == p2 ? '?' : '*';
538 while (*mask == '*') mask++;
540 ret = data != NULL && *data == '\0' && *mask == '\0';
546 /* Return TRUE if all characters in `str' are numbers.
547 Stop when `end_char' is found from string. */
548 int is_numeric(const char *str, char end_char)
550 g_return_val_if_fail(str != NULL, FALSE);
552 if (*str == '\0' || *str == end_char)
555 while (*str != '\0' && *str != end_char) {
556 if (!i_isdigit(*str)) return FALSE;
563 /* replace all `from' chars in string to `to' chars. returns `str' */
564 char *replace_chars(char *str, char from, char to)
568 for (p = str; *p != '\0'; p++) {
569 if (*p == from) *p = to;
574 int octal2dec(int octal)
587 int dec2octal(int decimal)
592 while (decimal > 0) {
593 octal += (decimal & 7)*(pos == 0 ? 1 : pos);
601 /* convert all low-ascii (<32) to ^<A..> combinations */
602 char *show_lowascii(const char *channel)
606 str = p = g_malloc(strlen(channel)*2+1);
607 while (*channel != '\0') {
608 if ((unsigned char) *channel >= 32)
612 *p++ = *channel + 'A'-1;
621 /* Get time in human readable form with localtime() + asctime() */
622 char *my_asctime(time_t t)
629 str = g_strdup(asctime(tm));
632 if (len > 0) str[len-1] = '\0';
636 /* Returns number of columns needed to print items.
637 save_column_widths is filled with length of each column. */
638 int get_max_column_count(GSList *items, COLUMN_LEN_FUNC len_func,
639 int max_width, int max_columns,
640 int item_extra, int item_min_size,
641 int **save_column_widths, int *rows)
644 int **columns, *columns_width, *columns_rows;
645 int item_pos, items_count;
646 int ret, len, max_len, n, col;
648 items_count = g_slist_length(items);
649 if (items_count == 0) {
650 *save_column_widths = NULL;
655 len = max_width/(item_extra+item_min_size);
656 if (len <= 0) len = 1;
657 if (max_columns <= 0 || len < max_columns)
660 columns = g_new0(int *, max_columns);
661 columns_width = g_new0(int, max_columns);
662 columns_rows = g_new0(int, max_columns);
664 for (n = 1; n < max_columns; n++) {
665 columns[n] = g_new0(int, n+1);
666 columns_rows[n] = items_count <= n+1 ? 1 :
667 (items_count+n)/(n+1);
670 /* for each possible column count, save the column widths and
671 find the biggest column count that fits to screen. */
672 item_pos = 0; max_len = 0;
673 for (tmp = items; tmp != NULL; tmp = tmp->next) {
674 len = item_extra+len_func(tmp->data);
678 for (n = 1; n < max_columns; n++) {
679 if (columns_width[n] > max_width)
680 continue; /* too wide */
682 col = item_pos/columns_rows[n];
683 if (columns[n][col] < len) {
684 columns_width[n] += len-columns[n][col];
685 columns[n][col] = len;
692 for (n = max_columns-1; n >= 1; n--) {
693 if (columns_width[n] <= max_width &&
699 *save_column_widths = g_new(int, ret);
701 **save_column_widths = max_len;
704 memcpy(*save_column_widths, columns[ret-1], sizeof(int)*ret);
705 *rows = columns_rows[ret-1];
708 for (n = 1; n < max_columns; n++)
710 g_free(columns_width);
711 g_free(columns_rows);
717 /* Return a column sorted copy of a list. */
718 GSList *columns_sort_list(GSList *list, int rows)
720 GSList *tmp, *sorted;
723 if (list == NULL || rows == 0)
728 for (row = 0; row < rows; row++) {
729 tmp = g_slist_nth(list, row);
731 for (; tmp != NULL; tmp = tmp->next) {
734 sorted = g_slist_append(sorted, tmp->data);
739 g_return_val_if_fail(g_slist_length(sorted) ==
740 g_slist_length(list), sorted);
744 /* Expand escape string, the first character in data should be the
745 one after '\'. Returns the expanded character or -1 if error. */
746 int expand_escape(const char **data)
762 if (!i_isxdigit((*data)[1]) || !i_isxdigit((*data)[2]))
765 digit[0] = (*data)[1];
766 digit[1] = (*data)[2];
769 return strtol(digit, NULL, 16);
771 /* control character (\cA = ^A) */
773 return i_toupper(**data) - 64;
775 if (!i_isdigit(**data))
779 digit[0] = (*data)[0];
780 digit[1] = (*data)[1];
781 digit[2] = (*data)[2];
784 return strtol(digit, NULL, 8);