1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include "utils.h"
30 #include "properties.h"
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <cx/string.h>
36 #include <cx/buffer.h>
37
38 UiPathElm* ui_default_pathelm_func(
const char* full_path,
size_t len,
size_t* ret_nelm,
void* data) {
39 cxstring *pathelms;
40 size_t nelm = cx_strsplit_a(cxDefaultAllocator, cx_strn(full_path, len),
CX_STR(
"/"),
4096, &pathelms);
41
42 if (nelm ==
0) {
43 *ret_nelm =
0;
44 return NULL;
45 }
46
47 UiPathElm* elms = (UiPathElm*)calloc(nelm,
sizeof(UiPathElm));
48 size_t n = nelm;
49 int j =
0;
50 for (
int i =
0; i < nelm; i++) {
51 cxstring c = pathelms[i];
52 if (c.length ==
0) {
53 if (i ==
0) {
54 c.length =
1;
55 }
56 else {
57 n--;
58 continue;
59 }
60 }
61
62 cxmutstr m = cx_strdup(c);
63 elms[j].name = m.ptr;
64 elms[j].name_len = m.length;
65
66 size_t elm_path_len = c.ptr + c.length - full_path;
67 cxmutstr elm_path = cx_strdup(cx_strn(full_path, elm_path_len));
68 elms[j].path = elm_path.ptr;
69 elms[j].path_len = elm_path.length;
70
71 j++;
72 }
73 *ret_nelm = n;
74
75 return elms;
76 }
77
78 void ui_get_window_default_width(
int *width,
int *height) {
79 const char *width_str = ui_get_property(
"ui.window.width");
80 const char *height_str = ui_get_property(
"ui.window.height");
81 if(width_str && height_str) {
82 int w = atoi(width_str);
83 int h = atoi(height_str);
84 if(w >
0 && h >
0) {
85 *width = w;
86 *height = h;
87 }
88 }
89 }
90
91
92 static cxmutstr escape_string(cxstring str, bool escape_slash) {
93
94
95 CxBuffer buf = {
0};
96
97 bool all_printable = true;
98 for (
size_t i =
0; i < str.length; i++) {
99 unsigned char c = str.ptr[i];
100 bool escape = c <
0x20 || c ==
'\\' || c ==
'""'
101 || (escape_slash && c ==
'/');
102
103 if (all_printable && escape) {
104 size_t capa = str.length +
32;
105 char *space = cxMallocDefault(capa);
106 if (space ==
NULL)
return cx_mutstrn(
NULL,
0);
107 cxBufferInit(&buf, space, capa,
NULL,
CX_BUFFER_AUTO_EXTEND);
108 cxBufferWrite(str.ptr,
1, i, &buf);
109 all_printable = false;
110 }
111 if (escape) {
112 cxBufferPut(&buf,
'\\');
113 if (c ==
'\"') {
114 cxBufferPut(&buf,
'\"');
115 }
else if (c ==
'\n') {
116 cxBufferPut(&buf,
'n');
117 }
else if (c ==
'\t') {
118 cxBufferPut(&buf,
't');
119 }
else if (c ==
'\r') {
120 cxBufferPut(&buf,
'r');
121 }
else if (c ==
'\\') {
122 cxBufferPut(&buf,
'\\');
123 }
else if (c ==
'/') {
124 cxBufferPut(&buf,
'/');
125 }
else if (c ==
'\f') {
126 cxBufferPut(&buf,
'f');
127 }
else if (c ==
'\b') {
128 cxBufferPut(&buf,
'b');
129 }
else {
130 char code[
6];
131 snprintf(code,
sizeof(code),
"u%04x", (
unsigned int) c);
132 cxBufferPutString(&buf, code);
133 }
134 }
else if (!all_printable) {
135 cxBufferPut(&buf, c);
136 }
137 }
138 cxmutstr ret;
139 if (all_printable) {
140
141 ret = cx_mutstrn((
char*)str.ptr, str.length);
142 }
else {
143 ret = cx_mutstrn(buf.space, buf.size);
144 }
145 cxBufferDestroy(&buf);
146 return ret;
147 }
148
149 cxmutstr ui_escape_string(cxstring str) {
150 return escape_string(str,
FALSE);
151 }
152