return (double)time(NULL);
#endif
}
+
+std::string
+from_owned_cstr(char* str)
+{
+ std::string result;
+ if (str) {
+ result = str;
+ free(str);
+ }
+ return result;
+}
+
+std::string
+from_cstr(const char* str)
+{
+ return str ? str : std::string{};
+}
#include "system.hpp"
+#include <string>
+
void cc_log(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
void cc_bulklog(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
void cc_log_argv(const char* prefix, char** argv);
char* subst_env_in_string(const char* str, char** errmsg);
void set_cloexec_flag(int fd);
double time_seconds();
+
+// Convert an owned char* string `str` to an std::string and free `str`.
+std::string from_owned_cstr(char* str);
+
+// Convert a char* string `str` to an std::string, if `str` is NULL return "".
+std::string from_cstr(const char* str);
CHECK_STR_EQ("00010203", result);
}
+TEST(from_cstr)
+{
+ char* cstr1 = x_strdup("foo");
+ const char* cstr2 = "bar";
+ const char* cstr3 = nullptr;
+ const char* cstr4 = "";
+
+ std::string str1 = from_cstr(cstr1);
+ std::string str2 = from_cstr(cstr2);
+ std::string str3 = from_cstr(cstr3);
+ std::string str4 = from_cstr(cstr4);
+
+ CHECK(str1 == "foo");
+ CHECK(str2 == "bar");
+ CHECK(str3 == "");
+ CHECK(str4 == "");
+
+ free(cstr1);
+}
+
+TEST(from_owned_cstr)
+{
+ char* cstr1 = x_strdup("foo");
+ char* cstr2 = x_strdup("bar");
+ char* cstr3 = nullptr;
+ char* cstr4 = x_strdup("");
+
+ std::string str1 = from_owned_cstr(cstr1);
+ std::string str2 = from_owned_cstr(cstr2);
+ std::string str3 = from_owned_cstr(cstr3);
+ std::string str4 = from_owned_cstr(cstr4);
+
+ CHECK(str1 == "foo");
+ CHECK(str2 == "bar");
+ CHECK(str3 == "");
+ CHECK(str4 == "");
+}
+
TEST_SUITE_END