aboutsummaryrefslogtreecommitdiffstats
path: root/sway/stringop.c
diff options
context:
space:
mode:
authorLibravatar taiyu <taiyu.len@gmail.com>2015-09-07 14:29:40 -0700
committerLibravatar taiyu <taiyu.len@gmail.com>2015-09-07 14:29:40 -0700
commit71af5b7ddebe6b714f3be65bf8c810c952a7e0af (patch)
tree20eac4917bad5c08c89bffa1c33063ebda03a2f5 /sway/stringop.c
parentMerge pull request #172 from taiyu-len/master (diff)
downloadsway-71af5b7ddebe6b714f3be65bf8c810c952a7e0af.tar.gz
sway-71af5b7ddebe6b714f3be65bf8c810c952a7e0af.tar.zst
sway-71af5b7ddebe6b714f3be65bf8c810c952a7e0af.zip
config modes
Diffstat (limited to 'sway/stringop.c')
-rw-r--r--sway/stringop.c151
1 files changed, 108 insertions, 43 deletions
diff --git a/sway/stringop.c b/sway/stringop.c
index c39e2c34..d2b74655 100644
--- a/sway/stringop.c
+++ b/sway/stringop.c
@@ -1,34 +1,26 @@
1#include "stringop.h"
2#include <stdlib.h> 1#include <stdlib.h>
3#include <stdio.h> 2#include <stdio.h>
3#include <strings.h>
4#include <ctype.h>
5#include "stringop.h"
6#include "log.h"
4#include "string.h" 7#include "string.h"
5#include "list.h" 8#include "list.h"
6#include <strings.h> 9
7#include <log.h> 10const char *whitespace = " \f\n\r\t\v";
8 11
9/* Note: This returns 8 characters for trimmed_start per tab character. */ 12/* Note: This returns 8 characters for trimmed_start per tab character. */
10char *strip_whitespace(char *_str, int *trimmed_start) { 13char *strip_whitespace(char *_str) {
11 *trimmed_start = 0; 14 int ws = strspn(_str, whitespace);
12 if (*_str == '\0') 15 int len = strlen(_str) - ws + 1;
13 return _str; 16 sway_log(L_DEBUG,"%d, %d..",ws,len);
14 char *strold = _str; 17 char *str = malloc(len);
15 while (*_str == ' ' || *_str == '\t') { 18 strcpy(str, _str+ws);
16 if (*_str == '\t') { 19 free(_str);
17 *trimmed_start += 8;
18 } else {
19 *trimmed_start += 1;
20 }
21 _str++;
22 }
23 char *str = malloc(strlen(_str) + 1);
24 strcpy(str, _str);
25 free(strold);
26 int i;
27 for (i = 0; str[i] != '\0'; ++i);
28 do { 20 do {
29 i--; 21 len--;
30 } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); 22 } while (len >= 0 && (str[len] == ' ' || str[len] == '\t'));
31 str[i + 1] = '\0'; 23 str[len + 1] = '\0';
32 return str; 24 return str;
33} 25}
34 26
@@ -41,7 +33,7 @@ char *strip_comments(char *str) {
41 } else if (str[i] == '\'' && !in_string) { 33 } else if (str[i] == '\'' && !in_string) {
42 in_character = !in_character; 34 in_character = !in_character;
43 } else if (!in_character && !in_string) { 35 } else if (!in_character && !in_string) {
44 if (str[i] == '#' && i == 0) { 36 if (str[i] == '#') {
45 str[i] = '\0'; 37 str[i] = '\0';
46 break; 38 break;
47 } 39 }
@@ -51,26 +43,45 @@ char *strip_comments(char *str) {
51 return str; 43 return str;
52} 44}
53 45
46void strip_quotes(char *str) {
47 bool in_str = false;
48 bool in_chr = false;
49 bool escaped = false;
50 char *end = strchr(str,0);
51 while (*str) {
52 if (*str == '\'' && !in_str && !escaped) {
53 in_chr = !in_chr;
54 goto shift_over;
55 } else if (*str == '\"' && !in_chr && !escaped) {
56 in_str = !in_str;
57 goto shift_over;
58 } else if (*str == '\\') {
59 escaped = !escaped;
60 ++str;
61 continue;
62 }
63 escaped = false;
64 ++str;
65 continue;
66 shift_over:
67 memmove(str, str+1, end-- - str);
68 }
69 *end = '\0';
70}
71
54list_t *split_string(const char *str, const char *delims) { 72list_t *split_string(const char *str, const char *delims) {
55 list_t *res = create_list(); 73 list_t *res = create_list();
56 int i, j; 74 char *copy = malloc(strlen(str) + 1);
57 int len = strlen(str); 75 char *token;
58 for (i = 0, j = 0; i < len + 1; ++i) { 76 strcpy(copy, str);
59 if (strchr(delims, str[i]) || i == len) { 77
60 if (i - j == 0) { 78 token = strtok(copy, delims);
61 continue; 79 while(token) {
62 } 80 token = strdup(token);
63 char *left = malloc(i - j + 1); 81 list_add(res, token);
64 memcpy(left, str + j, i - j); 82 token = strtok(NULL, delims);
65 left[i - j] = 0;
66 list_add(res, left);
67 j = i + 1;
68 while (j <= len && str[j] && strchr(delims, str[j])) {
69 j++;
70 i++;
71 }
72 }
73 } 83 }
84 free(copy);
74 return res; 85 return res;
75} 86}
76 87
@@ -82,6 +93,60 @@ void free_flat_list(list_t *list) {
82 list_free(list); 93 list_free(list);
83} 94}
84 95
96char **split_args(const char *start, int *argc) {
97 *argc = 0;
98 int alloc = 2;
99 char **parts = malloc(sizeof(char *) * alloc);
100 bool in_token = false;
101 bool in_string = false;
102 bool in_char = false;
103 bool escaped = false;
104 const char *end = start;
105 while (*start) {
106 if (!in_token) {
107 start = (end += strspn(end, whitespace));
108 in_token = true;
109 }
110 if (*end == '"' && !in_char && !escaped) {
111 in_string = !in_string;
112 } else if (*end == '\'' && !in_string && !escaped) {
113 in_char = !in_char;
114 } else if (*end == '\\') {
115 escaped = !escaped;
116 } else if (*end == '\0' || (!in_string && !in_char && !escaped
117 && strchr(whitespace, *end))) {
118 goto add_part;
119 }
120 if (*end != '\\') {
121 escaped = false;
122 }
123 ++end;
124 continue;
125 add_part:
126 if (end - start > 0) {
127 char *token = malloc(end - start + 1);
128 strncpy(token, start, end - start + 1);
129 token[end - start] = '\0';
130 strip_quotes(token);
131 unescape_string(token);
132 parts[*argc] = token;
133 if (++*argc == alloc) {
134 parts = realloc(parts, (alloc *= 2) * sizeof(char *));
135 }
136 }
137 in_token = false;
138 escaped = false;
139 }
140 return parts;
141}
142
143void free_argv(int argc, char **argv) {
144 while (--argc) {
145 free(argv[argc]);
146 }
147 free(argv);
148}
149
85char *code_strstr(const char *haystack, const char *needle) { 150char *code_strstr(const char *haystack, const char *needle) {
86 /* TODO */ 151 /* TODO */
87 return strstr(haystack, needle); 152 return strstr(haystack, needle);
@@ -177,7 +242,7 @@ int unescape_string(char *string) {
177 string[i - 1] = c; 242 string[i - 1] = c;
178 } 243 }
179 } 244 }
180 memmove(string + i, string + i + shift, len - i); 245 memmove(string + i, string + i + shift, len - i + 1);
181 } 246 }
182 } 247 }
183 return len; 248 return len;