ui/common/utils.c

changeset 115
e57ca2747782
equal deleted inserted replaced
114:3da24640513a 115:e57ca2747782
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
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 // from UCX json.c
92 static cxmutstr escape_string(cxstring str, bool escape_slash) {
93 // note: this function produces the string without enclosing quotes
94 // the reason is that we don't want to allocate memory just for that
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 // don't copy the string when we don't need to escape anything
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 }

mercurial