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");
150 unit = _( /* size unit */ "KiB");
154 unit = _( /* size unit */ "MiB");
158 unit = _( /* size unit */ "GiB");
162 unit = _( /* size unit */ "TiB");
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, " "))
214 fprintf (stderr, "%s - %llu %llu\n", tok, ret, last);
216 while ( (table[i].name != NULL) &&
217 (0 != strcasecmp (table[i].name, tok) ) )
219 if (table[i].name != NULL)
220 last *= table[i].value;
225 if (1 != sscanf (tok, "%llu", &last))
226 return GNUNET_SYSERR; /* expected number */
236 * Convert a given fancy human-readable time to our internal
239 * @param fancy_size human readable string (i.e. 1 minute)
240 * @param rtime set to the relative time
241 * @return GNUNET_OK on success, GNUNET_SYSERR on error
244 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
245 struct GNUNET_TIME_Relative *rtime)
249 unsigned long long value;
254 { "min", 60 * 1000 },
255 { "minutes", 60 * 1000 },
257 { "h", 60 * 60 * 1000 },
258 { "d", 24 * 60 * 60 * 1000 },
259 { "a", 31557600 /* year */ },
262 unsigned long long ret;
265 unsigned long long last;
268 if ((0 == strcasecmp (fancy_size, "infinity")) ||
269 (0 == strcasecmp (fancy_size, "forever")))
271 *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
276 in = GNUNET_strdup (fancy_size);
277 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
280 while ( (table[i].name != NULL) &&
281 (0 != strcasecmp (table[i].name, tok) ) )
283 if (table[i].name != NULL)
284 last *= table[i].value;
289 if (1 != sscanf (tok, "%llu", &last))
290 return GNUNET_SYSERR; /* expected number */
294 rtime->rel_value = (uint64_t) ret;
300 * Convert the len characters long character sequence
301 * given in input that is in the given charset
303 * @return the converted string (0-terminated),
304 * if conversion fails, a copy of the orignal
305 * string is returned.
308 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
312 #if ENABLE_NLS && HAVE_ICONV
319 cd = iconv_open ("UTF-8", charset);
320 if (cd == (iconv_t) - 1)
322 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
323 LOG (GNUNET_ERROR_TYPE_WARNING,
324 _("Character set requested was `%s'\n"), charset);
325 ret = GNUNET_malloc (len + 1);
326 memcpy (ret, input, len);
330 tmpSize = 3 * len + 4;
331 tmp = GNUNET_malloc (tmpSize);
335 #if FREEBSD || DARWIN || WINDOWS
336 (const char **) &input,
340 &len, &itmp, &finSize) == SIZE_MAX)
342 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
345 ret = GNUNET_malloc (len + 1);
346 memcpy (ret, input, len);
350 ret = GNUNET_malloc (tmpSize - finSize + 1);
351 memcpy (ret, tmp, tmpSize - finSize);
352 ret[tmpSize - finSize] = '\0';
354 if (0 != iconv_close (cd))
355 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
358 ret = GNUNET_malloc (len + 1);
359 memcpy (ret, input, len);
367 * Complete filename (a la shell) from abbrevition.
368 * @param fil the name of the file, may contain ~/ or
369 * be relative to the current directory
370 * @returns the full file name,
371 * NULL is returned on error
374 GNUNET_STRINGS_filename_expand (const char *fil)
392 if (fil[0] == DIR_SEPARATOR)
393 /* absolute path, just copy */
394 return GNUNET_strdup (fil);
397 fm = getenv ("HOME");
400 LOG (GNUNET_ERROR_TYPE_WARNING,
402 ("Failed to expand `$HOME': environment variable `HOME' not set"));
405 fm = GNUNET_strdup (fm);
406 /* do not copy '~' */
409 /* skip over dir seperator to be consistent */
410 if (fil_ptr[0] == DIR_SEPARATOR)
421 buffer = GNUNET_malloc (len);
422 if (getcwd (buffer, len) != NULL)
427 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
430 GNUNET_free (buffer);
433 GNUNET_free (buffer);
438 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
439 buffer = getenv ("PWD"); /* alternative */
441 fm = GNUNET_strdup (buffer);
444 fm = GNUNET_strdup ("./"); /* give up */
446 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
447 buffer = GNUNET_malloc (n);
448 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
449 (fm[strlen (fm) - 1] ==
450 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
454 fn = GNUNET_malloc (MAX_PATH + 1);
456 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
458 SetErrnoFromWinError (lRet);
459 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
462 /* is the path relative? */
463 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
465 char szCurDir[MAX_PATH + 1];
467 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
468 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
470 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
471 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
474 buffer = GNUNET_malloc (MAX_PATH + 1);
475 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
486 * Give relative time in human-readable fancy format.
488 * @param delta time in milli seconds
489 * @return time as human-readable string
492 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
494 const char *unit = _( /* time unit */ "ms");
496 uint64_t dval = delta.rel_value;
498 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
499 return GNUNET_strdup (_("eternity"));
503 unit = _( /* time unit */ "s");
507 unit = _( /* time unit */ "m");
511 unit = _( /* time unit */ "h");
515 unit = _( /* time unit */ " days");
520 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
526 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
527 * return value does not include the newline character.
529 * @param t time to convert
530 * @return absolute time in human-readable format
533 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
538 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
539 return GNUNET_strdup (_("end of time"));
540 tt = t.abs_value / 1000;
542 ret = ctime_r (&tt, GNUNET_malloc (32));
544 ret = GNUNET_strdup (ctime (&tt));
546 ret[strlen (ret) - 1] = '\0';
552 /* end of strings.c */