aboutsummaryrefslogtreecommitdiff
path: root/sway/stringop.c
diff options
context:
space:
mode:
authorminus <minus@mnus.de>2015-08-20 21:08:13 +0200
committerminus <minus@mnus.de>2015-08-20 21:13:01 +0200
commit754793aad4c0f0b93e30804c70807e9984741ae0 (patch)
tree783cf48873e5a3944cf670461bed69b140e56fc9 /sway/stringop.c
parentd965ce4a1a100f09d25d54ea32de856e89fcf7bb (diff)
added IPC messages get_workspaces and get_outputs
No escaping on container names is done yet, as well as some values are hardcoded because they don't exist yet.
Diffstat (limited to 'sway/stringop.c')
-rw-r--r--sway/stringop.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/sway/stringop.c b/sway/stringop.c
index 1dff97bf..c39e2c34 100644
--- a/sway/stringop.c
+++ b/sway/stringop.c
@@ -4,6 +4,7 @@
#include "string.h"
#include "list.h"
#include <strings.h>
+#include <log.h>
/* Note: This returns 8 characters for trimmed_start per tab character. */
char *strip_whitespace(char *_str, int *trimmed_start) {
@@ -197,3 +198,41 @@ char *join_args(char **argv, int argc) {
res[len - 1] = '\0';
return res;
}
+
+/*
+ * Join a list of strings, adding separator in between. Separator can be NULL.
+ */
+char *join_list(list_t *list, char *separator) {
+ if (!sway_assert(list != NULL, "list != NULL") || list->length == 0) {
+ return NULL;
+ }
+
+ size_t len = 1; // NULL terminator
+ size_t sep_len = 0;
+ if (separator != NULL) {
+ sep_len = strlen(separator);
+ len += (list->length - 1) * sep_len;
+ }
+
+ for (int i = 0; i < list->length; i++) {
+ len += strlen(list->items[i]);
+ }
+
+ char *res = malloc(len);
+
+ char *p = res + strlen(list->items[0]);
+ strcpy(res, list->items[0]);
+
+ for (int i = 1; i < list->length; i++) {
+ if (sep_len) {
+ memcpy(p, separator, sep_len);
+ p += sep_len;
+ }
+ strcpy(p, list->items[i]);
+ p += strlen(list->items[i]);
+ }
+
+ *p = '\0';
+
+ return res;
+}