<commit_before>/**
 * @file printer/yang.c
 * @author <NAME> <<EMAIL>>
 * @brief YANG printer for libyang data model structure
 *
 * Copyright (c) 2015 CESNET, z.s.p.o.
 *
 * 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.
 * 3. Neither the name of the Company nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../common.h"
#include "../tree.h"

#define INDENT ""
#define LEVEL (level*2)

static void yang_print_mnode(FILE *f, int level, struct ly_mnode *mnode,
                             int mask);

static void yang_print_text(FILE *f, int level, const char *name,
                            const char *text)
{
	const char *s, *t;

	fprintf(f, "%*s%s\n", LEVEL, INDENT, name);
	level++;

	fprintf(f, "%*s\"", LEVEL, INDENT);
	t = text;
	while((s = strchr(t, '\n'))) {
		fwrite(t, sizeof *t, s - t + 1, f);
		t = s + 1;
		fprintf(f, "%*s", LEVEL, INDENT);
	}

	fprintf(f, "%s\";\n\n", t);
	level--;

}

/*
 * Covers:
 * description, reference, status
 */
static void yang_print_mnode_common(FILE *f, int level, struct ly_mnode *mnode)
{
	if (mnode->flags & LY_NODE_STATUS_CURR) {
		fprintf(f, "%*sstatus \"current\";\n", LEVEL, INDENT);
	} else if (mnode->flags & LY_NODE_STATUS_DEPRC) {
		fprintf(f, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
	} else if (mnode->flags & LY_NODE_STATUS_OBSLT) {
		fprintf(f, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
	}

	if (mnode->dsc) {
		yang_print_text(f, level, "description", mnode->dsc);
	}
	if (mnode->ref) {
		yang_print_text(f, level, "reference", mnode->ref);
	}
}

/*
 * Covers:
 * config
 * description, reference, status
 */
static void yang_print_mnode_common2(FILE *f, int level, struct ly_mnode *mnode)
{
	if (!mnode->parent || (mnode->parent->flags & LY_NODE_CONFIG_MASK) != (mnode->flags & LY_NODE_CONFIG_MASK)) {
		/* print config only when it differs from the parent or in root */
		if (mnode->flags & LY_NODE_CONFIG_W) {
			fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT);
		} else if (mnode->flags & LY_NODE_CONFIG_R) {
			fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT);
		}
	}

	yang_print_mnode_common(f, level, mnode);
}

static void yang_print_type(FILE *f, int level, struct ly_module *module, struct ly_type *type)
{
	int i;

	if (type->prefix) {
		fprintf(f, "%*stype %s:%s {\n", LEVEL, INDENT, type->prefix, type->der->name);
	} else {
		fprintf(f, "%*stype %s {\n", LEVEL, INDENT, type->der->name);
	}
	level++;<commit_msg>yang printer: NEW: mandatory print<commit_after>/**
 * @file printer/yang.c
 * @author <NAME> <<EMAIL>>
 * @brief YANG printer for libyang data model structure
 *
 * Copyright (c) 2015 CESNET, z.s.p.o.
 *
 * 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.
 * 3. Neither the name of the Company nor the names of its contributors
 *    may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../common.h"
#include "../tree.h"

#define INDENT ""
#define LEVEL (level*2)

static void yang_print_mnode(FILE *f, int level, struct ly_mnode *mnode,
                             int mask);

static void yang_print_text(FILE *f, int level, const char *name,
                            const char *text)
{
	const char *s, *t;

	fprintf(f, "%*s%s\n", LEVEL, INDENT, name);
	level++;

	fprintf(f, "%*s\"", LEVEL, INDENT);
	t = text;
	while((s = strchr(t, '\n'))) {
		fwrite(t, sizeof *t, s - t + 1, f);
		t = s + 1;
		fprintf(f, "%*s", LEVEL, INDENT);
	}

	fprintf(f, "%s\";\n\n", t);
	level--;

}

/*
 * Covers:
 * description, reference, status
 */
static void yang_print_mnode_common(FILE *f, int level, struct ly_mnode *mnode)
{
	if (mnode->flags & LY_NODE_STATUS_CURR) {
		fprintf(f, "%*sstatus \"current\";\n", LEVEL, INDENT);
	} else if (mnode->flags & LY_NODE_STATUS_DEPRC) {
		fprintf(f, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
	} else if (mnode->flags & LY_NODE_STATUS_OBSLT) {
		fprintf(f, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
	}

	if (mnode->dsc) {
		yang_print_text(f, level, "description", mnode->dsc);
	}
	if (mnode->ref) {
		yang_print_text(f, level, "reference", mnode->ref);
	}
}

/*
 * Covers:
 * config, mandatory
 * description, reference, status
 */
static void yang_print_mnode_common2(FILE *f, int level, struct ly_mnode *mnode)
{
	if (!mnode->parent || (mnode->parent->flags & LY_NODE_CONFIG_MASK) != (mnode->flags & LY_NODE_CONFIG_MASK)) {
		/* print config only when it differs from the parent or in root */
		if (mnode->flags & LY_NODE_CONFIG_W) {
			fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT);
		} else if (mnode->flags & LY_NODE_CONFIG_R) {
			fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT);
		}
	}

	if (mnode->flags & LY_NODE_MAND_TRUE) {
		fprintf(f, "%*smandatory \"true\";\n", LEVEL, INDENT);
	} else if (mnode->flags & LY_NODE_MAND_FALSE) {
		fprintf(f, "%*smandatory \"false\";\n", LEVEL, INDENT);
	}

	yang_print_mnode_common(f, level, mnode);
}

static void yang_print_type(FILE *f, int level, struct ly_module *module, struct ly_type *type)
{
	int i;

	if (type->prefix) {
		fprintf(f, "%*stype %s:%s {\n", LEVEL, INDENT, type->prefix, type->der->name);
	} else {
		fprintf(f, "%*stype %s {\n", LEVEL, INDENT, type->der->name);
	}
	level++;
{'fraction_non_alphanumeric': 0.154947302186566, 'fraction_numerical': 0.0034607519270095956, 'mean_word_length': 3.692250922509225, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7443037', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2303, 'n_words': 679}