Implement a string_printf utility function.
authorMichael Vrable <vrable@cs.hmc.edu>
Wed, 12 Jun 2013 17:53:42 +0000 (10:53 -0700)
committerMichael Vrable <vrable@cs.hmc.edu>
Sun, 26 Jan 2014 20:43:42 +0000 (12:43 -0800)
main.cc
util.cc
util.h

diff --git a/main.cc b/main.cc
index 91d1cef..5ac0f8c 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -292,9 +292,8 @@ int64_t dumpfile(int fd, dictionary &file_info, const string &path,
                     if (object_group == 0) {
                         o->set_group("data");
                     } else {
-                        char group[32];
-                        sprintf(group, "compacted-%d", object_group);
-                        o->set_group(group);
+                        o->set_group(string_printf("compacted-%d",
+                                                   object_group));
                     }
                     if (status == NULL)
                         status = "partial";
diff --git a/util.cc b/util.cc
index 76efe0f..f89569b 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -20,6 +20,7 @@
 /* Utility functions for converting various datatypes to text format (and
  * later, for parsing them back, perhaps). */
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
@@ -37,6 +38,25 @@ using std::map;
 using std::ostream;
 using std::string;
 
+/* Like sprintf, but returns the output as an allocated string. */
+string string_printf(const char *fmt, ...)
+{
+    va_list args;
+    char *output = NULL;
+    string result;
+
+    va_start(args, fmt);
+    if (vasprintf(&output, fmt, args) < 0) {
+        /* TODO: Error signalling? */
+        return result;
+    }
+    result = output;
+    va_end(args);
+
+    free(output);
+    return result;
+}
+
 /* Perform URI-style escaping of a string.  Bytes which cannot be represented
  * directly are encoded in the form %xx (where "xx" is a string of two
  * hexadecimal digits). */
@@ -96,20 +116,12 @@ string uri_decode(const string &in)
  * advisory.  For negative numbers, will always use decimal. */
 string encode_int(long long n, int base)
 {
-    char buf[64];
-
-    if (n >= 0 && base == 16) {
-        sprintf(buf, "0x%llx", n);
-        return buf;
-    }
-
-    if (n > 0 && base == 8) {
-        sprintf(buf, "0%llo", n);
-        return buf;
-    }
-
-    sprintf(buf, "%lld", n);
-    return buf;
+    if (n >= 0 && base == 16)
+        return string_printf("0x%llx", n);
+    else if (n > 0 && base == 8)
+        return string_printf("0%llo", n);
+    else
+        return string_printf("%lld", n);
 }
 
 /* Parse the string representation of an integer.  Accepts decimal, octal, and
diff --git a/util.h b/util.h
index 0d1b45d..b55320e 100644 (file)
--- a/util.h
+++ b/util.h
@@ -27,6 +27,9 @@
 #include <map>
 #include <string>
 
+std::string string_printf(const char *fmt, ...)
+    __attribute__((format(printf, 1, 2)));
+
 std::string uri_encode(const std::string &in);
 std::string uri_decode(const std::string &in);
 std::string encode_int(long long n, int base=10);