comparison src/util.h @ 7108:6faeeecab0dc

[gaim-migrate @ 7673] Put the rest of util.[ch] into namespaces and sectioned off the functions. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Wed, 01 Oct 2003 07:15:53 +0000
parents 9220c7490cd1
children d40966338ea6
comparison
equal deleted inserted replaced
7107:9220c7490cd1 7108:6faeeecab0dc
32 32
33 #ifdef __cplusplus 33 #ifdef __cplusplus
34 extern "C" { 34 extern "C" {
35 #endif 35 #endif
36 36
37 /** 37 /**************************************************************************/
38 * Normalizes a string, so that it is suitable for comparison. 38 /** @name Base16 Functions */
39 * 39 /**************************************************************************/
40 * The returned string will point to a static buffer, so if the 40 /*@{*/
41 * string is intended to be kept long-term, you <i>must</i> 41
42 * g_strdup() it. Also, calling normalize() twice in the same line 42 /**
43 * will lead to problems. 43 * Converts a string to its base-16 equivalent.
44 * 44 *
45 * @param str The string to normalize. 45 * @param str The string to convert.
46 * 46 * @param len The length of the string.
47 * @return A pointer to the normalized version stored in a static buffer. 47 *
48 */ 48 * @return The base-16 string.
49 char *gaim_normalize(const char *str); 49 *
50 * @see frombase16()
51 */
52 unsigned char *gaim_base16_encode(const unsigned char *str, int len);
53
54 /**
55 * Converts a string back from its base-16 equivalent.
56 *
57 * @param str The string to convert back.
58 * @param ret_str The returned, non-base-16 string.
59 *
60 * @return The length of the returned string.
61 *
62 * @see tobase16()
63 */
64 int gaim_base16_decode(const char *str, unsigned char **ret_str);
65
66 /*@}*/
67
68
69 /**************************************************************************/
70 /** @name Base64 Functions */
71 /**************************************************************************/
72 /*@{*/
50 73
51 /** 74 /**
52 * Converts a string to its base-64 equivalent. 75 * Converts a string to its base-64 equivalent.
53 * 76 *
54 * @param buf The data to convert. 77 * @param buf The data to convert.
56 * 79 *
57 * @return The base-64 version of @a str. 80 * @return The base-64 version of @a str.
58 * 81 *
59 * @see frombase64() 82 * @see frombase64()
60 */ 83 */
61 char *gaim_base64_encode(const unsigned char *buf, size_t len); 84 unsigned char *gaim_base64_encode(const unsigned char *buf, size_t len);
62 85
63 /** 86 /**
64 * Converts a string back from its base-64 equivalent. 87 * Converts a string back from its base-64 equivalent.
65 * 88 *
66 * @param str The string to convert back. 89 * @param str The string to convert back.
69 * 92 *
70 * @see tobase64() 93 * @see tobase64()
71 */ 94 */
72 void gaim_base64_decode(const char *str, char **ret_str, int *ret_len); 95 void gaim_base64_decode(const char *str, char **ret_str, int *ret_len);
73 96
74 /** 97 /*@}*/
75 * Converts a string to its base-16 equivalent. 98
76 * 99
77 * @param str The string to convert. 100 /**************************************************************************/
78 * @param len The length of the string. 101 /** @name Date/Time Functions */
79 * 102 /**************************************************************************/
80 * @return The base-16 string. 103 /*@{*/
81 *
82 * @see frombase16()
83 */
84 unsigned char *gaim_base16_encode(const unsigned char *str, int len);
85
86 /**
87 * Converts a string back from its base-16 equivalent.
88 *
89 * @param str The string to convert back.
90 * @param ret_str The returned, non-base-16 string.
91 *
92 * @return The length of the returned string.
93 *
94 * @see tobase16()
95 */
96 int gaim_base16_decode(const char *str, unsigned char **ret_str);
97 104
98 /** 105 /**
99 * Returns the current local time in hour:minute:second form. 106 * Returns the current local time in hour:minute:second form.
100 * 107 *
101 * The returned string is stored in a static buffer, so the result 108 * The returned string is stored in a static buffer, so the result
103 * 110 *
104 * @return The current local time. 111 * @return The current local time.
105 * 112 *
106 * @see full_date() 113 * @see full_date()
107 */ 114 */
108 char *date(void); 115 char *gaim_date(void);
109
110 /**
111 * Converts seconds into a human-readable form.
112 *
113 * @param sec The seconds.
114 *
115 * @return A human-readable form, containing days, hours, minutes, and
116 * seconds.
117 */
118 char *sec_to_text(guint sec);
119 116
120 /** 117 /**
121 * Returns the date and time in human-readable form. 118 * Returns the date and time in human-readable form.
122 * 119 *
123 * The returned string is stored in a static buffer, so the result 120 * The returned string is stored in a static buffer, so the result
125 * 122 *
126 * @return The date and time in human-readable form. 123 * @return The date and time in human-readable form.
127 * 124 *
128 * @see date() 125 * @see date()
129 */ 126 */
130 char *full_date(void); 127 char *gaim_date_full(void);
131
132 /**
133 * Looks for %n, %d, or %t in a string, and replaces them with the
134 * specified name, date, and time, respectively.
135 *
136 * The returned string is stored in a static buffer, so the result
137 * should be g_strdup()'d if it's intended to be used for long.
138 *
139 * @param str The string that may contain the special variables.
140 * @param name The sender name.
141 *
142 * @return A new string where the special variables are expanded.
143 */
144 char *away_subs(const char *str, const char *name);
145
146 /**`
147 * Returns the user's home directory.
148 *
149 * @return The user's home directory.
150 *
151 * @see gaim_user_dir()
152 */
153 const gchar *gaim_home_dir(void);
154
155 /**
156 * Returns the gaim settings directory in the user's home directory.
157 *
158 * @return The gaim settings directory.
159 *
160 * @see gaim_home_dir()
161 */
162 char *gaim_user_dir(void);
163
164 /**
165 * Copies a string and replaces all HTML linebreaks with newline characters.
166 *
167 * @param dest The destination string.
168 * @param src The source string.
169 * @param dest_len The destination string length.
170 *
171 * @see strncpy_withhtml()
172 * @see strdup_withhtml()
173 */
174 void strncpy_nohtml(gchar *dest, const gchar *src, size_t dest_len);
175
176 /**
177 * Copies a string and replaces all newline characters with HTML linebreaks.
178 *
179 * @param dest The destination string.
180 * @param src The source string.
181 * @param dest_len The destination string length.
182 *
183 * @see strncpy_nohtml()
184 * @see strdup_withhtml()
185 */
186 void strncpy_withhtml(gchar *dest, const gchar *src, size_t dest_len);
187
188 /**
189 * Duplicates a string and replaces all newline characters from the
190 * source string with HTML linebreaks.
191 *
192 * @param src The source string.
193 *
194 * @return The new string.
195 *
196 * @see strncpy_nohtml()
197 * @see strncpy_withhtml()
198 */
199 gchar *strdup_withhtml(const gchar *src);
200
201 /**
202 * Ensures that all linefeeds have a matching carriage return.
203 *
204 * @param str The source string.
205 *
206 * @return The string with carriage returns.
207 */
208 char *add_cr(const char *str);
209
210 /**
211 * Strips all linefeeds from a string.
212 *
213 * @param str The string to strip linefeeds from.
214 */
215 void strip_linefeed(char *str);
216 128
217 /** 129 /**
218 * Builds a time_t from the supplied information. 130 * Builds a time_t from the supplied information.
219 * 131 *
220 * @param year The year. 132 * @param year The year.
224 * @param min The minute. 136 * @param min The minute.
225 * @param sec The second. 137 * @param sec The second.
226 * 138 *
227 * @return A time_t. 139 * @return A time_t.
228 */ 140 */
229 time_t get_time(int year, int month, int day, 141 time_t gaim_time_build(int year, int month, int day, int hour,
230 int hour, int min, int sec); 142 int min, int sec);
231 143
232 /** 144 /*@}*/
233 * Creates a temporary file and returns a file pointer to it. 145
234 * 146
235 * This is like mkstemp(), but returns a file pointer and uses a 147 /**************************************************************************/
236 * pre-set template. It uses the semantics of tempnam() for the 148 /** @name Markup Functions */
237 * directory to use and allocates the space for the file path. 149 /**************************************************************************/
238 * 150 /*@{*/
239 * The caller is responsible for closing the file and removing it when
240 * done, as well as freeing the space pointed to by @a path with
241 * g_free().
242 *
243 * @param path The returned path to the temp file.
244 *
245 * @return A file pointer to the temporary file, or @c NULL on failure.
246 */
247 FILE *gaim_mkstemp(gchar **path);
248
249 /**
250 * Checks if the given program name is valid and executable.
251 *
252 * @parm program The file name of the application.
253 *
254 * @return True if the program is runable.
255 */
256 gboolean program_is_valid(const char *program);
257
258 /**
259 * Attempts to convert a string to UTF-8 from an unknown encoding.
260 *
261 * This function checks the locale and tries sane defaults.
262 *
263 * @param str The source string.
264 *
265 * @return The UTF-8 string, or @c NULL if it could not be converted.
266 */
267 char *gaim_try_conv_to_utf8(const char *str);
268
269 /**
270 * Returns the IP address from a socket file descriptor.
271 *
272 * @param fd The socket file descriptor.
273 *
274 * @return The IP address, or @c NULL on error.
275 */
276 char *gaim_getip_from_fd(int fd);
277
278 /**
279 * Compares two UTF-8 strings.
280 *
281 * @param a The first string.
282 * @param b The second string.
283 *
284 * @return -1 if @a is less than @a b.
285 * 0 if @a is equal to @a b.
286 * 1 if @a is greater than @a b.
287 */
288 gint gaim_utf8_strcasecmp(const gchar *a, const gchar *b);
289
290 /**
291 * Given a string, this replaces one substring with another
292 * and returns a newly allocated string.
293 *
294 * @param string The string from which to replace stuff.
295 * @param delimiter The substring you want replaced.
296 * @param replacement The substring you want inserted in place
297 * of the delimiting substring.
298 */
299 gchar *gaim_strreplace(const gchar *string, const gchar *delimiter,
300 const gchar *replacement);
301
302 /**
303 * This is like strstr, except that it ignores ASCII case in
304 * searching for the substring.
305 *
306 * @param haystack The string to search in.
307 * @param needle The substring to find.
308 *
309 * @return the location of the substring if found, or NULL if not
310 */
311 const char *gaim_strcasestr(const char *haystack, const char *needle);
312
313 /**
314 * Returns a string representing a filesize in the appropriate
315 * units (MB, KB, GB, etc.)
316 *
317 * @param size The size
318 *
319 * @return The string in units form. This must be freed.
320 */
321 char *gaim_str_size_to_units(size_t size);
322 151
323 /** 152 /**
324 * Finds a HTML tag matching the given name. 153 * Finds a HTML tag matching the given name.
325 * 154 *
326 * This locates an HTML tag's start and end, and stores its attributes 155 * This locates an HTML tag's start and end, and stores its attributes
394 * 223 *
395 * @return The linkified text. 224 * @return The linkified text.
396 */ 225 */
397 char *gaim_markup_linkify(const char *str); 226 char *gaim_markup_linkify(const char *str);
398 227
228 /*@}*/
229
230
231 /**************************************************************************/
232 /** @name Path/Filename Functions */
233 /**************************************************************************/
234 /*@{*/
235
236 /**
237 * Returns the user's home directory.
238 *
239 * @return The user's home directory.
240 *
241 * @see gaim_user_dir()
242 */
243 const gchar *gaim_home_dir(void);
244
245 /**
246 * Returns the gaim settings directory in the user's home directory.
247 *
248 * @return The gaim settings directory.
249 *
250 * @see gaim_home_dir()
251 */
252 char *gaim_user_dir(void);
253
254 /**
255 * Creates a temporary file and returns a file pointer to it.
256 *
257 * This is like mkstemp(), but returns a file pointer and uses a
258 * pre-set template. It uses the semantics of tempnam() for the
259 * directory to use and allocates the space for the file path.
260 *
261 * The caller is responsible for closing the file and removing it when
262 * done, as well as freeing the space pointed to by @a path with
263 * g_free().
264 *
265 * @param path The returned path to the temp file.
266 *
267 * @return A file pointer to the temporary file, or @c NULL on failure.
268 */
269 FILE *gaim_mkstemp(char **path);
270
271 /**
272 * Checks if the given program name is valid and executable.
273 *
274 * @parm program The file name of the application.
275 *
276 * @return True if the program is runable.
277 */
278 gboolean gaim_program_is_valid(const char *program);
279
280 /**
281 * Returns the IP address from a socket file descriptor.
282 *
283 * @param fd The socket file descriptor.
284 *
285 * @return The IP address, or @c NULL on error.
286 */
287 char *gaim_fd_get_ip(int fd);
288
289 /*@}*/
290
291
292 /**************************************************************************/
293 /** @name String Functions */
294 /**************************************************************************/
295 /*@{*/
296
297 /**
298 * Normalizes a string, so that it is suitable for comparison.
299 *
300 * The returned string will point to a static buffer, so if the
301 * string is intended to be kept long-term, you <i>must</i>
302 * g_strdup() it. Also, calling normalize() twice in the same line
303 * will lead to problems.
304 *
305 * @param str The string to normalize.
306 *
307 * @return A pointer to the normalized version stored in a static buffer.
308 */
309 char *gaim_normalize(const char *str);
310
311 /**
312 * Looks for %n, %d, or %t in a string, and replaces them with the
313 * specified name, date, and time, respectively.
314 *
315 * The returned string is stored in a static buffer, so the result
316 * should be g_strdup()'d if it's intended to be used for long.
317 *
318 * @param str The string that may contain the special variables.
319 * @param name The sender name.
320 *
321 * @return A new string where the special variables are expanded.
322 */
323 char *gaim_str_sub_away_formatters(const char *str, const char *name);
324
325 /**
326 * Copies a string and replaces all HTML linebreaks with newline characters.
327 *
328 * @param dest The destination string.
329 * @param src The source string.
330 * @param dest_len The destination string length.
331 *
332 * @see gaim_strncpy_withhtml()
333 * @see gaim_strdup_withhtml()
334 */
335 void gaim_strncpy_nohtml(char *dest, const char *src, size_t dest_len);
336
337 /**
338 * Copies a string and replaces all newline characters with HTML linebreaks.
339 *
340 * @param dest The destination string.
341 * @param src The source string.
342 * @param dest_len The destination string length.
343 *
344 * @see gaim_strncpy_nohtml()
345 * @see gaim_strdup_withhtml()
346 */
347 void gaim_strncpy_withhtml(gchar *dest, const gchar *src, size_t dest_len);
348
349 /**
350 * Duplicates a string and replaces all newline characters from the
351 * source string with HTML linebreaks.
352 *
353 * @param src The source string.
354 *
355 * @return The new string.
356 *
357 * @see gaim_strncpy_nohtml()
358 * @see gaim_strncpy_withhtml()
359 */
360 char *gaim_strdup_withhtml(const char *src);
361
362 /**
363 * Ensures that all linefeeds have a matching carriage return.
364 *
365 * @param str The source string.
366 *
367 * @return The string with carriage returns.
368 */
369 char *gaim_str_add_cr(const char *str);
370
371 /**
372 * Strips all linefeeds from a string.
373 *
374 * @param str The string to strip linefeeds from.
375 */
376 void gaim_str_strip_linefeed(char *str);
377
378 /**
379 * Given a string, this replaces one substring with another
380 * and returns a newly allocated string.
381 *
382 * @param string The string from which to replace stuff.
383 * @param delimiter The substring you want replaced.
384 * @param replacement The substring you want inserted in place
385 * of the delimiting substring.
386 */
387 char *gaim_strreplace(const char *string, const char *delimiter,
388 const char *replacement);
389
390 /**
391 * This is like strstr, except that it ignores ASCII case in
392 * searching for the substring.
393 *
394 * @param haystack The string to search in.
395 * @param needle The substring to find.
396 *
397 * @return the location of the substring if found, or NULL if not
398 */
399 const char *gaim_strcasestr(const char *haystack, const char *needle);
400
401 /**
402 * Returns a string representing a filesize in the appropriate
403 * units (MB, KB, GB, etc.)
404 *
405 * @param size The size
406 *
407 * @return The string in units form. This must be freed.
408 */
409 char *gaim_str_size_to_units(size_t size);
410
411 /**
412 * Converts seconds into a human-readable form.
413 *
414 * @param sec The seconds.
415 *
416 * @return A human-readable form, containing days, hours, minutes, and
417 * seconds.
418 */
419 char *gaim_str_seconds_to_string(guint sec);
420
421 /*@}*/
422
423
424 /**************************************************************************/
425 /** @name URI/URL Functions */
426 /**************************************************************************/
427 /*@{*/
428
399 /** 429 /**
400 * Parses a URL, returning its host, port, and file path. 430 * Parses a URL, returning its host, port, and file path.
401 * 431 *
402 * The returned data must be freed. 432 * The returned data must be freed.
403 * 433 *
423 void gaim_url_fetch(const char *url, gboolean full, 453 void gaim_url_fetch(const char *url, gboolean full,
424 const char *user_agent, gboolean http11, 454 const char *user_agent, gboolean http11,
425 void (*cb)(void *, const char *, size_t), 455 void (*cb)(void *, const char *, size_t),
426 void *data); 456 void *data);
427 457
458 /*@}*/
459
460
461 /**************************************************************************
462 * UTF8 String Functions
463 **************************************************************************/
464 /*@{*/
465
466 /**
467 * Attempts to convert a string to UTF-8 from an unknown encoding.
468 *
469 * This function checks the locale and tries sane defaults.
470 *
471 * @param str The source string.
472 *
473 * @return The UTF-8 string, or @c NULL if it could not be converted.
474 */
475 char *gaim_utf8_try_convert(const char *str);
476
477 /**
478 * Compares two UTF-8 strings.
479 *
480 * @param a The first string.
481 * @param b The second string.
482 *
483 * @return -1 if @a is less than @a b.
484 * 0 if @a is equal to @a b.
485 * 1 if @a is greater than @a b.
486 */
487 int gaim_utf8_strcasecmp(const char *a, const char *b);
488
489 /*@}*/
490
428 #ifdef __cplusplus 491 #ifdef __cplusplus
429 } 492 }
430 #endif 493 #endif
431 494
432 #endif /* _GAIM_UTIL_H_ */ 495 #endif /* _GAIM_UTIL_H_ */