Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / strings.c
index 18f6582e8db649c3fa918a644667377275bd797c..fa445f69423c3b0bcfde1fe799e910433e05b82e 100644 (file)
@@ -26,7 +26,7 @@
  */
 
 #include "platform.h"
-#if HAVE_ICONV_H
+#if HAVE_ICONV
 #include <iconv.h>
 #endif
 #include "gnunet_common.h"
  * used to parse the buffer back into individual
  * strings.
  *
+ * @param buffer the buffer to fill with strings, can
+ *               be NULL in which case only the necessary
+ *               amount of space will be calculated
+ * @param size number of bytes available in buffer
+ * @param count number of strings that follow
+ * @param ... count 0-terminated strings to copy to buffer
  * @return number of bytes written to the buffer
  *         (or number of bytes that would have been written)
  */
-unsigned int
+size_t
 GNUNET_STRINGS_buffer_fill (char *buffer,
-                            unsigned int size, unsigned int count, ...)
+                            size_t size, unsigned int count, ...)
 {
-  unsigned int needed;
-  unsigned int slen;
+  size_t needed;
+  size_t slen;
   const char *s;
   va_list ap;
 
@@ -91,7 +97,7 @@ GNUNET_STRINGS_buffer_fill (char *buffer,
  */
 unsigned int
 GNUNET_STRINGS_buffer_tokenize (const char *buffer,
-                                unsigned int size, unsigned int count, ...)
+                                size_t size, unsigned int count, ...)
 {
   unsigned int start;
   unsigned int needed;
@@ -122,6 +128,9 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer,
 
 /**
  * Convert a given filesize into a fancy human-readable format.
+ *
+ * @param size number of bytes
+ * @return fancy representation of the size (possibly rounded) for humans
  */
 char *
 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
@@ -150,7 +159,7 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
         }
     }
   ret = GNUNET_malloc (32);
-  GNUNET_snprintf (ret, 32, "%llu%s", size, unit);
+  GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
   return ret;
 }
 
@@ -189,7 +198,13 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
   tmp = GNUNET_malloc (tmpSize);
   itmp = tmp;
   finSize = tmpSize;
-  if (iconv (cd, (char **) &input, &len, &itmp, &finSize) == (size_t) - 1)
+  if (iconv (cd,
+#if FREEBSD || DARWIN || WINDOWS
+             (const char **) &input,
+#else
+             (char **) &input,
+#endif
+             &len, &itmp, &finSize) == SIZE_MAX)
     {
       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "iconv");
       iconv_close (cd);
@@ -337,36 +352,40 @@ GNUNET_STRINGS_filename_expand (const char *fil)
 
 /**
  * Give relative time in human-readable fancy format.
+ *
  * @param delta time in milli seconds
+ * @return time as human-readable string
  */
 char *
-GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative del)
+GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
 {
   const char *unit = _( /* time unit */ "ms");
   char *ret;
-  uint64_t delta = del.value;
+  uint64_t dval = delta.rel_value;
 
-  if (delta > 5 * 1000)
+  if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
+    return GNUNET_strdup (_("eternity"));
+  if (dval > 5 * 1000)
     {
-      delta = delta / 1000;
+      dval = dval / 1000;
       unit = _( /* time unit */ "s");
-      if (delta > 5 * 60)
+      if (dval > 5 * 60)
         {
-          delta = delta / 60;
+          dval = dval / 60;
           unit = _( /* time unit */ "m");
-          if (delta > 5 * 60)
+          if (dval > 5 * 60)
             {
-              delta = delta / 60;
+              dval = dval / 60;
               unit = _( /* time unit */ "h");
-              if (delta > 5 * 24)
+              if (dval > 5 * 24)
                 {
-                  delta = delta / 24;
+                  dval = dval / 24;
                   unit = _( /* time unit */ " days");
                 }
             }
         }
     }
-  GNUNET_asprintf (&ret, "%llu%s", delta, unit);
+  GNUNET_asprintf (&ret, "%llu %s", dval, unit);
   return ret;
 }
 
@@ -374,6 +393,9 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative del)
 /**
  * "man ctime_r", except for GNUnet time; also, unlike ctime, the
  * return value does not include the newline character.
+ *
+ * @param t time to convert
+ * @return absolute time in human-readable format
  */
 char *
 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
@@ -381,7 +403,9 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
   time_t tt;
   char *ret;
 
-  tt = t.value / 1000;
+  if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
+    return GNUNET_strdup (_("end of time"));
+  tt = t.abs_value / 1000;
 #ifdef ctime_r
   ret = ctime_r (&tt, GNUNET_malloc (32));
 #else