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 the len characters long character sequence
175 * given in input that is in the given charset
177 * @return the converted string (0-terminated),
178 * if conversion fails, a copy of the orignal
179 * string is returned.
182 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
186 #if ENABLE_NLS && HAVE_ICONV
193 cd = iconv_open ("UTF-8", charset);
194 if (cd == (iconv_t) - 1)
196 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
197 LOG (GNUNET_ERROR_TYPE_WARNING,
198 _("Character set requested was `%s'\n"), charset);
199 ret = GNUNET_malloc (len + 1);
200 memcpy (ret, input, len);
204 tmpSize = 3 * len + 4;
205 tmp = GNUNET_malloc (tmpSize);
209 #if FREEBSD || DARWIN || WINDOWS
210 (const char **) &input,
214 &len, &itmp, &finSize) == SIZE_MAX)
216 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
219 ret = GNUNET_malloc (len + 1);
220 memcpy (ret, input, len);
224 ret = GNUNET_malloc (tmpSize - finSize + 1);
225 memcpy (ret, tmp, tmpSize - finSize);
226 ret[tmpSize - finSize] = '\0';
228 if (0 != iconv_close (cd))
229 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
232 ret = GNUNET_malloc (len + 1);
233 memcpy (ret, input, len);
241 * Complete filename (a la shell) from abbrevition.
242 * @param fil the name of the file, may contain ~/ or
243 * be relative to the current directory
244 * @returns the full file name,
245 * NULL is returned on error
248 GNUNET_STRINGS_filename_expand (const char *fil)
266 if (fil[0] == DIR_SEPARATOR)
267 /* absolute path, just copy */
268 return GNUNET_strdup (fil);
271 fm = getenv ("HOME");
274 LOG (GNUNET_ERROR_TYPE_WARNING,
276 ("Failed to expand `$HOME': environment variable `HOME' not set"));
279 fm = GNUNET_strdup (fm);
280 /* do not copy '~' */
283 /* skip over dir seperator to be consistent */
284 if (fil_ptr[0] == DIR_SEPARATOR)
295 buffer = GNUNET_malloc (len);
296 if (getcwd (buffer, len) != NULL)
301 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
304 GNUNET_free (buffer);
307 GNUNET_free (buffer);
312 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
313 buffer = getenv ("PWD"); /* alternative */
315 fm = GNUNET_strdup (buffer);
318 fm = GNUNET_strdup ("./"); /* give up */
320 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
321 buffer = GNUNET_malloc (n);
322 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
323 (fm[strlen (fm) - 1] ==
324 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
328 fn = GNUNET_malloc (MAX_PATH + 1);
330 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
332 SetErrnoFromWinError (lRet);
333 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
336 /* is the path relative? */
337 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
339 char szCurDir[MAX_PATH + 1];
341 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
342 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
344 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
345 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
348 buffer = GNUNET_malloc (MAX_PATH + 1);
349 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
360 * Give relative time in human-readable fancy format.
362 * @param delta time in milli seconds
363 * @return time as human-readable string
366 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
368 const char *unit = _( /* time unit */ "ms");
370 uint64_t dval = delta.rel_value;
372 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
373 return GNUNET_strdup (_("eternity"));
377 unit = _( /* time unit */ "s");
381 unit = _( /* time unit */ "m");
385 unit = _( /* time unit */ "h");
389 unit = _( /* time unit */ " days");
394 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
400 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
401 * return value does not include the newline character.
403 * @param t time to convert
404 * @return absolute time in human-readable format
407 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
412 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
413 return GNUNET_strdup (_("end of time"));
414 tt = t.abs_value / 1000;
416 ret = ctime_r (&tt, GNUNET_malloc (32));
418 ret = GNUNET_strdup (ctime (&tt));
420 ret[strlen (ret) - 1] = '\0';
426 /* end of strings.c */