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, size_t size, unsigned int count, ...)
69 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, size_t size,
100 unsigned int count, ...)
108 va_start (ap, count);
111 r = va_arg (ap, const char **);
114 while ((needed < size) && (buffer[needed] != '\0'))
119 return 0; /* error */
122 needed++; /* skip 0-termination */
131 * Convert a given filesize into a fancy human-readable format.
133 * @param size number of bytes
134 * @return fancy representation of the size (possibly rounded) for humans
137 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
139 const char *unit = _( /* size unit */ "b");
145 unit = _( /* size unit */ "KiB");
149 unit = _( /* size unit */ "MiB");
153 unit = _( /* size unit */ "GiB");
157 unit = _( /* size unit */ "TiB");
162 ret = GNUNET_malloc (32);
163 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
169 * Convert the len characters long character sequence
170 * given in input that is in the given charset
172 * @return the converted string (0-terminated),
173 * if conversion fails, a copy of the orignal
174 * string is returned.
177 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
181 #if ENABLE_NLS && HAVE_ICONV
188 cd = iconv_open ("UTF-8", charset);
189 if (cd == (iconv_t) - 1)
191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
193 _("Character set requested was `%s'\n"), charset);
194 ret = GNUNET_malloc (len + 1);
195 memcpy (ret, input, len);
199 tmpSize = 3 * len + 4;
200 tmp = GNUNET_malloc (tmpSize);
204 #if FREEBSD || DARWIN || WINDOWS
205 (const char **) &input,
209 &len, &itmp, &finSize) == SIZE_MAX)
211 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv");
214 ret = GNUNET_malloc (len + 1);
215 memcpy (ret, input, len);
219 ret = GNUNET_malloc (tmpSize - finSize + 1);
220 memcpy (ret, tmp, tmpSize - finSize);
221 ret[tmpSize - finSize] = '\0';
223 if (0 != iconv_close (cd))
224 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
227 ret = GNUNET_malloc (len + 1);
228 memcpy (ret, input, len);
236 * Complete filename (a la shell) from abbrevition.
237 * @param fil the name of the file, may contain ~/ or
238 * be relative to the current directory
239 * @returns the full file name,
240 * NULL is returned on error
243 GNUNET_STRINGS_filename_expand (const char *fil)
261 if (fil[0] == DIR_SEPARATOR)
262 /* absolute path, just copy */
263 return GNUNET_strdup (fil);
266 fm = getenv ("HOME");
269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 ("Failed to expand `$HOME': environment variable `HOME' not set"));
274 fm = GNUNET_strdup (fm);
275 /* do not copy '~' */
278 /* skip over dir seperator to be consistent */
279 if (fil_ptr[0] == DIR_SEPARATOR)
290 buffer = GNUNET_malloc (len);
291 if (getcwd (buffer, len) != NULL)
296 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
299 GNUNET_free (buffer);
302 GNUNET_free (buffer);
307 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getcwd");
308 buffer = getenv ("PWD"); /* alternative */
310 fm = GNUNET_strdup (buffer);
313 fm = GNUNET_strdup ("./"); /* give up */
315 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
316 buffer = GNUNET_malloc (n);
317 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
318 (fm[strlen (fm) - 1] ==
319 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
323 fn = GNUNET_malloc (MAX_PATH + 1);
325 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
327 SetErrnoFromWinError (lRet);
328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
331 /* is the path relative? */
332 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
334 char szCurDir[MAX_PATH + 1];
336 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
337 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
339 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
340 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
343 buffer = GNUNET_malloc (MAX_PATH + 1);
344 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
355 * Give relative time in human-readable fancy format.
357 * @param delta time in milli seconds
358 * @return time as human-readable string
361 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
363 const char *unit = _( /* time unit */ "ms");
365 uint64_t dval = delta.rel_value;
367 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
368 return GNUNET_strdup (_("eternity"));
372 unit = _( /* time unit */ "s");
376 unit = _( /* time unit */ "m");
380 unit = _( /* time unit */ "h");
384 unit = _( /* time unit */ " days");
389 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
395 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
396 * return value does not include the newline character.
398 * @param t time to convert
399 * @return absolute time in human-readable format
402 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
407 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
408 return GNUNET_strdup (_("end of time"));
409 tt = t.abs_value / 1000;
411 ret = ctime_r (&tt, GNUNET_malloc (32));
413 ret = GNUNET_strdup (ctime (&tt));
415 ret[strlen (ret) - 1] = '\0';
421 /* end of strings.c */