ui/common/utils.c

Fri, 12 Dec 2025 12:44:03 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Dec 2025 12:44:03 +0100
changeset 994
c9395b30e5c8
parent 985
93f07ccfd997
permissions
-rw-r--r--

fix uic_store_app_properties

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#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;
        }
    }
}

// from UCX json.c
static cxmutstr escape_string(cxstring str, bool escape_slash) {
    // note: this function produces the string without enclosing quotes
    // the reason is that we don't want to allocate memory just for that
    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) {
        // don't copy the string when we don't need to escape anything
        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);
}

mercurial