Beispiel #1
0
static char *default_import_callback(void *ctx, const char *dir, const char *file,
                                     char **found_here_cptr, int *success)
{
    auto *vm = static_cast<JsonnetVm *>(ctx);

    std::string input, found_here, err_msg;

    ImportStatus status = try_path(dir, file, input, found_here, err_msg);

    std::vector<std::string> jpaths(vm->jpaths);

    // If not found, try library search path.
    while (status == IMPORT_STATUS_FILE_NOT_FOUND) {
        if (jpaths.size() == 0) {
            *success = 0;
            const char *err = "no match locally or in the Jsonnet library paths.";
            char *r = jsonnet_realloc(vm, nullptr, std::strlen(err) + 1);
            std::strcpy(r, err);
            return r;
        }
        status = try_path(jpaths.back(), file, input, found_here, err_msg);
        jpaths.pop_back();
    }

    if (status == IMPORT_STATUS_IO_ERROR) {
        *success = 0;
        return from_string(vm, err_msg);
    } else {
        assert(status == IMPORT_STATUS_OK);
        *success = 1;
        *found_here_cptr = from_string(vm, found_here);
        return from_string(vm, input);
    }
}
Beispiel #2
0
static char *import_callback (void *ctx_, const char *dir, const char *file, int *success)
{
    const auto &ctx = *static_cast<ImportCallbackContext*>(ctx_);

    std::string input;

    ImportStatus status = try_path(dir, file, input);

    std::vector<std::string> jpaths(*ctx.jpaths);

    // If not found, try library search path.
    while (status == IMPORT_STATUS_FILE_NOT_FOUND) {
        if (jpaths.size() == 0) {
            *success = 0;
            const char *err = "No match locally or in the Jsonnet library path.";
            char *r = jsonnet_realloc(ctx.vm, nullptr, std::strlen(err) + 1);
            std::strcpy(r, err);
            return r;
        }
        status = try_path(jpaths.back(), file, input);
        jpaths.pop_back();
    }

    if (status == IMPORT_STATUS_IO_ERROR) {
        *success = 0;
        const char *err = std::strerror(errno);
        char *r = jsonnet_realloc(ctx.vm, nullptr, std::strlen(err) + 1);
        std::strcpy(r, err);
        return r;
    } else {
        assert(status == IMPORT_STATUS_OK);
        *success = 1;
        char *r = jsonnet_realloc(ctx.vm, nullptr, input.length() + 1);
        std::strcpy(r, input.c_str());
        return r;
    }
}