<commit_before>/*
 * libiio - Library for interfacing industrial I/O (IIO) devices
 *
 * Copyright (C) 2014 Analog Devices, Inc.
 * Author: <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * */

#include "debug.h"
#include "iio-private.h"

#include <errno.h>
#include <libxml/tree.h>
#include <string.h>

static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
{
	xmlAttr *attr;
	char **attrs, *name = NULL;

	for (attr = n->properties; attr; attr = attr->next) {
		if (!strcmp((char *) attr->name, "name")) {
			name = strdup((char *) attr->children->content);
		} else {
			WARNING("Unknown field \'%s\' in channel %s\n",
					attr->name, chn->id);
		}
	}

	if (!name) {
		ERROR("Incomplete attribute in channel %s\n", chn->id);
		goto err_free;
	}

	attrs = realloc(chn->attrs, (1 + chn->nb_attrs) * sizeof(char *));
	if (!attrs)
		goto err_free;

	attrs[chn->nb_attrs++] = name;
	chn->attrs = attrs;
	return 0;

err_free:
	if (name)
		free(name);
	return -1;
}

static int add_attr_to_device(struct iio_device *dev, xmlNode *n, bool is_debug)
{
	xmlAttr *attr;
	char **attrs, *name = NULL;

	for (attr = n->properties; attr; attr = attr->next) {
		if (!strcmp((char *) attr->name, "name")) {
			name = strdup((char *) attr->children->content);
		} else {
			WARNING("Unknown field \'%s\' in device %s\n",
					attr->name, dev->id);
		}
	}

	if (!name) {
		ERROR("Incomplete attribute in device %s\n", dev->id);
		goto err_free;
	}

	if (is_debug)
		attrs = realloc(dev->debug_attrs,
				(1 + dev->nb_debug_attrs) * sizeof(char *));
	else
		attrs = realloc(dev->attrs,
				(1 + dev->nb_attrs) * sizeof(char *));
	if (!attrs)
		goto err_free;

	if (is_debug) {
		attrs[dev->nb_debug_attrs++] = name;
		dev->debug_attrs = attrs;
	} else {
		attrs[dev->nb_attrs++] = name;
		dev->attrs = attrs;
	}
	return 0;

err_free:
	if (name)
		free(name);
	return -1;
}

static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
{
	xmlAttr *attr;
	struct iio_channel *chn = calloc(1, sizeof(*chn));
	if (!chn)
		return NULL;

	chn->dev = dev;

	for (attr = n->properties; attr; attr = attr->next) {
		const char *name = (const char *) attr->name,
		      *content = (const char *) attr->children->content;
		if (!strcmp(name, "name")) {
			chn->name = strdup(content);
		} else if (!strcmp(name, "id")) {
			chn->id = strdup(content);
		} else if (!strcmp(name, "type")) {
			if (!strcmp(content, "output"))
				chn->is_output = true;
			else if (strcmp(content, "input"))
				WARNING("Unknown channel type %s\n", content);
		} else {<commit_msg>XML backend: Detect channels that are scan elements<commit_after>/*
 * libiio - Library for interfacing industrial I/O (IIO) devices
 *
 * Copyright (C) 2014 Analog Devices, Inc.
 * Author: <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * */

#include "debug.h"
#include "iio-private.h"

#include <errno.h>
#include <libxml/tree.h>
#include <string.h>

static int add_attr_to_channel(struct iio_channel *chn, xmlNode *n)
{
	xmlAttr *attr;
	char **attrs, *name = NULL;

	for (attr = n->properties; attr; attr = attr->next) {
		if (!strcmp((char *) attr->name, "name")) {
			name = strdup((char *) attr->children->content);
		} else {
			WARNING("Unknown field \'%s\' in channel %s\n",
					attr->name, chn->id);
		}
	}

	if (!name) {
		ERROR("Incomplete attribute in channel %s\n", chn->id);
		goto err_free;
	}

	attrs = realloc(chn->attrs, (1 + chn->nb_attrs) * sizeof(char *));
	if (!attrs)
		goto err_free;

	attrs[chn->nb_attrs++] = name;
	chn->attrs = attrs;
	return 0;

err_free:
	if (name)
		free(name);
	return -1;
}

static int add_attr_to_device(struct iio_device *dev, xmlNode *n, bool is_debug)
{
	xmlAttr *attr;
	char **attrs, *name = NULL;

	for (attr = n->properties; attr; attr = attr->next) {
		if (!strcmp((char *) attr->name, "name")) {
			name = strdup((char *) attr->children->content);
		} else {
			WARNING("Unknown field \'%s\' in device %s\n",
					attr->name, dev->id);
		}
	}

	if (!name) {
		ERROR("Incomplete attribute in device %s\n", dev->id);
		goto err_free;
	}

	if (is_debug)
		attrs = realloc(dev->debug_attrs,
				(1 + dev->nb_debug_attrs) * sizeof(char *));
	else
		attrs = realloc(dev->attrs,
				(1 + dev->nb_attrs) * sizeof(char *));
	if (!attrs)
		goto err_free;

	if (is_debug) {
		attrs[dev->nb_debug_attrs++] = name;
		dev->debug_attrs = attrs;
	} else {
		attrs[dev->nb_attrs++] = name;
		dev->attrs = attrs;
	}
	return 0;

err_free:
	if (name)
		free(name);
	return -1;
}

static struct iio_channel * create_channel(struct iio_device *dev, xmlNode *n)
{
	xmlAttr *attr;
	struct iio_channel *chn = calloc(1, sizeof(*chn));
	if (!chn)
		return NULL;

	chn->dev = dev;

	for (attr = n->properties; attr; attr = attr->next) {
		const char *name = (const char *) attr->name,
		      *content = (const char *) attr->children->content;
		if (!strcmp(name, "name")) {
			chn->name = strdup(content);
		} else if (!strcmp(name, "id")) {
			chn->id = strdup(content);
		} else if (!strcmp(name, "type")) {
			if (!strcmp(content, "output"))
				chn->is_output = true;
			else if (strcmp(content, "input"))
				WARNING("Unknown channel type %s\n", content);
		} else if (!strcmp(name, "scan_element")) {
			chn->is_scan_element = !strcmp(content, "true");
		} else {
{'fraction_non_alphanumeric': 0.1612851662404092, 'fraction_numerical': 0.004475703324808184, 'mean_word_length': 3.6624441132637853, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4236864', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2206, 'n_words': 664}