diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-08-04 21:30:40 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-08-04 21:30:40 -0400 |
commit | 542ef0c77700e67a95fcd08b5f305d1ab42046e1 (patch) | |
tree | 17333ebc1a65a28b9538de2da9264a45fc0cf9a9 /sway/stringop.c | |
parent | Initial commit (diff) | |
download | sway-542ef0c77700e67a95fcd08b5f305d1ab42046e1.tar.gz sway-542ef0c77700e67a95fcd08b5f305d1ab42046e1.tar.zst sway-542ef0c77700e67a95fcd08b5f305d1ab42046e1.zip |
Pull in some scas code and read i3 config file
Diffstat (limited to 'sway/stringop.c')
-rw-r--r-- | sway/stringop.c | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/sway/stringop.c b/sway/stringop.c new file mode 100644 index 00000000..0e7ad6a9 --- /dev/null +++ b/sway/stringop.c | |||
@@ -0,0 +1,150 @@ | |||
1 | #include "stringop.h" | ||
2 | #include <stdlib.h> | ||
3 | #include <stdio.h> | ||
4 | #include "string.h" | ||
5 | #include "list.h" | ||
6 | #include <strings.h> | ||
7 | |||
8 | /* Note: This returns 8 characters for trimmed_start per tab character. */ | ||
9 | char *strip_whitespace(char *_str, int *trimmed_start) { | ||
10 | *trimmed_start = 0; | ||
11 | if (*_str == '\0') | ||
12 | return _str; | ||
13 | char *strold = _str; | ||
14 | while (*_str == ' ' || *_str == '\t') { | ||
15 | if (*_str == '\t') { | ||
16 | *trimmed_start += 8; | ||
17 | } else { | ||
18 | *trimmed_start += 1; | ||
19 | } | ||
20 | _str++; | ||
21 | } | ||
22 | char *str = malloc(strlen(_str) + 1); | ||
23 | strcpy(str, _str); | ||
24 | free(strold); | ||
25 | int i; | ||
26 | for (i = 0; str[i] != '\0'; ++i); | ||
27 | do { | ||
28 | i--; | ||
29 | } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); | ||
30 | str[i + 1] = '\0'; | ||
31 | return str; | ||
32 | } | ||
33 | |||
34 | char *strip_comments(char *str) { | ||
35 | int in_string = 0, in_character = 0; | ||
36 | int i = 0; | ||
37 | while (str[i] != '\0') { | ||
38 | if (str[i] == '"' && !in_character) { | ||
39 | in_string = !in_string; | ||
40 | } else if (str[i] == '\'' && !in_string) { | ||
41 | in_character = !in_character; | ||
42 | } else if (!in_character && !in_string) { | ||
43 | if (str[i] == '#') { | ||
44 | str[i] = '\0'; | ||
45 | break; | ||
46 | } | ||
47 | } | ||
48 | ++i; | ||
49 | } | ||
50 | return str; | ||
51 | } | ||
52 | |||
53 | list_t *split_string(const char *str, const char *delims) { | ||
54 | list_t *res = create_list(); | ||
55 | int i, j; | ||
56 | for (i = 0, j = 0; i < strlen(str) + 1; ++i) { | ||
57 | if (strchr(delims, str[i]) || i == strlen(str)) { | ||
58 | if (i - j == 0) { | ||
59 | continue; | ||
60 | } | ||
61 | char *left = malloc(i - j + 1); | ||
62 | memcpy(left, str + j, i - j); | ||
63 | left[i - j] = 0; | ||
64 | list_add(res, left); | ||
65 | j = i + 1; | ||
66 | while (j <= strlen(str) && str[j] && strchr(delims, str[j])) { | ||
67 | j++; | ||
68 | i++; | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | return res; | ||
73 | } | ||
74 | |||
75 | void free_flat_list(list_t *list) { | ||
76 | int i; | ||
77 | for (i = 0; i < list->length; ++i) { | ||
78 | free(list->items[i]); | ||
79 | } | ||
80 | list_free(list); | ||
81 | } | ||
82 | |||
83 | char *code_strstr(const char *haystack, const char *needle) { | ||
84 | /* TODO */ | ||
85 | return strstr(haystack, needle); | ||
86 | } | ||
87 | |||
88 | char *code_strchr(const char *str, char delimiter) { | ||
89 | int in_string = 0, in_character = 0; | ||
90 | int i = 0; | ||
91 | while (str[i] != '\0') { | ||
92 | if (str[i] == '"' && !in_character) { | ||
93 | in_string = !in_string; | ||
94 | } else if (str[i] == '\'' && !in_string) { | ||
95 | in_character = !in_character; | ||
96 | } else if (!in_character && !in_string) { | ||
97 | if (str[i] == delimiter) { | ||
98 | return (char *)str + i; | ||
99 | } | ||
100 | } | ||
101 | ++i; | ||
102 | } | ||
103 | return NULL; | ||
104 | } | ||
105 | |||
106 | int unescape_string(char *string) { | ||
107 | /* TODO: More C string escapes */ | ||
108 | int len = strlen(string); | ||
109 | int i; | ||
110 | for (i = 0; string[i]; ++i) { | ||
111 | if (string[i] == '\\') { | ||
112 | --len; | ||
113 | switch (string[++i]) { | ||
114 | case '0': | ||
115 | string[i - 1] = '\0'; | ||
116 | memmove(string + i, string + i + 1, len - i); | ||
117 | break; | ||
118 | case 'a': | ||
119 | string[i - 1] = '\a'; | ||
120 | memmove(string + i, string + i + 1, len - i); | ||
121 | break; | ||
122 | case 'b': | ||
123 | string[i - 1] = '\b'; | ||
124 | memmove(string + i, string + i + 1, len - i); | ||
125 | break; | ||
126 | case 't': | ||
127 | string[i - 1] = '\t'; | ||
128 | memmove(string + i, string + i + 1, len - i); | ||
129 | break; | ||
130 | case 'n': | ||
131 | string[i - 1] = '\n'; | ||
132 | memmove(string + i, string + i + 1, len - i); | ||
133 | break; | ||
134 | case 'v': | ||
135 | string[i - 1] = '\v'; | ||
136 | memmove(string + i, string + i + 1, len - i); | ||
137 | break; | ||
138 | case 'f': | ||
139 | string[i - 1] = '\f'; | ||
140 | memmove(string + i, string + i + 1, len - i); | ||
141 | break; | ||
142 | case 'r': | ||
143 | string[i - 1] = '\r'; | ||
144 | memmove(string + i, string + i + 1, len - i); | ||
145 | break; | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | return len; | ||
150 | } | ||