summaryrefslogtreecommitdiff
path: root/cgats/cgatsstd.c
blob: 22c17ed58cba8271de44dd8d7d92084b5d063960 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

/* 
 * cgats library stdio and malloc utility classes.
 * Version 2.05
 *
 * These are kept in a separate file to allow them to be
 * selectively ommitted from the cgats library.
 */

/*
 * Author: Graeme W. Gill
 * Date:   2002/10/24
 *
 * Copyright 2002, Graeme W. Gill
 * All rights reserved.
 *
 * This material is licensed with an "MIT" free use license:-
 * see the License4.txt file in this directory for licensing details.
 *
 */



#ifndef COMBINED_STD

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

#include "cgats.h"

#ifdef NEVER	/* Not sure if this is needed with some combinations */

/* Implimentation function - register an error */
/* Return the error number */
/* [Note that this is a duplicate of the one in cgats.c] */
static int
err(cgats *p, int errc, const char *fmt, ...) {
	va_list args;

	p->errc = errc;
	va_start(args, fmt);
	vsprintf(p->err, fmt, args);
	va_end(args);

	/* If this is the first registered error */
	if (p->ferrc != 0) {
		p->ferrc = p->errc;
		strcpy(p->ferr, p->err);
	}

	return errc;
}

#endif /* NEVER */

#endif /* !COMBINED_STD */

#if defined(SEPARATE_STD) || defined(COMBINED_STD)

/* Create an empty cgats object, default standard allocator */
cgats *new_cgats(void) {
	cgats *p;
	cgatsAlloc *al;			/* memory allocator */

	if ((al = new_cgatsAllocStd()) == NULL)
		return NULL;

	if ((p = new_cgats_al(al)) == NULL) {
		al->del(al);
		return NULL;
	}

	p->del_al = 1;			/* Get cgets->del to cleanup allocator */
	return p;
}

/* Read a cgats file into structure */
/* Return non-zero if there was an error */
/* and set p->errc * p->err */
CGATS_STATIC
int
cgats_read_name(cgats *p, const char *filename) {
	int rv;
	cgatsFile *fp;

	p->errc = 0;
	p->err[0] = '\000';

	if ((fp = new_cgatsFileStd_name(filename, "r")) == NULL)
		return err(p,-1,"Unable to open file '%s' for reading",filename);
	rv = p->read(p, fp);
	fp->del(fp);

	return rv;
}

/* Write a cgats file into structure */
/* Return -ve, errc & err if there was an error */
CGATS_STATIC
int
cgats_write_name(cgats *p, const char *filename) {
	int rv;
	cgatsFile *fp;

	if ((fp = new_cgatsFileStd_name(filename, "w")) == NULL)
		return err(p,-1,"Unable to open file '%s' for writing",filename);
	rv = p->write(p, fp);
	fp->del(fp);

	return rv;
}

#endif /* defined(SEPARATE_STD) || defined(COMBINED_STD) */