chiark / gitweb /
bus: internalize a lot of protocol definitions
[elogind.git] / src / shared / strv.c
index ec25755289a2c5a8e9aecc689693e5e0269ec0a4..607c221ae66790b86311bdc36625039e2f401c20 100644 (file)
@@ -64,15 +64,7 @@ void strv_free(char **l) {
         free(l);
 }
 
-void strv_freep(char ***l) {
-        if (!l)
-                return;
-
-        strv_free(*l);
-        *l = NULL;
-}
-
-char **strv_copy(char **l) {
+char **strv_copy(char * const *l) {
         char **r, **k;
 
         k = r = new(char*, strv_length(l) + 1);
@@ -92,7 +84,7 @@ char **strv_copy(char **l) {
         return r;
 }
 
-unsigned strv_length(char **l) {
+unsigned strv_length(char * const *l) {
         unsigned n = 0;
 
         if (!l)
@@ -305,6 +297,31 @@ char **strv_split_quoted(const char *s) {
         return r;
 }
 
+char **strv_split_newlines(const char *s) {
+        char **l;
+        unsigned n;
+
+        assert(s);
+
+        /* Special version of strv_split() that splits on newlines and
+         * suppresses an empty string at the end */
+
+        l = strv_split(s, NEWLINE);
+        if (!l)
+                return NULL;
+
+        n = strv_length(l);
+        if (n <= 0)
+                return l;
+
+        if (isempty(l[n-1])) {
+                free(l[n-1]);
+                l[n-1] = NULL;
+        }
+
+        return l;
+}
+
 char *strv_join(char **l, const char *separator) {
         char *r, *e;
         char **s;
@@ -339,6 +356,43 @@ char *strv_join(char **l, const char *separator) {
         return r;
 }
 
+char *strv_join_quoted(char **l) {
+        char *buf = NULL;
+        char **s;
+        size_t allocated = 0, len = 0;
+
+        STRV_FOREACH(s, l) {
+                /* assuming here that escaped string cannot be more
+                 * than twice as long, and reserving space for the
+                 * separator and quotes.
+                 */
+                _cleanup_free_ char *esc = NULL;
+                size_t needed;
+
+                if (!GREEDY_REALLOC(buf, allocated,
+                                    len + strlen(*s) * 2 + 3))
+                        goto oom;
+
+                esc = cescape(*s);
+                if (!esc)
+                        goto oom;
+
+                needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
+                                  len > 0 ? " " : "", esc);
+                assert(needed < allocated - len);
+                len += needed;
+        }
+
+        if (!buf)
+                buf = malloc0(1);
+
+        return buf;
+
+ oom:
+        free(buf);
+        return NULL;
+}
+
 char **strv_append(char **l, const char *s) {
         char **r, **k;
 
@@ -370,32 +424,43 @@ fail:
         return NULL;
 }
 
-int strv_extend(char ***l, const char *value) {
+int strv_push(char ***l, char *value) {
         char **c;
-        char *v;
         unsigned n;
 
         if (!value)
                 return 0;
 
-        v = strdup(value);
-        if (!v)
-                return -ENOMEM;
-
         n = strv_length(*l);
         c = realloc(*l, sizeof(char*) * (n + 2));
-        if (!c) {
-                free(v);
+        if (!c)
                 return -ENOMEM;
-        }
 
-        c[n] = v;
+        c[n] = value;
         c[n+1] = NULL;
 
         *l = c;
         return 0;
 }
 
+int strv_extend(char ***l, const char *value) {
+        char *v;
+        int r;
+
+        if (!value)
+                return 0;
+
+        v = strdup(value);
+        if (!v)
+                return -ENOMEM;
+
+        r = strv_push(l, v);
+        if (r < 0)
+                free(v);
+
+        return r;
+}
+
 char **strv_uniq(char **l) {
         char **i;
 
@@ -466,7 +531,7 @@ char **strv_parse_nulstr(const char *s, size_t l) {
         assert(s || l <= 0);
 
         if (l <= 0)
-                return strv_new(NULL, NULL);
+                return new0(char*, 1);
 
         for (p = s; p < s + l; p++)
                 if (*p == 0)