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,
74 s = va_arg (ap, const char *);
76 slen = strlen (s) + 1;
79 GNUNET_assert (needed + slen <= size);
80 memcpy (&buffer[needed], s, slen);
91 * Given a buffer of a given size, find "count"
92 * 0-terminated strings in the buffer and assign
93 * the count (varargs) of type "const char**" to the
94 * locations of the respective strings in the
97 * @param buffer the buffer to parse
98 * @param size size of the buffer
99 * @param count number of strings to locate
100 * @return offset of the character after the last 0-termination
101 * in the buffer, or 0 on error.
104 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
105 unsigned int count, ...)
113 va_start (ap, count);
116 r = va_arg (ap, const char **);
119 while ((needed < size) && (buffer[needed] != '\0'))
124 return 0; /* error */
127 needed++; /* skip 0-termination */
136 * Convert a given filesize into a fancy human-readable format.
138 * @param size number of bytes
139 * @return fancy representation of the size (possibly rounded) for humans
142 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
144 const char *unit = _( /* size unit */ "b");
167 ret = GNUNET_malloc (32);
168 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
174 * Convert a given fancy human-readable size to bytes.
176 * @param fancy_size human readable string (i.e. 1 MB)
177 * @param size set to the size in bytes
178 * @return GNUNET_OK on success, GNUNET_SYSERR on error
181 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
182 unsigned long long *size)
186 unsigned long long value;
191 { "MiB", 1024 * 1024 },
192 { "MB", 1000 * 1000 },
193 { "GiB", 1024 * 1024 * 1024 },
194 { "GB", 1000 * 1000 * 1000 },
195 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
196 { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
197 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
198 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
199 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
200 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
203 unsigned long long ret;
206 unsigned long long last;
211 in = GNUNET_strdup (fancy_size);
212 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
215 while ( (table[i].name != NULL) &&
216 (0 != strcasecmp (table[i].name, tok) ) )
218 if (table[i].name != NULL)
219 last *= table[i].value;
224 if (1 != sscanf (tok, "%llu", &last))
225 return GNUNET_SYSERR; /* expected number */
235 * Convert a given fancy human-readable time to our internal
238 * @param fancy_size human readable string (i.e. 1 minute)
239 * @param rtime set to the relative time
240 * @return GNUNET_OK on success, GNUNET_SYSERR on error
243 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
244 struct GNUNET_TIME_Relative *rtime)
248 unsigned long long value;
253 { "min", 60 * 1000 },
254 { "minutes", 60 * 1000 },
256 { "h", 60 * 60 * 1000 },
257 { "d", 24 * 60 * 60 * 1000 },
258 { "a", 31557600 /* year */ },
261 unsigned long long ret;
264 unsigned long long last;
267 if ((0 == strcasecmp (fancy_size, "infinity")) ||
268 (0 == strcasecmp (fancy_size, "forever")))
270 *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
275 in = GNUNET_strdup (fancy_size);
276 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
279 while ( (table[i].name != NULL) &&
280 (0 != strcasecmp (table[i].name, tok) ) )
282 if (table[i].name != NULL)
283 last *= table[i].value;
288 if (1 != sscanf (tok, "%llu", &last))
289 return GNUNET_SYSERR; /* expected number */
293 rtime->rel_value = (uint64_t) ret;
299 * Convert the len characters long character sequence
300 * given in input that is in the given charset
302 * @return the converted string (0-terminated),
303 * if conversion fails, a copy of the orignal
304 * string is returned.
307 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
311 #if ENABLE_NLS && HAVE_ICONV
318 cd = iconv_open ("UTF-8", charset);
319 if (cd == (iconv_t) - 1)
321 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
322 LOG (GNUNET_ERROR_TYPE_WARNING,
323 _("Character set requested was `%s'\n"), charset);
324 ret = GNUNET_malloc (len + 1);
325 memcpy (ret, input, len);
329 tmpSize = 3 * len + 4;
330 tmp = GNUNET_malloc (tmpSize);
334 #if FREEBSD || DARWIN || WINDOWS
335 (const char **) &input,
339 &len, &itmp, &finSize) == SIZE_MAX)
341 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
344 ret = GNUNET_malloc (len + 1);
345 memcpy (ret, input, len);
349 ret = GNUNET_malloc (tmpSize - finSize + 1);
350 memcpy (ret, tmp, tmpSize - finSize);
351 ret[tmpSize - finSize] = '\0';
353 if (0 != iconv_close (cd))
354 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
357 ret = GNUNET_malloc (len + 1);
358 memcpy (ret, input, len);
366 * Complete filename (a la shell) from abbrevition.
367 * @param fil the name of the file, may contain ~/ or
368 * be relative to the current directory
369 * @returns the full file name,
370 * NULL is returned on error
373 GNUNET_STRINGS_filename_expand (const char *fil)
391 if (fil[0] == DIR_SEPARATOR)
392 /* absolute path, just copy */
393 return GNUNET_strdup (fil);
396 fm = getenv ("HOME");
399 LOG (GNUNET_ERROR_TYPE_WARNING,
401 ("Failed to expand `$HOME': environment variable `HOME' not set"));
404 fm = GNUNET_strdup (fm);
405 /* do not copy '~' */
408 /* skip over dir seperator to be consistent */
409 if (fil_ptr[0] == DIR_SEPARATOR)
420 buffer = GNUNET_malloc (len);
421 if (getcwd (buffer, len) != NULL)
426 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
429 GNUNET_free (buffer);
432 GNUNET_free (buffer);
437 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
438 buffer = getenv ("PWD"); /* alternative */
440 fm = GNUNET_strdup (buffer);
443 fm = GNUNET_strdup ("./"); /* give up */
445 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
446 buffer = GNUNET_malloc (n);
447 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
448 (fm[strlen (fm) - 1] ==
449 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
453 fn = GNUNET_malloc (MAX_PATH + 1);
455 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
457 SetErrnoFromWinError (lRet);
458 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
461 /* is the path relative? */
462 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
464 char szCurDir[MAX_PATH + 1];
466 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
467 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
469 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
470 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
473 buffer = GNUNET_malloc (MAX_PATH + 1);
474 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
485 * Give relative time in human-readable fancy format.
487 * @param delta time in milli seconds
488 * @return time as human-readable string
491 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
493 const char *unit = _( /* time unit */ "ms");
495 uint64_t dval = delta.rel_value;
497 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
498 return GNUNET_strdup (_("eternity"));
502 unit = _( /* time unit */ "s");
506 unit = _( /* time unit */ "m");
510 unit = _( /* time unit */ "h");
514 unit = _( /* time unit */ " days");
519 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
525 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
526 * return value does not include the newline character.
528 * @param t time to convert
529 * @return absolute time in human-readable format
532 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
537 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
538 return GNUNET_strdup (_("end of time"));
539 tt = t.abs_value / 1000;
541 ret = ctime_r (&tt, GNUNET_malloc (32));
543 ret = GNUNET_strdup (ctime (&tt));
545 ret[strlen (ret) - 1] = '\0';
551 /* end of strings.c */