2 This file is part of GNUnet.
3 (C) 2005, 2006 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/strings.c
23 * @brief string functions
25 * @author Christian Grothoff
32 #include "gnunet_common.h"
33 #include "gnunet_strings_lib.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
41 * Fill a buffer of the given size with
42 * count 0-terminated strings (given as varargs).
43 * If "buffer" is NULL, only compute the amount of
44 * space required (sum of "strlen(arg)+1").
46 * Unlike using "snprintf" with "%s", this function
47 * will add 0-terminators after each string. The
48 * "GNUNET_string_buffer_tokenize" function can be
49 * used to parse the buffer back into individual
52 * @param buffer the buffer to fill with strings, can
53 * be NULL in which case only the necessary
54 * amount of space will be calculated
55 * @param size number of bytes available in buffer
56 * @param count number of strings that follow
57 * @param ... count 0-terminated strings to copy to buffer
58 * @return number of bytes written to the buffer
59 * (or number of bytes that would have been written)
62 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
73 s = va_arg (ap, const char *);
75 slen = strlen (s) + 1;
78 GNUNET_assert (needed + slen <= size);
79 memcpy (&buffer[needed], s, slen);
90 * Given a buffer of a given size, find "count"
91 * 0-terminated strings in the buffer and assign
92 * the count (varargs) of type "const char**" to the
93 * locations of the respective strings in the
96 * @param buffer the buffer to parse
97 * @param size size of the buffer
98 * @param count number of strings to locate
99 * @return offset of the character after the last 0-termination
100 * in the buffer, or 0 on error.
103 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
104 unsigned int count, ...)
112 va_start (ap, count);
115 r = va_arg (ap, const char **);
118 while ((needed < size) && (buffer[needed] != '\0'))
123 return 0; /* error */
126 needed++; /* skip 0-termination */
135 * Convert a given filesize into a fancy human-readable format.
137 * @param size number of bytes
138 * @return fancy representation of the size (possibly rounded) for humans
141 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
143 const char *unit = _( /* size unit */ "b");
166 ret = GNUNET_malloc (32);
167 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
173 * Convert a given fancy human-readable size to bytes.
175 * @param fancy_size human readable string (i.e. 1 MB)
176 * @param size set to the size in bytes
177 * @return GNUNET_OK on success, GNUNET_SYSERR on error
180 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
181 unsigned long long *size)
186 unsigned long long value;
200 "GiB", 1024 * 1024 * 1024},
202 "GB", 1000 * 1000 * 1000},
204 "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
206 "TB", 1000LL * 1000LL * 1000LL * 1024LL},
208 "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
210 "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
212 "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
214 "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
218 unsigned long long ret;
221 unsigned long long last;
226 in = GNUNET_strdup (fancy_size);
227 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
230 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
232 if (table[i].name != NULL)
233 last *= table[i].value;
238 if (1 != sscanf (tok, "%llu", &last))
241 return GNUNET_SYSERR; /* expected number */
253 * Convert a given fancy human-readable time to our internal
256 * @param fancy_size human readable string (i.e. 1 minute)
257 * @param rtime set to the relative time
258 * @return GNUNET_OK on success, GNUNET_SYSERR on error
261 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
262 struct GNUNET_TIME_Relative *rtime)
267 unsigned long long value;
279 "minutes", 60 * 1000},
283 "h", 60 * 60 * 1000},
285 "d", 24 * 60 * 60 * 1000},
287 "a", 31557600 /* year */ },
291 unsigned long long ret;
294 unsigned long long last;
297 if ((0 == strcasecmp (fancy_size, "infinity")) ||
298 (0 == strcasecmp (fancy_size, "forever")))
300 *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
305 in = GNUNET_strdup (fancy_size);
306 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
309 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
311 if (table[i].name != NULL)
312 last *= table[i].value;
317 if (1 != sscanf (tok, "%llu", &last))
320 return GNUNET_SYSERR; /* expected number */
325 rtime->rel_value = (uint64_t) ret;
332 * Convert the len characters long character sequence
333 * given in input that is in the given charset
335 * @return the converted string (0-terminated),
336 * if conversion fails, a copy of the orignal
337 * string is returned.
340 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
344 #if ENABLE_NLS && HAVE_ICONV
351 cd = iconv_open ("UTF-8", charset);
352 if (cd == (iconv_t) - 1)
354 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
355 LOG (GNUNET_ERROR_TYPE_WARNING, _("Character set requested was `%s'\n"),
357 ret = GNUNET_malloc (len + 1);
358 memcpy (ret, input, len);
362 tmpSize = 3 * len + 4;
363 tmp = GNUNET_malloc (tmpSize);
367 #if FREEBSD || DARWIN || WINDOWS
368 (const char **) &input,
372 &len, &itmp, &finSize) == SIZE_MAX)
374 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
377 ret = GNUNET_malloc (len + 1);
378 memcpy (ret, input, len);
382 ret = GNUNET_malloc (tmpSize - finSize + 1);
383 memcpy (ret, tmp, tmpSize - finSize);
384 ret[tmpSize - finSize] = '\0';
386 if (0 != iconv_close (cd))
387 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
390 ret = GNUNET_malloc (len + 1);
391 memcpy (ret, input, len);
399 * Complete filename (a la shell) from abbrevition.
400 * @param fil the name of the file, may contain ~/ or
401 * be relative to the current directory
402 * @returns the full file name,
403 * NULL is returned on error
406 GNUNET_STRINGS_filename_expand (const char *fil)
424 if (fil[0] == DIR_SEPARATOR)
425 /* absolute path, just copy */
426 return GNUNET_strdup (fil);
429 fm = getenv ("HOME");
432 LOG (GNUNET_ERROR_TYPE_WARNING,
433 _("Failed to expand `$HOME': environment variable `HOME' not set"));
436 fm = GNUNET_strdup (fm);
437 /* do not copy '~' */
440 /* skip over dir seperator to be consistent */
441 if (fil_ptr[0] == DIR_SEPARATOR)
452 buffer = GNUNET_malloc (len);
453 if (getcwd (buffer, len) != NULL)
458 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
461 GNUNET_free (buffer);
464 GNUNET_free (buffer);
469 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
470 buffer = getenv ("PWD"); /* alternative */
472 fm = GNUNET_strdup (buffer);
475 fm = GNUNET_strdup ("./"); /* give up */
477 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
478 buffer = GNUNET_malloc (n);
479 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
480 (fm[strlen (fm) - 1] ==
481 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
485 fn = GNUNET_malloc (MAX_PATH + 1);
487 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
489 SetErrnoFromWinError (lRet);
490 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
493 /* is the path relative? */
494 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
496 char szCurDir[MAX_PATH + 1];
498 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
499 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
501 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
502 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
505 buffer = GNUNET_malloc (MAX_PATH + 1);
506 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
517 * Give relative time in human-readable fancy format.
519 * @param delta time in milli seconds
520 * @return time as human-readable string
523 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
525 const char *unit = _( /* time unit */ "ms");
527 uint64_t dval = delta.rel_value;
529 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
530 return GNUNET_strdup (_("eternity"));
534 unit = _( /* time unit */ "s");
538 unit = _( /* time unit */ "m");
542 unit = _( /* time unit */ "h");
546 unit = _( /* time unit */ " days");
551 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
557 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
558 * return value does not include the newline character.
560 * @param t time to convert
561 * @return absolute time in human-readable format
564 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
569 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
570 return GNUNET_strdup (_("end of time"));
571 tt = t.abs_value / 1000;
573 ret = ctime_r (&tt, GNUNET_malloc (32));
575 ret = GNUNET_strdup (ctime (&tt));
577 ret[strlen (ret) - 1] = '\0';
583 /* end of strings.c */