Example #1
0
File: util.c Project: hirkmt/vimb
/**
 * Prepend new data to file.
 *
 * @file:   File to prepend the data
 * @format: Format string used to process va_list
 */
gboolean util_file_prepend(const char *file, const char *format, ...)
{
    gboolean res = false;
    va_list args;
    char *content;
    FILE *f;

    content = util_get_file_contents(file, NULL);
    if ((f = fopen(file, "w"))) {
        FLOCK(fileno(f), F_WRLCK);

        va_start(args, format);
        /* write new content to the file */
        vfprintf(f, format, args);
        va_end(args);

        /* append previous file content */
        fputs(content, f);

        FLOCK(fileno(f), F_UNLCK);
        fclose(f);

        res = true;
    }
    g_free(content);

    return res;
}
Example #2
0
File: input.c Project: semarie/vimb
static void resume_editor(GPid pid, int status, EditorData *data)
{
    char *text;
    if (status == 0) {
        text = util_get_file_contents(data->file, NULL);
        if (text) {
            dom_editable_element_set_value(data->element, text);
        }
        g_free(text);
    }
    dom_editable_element_set_disable(data->element, false);

    g_unlink(data->file);
    g_free(data->file);
    g_slice_free(EditorData, data);
    g_spawn_close_pid(pid);
}
Example #3
0
File: input.c Project: ckolos/vimb
static void resume_editor(GPid pid, int status, EditorData *data)
{
    char *text, *tmp;
    char *jscode;

    g_assert(pid);
    g_assert(data);
    g_assert(data->c);
    g_assert(data->file);

    if (status == 0) {
        /* get the text the editor stored */
        text = util_get_file_contents(data->file, NULL);

        if (text) {
            /* escape the text to form a valid JS string */
            /* TODO: could go into util.c maybe */
            struct search_replace {
                const char* search;
                const char* replace;
            } escapes[] = {
                {"\x01", ""},
                {"\x02", ""},
                {"\x03", ""},
                {"\x04", ""},
                {"\x05", ""},
                {"\x06", ""},
                {"\a", ""},
                {"\b", ""},
                {"\t", "\\t"},
                {"\n", "\\n"},
                {"\v", ""},
                {"\f", ""},
                {"\r", ""},
                {"\x0E", ""},
                {"\x0F", ""},
                {"\x10", ""},
                {"\x11", ""},
                {"\x12", ""},
                {"\x13", ""},
                {"\x14", ""},
                {"\x15", ""},
                {"\x16", ""},
                {"\x17", ""},
                {"\x18", ""},
                {"\x19", ""},
                {"\x1A", ""},
                {"\x1B", ""},
                {"\x1C", ""},
                {"\x1D", ""},
                {"\x1E", ""},
                {"\x1F", ""},
                {"\"", "\\\""},
                {"\x7F", ""},
                {NULL, NULL},
            };

            for(struct search_replace *i = escapes; i->search; i++) {
                tmp = util_str_replace(i->search, i->replace, text);
                g_free(text);
                text = tmp;
            }

            /* put the text back into the element */
            jscode = g_strdup_printf("vimb_input_mode_element.value=\"%s\"", text);
            ext_proxy_eval_script(data->c, jscode, NULL);

            g_free(jscode);
            g_free(text);
        }
    }

    ext_proxy_eval_script(data->c,
            "vimb_input_mode_element.disabled=false;"
            "vimb_input_mode_element.focus()", NULL);

    g_unlink(data->file);
    g_free(data->file);
    g_slice_free(EditorData, data);
    g_spawn_close_pid(pid);
}