File: validation.c

package info (click to toggle)
xdffileio 0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,780 kB
  • sloc: sh: 11,327; ansic: 7,695; makefile: 112
file content (91 lines) | stat: -rw-r--r-- 2,388 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
    Copyright (C) 2010  EPFL (Ecole Polytechnique Fédérale de Lausanne)
    Laboratory CNBI (Chair in Non-Invasive Brain-Machine Interface)
    Nicolas Bourdaud <nicolas.bourdaud@epfl.ch>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <xdfio.h>
#include "validation.h"

int test_closest_type(struct xdf* xdf, unsigned int ntype,
		 enum xdftype *allowed_type)
{
	unsigned int i, j;
	int is_supported, ret;

	for (i=0; i<XDF_NUM_DATA_TYPES; i++) {
		ret = xdf_closest_type(xdf, i);
		if (ret < 0)
			return -1;

		// Check ret is supported type
		is_supported = 0;
		for (j=0; j<ntype; j++) {
			if ((enum xdftype)ret == allowed_type[j])
				is_supported = 1;
		}
		if (!is_supported) {
			fprintf(stderr,	"\txdf_closest_type returns unsupported type\n");
			return -1;
		}
	}
	return 0;
}


int test_validation_param(enum xdffiletype ftype, unsigned int ntype,
					enum xdftype *allowed_type)
{
	int r1, r2, is_supported, retcode = 0;
	enum xdftype curr;
	unsigned int i, j;
	struct xdf *xdf;
	struct xdfch *ch;

	xdf = xdf_open("datafile", XDF_WRITE, ftype);
	if (!xdf) {
		fprintf(stderr, "\nCannot create an XDF file: (%i) %s\n",
				errno, strerror(errno));
	}

	ch = xdf_add_channel(xdf, NULL);

	for (i=0; i<XDF_NUM_DATA_TYPES; i++) {
		curr = i;
		r1 = xdf_set_chconf(ch, XDF_CF_STOTYPE, curr, XDF_NOF);
		r2 = xdf_set_conf(xdf, XDF_CF_STOTYPE, curr, XDF_NOF);

		is_supported = 0;
		for (j=0; j<ntype; j++) {
			if (curr == allowed_type[j])
				is_supported = 1;
		}

		if ((is_supported && (r1 || r2))
		    || (!is_supported && (!r1 || !r2)))
			retcode = -1;
	}

	if (!retcode)
		retcode = test_closest_type(xdf, ntype, allowed_type);

	xdf_close(xdf);
	
	unlink("datafile");
	return retcode;
}