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"
37 * Fill a buffer of the given size with
38 * count 0-terminated strings (given as varargs).
39 * If "buffer" is NULL, only compute the amount of
40 * space required (sum of "strlen(arg)+1").
42 * Unlike using "snprintf" with "%s", this function
43 * will add 0-terminators after each string. The
44 * "GNUNET_string_buffer_tokenize" function can be
45 * used to parse the buffer back into individual
48 * @param buffer the buffer to fill with strings, can
49 * be NULL in which case only the necessary
50 * amount of space will be calculated
51 * @param size number of bytes available in buffer
52 * @param count number of strings that follow
53 * @param ... count 0-terminated strings to copy to buffer
54 * @return number of bytes written to the buffer
55 * (or number of bytes that would have been written)
58 GNUNET_STRINGS_buffer_fill (char *buffer,
59 size_t size, unsigned int count, ...)
70 s = va_arg (ap, const char *);
71 slen = strlen (s) + 1;
74 GNUNET_assert (needed + slen <= size);
75 memcpy (&buffer[needed], s, slen);
86 * Given a buffer of a given size, find "count"
87 * 0-terminated strings in the buffer and assign
88 * the count (varargs) of type "const char**" to the
89 * locations of the respective strings in the
92 * @param buffer the buffer to parse
93 * @param size size of the buffer
94 * @param count number of strings to locate
95 * @return offset of the character after the last 0-termination
96 * in the buffer, or 0 on error.
99 GNUNET_STRINGS_buffer_tokenize (const char *buffer,
100 size_t size, unsigned int count, ...)
108 va_start (ap, count);
111 r = va_arg (ap, const char **);
113 while ((needed < size) && (buffer[needed] != '\0'))
118 return 0; /* error */
121 needed++; /* skip 0-termination */
130 * Convert a given filesize into a fancy human-readable format.
132 * @param size number of bytes
133 * @return fancy representation of the size (possibly rounded) for humans
136 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
138 const char *unit = _( /* size unit */ "b");
144 unit = _( /* size unit */ "KiB");
148 unit = _( /* size unit */ "MiB");
152 unit = _( /* size unit */ "GiB");
156 unit = _( /* size unit */ "TiB");
161 ret = GNUNET_malloc (32);
162 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
168 * Convert the len characters long character sequence
169 * given in input that is in the given charset
171 * @return the converted string (0-terminated),
172 * if conversion fails, a copy of the orignal
173 * string is returned.
176 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
179 #if ENABLE_NLS && HAVE_ICONV
186 cd = iconv_open ("UTF-8", charset);
187 if (cd == (iconv_t) - 1)
189 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
191 _("Character set requested was `%s'\n"), charset);
192 ret = GNUNET_malloc (len + 1);
193 memcpy (ret, input, len);
197 tmpSize = 3 * len + 4;
198 tmp = GNUNET_malloc (tmpSize);
202 #if FREEBSD || DARWIN
203 (const char **) &input,
207 &len, &itmp, &finSize) == SIZE_MAX)
209 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv");
212 ret = GNUNET_malloc (len + 1);
213 memcpy (ret, input, len);
217 ret = GNUNET_malloc (tmpSize - finSize + 1);
218 memcpy (ret, tmp, tmpSize - finSize);
219 ret[tmpSize - finSize] = '\0';
221 if (0 != iconv_close (cd))
222 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
225 ret = GNUNET_malloc (len + 1);
226 memcpy (ret, input, len);
234 * Complete filename (a la shell) from abbrevition.
235 * @param fil the name of the file, may contain ~/ or
236 * be relative to the current directory
237 * @returns the full file name,
238 * NULL is returned on error
241 GNUNET_STRINGS_filename_expand (const char *fil)
258 if (fil[0] == DIR_SEPARATOR)
259 /* absolute path, just copy */
260 return GNUNET_strdup (fil);
263 fm = getenv ("HOME");
266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268 ("Failed to expand `$HOME': environment variable `HOME' not set"));
271 fm = GNUNET_strdup (fm);
272 /* do not copy '~' */
275 /* skip over dir seperator to be consistent */
276 if (fil_ptr[0] == DIR_SEPARATOR)
287 buffer = GNUNET_malloc (len);
288 if (getcwd (buffer, len) != NULL)
293 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
296 GNUNET_free (buffer);
299 GNUNET_free (buffer);
304 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getcwd");
305 buffer = getenv ("PWD"); /* alternative */
307 fm = GNUNET_strdup (buffer);
310 fm = GNUNET_strdup ("./"); /* give up */
312 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
313 buffer = GNUNET_malloc (n);
314 GNUNET_snprintf (buffer, n, "%s%s%s",
316 (fm[strlen (fm) - 1] ==
317 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
321 fn = GNUNET_malloc (MAX_PATH + 1);
323 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
325 SetErrnoFromWinError (lRet);
326 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
327 "plibc_conv_to_win_path");
330 /* is the path relative? */
331 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
333 char szCurDir[MAX_PATH + 1];
334 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
335 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
337 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
338 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
339 "GetCurrentDirectory");
342 buffer = GNUNET_malloc (MAX_PATH + 1);
343 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
354 * Give relative time in human-readable fancy format.
356 * @param delta time in milli seconds
357 * @return time as human-readable string
360 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
362 const char *unit = _( /* time unit */ "ms");
364 uint64_t dval = delta.value;
366 if (delta.value == GNUNET_TIME_UNIT_FOREVER_REL.value)
367 return GNUNET_strdup (_("eternity"));
371 unit = _( /* time unit */ "s");
375 unit = _( /* time unit */ "m");
379 unit = _( /* time unit */ "h");
383 unit = _( /* time unit */ " days");
388 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
394 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
395 * return value does not include the newline character.
397 * @param t time to convert
398 * @return absolute time in human-readable format
401 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
406 if (t.value == GNUNET_TIME_UNIT_FOREVER_ABS.value)
407 return GNUNET_strdup (_("end of time"));
410 ret = ctime_r (&tt, GNUNET_malloc (32));
412 ret = GNUNET_strdup (ctime (&tt));
414 ret[strlen (ret) - 1] = '\0';
420 /* end of strings.c */