| 1 /** |
|
| 2 * @file util.h Utility Functions |
|
| 3 * @ingroup core |
|
| 4 * |
|
| 5 * gaim |
|
| 6 * |
|
| 7 * Gaim is the legal property of its developers, whose names are too numerous |
|
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 9 * source distribution. |
|
| 10 * |
|
| 11 * This program is free software; you can redistribute it and/or modify |
|
| 12 * it under the terms of the GNU General Public License as published by |
|
| 13 * the Free Software Foundation; either version 2 of the License, or |
|
| 14 * (at your option) any later version. |
|
| 15 * |
|
| 16 * This program is distributed in the hope that it will be useful, |
|
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 19 * GNU General Public License for more details. |
|
| 20 * |
|
| 21 * You should have received a copy of the GNU General Public License |
|
| 22 * along with this program; if not, write to the Free Software |
|
| 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 24 * |
|
| 25 * @todo Rename the functions so that they live somewhere in the gaim |
|
| 26 * namespace. |
|
| 27 */ |
|
| 28 #ifndef _GAIM_UTIL_H_ |
|
| 29 #define _GAIM_UTIL_H_ |
|
| 30 |
|
| 31 #include <stdio.h> |
|
| 32 |
|
| 33 #include "account.h" |
|
| 34 #include "xmlnode.h" |
|
| 35 |
|
| 36 #ifdef __cplusplus |
|
| 37 extern "C" { |
|
| 38 #endif |
|
| 39 |
|
| 40 typedef struct _GaimMenuAction |
|
| 41 { |
|
| 42 char *label; |
|
| 43 GaimCallback callback; |
|
| 44 gpointer data; |
|
| 45 GList *children; |
|
| 46 } GaimMenuAction; |
|
| 47 |
|
| 48 typedef char *(*GaimInfoFieldFormatCallback)(const char *field, size_t len); |
|
| 49 |
|
| 50 /** |
|
| 51 * A key-value pair. |
|
| 52 * |
|
| 53 * This is used by, among other things, gaim_gtk_combo* functions to pass in a |
|
| 54 * list of key-value pairs so it can display a user-friendly value. |
|
| 55 */ |
|
| 56 typedef struct _GaimKeyValuePair |
|
| 57 { |
|
| 58 gchar *key; |
|
| 59 void *value; |
|
| 60 |
|
| 61 } GaimKeyValuePair; |
|
| 62 |
|
| 63 /** |
|
| 64 * Creates a new GaimMenuAction. |
|
| 65 * |
|
| 66 * @param label The text label to display for this action. |
|
| 67 * @param callback The function to be called when the action is used on |
|
| 68 * the selected item. |
|
| 69 * @param data Additional data to be passed to the callback. |
|
| 70 * @param children A GList of GaimMenuActions to be added as a submenu |
|
| 71 * of the action. |
|
| 72 * @return The GaimMenuAction. |
|
| 73 */ |
|
| 74 GaimMenuAction *gaim_menu_action_new(const char *label, GaimCallback callback, |
|
| 75 gpointer data, GList *children); |
|
| 76 |
|
| 77 /** |
|
| 78 * Frees a GaimMenuAction |
|
| 79 * |
|
| 80 * @param act The GaimMenuAction to free. |
|
| 81 */ |
|
| 82 void gaim_menu_action_free(GaimMenuAction *act); |
|
| 83 |
|
| 84 /**************************************************************************/ |
|
| 85 /** @name Base16 Functions */ |
|
| 86 /**************************************************************************/ |
|
| 87 /*@{*/ |
|
| 88 |
|
| 89 /** |
|
| 90 * Converts a chunk of binary data to its base-16 equivalent. |
|
| 91 * |
|
| 92 * @param data The data to convert. |
|
| 93 * @param len The length of the data. |
|
| 94 * |
|
| 95 * @return The base-16 string in the ASCII encoding. Must be |
|
| 96 * g_free'd when no longer needed. |
|
| 97 * |
|
| 98 * @see gaim_base16_decode() |
|
| 99 */ |
|
| 100 gchar *gaim_base16_encode(const guchar *data, gsize len); |
|
| 101 |
|
| 102 /** |
|
| 103 * Converts an ASCII string of base-16 encoded data to |
|
| 104 * the binary equivalent. |
|
| 105 * |
|
| 106 * @param str The base-16 string to convert to raw data. |
|
| 107 * @param ret_len The length of the returned data. You can |
|
| 108 * pass in NULL if you're sure that you know |
|
| 109 * the length of the decoded data, or if you |
|
| 110 * know you'll be able to use strlen to |
|
| 111 * determine the length, etc. |
|
| 112 * |
|
| 113 * @return The raw data. Must be g_free'd when no longer needed. |
|
| 114 * |
|
| 115 * @see gaim_base16_encode() |
|
| 116 */ |
|
| 117 guchar *gaim_base16_decode(const char *str, gsize *ret_len); |
|
| 118 |
|
| 119 /*@}*/ |
|
| 120 |
|
| 121 /**************************************************************************/ |
|
| 122 /** @name Base64 Functions */ |
|
| 123 /**************************************************************************/ |
|
| 124 /*@{*/ |
|
| 125 |
|
| 126 /** |
|
| 127 * Converts a chunk of binary data to its base-64 equivalent. |
|
| 128 * |
|
| 129 * @param data The data to convert. |
|
| 130 * @param len The length of the data. |
|
| 131 * |
|
| 132 * @return The base-64 string in the ASCII encoding. Must be |
|
| 133 * g_free'd when no longer needed. |
|
| 134 * |
|
| 135 * @see gaim_base64_decode() |
|
| 136 */ |
|
| 137 gchar *gaim_base64_encode(const guchar *data, gsize len); |
|
| 138 |
|
| 139 /** |
|
| 140 * Converts an ASCII string of base-64 encoded data to |
|
| 141 * the binary equivalent. |
|
| 142 * |
|
| 143 * @param str The base-64 string to convert to raw data. |
|
| 144 * @param ret_len The length of the returned data. You can |
|
| 145 * pass in NULL if you're sure that you know |
|
| 146 * the length of the decoded data, or if you |
|
| 147 * know you'll be able to use strlen to |
|
| 148 * determine the length, etc. |
|
| 149 * |
|
| 150 * @return The raw data. Must be g_free'd when no longer needed. |
|
| 151 * |
|
| 152 * @see gaim_base64_encode() |
|
| 153 */ |
|
| 154 guchar *gaim_base64_decode(const char *str, gsize *ret_len); |
|
| 155 |
|
| 156 /*@}*/ |
|
| 157 |
|
| 158 /**************************************************************************/ |
|
| 159 /** @name Quoted Printable Functions */ |
|
| 160 /**************************************************************************/ |
|
| 161 /*@{*/ |
|
| 162 |
|
| 163 /** |
|
| 164 * Converts a quoted printable string back to its readable equivalent. |
|
| 165 * What is a quoted printable string, you ask? It's an encoding used |
|
| 166 * to transmit binary data as ASCII. It's intended purpose is to send |
|
| 167 * e-mails containing non-ASCII characters. Wikipedia has a pretty good |
|
| 168 * explanation. Also see RFC 2045. |
|
| 169 * |
|
| 170 * @param str The quoted printable ASCII string to convert to raw data. |
|
| 171 * @param ret_len The length of the returned data. |
|
| 172 * |
|
| 173 * @return The readable string. Must be g_free'd when no longer needed. |
|
| 174 */ |
|
| 175 guchar *gaim_quotedp_decode(const char *str, gsize *ret_len); |
|
| 176 |
|
| 177 /*@}*/ |
|
| 178 |
|
| 179 /**************************************************************************/ |
|
| 180 /** @name MIME Functions */ |
|
| 181 /**************************************************************************/ |
|
| 182 /*@{*/ |
|
| 183 |
|
| 184 /** |
|
| 185 * Converts a MIME header field string back to its readable equivalent |
|
| 186 * according to RFC 2047. Basically, a header is plain ASCII and can |
|
| 187 * contain any number of sections called "encoded-words." The format |
|
| 188 * of an encoded word is =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= |
|
| 189 * =? designates the beginning of the encoded-word |
|
| 190 * ?= designates the end of the encoded-word |
|
| 191 * |
|
| 192 * An encoded word is segmented into three pieces by the use of a |
|
| 193 * question mark. The first piece is the character set, the second |
|
| 194 * piece is the encoding, and the third piece is the encoded text. |
|
| 195 * |
|
| 196 * @param str The ASCII string, possibly containing any number of |
|
| 197 * encoded-word sections. |
|
| 198 * |
|
| 199 * @return The string, with any encoded-word sections decoded and |
|
| 200 * converted to UTF-8. Must be g_free'd when no longer |
|
| 201 * needed. |
|
| 202 */ |
|
| 203 char *gaim_mime_decode_field(const char *str); |
|
| 204 |
|
| 205 /*@}*/ |
|
| 206 |
|
| 207 |
|
| 208 /**************************************************************************/ |
|
| 209 /** @name Date/Time Functions */ |
|
| 210 /**************************************************************************/ |
|
| 211 /*@{*/ |
|
| 212 |
|
| 213 /** |
|
| 214 * Formats a time into the specified format. |
|
| 215 * |
|
| 216 * This is essentially strftime(), but it has a static buffer |
|
| 217 * and handles the UTF-8 conversion for the caller. |
|
| 218 * |
|
| 219 * This function also provides the GNU %z formatter if the underlying C |
|
| 220 * library doesn't. However, the format string parser is very naive, which |
|
| 221 * means that conversions specifiers to %z cannot be guaranteed. The GNU |
|
| 222 * strftime(3) man page describes %z as: 'The time-zone as hour offset from |
|
| 223 * GMT. Required to emit RFC822-conformant dates |
|
| 224 * (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)' |
|
| 225 * |
|
| 226 * On Windows, this function also converts the results for %Z from a timezone |
|
| 227 * name (as returned by the system strftime() %Z format string) to a timezone |
|
| 228 * abbreviation (as is the case on Unix). As with %z, conversion specifiers |
|
| 229 * should not be used. |
|
| 230 * |
|
| 231 * @param format The format string, in UTF-8 |
|
| 232 * @param tm The time to format, or @c NULL to use the current local time |
|
| 233 * |
|
| 234 * @return The formatted time, in UTF-8. |
|
| 235 * |
|
| 236 * @note @a format is required to be in UTF-8. This differs from strftime(), |
|
| 237 * where the format is provided in the locale charset. |
|
| 238 */ |
|
| 239 const char *gaim_utf8_strftime(const char *format, const struct tm *tm); |
|
| 240 |
|
| 241 /** |
|
| 242 * Formats a time into the user's preferred short date format. |
|
| 243 * |
|
| 244 * The returned string is stored in a static buffer, so the result |
|
| 245 * should be g_strdup()'d if it's going to be kept. |
|
| 246 * |
|
| 247 * @param tm The time to format, or @c NULL to use the current local time |
|
| 248 * |
|
| 249 * @return The date, formatted as per the user's settings. |
|
| 250 */ |
|
| 251 const char *gaim_date_format_short(const struct tm *tm); |
|
| 252 |
|
| 253 /** |
|
| 254 * Formats a time into the user's preferred short date plus time format. |
|
| 255 * |
|
| 256 * The returned string is stored in a static buffer, so the result |
|
| 257 * should be g_strdup()'d if it's going to be kept. |
|
| 258 * |
|
| 259 * @param tm The time to format, or @c NULL to use the current local time |
|
| 260 * |
|
| 261 * @return The timestamp, formatted as per the user's settings. |
|
| 262 */ |
|
| 263 const char *gaim_date_format_long(const struct tm *tm); |
|
| 264 |
|
| 265 /** |
|
| 266 * Formats a time into the user's preferred full date and time format. |
|
| 267 * |
|
| 268 * The returned string is stored in a static buffer, so the result |
|
| 269 * should be g_strdup()'d if it's going to be kept. |
|
| 270 * |
|
| 271 * @param tm The time to format, or @c NULL to use the current local time |
|
| 272 * |
|
| 273 * @return The date and time, formatted as per the user's settings. |
|
| 274 */ |
|
| 275 const char *gaim_date_format_full(const struct tm *tm); |
|
| 276 |
|
| 277 /** |
|
| 278 * Formats a time into the user's preferred time format. |
|
| 279 * |
|
| 280 * The returned string is stored in a static buffer, so the result |
|
| 281 * should be g_strdup()'d if it's going to be kept. |
|
| 282 * |
|
| 283 * @param tm The time to format, or @c NULL to use the current local time |
|
| 284 * |
|
| 285 * @return The time, formatted as per the user's settings. |
|
| 286 */ |
|
| 287 const char *gaim_time_format(const struct tm *tm); |
|
| 288 |
|
| 289 /** |
|
| 290 * Builds a time_t from the supplied information. |
|
| 291 * |
|
| 292 * @param year The year. |
|
| 293 * @param month The month. |
|
| 294 * @param day The day. |
|
| 295 * @param hour The hour. |
|
| 296 * @param min The minute. |
|
| 297 * @param sec The second. |
|
| 298 * |
|
| 299 * @return A time_t. |
|
| 300 */ |
|
| 301 time_t gaim_time_build(int year, int month, int day, int hour, |
|
| 302 int min, int sec); |
|
| 303 |
|
| 304 /** Used by gaim_str_to_time to indicate no timezone offset was |
|
| 305 * specified in the timestamp string. */ |
|
| 306 #define GAIM_NO_TZ_OFF -500000 |
|
| 307 |
|
| 308 /** |
|
| 309 * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns |
|
| 310 * a time_t. |
|
| 311 * |
|
| 312 * @param timestamp The timestamp |
|
| 313 * @param utc Assume UTC if no timezone specified |
|
| 314 * @param tm If not @c NULL, the caller can get a copy of the |
|
| 315 * struct tm used to calculate the time_t return value. |
|
| 316 * @param tz_off If not @c NULL, the caller can get a copy of the |
|
| 317 * timezone offset (from UTC) used to calculate the time_t |
|
| 318 * return value. Note: Zero is a valid offset. As such, |
|
| 319 * the value of the macro @c GAIM_NO_TZ_OFF indicates no |
|
| 320 * offset was specified (which means that the local |
|
| 321 * timezone was used in the calculation). |
|
| 322 * @param rest If not @c NULL, the caller can get a pointer to the |
|
| 323 * part of @a timestamp left over after parsing is |
|
| 324 * completed, if it's not the end of @a timestamp. |
|
| 325 * |
|
| 326 * @return A time_t. |
|
| 327 */ |
|
| 328 time_t gaim_str_to_time(const char *timestamp, gboolean utc, |
|
| 329 struct tm *tm, long *tz_off, const char **rest); |
|
| 330 |
|
| 331 /*@}*/ |
|
| 332 |
|
| 333 |
|
| 334 /**************************************************************************/ |
|
| 335 /** @name Markup Functions */ |
|
| 336 /**************************************************************************/ |
|
| 337 /*@{*/ |
|
| 338 |
|
| 339 /** |
|
| 340 * Finds an HTML tag matching the given name. |
|
| 341 * |
|
| 342 * This locates an HTML tag's start and end, and stores its attributes |
|
| 343 * in a GData hash table. The names of the attributes are lower-cased |
|
| 344 * in the hash table, and the name of the tag is case insensitive. |
|
| 345 * |
|
| 346 * @param needle the name of the tag |
|
| 347 * @param haystack the null-delimited string to search in |
|
| 348 * @param start a pointer to the start of the tag if found |
|
| 349 * @param end a pointer to the end of the tag if found |
|
| 350 * @param attributes the attributes, if the tag was found |
|
| 351 * @return TRUE if the tag was found |
|
| 352 */ |
|
| 353 gboolean gaim_markup_find_tag(const char *needle, const char *haystack, |
|
| 354 const char **start, const char **end, |
|
| 355 GData **attributes); |
|
| 356 |
|
| 357 /** |
|
| 358 * Extracts a field of data from HTML. |
|
| 359 * |
|
| 360 * This is a scary function. See protocols/msn/msn.c and |
|
| 361 * protocols/yahoo/yahoo_profile.c for example usage. |
|
| 362 * |
|
| 363 * @param str The string to parse. |
|
| 364 * @param len The size of str. |
|
| 365 * @param dest The destination GString to append the new |
|
| 366 * field info to. |
|
| 367 * @param start_token The beginning token. |
|
| 368 * @param skip The number of characters to skip after the |
|
| 369 * start token. |
|
| 370 * @param end_token The ending token. |
|
| 371 * @param check_value The value that the last character must meet. |
|
| 372 * @param no_value_token The token indicating no value is given. |
|
| 373 * @param display_name The short descriptive name to display for this token. |
|
| 374 * @param is_link TRUE if this should be a link, or FALSE otherwise. |
|
| 375 * @param link_prefix The prefix for the link. |
|
| 376 * @param format_cb A callback to format the value before adding it. |
|
| 377 * |
|
| 378 * @return TRUE if successful, or FALSE otherwise. |
|
| 379 */ |
|
| 380 gboolean gaim_markup_extract_info_field(const char *str, int len, GString *dest, |
|
| 381 const char *start_token, int skip, |
|
| 382 const char *end_token, char check_value, |
|
| 383 const char *no_value_token, |
|
| 384 const char *display_name, gboolean is_link, |
|
| 385 const char *link_prefix, |
|
| 386 GaimInfoFieldFormatCallback format_cb); |
|
| 387 |
|
| 388 /** |
|
| 389 * Converts HTML markup to XHTML. |
|
| 390 * |
|
| 391 * @param html The HTML markup. |
|
| 392 * @param dest_xhtml The destination XHTML output. |
|
| 393 * @param dest_plain The destination plain-text output. |
|
| 394 */ |
|
| 395 void gaim_markup_html_to_xhtml(const char *html, char **dest_xhtml, |
|
| 396 char **dest_plain); |
|
| 397 |
|
| 398 /** |
|
| 399 * Strips HTML tags from a string. |
|
| 400 * |
|
| 401 * @param str The string to strip HTML from. |
|
| 402 * |
|
| 403 * @return The new string without HTML. This must be freed. |
|
| 404 */ |
|
| 405 char *gaim_markup_strip_html(const char *str); |
|
| 406 |
|
| 407 /** |
|
| 408 * Adds the necessary HTML code to turn URIs into HTML links in a string. |
|
| 409 * |
|
| 410 * @param str The string to linkify. |
|
| 411 * |
|
| 412 * @return The linkified text. |
|
| 413 */ |
|
| 414 char *gaim_markup_linkify(const char *str); |
|
| 415 |
|
| 416 /** |
|
| 417 * Unescapes HTML entities to their literal characters. |
|
| 418 * For example "&" is replaced by '&' and so on. |
|
| 419 * Actually only "&", """, "<" and ">" are currently |
|
| 420 * supported. |
|
| 421 * |
|
| 422 * @param html The string in which to unescape any HTML entities |
|
| 423 * |
|
| 424 * @return the text with HTML entities literalized |
|
| 425 */ |
|
| 426 char *gaim_unescape_html(const char *html); |
|
| 427 |
|
| 428 /** |
|
| 429 * Returns a newly allocated substring of the HTML UTF-8 string "str". |
|
| 430 * The markup is preserved such that the substring will have the same |
|
| 431 * formatting as original string, even though some tags may have been |
|
| 432 * opened before "x", or may close after "y". All open tags are closed |
|
| 433 * at the end of the returned string, in the proper order. |
|
| 434 * |
|
| 435 * Note that x and y are in character offsets, not byte offsets, and |
|
| 436 * are offsets into an unformatted version of str. Because of this, |
|
| 437 * this function may be sensitive to changes in GtkIMHtml and may break |
|
| 438 * when used with other UI's. libgaim users are encouraged to report and |
|
| 439 * work out any problems encountered. |
|
| 440 * |
|
| 441 * @param str The input NUL terminated, HTML, UTF-8 (or ASCII) string. |
|
| 442 * @param x The character offset into an unformatted version of str to |
|
| 443 * begin at. |
|
| 444 * @param y The character offset (into an unformatted vesion of str) of |
|
| 445 * one past the last character to include in the slice. |
|
| 446 * |
|
| 447 * @return The HTML slice of string, with all formatting retained. |
|
| 448 */ |
|
| 449 char *gaim_markup_slice(const char *str, guint x, guint y); |
|
| 450 |
|
| 451 /** |
|
| 452 * Returns a newly allocated string containing the name of the tag |
|
| 453 * located at "tag". Tag is expected to point to a '<', and contain |
|
| 454 * a '>' sometime after that. If there is no '>' and the string is |
|
| 455 * not NUL terminated, this function can be expected to segfault. |
|
| 456 * |
|
| 457 * @param tag The string starting a HTML tag. |
|
| 458 * @return A string containing the name of the tag. |
|
| 459 */ |
|
| 460 char *gaim_markup_get_tag_name(const char *tag); |
|
| 461 |
|
| 462 /*@}*/ |
|
| 463 |
|
| 464 |
|
| 465 /**************************************************************************/ |
|
| 466 /** @name Path/Filename Functions */ |
|
| 467 /**************************************************************************/ |
|
| 468 /*@{*/ |
|
| 469 |
|
| 470 /** |
|
| 471 * Returns the user's home directory. |
|
| 472 * |
|
| 473 * @return The user's home directory. |
|
| 474 * |
|
| 475 * @see gaim_user_dir() |
|
| 476 */ |
|
| 477 const gchar *gaim_home_dir(void); |
|
| 478 |
|
| 479 /** |
|
| 480 * Returns the gaim settings directory in the user's home directory. |
|
| 481 * This is usually ~/.gaim |
|
| 482 * |
|
| 483 * @return The gaim settings directory. |
|
| 484 * |
|
| 485 * @see gaim_home_dir() |
|
| 486 */ |
|
| 487 const char *gaim_user_dir(void); |
|
| 488 |
|
| 489 /** |
|
| 490 * Define a custom gaim settings directory, overriding the default (user's home directory/.gaim) |
|
| 491 * @param dir The custom settings directory |
|
| 492 */ |
|
| 493 void gaim_util_set_user_dir(const char *dir); |
|
| 494 |
|
| 495 /** |
|
| 496 * Builds a complete path from the root, making any directories along |
|
| 497 * the path which do not already exist. |
|
| 498 * |
|
| 499 * @param path The path you wish to create. Note that it must start |
|
| 500 * from the root or this function will fail. |
|
| 501 * @param mode Unix-style permissions for this directory. |
|
| 502 * |
|
| 503 * @return 0 for success, nonzero on any error. |
|
| 504 */ |
|
| 505 int gaim_build_dir(const char *path, int mode); |
|
| 506 |
|
| 507 /** |
|
| 508 * Write a string of data to a file of the given name in the Gaim |
|
| 509 * user directory ($HOME/.gaim by default). The data is typically |
|
| 510 * a serialized version of one of Gaim's config files, such as |
|
| 511 * prefs.xml, accounts.xml, etc. And the string is typically |
|
| 512 * obtained using xmlnode_to_formatted_str. However, this function |
|
| 513 * should work fine for saving binary files as well. |
|
| 514 * |
|
| 515 * @param filename The basename of the file to write in the gaim_user_dir. |
|
| 516 * @param data A null-terminated string of data to write. |
|
| 517 * @param size The size of the data to save. If data is |
|
| 518 * null-terminated you can pass in -1. |
|
| 519 * |
|
| 520 * @return TRUE if the file was written successfully. FALSE otherwise. |
|
| 521 */ |
|
| 522 gboolean gaim_util_write_data_to_file(const char *filename, const char *data, |
|
| 523 size_t size); |
|
| 524 |
|
| 525 /** |
|
| 526 * Read the contents of a given file and parse the results into an |
|
| 527 * xmlnode tree structure. This is intended to be used to read |
|
| 528 * Gaim's configuration xml files (prefs.xml, pounces.xml, etc.) |
|
| 529 * |
|
| 530 * @param filename The basename of the file to open in the gaim_user_dir. |
|
| 531 * @param description A very short description of the contents of this |
|
| 532 * file. This is used in error messages shown to the |
|
| 533 * user when the file can not be opened. For example, |
|
| 534 * "preferences," or "buddy pounces." |
|
| 535 * |
|
| 536 * @return An xmlnode tree of the contents of the given file. Or NULL, if |
|
| 537 * the file does not exist or there was an error reading the file. |
|
| 538 */ |
|
| 539 xmlnode *gaim_util_read_xml_from_file(const char *filename, |
|
| 540 const char *description); |
|
| 541 |
|
| 542 /** |
|
| 543 * Creates a temporary file and returns a file pointer to it. |
|
| 544 * |
|
| 545 * This is like mkstemp(), but returns a file pointer and uses a |
|
| 546 * pre-set template. It uses the semantics of tempnam() for the |
|
| 547 * directory to use and allocates the space for the file path. |
|
| 548 * |
|
| 549 * The caller is responsible for closing the file and removing it when |
|
| 550 * done, as well as freeing the space pointed to by @a path with |
|
| 551 * g_free(). |
|
| 552 * |
|
| 553 * @param path The returned path to the temp file. |
|
| 554 * @param binary Text or binary, for platforms where it matters. |
|
| 555 * |
|
| 556 * @return A file pointer to the temporary file, or @c NULL on failure. |
|
| 557 */ |
|
| 558 FILE *gaim_mkstemp(char **path, gboolean binary); |
|
| 559 |
|
| 560 /** |
|
| 561 * Checks if the given program name is valid and executable. |
|
| 562 * |
|
| 563 * @param program The file name of the application. |
|
| 564 * |
|
| 565 * @return TRUE if the program is runable. |
|
| 566 */ |
|
| 567 gboolean gaim_program_is_valid(const char *program); |
|
| 568 |
|
| 569 /** |
|
| 570 * Check if running GNOME. |
|
| 571 * |
|
| 572 * @return TRUE if running GNOME, FALSE otherwise. |
|
| 573 */ |
|
| 574 gboolean gaim_running_gnome(void); |
|
| 575 |
|
| 576 /** |
|
| 577 * Check if running KDE. |
|
| 578 * |
|
| 579 * @return TRUE if running KDE, FALSE otherwise. |
|
| 580 */ |
|
| 581 gboolean gaim_running_kde(void); |
|
| 582 |
|
| 583 /** |
|
| 584 * Returns the IP address from a socket file descriptor. |
|
| 585 * |
|
| 586 * @param fd The socket file descriptor. |
|
| 587 * |
|
| 588 * @return The IP address, or @c NULL on error. |
|
| 589 */ |
|
| 590 char *gaim_fd_get_ip(int fd); |
|
| 591 |
|
| 592 /*@}*/ |
|
| 593 |
|
| 594 |
|
| 595 /**************************************************************************/ |
|
| 596 /** @name String Functions */ |
|
| 597 /**************************************************************************/ |
|
| 598 /*@{*/ |
|
| 599 |
|
| 600 /** |
|
| 601 * Normalizes a string, so that it is suitable for comparison. |
|
| 602 * |
|
| 603 * The returned string will point to a static buffer, so if the |
|
| 604 * string is intended to be kept long-term, you <i>must</i> |
|
| 605 * g_strdup() it. Also, calling normalize() twice in the same line |
|
| 606 * will lead to problems. |
|
| 607 * |
|
| 608 * @param account The account the string belongs to, or NULL if you do |
|
| 609 * not know the account. If you use NULL, the string |
|
| 610 * will still be normalized, but if the PRPL uses a |
|
| 611 * custom normalization function then the string may |
|
| 612 * not be normalized correctly. |
|
| 613 * @param str The string to normalize. |
|
| 614 * |
|
| 615 * @return A pointer to the normalized version stored in a static buffer. |
|
| 616 */ |
|
| 617 const char *gaim_normalize(const GaimAccount *account, const char *str); |
|
| 618 |
|
| 619 /** |
|
| 620 * Normalizes a string, so that it is suitable for comparison. |
|
| 621 * |
|
| 622 * This is one possible implementation for the PRPL callback |
|
| 623 * function "normalize." It returns a lowercase and UTF-8 |
|
| 624 * normalized version of the string. |
|
| 625 * |
|
| 626 * @param account The account the string belongs to. |
|
| 627 * @param str The string to normalize. |
|
| 628 * |
|
| 629 * @return A pointer to the normalized version stored in a static buffer. |
|
| 630 */ |
|
| 631 const char *gaim_normalize_nocase(const GaimAccount *account, const char *str); |
|
| 632 |
|
| 633 /** |
|
| 634 * Compares two strings to see if the first contains the second as |
|
| 635 * a proper prefix. |
|
| 636 * |
|
| 637 * @param s The string to check. |
|
| 638 * @param p The prefix in question. |
|
| 639 * |
|
| 640 * @return TRUE if p is a prefix of s, otherwise FALSE. |
|
| 641 */ |
|
| 642 gboolean gaim_str_has_prefix(const char *s, const char *p); |
|
| 643 |
|
| 644 /** |
|
| 645 * Compares two strings to see if the second is a proper suffix |
|
| 646 * of the first. |
|
| 647 * |
|
| 648 * @param s The string to check. |
|
| 649 * @param x The suffix in question. |
|
| 650 * |
|
| 651 * @return TRUE if x is a a suffix of s, otherwise FALSE. |
|
| 652 */ |
|
| 653 gboolean gaim_str_has_suffix(const char *s, const char *x); |
|
| 654 |
|
| 655 /** |
|
| 656 * Duplicates a string and replaces all newline characters from the |
|
| 657 * source string with HTML linebreaks. |
|
| 658 * |
|
| 659 * @param src The source string. |
|
| 660 * |
|
| 661 * @return The new string. Must be g_free'd by the caller. |
|
| 662 */ |
|
| 663 gchar *gaim_strdup_withhtml(const gchar *src); |
|
| 664 |
|
| 665 /** |
|
| 666 * Ensures that all linefeeds have a matching carriage return. |
|
| 667 * |
|
| 668 * @param str The source string. |
|
| 669 * |
|
| 670 * @return The string with carriage returns. |
|
| 671 */ |
|
| 672 char *gaim_str_add_cr(const char *str); |
|
| 673 |
|
| 674 /** |
|
| 675 * Strips all instances of the given character from the |
|
| 676 * given string. The string is modified in place. This |
|
| 677 * is useful for stripping new line characters, for example. |
|
| 678 * |
|
| 679 * Example usage: |
|
| 680 * gaim_str_strip_char(my_dumb_string, '\n'); |
|
| 681 * |
|
| 682 * @param str The string to strip characters from. |
|
| 683 * @param thechar The character to strip from the given string. |
|
| 684 */ |
|
| 685 void gaim_str_strip_char(char *str, char thechar); |
|
| 686 |
|
| 687 /** |
|
| 688 * Given a string, this replaces all instances of one character |
|
| 689 * with another. This happens inline (the original string IS |
|
| 690 * modified). |
|
| 691 * |
|
| 692 * @param string The string from which to replace stuff. |
|
| 693 * @param delimiter The character you want replaced. |
|
| 694 * @param replacement The character you want inserted in place |
|
| 695 * of the delimiting character. |
|
| 696 */ |
|
| 697 void gaim_util_chrreplace(char *string, char delimiter, |
|
| 698 char replacement); |
|
| 699 |
|
| 700 /** |
|
| 701 * Given a string, this replaces one substring with another |
|
| 702 * and returns a newly allocated string. |
|
| 703 * |
|
| 704 * @param string The string from which to replace stuff. |
|
| 705 * @param delimiter The substring you want replaced. |
|
| 706 * @param replacement The substring you want inserted in place |
|
| 707 * of the delimiting substring. |
|
| 708 * |
|
| 709 * @return A new string, after performing the substitution. |
|
| 710 * free this with g_free(). |
|
| 711 */ |
|
| 712 gchar *gaim_strreplace(const char *string, const char *delimiter, |
|
| 713 const char *replacement); |
|
| 714 |
|
| 715 |
|
| 716 /** |
|
| 717 * Given a string, this replaces any utf-8 substrings in that string with |
|
| 718 * the corresponding numerical character reference, and returns a newly |
|
| 719 * allocated string. |
|
| 720 * |
|
| 721 * @param in The string which might contain utf-8 substrings |
|
| 722 * |
|
| 723 * @return A new string, with utf-8 replaced with numerical character |
|
| 724 * references, free this with g_free() |
|
| 725 */ |
|
| 726 char *gaim_utf8_ncr_encode(const char *in); |
|
| 727 |
|
| 728 |
|
| 729 /** |
|
| 730 * Given a string, this replaces any numerical character references |
|
| 731 * in that string with the corresponding actual utf-8 substrings, |
|
| 732 * and returns a newly allocated string. |
|
| 733 * |
|
| 734 * @param in The string which might contain numerical character references. |
|
| 735 * |
|
| 736 * @return A new string, with numerical character references |
|
| 737 * replaced with actual utf-8, free this with g_free(). |
|
| 738 */ |
|
| 739 char *gaim_utf8_ncr_decode(const char *in); |
|
| 740 |
|
| 741 |
|
| 742 /** |
|
| 743 * Given a string, this replaces one substring with another |
|
| 744 * ignoring case and returns a newly allocated string. |
|
| 745 * |
|
| 746 * @param string The string from which to replace stuff. |
|
| 747 * @param delimiter The substring you want replaced. |
|
| 748 * @param replacement The substring you want inserted in place |
|
| 749 * of the delimiting substring. |
|
| 750 * |
|
| 751 * @return A new string, after performing the substitution. |
|
| 752 * free this with g_free(). |
|
| 753 */ |
|
| 754 gchar *gaim_strcasereplace(const char *string, const char *delimiter, |
|
| 755 const char *replacement); |
|
| 756 |
|
| 757 /** |
|
| 758 * This is like strstr, except that it ignores ASCII case in |
|
| 759 * searching for the substring. |
|
| 760 * |
|
| 761 * @param haystack The string to search in. |
|
| 762 * @param needle The substring to find. |
|
| 763 * |
|
| 764 * @return the location of the substring if found, or NULL if not |
|
| 765 */ |
|
| 766 const char *gaim_strcasestr(const char *haystack, const char *needle); |
|
| 767 |
|
| 768 /** |
|
| 769 * Returns a string representing a filesize in the appropriate |
|
| 770 * units (MB, KB, GB, etc.) |
|
| 771 * |
|
| 772 * @param size The size |
|
| 773 * |
|
| 774 * @return The string in units form. This must be freed. |
|
| 775 */ |
|
| 776 char *gaim_str_size_to_units(size_t size); |
|
| 777 |
|
| 778 /** |
|
| 779 * Converts seconds into a human-readable form. |
|
| 780 * |
|
| 781 * @param sec The seconds. |
|
| 782 * |
|
| 783 * @return A human-readable form, containing days, hours, minutes, and |
|
| 784 * seconds. |
|
| 785 */ |
|
| 786 char *gaim_str_seconds_to_string(guint sec); |
|
| 787 |
|
| 788 /** |
|
| 789 * Converts a binary string into a NUL terminated ascii string, |
|
| 790 * replacing nonascii characters and characters below SPACE (including |
|
| 791 * NUL) into \\xyy, where yy are two hex digits. Also backslashes are |
|
| 792 * changed into two backslashes (\\\\). The returned, newly allocated |
|
| 793 * string can be outputted to the console, and must be g_free()d. |
|
| 794 * |
|
| 795 * @param binary A string of random data, possibly with embedded NULs |
|
| 796 * and such. |
|
| 797 * @param len The length in bytes of the input string. Must not be 0. |
|
| 798 * |
|
| 799 * @return A newly allocated ASCIIZ string. |
|
| 800 */ |
|
| 801 char *gaim_str_binary_to_ascii(const unsigned char *binary, guint len); |
|
| 802 /*@}*/ |
|
| 803 |
|
| 804 |
|
| 805 /**************************************************************************/ |
|
| 806 /** @name URI/URL Functions */ |
|
| 807 /**************************************************************************/ |
|
| 808 /*@{*/ |
|
| 809 |
|
| 810 /** |
|
| 811 * Parses a URL, returning its host, port, file path, username and password. |
|
| 812 * |
|
| 813 * The returned data must be freed. |
|
| 814 * |
|
| 815 * @param url The URL to parse. |
|
| 816 * @param ret_host The returned host. |
|
| 817 * @param ret_port The returned port. |
|
| 818 * @param ret_path The returned path. |
|
| 819 * @param ret_user The returned username. |
|
| 820 * @param ret_passwd The returned password. |
|
| 821 */ |
|
| 822 gboolean gaim_url_parse(const char *url, char **ret_host, int *ret_port, |
|
| 823 char **ret_path, char **ret_user, char **ret_passwd); |
|
| 824 |
|
| 825 typedef void (*GaimURLFetchCallback) (gpointer data, const char *buf, gsize len); |
|
| 826 |
|
| 827 /** |
|
| 828 * Fetches the data from a URL, and passes it to a callback function. |
|
| 829 * |
|
| 830 * @param url The URL. |
|
| 831 * @param full TRUE if this is the full URL, or FALSE if it's a |
|
| 832 * partial URL. |
|
| 833 * @param user_agent The user agent field to use, or NULL. |
|
| 834 * @param http11 TRUE if HTTP/1.1 should be used to download the file. |
|
| 835 * @param cb The callback function. |
|
| 836 * @param data The user data to pass to the callback function. |
|
| 837 */ |
|
| 838 #define gaim_url_fetch(url, full, user_agent, http11, cb, data) \ |
|
| 839 gaim_url_fetch_request(url, full, user_agent, http11, NULL, \ |
|
| 840 FALSE, cb, data); |
|
| 841 |
|
| 842 /** |
|
| 843 * Fetches the data from a URL, and passes it to a callback function. |
|
| 844 * |
|
| 845 * @param url The URL. |
|
| 846 * @param full TRUE if this is the full URL, or FALSE if it's a |
|
| 847 * partial URL. |
|
| 848 * @param user_agent The user agent field to use, or NULL. |
|
| 849 * @param http11 TRUE if HTTP/1.1 should be used to download the file. |
|
| 850 * @param request A HTTP request to send to the server instead of the |
|
| 851 * standard GET |
|
| 852 * @param include_headers if TRUE, include the HTTP headers in the |
|
| 853 * response |
|
| 854 * @param cb The callback function. |
|
| 855 * @param data The user data to pass to the callback function. |
|
| 856 */ |
|
| 857 void gaim_url_fetch_request(const char *url, gboolean full, |
|
| 858 const char *user_agent, gboolean http11, |
|
| 859 const char *request, gboolean include_headers, |
|
| 860 GaimURLFetchCallback cb, void *data); |
|
| 861 |
|
| 862 /** |
|
| 863 * Decodes a URL into a plain string. |
|
| 864 * |
|
| 865 * This will change hex codes and such to their ascii equivalents. |
|
| 866 * |
|
| 867 * @param str The string to translate. |
|
| 868 * |
|
| 869 * @return The resulting string. |
|
| 870 */ |
|
| 871 const char *gaim_url_decode(const char *str); |
|
| 872 |
|
| 873 /** |
|
| 874 * Encodes a URL into an escaped string. |
|
| 875 * |
|
| 876 * This will change non-alphanumeric characters to hex codes. |
|
| 877 * |
|
| 878 * @param str The string to translate. |
|
| 879 * |
|
| 880 * @return The resulting string. |
|
| 881 */ |
|
| 882 const char *gaim_url_encode(const char *str); |
|
| 883 |
|
| 884 /** |
|
| 885 * Checks if the given email address is syntactically valid. |
|
| 886 * |
|
| 887 * @param address The email address to validate. |
|
| 888 * |
|
| 889 * @return True if the email address is syntactically correct. |
|
| 890 */ |
|
| 891 gboolean gaim_email_is_valid(const char *address); |
|
| 892 |
|
| 893 /** |
|
| 894 * This function extracts a list of URIs from the a "text/uri-list" |
|
| 895 * string. It was "borrowed" from gnome_uri_list_extract_uris |
|
| 896 * |
|
| 897 * @param uri_list An uri-list in the standard format. |
|
| 898 * |
|
| 899 * @return A GList containing strings allocated with g_malloc |
|
| 900 * that have been splitted from uri-list. |
|
| 901 */ |
|
| 902 GList *gaim_uri_list_extract_uris(const gchar *uri_list); |
|
| 903 |
|
| 904 /** |
|
| 905 * This function extracts a list of filenames from a |
|
| 906 * "text/uri-list" string. It was "borrowed" from |
|
| 907 * gnome_uri_list_extract_filenames |
|
| 908 * |
|
| 909 * @param uri_list A uri-list in the standard format. |
|
| 910 * |
|
| 911 * @return A GList containing strings allocated with g_malloc that |
|
| 912 * contain the filenames in the uri-list. Note that unlike |
|
| 913 * gaim_uri_list_extract_uris() function, this will discard |
|
| 914 * any non-file uri from the result value. |
|
| 915 */ |
|
| 916 GList *gaim_uri_list_extract_filenames(const gchar *uri_list); |
|
| 917 |
|
| 918 /*@}*/ |
|
| 919 |
|
| 920 /************************************************************************** |
|
| 921 * UTF8 String Functions |
|
| 922 **************************************************************************/ |
|
| 923 /*@{*/ |
|
| 924 |
|
| 925 /** |
|
| 926 * Attempts to convert a string to UTF-8 from an unknown encoding. |
|
| 927 * |
|
| 928 * This function checks the locale and tries sane defaults. |
|
| 929 * |
|
| 930 * @param str The source string. |
|
| 931 * |
|
| 932 * @return The UTF-8 string, or @c NULL if it could not be converted. |
|
| 933 */ |
|
| 934 gchar *gaim_utf8_try_convert(const char *str); |
|
| 935 |
|
| 936 /** |
|
| 937 * Salvages the valid UTF-8 characters from a string, replacing any |
|
| 938 * invalid characters with a filler character (currently hardcoded to |
|
| 939 * '?'). |
|
| 940 * |
|
| 941 * @param str The source string. |
|
| 942 * |
|
| 943 * @return A valid UTF-8 string. |
|
| 944 */ |
|
| 945 gchar *gaim_utf8_salvage(const char *str); |
|
| 946 |
|
| 947 /** |
|
| 948 * Compares two UTF-8 strings case-insensitively. |
|
| 949 * |
|
| 950 * @param a The first string. |
|
| 951 * @param b The second string. |
|
| 952 * |
|
| 953 * @return -1 if @a is less than @a b. |
|
| 954 * 0 if @a is equal to @a b. |
|
| 955 * 1 if @a is greater than @a b. |
|
| 956 */ |
|
| 957 int gaim_utf8_strcasecmp(const char *a, const char *b); |
|
| 958 |
|
| 959 /** |
|
| 960 * Case insensitive search for a word in a string. The needle string |
|
| 961 * must be contained in the haystack string and not be immediately |
|
| 962 * preceded or immediately followed by another alpha-numeric character. |
|
| 963 * |
|
| 964 * @param haystack The string to search in. |
|
| 965 * @param needle The substring to find. |
|
| 966 * |
|
| 967 * @return TRUE if haystack has the word, otherwise FALSE |
|
| 968 */ |
|
| 969 gboolean gaim_utf8_has_word(const char *haystack, const char *needle); |
|
| 970 |
|
| 971 /** |
|
| 972 * Prints a UTF-8 message to the given file stream. The function |
|
| 973 * tries to convert the UTF-8 message to user's locale. If this |
|
| 974 * is not possible, the original UTF-8 text will be printed. |
|
| 975 * |
|
| 976 * @param filestream The file stream (e.g. STDOUT or STDERR) |
|
| 977 * @param message The message to print. |
|
| 978 */ |
|
| 979 void gaim_print_utf8_to_console(FILE *filestream, char *message); |
|
| 980 |
|
| 981 /** |
|
| 982 * Checks for messages starting with "/me " |
|
| 983 * |
|
| 984 * @param message The message to check |
|
| 985 * @param len The message length, or -1 |
|
| 986 * |
|
| 987 * @return TRUE if it starts with /me, and it has been removed, otherwise FALSE |
|
| 988 */ |
|
| 989 gboolean gaim_message_meify(char *message, size_t len); |
|
| 990 |
|
| 991 /** |
|
| 992 * Removes the underscore characters from a string used identify the mnemonic |
|
| 993 * character. |
|
| 994 * |
|
| 995 * @param in The string to strip |
|
| 996 * |
|
| 997 * @return The stripped string |
|
| 998 */ |
|
| 999 char *gaim_text_strip_mnemonic(const char *in); |
|
| 1000 |
|
| 1001 /*@}*/ |
|
| 1002 |
|
| 1003 /** |
|
| 1004 * Adds 8 to something. |
|
| 1005 * |
|
| 1006 * Blame SimGuy. |
|
| 1007 * |
|
| 1008 * @param x The number to add 8 to. |
|
| 1009 * |
|
| 1010 * @return x + 8 |
|
| 1011 */ |
|
| 1012 #define gaim_add_eight(x) ((x)+8) |
|
| 1013 |
|
| 1014 /** |
|
| 1015 * Does the reverse of gaim_escape_filename |
|
| 1016 * |
|
| 1017 * This will change hex codes and such to their ascii equivalents. |
|
| 1018 * |
|
| 1019 * @param str The string to translate. |
|
| 1020 * |
|
| 1021 * @return The resulting string. |
|
| 1022 */ |
|
| 1023 const char *gaim_unescape_filename(const char *str); |
|
| 1024 |
|
| 1025 /** |
|
| 1026 * Escapes filesystem-unfriendly characters from a filename |
|
| 1027 * |
|
| 1028 * @param str The string to translate. |
|
| 1029 * |
|
| 1030 * @return The resulting string. |
|
| 1031 */ |
|
| 1032 const char *gaim_escape_filename(const char *str); |
|
| 1033 |
|
| 1034 #ifdef __cplusplus |
|
| 1035 } |
|
| 1036 #endif |
|
| 1037 |
|
| 1038 #endif /* _GAIM_UTIL_H_ */ |
|