#include "utils.h"
#include "properties.h"
#include <stdio.h>
#include <string.h>
#include <cx/string.h>
#include <cx/buffer.h>
UiPathElm* ui_default_pathelm_func(
const char* full_path,
size_t len,
size_t* ret_nelm,
void* data) {
cxstring *pathelms;
size_t nelm = cx_strsplit_a(cxDefaultAllocator, cx_strn(full_path, len),
CX_STR(
"/"),
4096, &pathelms);
if (nelm ==
0) {
*ret_nelm =
0;
return NULL;
}
UiPathElm* elms = (UiPathElm*)calloc(nelm,
sizeof(UiPathElm));
size_t n = nelm;
int j =
0;
for (
int i =
0; i < nelm; i++) {
cxstring c = pathelms[i];
if (c.length ==
0) {
if (i ==
0) {
c.length =
1;
}
else {
n--;
continue;
}
}
cxmutstr m = cx_strdup(c);
elms[j].name = m.ptr;
elms[j].name_len = m.length;
size_t elm_path_len = c.ptr + c.length - full_path;
cxmutstr elm_path = cx_strdup(cx_strn(full_path, elm_path_len));
elms[j].path = elm_path.ptr;
elms[j].path_len = elm_path.length;
j++;
}
*ret_nelm = n;
return elms;
}
void ui_get_window_default_width(
int *width,
int *height) {
const char *width_str = ui_get_property(
"ui.window.width");
const char *height_str = ui_get_property(
"ui.window.height");
if(width_str && height_str) {
int w = atoi(width_str);
int h = atoi(height_str);
if(w >
0 && h >
0) {
*width = w;
*height = h;
}
}
}
static cxmutstr escape_string(cxstring str, bool escape_slash) {
CxBuffer buf = {
0};
bool all_printable = true;
for (
size_t i =
0; i < str.length; i++) {
unsigned char c = str.ptr[i];
bool escape = c <
0x20 || c ==
'\\' || c ==
'""'
|| (escape_slash && c ==
'/');
if (all_printable && escape) {
size_t capa = str.length +
32;
char *space = cxMallocDefault(capa);
if (space ==
NULL)
return cx_mutstrn(
NULL,
0);
cxBufferInit(&buf, space, capa,
NULL,
CX_BUFFER_AUTO_EXTEND);
cxBufferWrite(str.ptr,
1, i, &buf);
all_printable = false;
}
if (escape) {
cxBufferPut(&buf,
'\\');
if (c ==
'\"') {
cxBufferPut(&buf,
'\"');
}
else if (c ==
'\n') {
cxBufferPut(&buf,
'n');
}
else if (c ==
'\t') {
cxBufferPut(&buf,
't');
}
else if (c ==
'\r') {
cxBufferPut(&buf,
'r');
}
else if (c ==
'\\') {
cxBufferPut(&buf,
'\\');
}
else if (c ==
'/') {
cxBufferPut(&buf,
'/');
}
else if (c ==
'\f') {
cxBufferPut(&buf,
'f');
}
else if (c ==
'\b') {
cxBufferPut(&buf,
'b');
}
else {
char code[
6];
snprintf(code,
sizeof(code),
"u%04x", (
unsigned int) c);
cxBufferPutString(&buf, code);
}
}
else if (!all_printable) {
cxBufferPut(&buf, c);
}
}
cxmutstr ret;
if (all_printable) {
ret = cx_mutstrn((
char*)str.ptr, str.length);
}
else {
ret = cx_mutstrn(buf.space, buf.size);
}
cxBufferDestroy(&buf);
return ret;
}
cxmutstr ui_escape_string(cxstring str) {
return escape_string(str,
FALSE);
}