Merged with Irssi 0.8.6.
[silc.git] / apps / irssi / src / core / misc.h
1 #ifndef __MISC_H
2 #define __MISC_H
3
4 /* `str' should be type char[MAX_INT_STRLEN] */
5 #define ltoa(str, num) \
6         g_snprintf(str, sizeof(str), "%d", num)
7
8 typedef void* (*FOREACH_FIND_FUNC) (void *item, void *data);
9 typedef int (*COLUMN_LEN_FUNC)(void *data);
10
11 static inline int nearest_power(int num)
12 {
13         int n = 1;
14
15         while (n < num) n <<= 1;
16         return n;
17 }
18
19 /* Returns 1 if tv1 > tv2, -1 if tv2 > tv1 or 0 if they're equal. */
20 int g_timeval_cmp(const GTimeVal *tv1, const GTimeVal *tv2);
21 /* Returns "tv1 - tv2", returns the result in milliseconds. Note that
22    if the difference is too large, the result might be invalid. */
23 long get_timeval_diff(const GTimeVal *tv1, const GTimeVal *tv2);
24
25 /* find `item' from a space separated `list' */
26 int find_substr(const char *list, const char *item);
27 /* return how many items `array' has */
28 int strarray_length(char **array);
29 /* return index of `item' in `array' or -1 if not found */
30 int strarray_find(char **array, const char *item);
31
32 int execute(const char *cmd); /* returns pid or -1 = error */
33
34 GSList *gslist_find_string(GSList *list, const char *key);
35 GSList *gslist_find_icase_string(GSList *list, const char *key);
36 GList *glist_find_string(GList *list, const char *key);
37 GList *glist_find_icase_string(GList *list, const char *key);
38
39 void *gslist_foreach_find(GSList *list, FOREACH_FIND_FUNC func, const void *data);
40
41 /* `list' contains pointer to structure with a char* to string. */
42 char *gslistptr_to_string(GSList *list, int offset, const char *delimiter);
43 /* `list' contains char* */
44 char *gslist_to_string(GSList *list, const char *delimiter);
45
46 /* save all keys in hash table to linked list - you shouldn't remove any
47    items while using this list, use g_slist_free() after you're done with it */
48 GSList *hashtable_get_keys(GHashTable *hash);
49
50 /* strstr() with case-ignoring */
51 char *stristr(const char *data, const char *key);
52
53 /* like strstr(), but matches only for full words.
54    `icase' specifies if match is case sensitive */
55 char *strstr_full_case(const char *data, const char *key, int icase);
56 char *strstr_full(const char *data, const char *key);
57 char *stristr_full(const char *data, const char *key);
58
59 /* easy way to check if regexp matches */
60 int regexp_match(const char *str, const char *regexp);
61
62 /* Create the directory and all it's parent directories */
63 int mkpath(const char *path, int mode);
64 /* convert ~/ to $HOME */
65 char *convert_home(const char *path);
66
67 /* Case-insensitive string hash functions */
68 int g_istr_equal(gconstpointer v, gconstpointer v2);
69 unsigned int g_istr_hash(gconstpointer v);
70
71 /* Case-insensitive GCompareFunc func */
72 int g_istr_cmp(gconstpointer v, gconstpointer v2);
73
74 /* Find `mask' from `data', you can use * and ? wildcards. */
75 int match_wildcards(const char *mask, const char *data);
76
77 /* Return TRUE if all characters in `str' are numbers.
78    Stop when `end_char' is found from string. */
79 int is_numeric(const char *str, char end_char);
80
81 /* replace all `from' chars in string to `to' chars. returns `str' */
82 char *replace_chars(char *str, char from, char to);
83
84 /* octal <-> decimal conversions */
85 int octal2dec(int octal);
86 int dec2octal(int decimal);
87
88 /* string -> uoff_t */
89 uoff_t str_to_uofft(const char *str);
90
91 /* convert all low-ascii (<32) to ^<A..> combinations */
92 char *show_lowascii(const char *channel);
93
94 /* Get time in human readable form with localtime() + asctime() */
95 char *my_asctime(time_t t);
96
97 /* Returns number of columns needed to print items.
98    save_column_widths is filled with length of each column. */
99 int get_max_column_count(GSList *items, COLUMN_LEN_FUNC len_func,
100                          int max_width, int max_columns,
101                          int item_extra, int item_min_size,
102                          int **save_column_widths, int *rows);
103
104 /* Return a column sorted copy of a list. */
105 GSList *columns_sort_list(GSList *list, int rows);
106
107 /* Expand escape string, the first character in data should be the
108    one after '\'. Returns the expanded character or -1 if error. */
109 int expand_escape(const char **data);
110
111 /* Escape all '"', "'" and '\' chars with '\' */
112 char *escape_string(const char *str);
113
114 #endif