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);
201 if (iconv (cd, (char **) &input, &len, &itmp, &finSize) == (size_t) - 1)
203 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv");
206 ret = GNUNET_malloc (len + 1);
207 memcpy (ret, input, len);
211 ret = GNUNET_malloc (tmpSize - finSize + 1);
212 memcpy (ret, tmp, tmpSize - finSize);
213 ret[tmpSize - finSize] = '\0';
215 if (0 != iconv_close (cd))
216 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
219 ret = GNUNET_malloc (len + 1);
220 memcpy (ret, input, len);
228 * Complete filename (a la shell) from abbrevition.
229 * @param fil the name of the file, may contain ~/ or
230 * be relative to the current directory
231 * @returns the full file name,
232 * NULL is returned on error
235 GNUNET_STRINGS_filename_expand (const char *fil)
252 if (fil[0] == DIR_SEPARATOR)
253 /* absolute path, just copy */
254 return GNUNET_strdup (fil);
257 fm = getenv ("HOME");
260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
262 ("Failed to expand `$HOME': environment variable `HOME' not set"));
265 fm = GNUNET_strdup (fm);
266 /* do not copy '~' */
269 /* skip over dir seperator to be consistent */
270 if (fil_ptr[0] == DIR_SEPARATOR)
281 buffer = GNUNET_malloc (len);
282 if (getcwd (buffer, len) != NULL)
287 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
290 GNUNET_free (buffer);
293 GNUNET_free (buffer);
298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getcwd");
299 buffer = getenv ("PWD"); /* alternative */
301 fm = GNUNET_strdup (buffer);
304 fm = GNUNET_strdup ("./"); /* give up */
306 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
307 buffer = GNUNET_malloc (n);
308 GNUNET_snprintf (buffer, n, "%s%s%s",
310 (fm[strlen (fm) - 1] ==
311 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
315 fn = GNUNET_malloc (MAX_PATH + 1);
317 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
319 SetErrnoFromWinError (lRet);
320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
321 "plibc_conv_to_win_path");
324 /* is the path relative? */
325 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
327 char szCurDir[MAX_PATH + 1];
328 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
329 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
331 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
332 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
333 "GetCurrentDirectory");
336 buffer = GNUNET_malloc (MAX_PATH + 1);
337 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
348 * Give relative time in human-readable fancy format.
350 * @param del time in milli seconds
351 * @return time as human-readable string
354 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative del)
356 const char *unit = _( /* time unit */ "ms");
358 uint64_t delta = del.value;
360 if (delta > 5 * 1000)
362 delta = delta / 1000;
363 unit = _( /* time unit */ "s");
367 unit = _( /* time unit */ "m");
371 unit = _( /* time unit */ "h");
375 unit = _( /* time unit */ " days");
380 GNUNET_asprintf (&ret, "%llu%s", delta, unit);
386 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
387 * return value does not include the newline character.
389 * @param t time to convert
390 * @return absolute time in human-readable format
393 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
400 ret = ctime_r (&tt, GNUNET_malloc (32));
402 ret = GNUNET_strdup (ctime (&tt));
404 ret[strlen (ret) - 1] = '\0';
410 /* end of strings.c */