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 * @return number of bytes written to the buffer
49 * (or number of bytes that would have been written)
52 GNUNET_STRINGS_buffer_fill (char *buffer,
53 unsigned int size, unsigned int count, ...)
64 s = va_arg (ap, const char *);
65 slen = strlen (s) + 1;
68 GNUNET_assert (needed + slen <= size);
69 memcpy (&buffer[needed], s, slen);
80 * Given a buffer of a given size, find "count"
81 * 0-terminated strings in the buffer and assign
82 * the count (varargs) of type "const char**" to the
83 * locations of the respective strings in the
86 * @param buffer the buffer to parse
87 * @param size size of the buffer
88 * @param count number of strings to locate
89 * @return offset of the character after the last 0-termination
90 * in the buffer, or 0 on error.
93 GNUNET_STRINGS_buffer_tokenize (const char *buffer,
94 unsigned int size, unsigned int count, ...)
102 va_start (ap, count);
105 r = va_arg (ap, const char **);
107 while ((needed < size) && (buffer[needed] != '\0'))
112 return 0; /* error */
115 needed++; /* skip 0-termination */
124 * Convert a given filesize into a fancy human-readable format.
127 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
129 const char *unit = _( /* size unit */ "b");
135 unit = _( /* size unit */ "KiB");
139 unit = _( /* size unit */ "MiB");
143 unit = _( /* size unit */ "GiB");
147 unit = _( /* size unit */ "TiB");
152 ret = GNUNET_malloc (32);
153 GNUNET_snprintf (ret, 32, "%llu%s", size, unit);
159 * Convert the len characters long character sequence
160 * given in input that is in the given charset
162 * @return the converted string (0-terminated),
163 * if conversion fails, a copy of the orignal
164 * string is returned.
167 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
170 #if ENABLE_NLS && HAVE_ICONV
177 cd = iconv_open ("UTF-8", charset);
178 if (cd == (iconv_t) - 1)
180 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
182 _("Character set requested was `%s'\n"), charset);
183 ret = GNUNET_malloc (len + 1);
184 memcpy (ret, input, len);
188 tmpSize = 3 * len + 4;
189 tmp = GNUNET_malloc (tmpSize);
192 if (iconv (cd, (char **) &input, &len, &itmp, &finSize) == (size_t) - 1)
194 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv");
197 ret = GNUNET_malloc (len + 1);
198 memcpy (ret, input, len);
202 ret = GNUNET_malloc (tmpSize - finSize + 1);
203 memcpy (ret, tmp, tmpSize - finSize);
204 ret[tmpSize - finSize] = '\0';
206 if (0 != iconv_close (cd))
207 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
210 ret = GNUNET_malloc (len + 1);
211 memcpy (ret, input, len);
219 * Complete filename (a la shell) from abbrevition.
220 * @param fil the name of the file, may contain ~/ or
221 * be relative to the current directory
222 * @returns the full file name,
223 * NULL is returned on error
226 GNUNET_STRINGS_filename_expand (const char *fil)
243 if (fil[0] == DIR_SEPARATOR)
244 /* absolute path, just copy */
245 return GNUNET_strdup (fil);
248 fm = getenv ("HOME");
251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
253 ("Failed to expand `$HOME': environment variable `HOME' not set"));
256 fm = GNUNET_strdup (fm);
257 /* do not copy '~' */
260 /* skip over dir seperator to be consistent */
261 if (fil_ptr[0] == DIR_SEPARATOR)
272 buffer = GNUNET_malloc (len);
273 if (getcwd (buffer, len) != NULL)
278 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
281 GNUNET_free (buffer);
284 GNUNET_free (buffer);
289 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getcwd");
290 buffer = getenv ("PWD"); /* alternative */
292 fm = GNUNET_strdup (buffer);
295 fm = GNUNET_strdup ("./"); /* give up */
297 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
298 buffer = GNUNET_malloc (n);
299 GNUNET_snprintf (buffer, n, "%s%s%s",
301 (fm[strlen (fm) - 1] ==
302 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
306 fn = GNUNET_malloc (MAX_PATH + 1);
308 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
310 SetErrnoFromWinError (lRet);
311 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
312 "plibc_conv_to_win_path");
315 /* is the path relative? */
316 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
318 char szCurDir[MAX_PATH + 1];
319 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
320 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
322 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
323 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
324 "GetCurrentDirectory");
327 buffer = GNUNET_malloc (MAX_PATH + 1);
328 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
339 * Give relative time in human-readable fancy format.
340 * @param delta time in milli seconds
343 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative del)
345 const char *unit = _( /* time unit */ "ms");
347 uint64_t delta = del.value;
349 if (delta > 5 * 1000)
351 delta = delta / 1000;
352 unit = _( /* time unit */ "s");
356 unit = _( /* time unit */ "m");
360 unit = _( /* time unit */ "h");
364 unit = _( /* time unit */ " days");
369 GNUNET_asprintf (&ret, "%llu%s", delta, unit);
375 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
376 * return value does not include the newline character.
379 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
386 ret = ctime_r (&tt, GNUNET_malloc (32));
388 ret = GNUNET_strdup (ctime (&tt));
390 ret[strlen (ret) - 1] = '\0';
396 /* end of strings.c */