Esempio n. 1
0
static int
write_iff(struct lyout *out, const struct lys_module *module, struct lys_iffeature *expr, int prefix_kind,
          int *index_e, int *index_f)
{
    int count = 0, brackets_flag = *index_e;
    uint8_t op;
    struct lys_module *mod;

    op = iff_getop(expr->expr, *index_e);
    (*index_e)++;

    switch (op) {
    case LYS_IFF_F:
        if (lys_main_module(expr->features[*index_f]->module) != lys_main_module(module)) {
            if (prefix_kind == 0) {
                count += ly_print(out, "%s:", transform_module_name2import_prefix(module,
                                  lys_main_module(expr->features[*index_f]->module)->name));
            } else if (prefix_kind == 1) {
                count += ly_print(out, "%s:", lys_main_module(expr->features[*index_f]->module)->name);
            } else if (prefix_kind == 2) {
                count += ly_print(out, "%s:", lys_main_module(expr->features[*index_f]->module)->prefix);
            } else if (prefix_kind == 3) {
                mod =  lys_main_module(expr->features[*index_f]->module);
                count += ly_print(out, "%s%s%s:", mod->name, mod->rev_size ? "@" : "", mod->rev_size ? mod->rev[0].date : "");
            }
        }
        count += ly_print(out, expr->features[*index_f]->name);
        (*index_f)++;
        break;
    case LYS_IFF_NOT:
        count += ly_print(out, "not ");
        count += write_iff(out, module, expr, prefix_kind, index_e, index_f);
        break;
    case LYS_IFF_AND:
        if (brackets_flag) {
            /* AND need brackets only if previous op was not */
            if (*index_e < 2 || iff_getop(expr->expr, *index_e - 2) != LYS_IFF_NOT) {
                brackets_flag = 0;
            }
        }
        /* falls through */
    case LYS_IFF_OR:
        if (brackets_flag) {
            count += ly_print(out, "(");
        }
        count += write_iff(out, module, expr, prefix_kind, index_e, index_f);
        count += ly_print(out, " %s ", op == LYS_IFF_OR ? "or" : "and");
        count += write_iff(out, module, expr, prefix_kind, index_e, index_f);
        if (brackets_flag) {
            count += ly_print(out, ")");
        }
    }

    return count;
}
Esempio n. 2
0
static const char *
_transform_json2xml(const struct lys_module *module, const char *expr, int schema, const char ***prefixes,
                    const char ***namespaces, uint32_t *ns_count)
{
    const char *in, *id, *prefix;
    char *out, *col, *name;
    size_t out_size, out_used, id_len;
    const struct lys_module *mod;
    uint32_t i;

    assert(module && expr && ((!prefixes && !namespaces && !ns_count) || (prefixes && namespaces && ns_count)));

    if (ns_count) {
        *ns_count = 0;
        *prefixes = NULL;
        *namespaces = NULL;
    }

    in = expr;
    out_size = strlen(in) + 1;
    out = malloc(out_size);
    if (!out) {
        LOGMEM;
        return NULL;
    }
    out_used = 0;

    while (1) {
        col = strchr(in, ':');
        /* we're finished, copy the remaining part */
        if (!col) {
            strcpy(&out[out_used], in);
            out_used += strlen(in) + 1;
            assert(out_size == out_used);
            return lydict_insert_zc(module->ctx, out);
        }
        id = strpbrk_backwards(col - 1, "/ [\'\"", (col - in) - 1);
        if ((id[0] == '/') || (id[0] == ' ') || (id[0] == '[') || (id[0] == '\'') || (id[0] == '\"')) {
            ++id;
        }
        id_len = col - id;

        /* get the module */
        if (!schema) {
            name = strndup(id, id_len);
            mod = ly_ctx_get_module(module->ctx, name, NULL);
            free(name);
            if (!mod) {
                LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, id_len, id);
                goto fail;
            }
            prefix = mod->prefix;
        } else {
            name = strndup(id, id_len);
            prefix = transform_module_name2import_prefix(module, name);
            free(name);
            if (!prefix) {
                LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, id_len, id);
                goto fail;
            }
        }

        /* remember the namespace definition (only if it's new) */
        if (!schema && ns_count) {
            for (i = 0; i < *ns_count; ++i) {
                if (ly_strequal((*namespaces)[i], mod->ns, 1)) {
                    break;
                }
            }
            if (i == *ns_count) {
                ++(*ns_count);
                *prefixes = ly_realloc(*prefixes, *ns_count * sizeof **prefixes);
                if (!(*prefixes)) {
                    LOGMEM;
                    goto fail;
                }
                *namespaces = ly_realloc(*namespaces, *ns_count * sizeof **namespaces);
                if (!(*namespaces)) {
                    LOGMEM;
                    goto fail;
                }
                (*prefixes)[*ns_count - 1] = mod->prefix;
                (*namespaces)[*ns_count - 1] = mod->ns;
            }
        }

        /* adjust out size */
        out_size += strlen(prefix) - id_len;
        out = ly_realloc(out, out_size);
        if (!out) {
            LOGMEM;
            goto fail;
        }

        /* copy the data before prefix */
        strncpy(&out[out_used], in, id-in);
        out_used += id - in;

        /* copy the model prefix */
        strcpy(&out[out_used], prefix);
        out_used += strlen(prefix);

        /* copy ':' */
        out[out_used] = ':';
        ++out_used;

        /* finally adjust in pointer for next round */
        in = col + 1;
    }

    /* unreachable */
    LOGINT;

fail:
    if (!schema && ns_count) {
        free(*prefixes);
        free(*namespaces);
    }
    free(out);
    return NULL;
}