Example #1
0
File: list.c Project: abw/hemp
void
hemp_list_dump_list(
    HempList list
) {
//  hemp_debug("dump list: %p (%d items)\n", list, list->length);
    hemp_text_append_string(hemp_list_dump_buffer, "[\n");
    hemp_list_each(list, &hemp_list_dump_item);
    hemp_text_append_string(hemp_list_dump_buffer, "]\n");
}
Example #2
0
File: text.c Project: abw/hemp
HempText
hemp_text_insert_string(
    HempText     text,
    HempOffset   offset,
    HempString      insert
) {
    HempSize length = text->length;
    HempSize extra;

    /* negative offset counts back from the end of the text */
    if (offset < 0) {
        offset += length;
        if (offset < 0)
            offset = 0;
    }
    else if (offset > length || length == 0)
        return hemp_text_append_string(text, insert);

    extra = strlen(insert);
    length += extra;

    // hemp_debug("offset %d  length %d  extra %d  length %d\n", offset, text->length, extra, length);

    text = hemp_text_capacity(text, length);
    /* shift down contents of existing string from offset onwards */
    hemp_mem_copy(text->string + offset, text->string + offset + extra, text->length - offset);
    /* insert new string at offset */
    strncpy(text->string + offset, insert, extra);
    text->string[length] = '\0';
    text->length = length;

    return text;
}
Example #3
0
File: text.c Project: abw/hemp
HempText
hemp_text_from_string(
    HempString source
) {
    HempText text = hemp_text_new_size(strlen(source));
    hemp_text_append_string(text, source);
    return text;
}
Example #4
0
File: list.c Project: abw/hemp
HempBool
hemp_list_dump_item(
    HempList     list,
    HempPos      pos,
    HempValue    item
) {
    hemp_text_append_string(hemp_list_dump_buffer, "    ");
    if (hemp_is_text(item)) {
        hemp_text_append_text(hemp_list_dump_buffer, hemp_val_text(item));
    }
    else if (hemp_is_string(item)) {
        hemp_text_append_string(hemp_list_dump_buffer, hemp_val_str(item));
    }
    else {
        hemp_text_append_string(hemp_list_dump_buffer, hemp_type_name(item));
    }
    hemp_text_append_string(hemp_list_dump_buffer, ",\n");

    return HEMP_TRUE;
}
Example #5
0
HempModule
hemp_use_module(
    Hemp       hemp,
    HempString     type,
    HempString     name
) {
    hemp_debug_call("hemp_use_module(%s => %s)\n", type, name);

    HempValue  path = hemp_config_get(hemp, HEMP_CONFIG_MODPATH);
    HempString string;

    if (hemp_is_defined(path)) {
        string = hemp_to_string(path, hemp->context);
//      hemp_debug_msg("already got %s: %s\n", HEMP_CONFIG_MODPATH, string);
    }
    else {
        HempValue  dir  = hemp_config_get(hemp, HEMP_CONFIG_DIR);
        HempValue  mod  = hemp_config_get(hemp, HEMP_CONFIG_MODDIR);
        HempString dstr = hemp_to_string(dir, hemp->context);
        HempString mstr = hemp_to_string(mod, hemp->context);

        string = hemp_uri_path_join(dstr, mstr, 1);
//      hemp_debug_msg("constructed %s: %s\n", HEMP_CONFIG_MODPATH, string);

        /* ugly work-around so we can get the context to manage memory */
        HempText text = hemp_context_tmp_text_size(hemp->context, strlen(string) + 1);
        hemp_text_append_string(text, string);
        hemp_mem_free(string);
        string = text->string;
//        hemp_config_set(hemp, HEMP_CONFIG_MODPATH, hemp_text_val(text));
    }

    // TODO: need a way to save dotted path (hemp.module_path) back into config

    /* quick hack to get something working */
    HempString modpath = getenv("HEMP_MODULE_PATH");

    if (! modpath || ! *modpath) {
        modpath = string;
//        hemp_debug_msg("No HEMP_MODULE_PATH environment variable set\n");
//        return HEMP_FALSE;
    }

    /* TODO: sort this mess out */
    HempString tpath = hemp_uri_path_join(modpath, type, 1);
    HempString mpath = hemp_uri_path_join(tpath, name, 1);
    HempText   mtext = hemp_text_from_string(mpath);
    hemp_text_append_string(mtext, HEMP_MODULE_EXT);
    hemp_mem_free(mpath);
    hemp_mem_free(tpath);
//  hemp_debug_msg("constructed module path: %s\n", mtext->string);

    HempModule module = hemp_global_module(hemp->global, mtext->string);

    if (module->binder) {
        module->binder(module, hemp);
    }
    else if (module->error) {
        hemp_fatal(module->error);
    }
    else {
        /* should never happen - famous last word */
        hemp_fatal("No binder function for %s module", name);
    }

    hemp_text_free(mtext);
    return module;
}