/[ascend]/trunk/models/johnpye/datareader/texttable.h
ViewVC logotype

Contents of /trunk/models/johnpye/datareader/texttable.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 809 - (show annotations) (download) (as text)
Mon Aug 7 14:48:55 2006 UTC (13 years, 11 months ago) by johnpye
File MIME type: text/x-chdr
File size: 5994 byte(s)
A little more tinkering on the Data Reader. I have added routines
to read data from the TMY2 format, but they're a long way from being
fully implemented yet. The added files sun.c and sun.h should
provide some useful routines for sun position and time of day, for
use with the TMY2 reader functions.
1 /* TextTable: A small C library to read data tables in text files */
2
3 /*
4 * Copyright (c) 2003,2004, Jean-Sebastien Roy (js@jeannot.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /* @(#) $Jeannot: texttable.h,v 1.14 2004/10/26 17:47:29 js Exp $ */
27
28 #ifndef _TEXTTABLE_
29 #define _TEXTTABLE_
30
31 #include <stddef.h>
32 #include <stdio.h>
33
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37
38 /*
39 * TextTable types
40 */
41 /* We should add : quoted string */
42 typedef enum {
43 TEXTTABLE_CHAR = 0, /* char */
44 TEXTTABLE_SHORT, /* short */
45 TEXTTABLE_INT, /* int */
46 TEXTTABLE_LONG, /* long */
47 #ifdef HAS_LONGLONG
48 TEXTTABLE_LONGLONG, /* long long */
49 #endif
50 TEXTTABLE_FLOAT, /* float */
51 TEXTTABLE_DOUBLE, /* double */
52 TEXTTABLE_STRING, /* string */
53 TEXTTABLE_UNKNOWN, /* unknow type */
54 TEXTTABLE_TYPE_MAX
55 } texttable_type;
56
57 /*
58 * TextTable errors
59 */
60 typedef enum {
61 TEXTTABLE_NOERR = 0, /* no error */
62 TEXTTABLE_ENOMEM = 1, /* memory allocation error */
63 TEXTTABLE_EINVAL = 2, /* invalid argument */
64 TEXTTABLE_EUNKTYPE = 3, /* unknown type */
65 TEXTTABLE_ETOOFEWC = 4, /* too few columns */
66 TEXTTABLE_ETOOMANYC = 5, /* too many columns */
67 TEXTTABLE_EINDATA = 6, /* invalid data */
68 TEXTTABLE_EFERROR = 7, /* file error */
69 TEXTTABLE_ERR_MAX = 8
70 } texttable_error;
71
72 /*
73 * A TextTable
74 */
75 typedef struct texttable_
76 {
77 size_t columns;
78 size_t rows;
79 texttable_type *type;
80 char **name;
81 union
82 {
83 void **dvoid;
84 char **dchar;
85 short **dshort;
86 int **dint;
87 long **dlong;
88 #ifdef HAS_LONGLONG
89 long long **dlonglong;
90 #endif
91 float **dfloat;
92 double **ddouble;
93 char ***dstring;
94 } data;
95 } texttable;
96
97 extern size_t texttable_sizeof[TEXTTABLE_UNKNOWN];
98
99 /*
100 * Given a number of columns, allocate a texttable structure.
101 * Returns a texttable_error.
102 */
103 extern int texttable_new(texttable *tt, size_t columns);
104
105 /*
106 * Free a texttable structure including any allocated strings.
107 */
108 extern void texttable_free(texttable *tt);
109
110 /*
111 * Allocate a texttable structure by reading the first line of stream 'file'.
112 * The fields are separated by 'delimiter'.
113 * If 'hasnames' is != 0, names for the fields are extracted from the first line
114 * and the file position is set to the begining of the second line.
115 *
116 * Returns a texttable_error.
117 */
118 extern int texttable_readheader(texttable *tt, FILE *file, char delimiter,
119 int hasnames);
120
121 /*
122 * Guess the types of a texttable, according to the first 'rows' lines of
123 * the file (or all is rows is < 0).
124 * The fields are separated by 'delimiter'.
125 *
126 * Returns a texttable_error.
127 */
128 extern int texttable_guesstype(texttable *tt, FILE *file, char delimiter,
129 size_t rows);
130
131 /*
132 * Allocate the provided number of rows in the texttable
133 *
134 * Returns a texttable_error.
135 */
136 extern int texttable_allocate(texttable *tt, size_t rows);
137
138 /*
139 * Resize the data tables of a texttable to ensure maxrows rows are available.
140 * If maxrows < tt->rows, adjust tt->rows.
141 *
142 * Returns a texttable_error.
143 */
144 extern int texttable_reallocate(texttable *tt, size_t maxrows);
145
146 /*
147 * Append data from stream file into texttable tt
148 * The fields are separated by 'delimiter'.
149 *
150 * Returns a texttable_error.
151 */
152 extern int texttable_readdata(texttable *tt, FILE *file, char delimiter);
153
154 /*
155 * Write a texttable tt to stream 'file' using delimiter 'delimiter'.
156 * If hasnames != 0, write the names of the fields in the first line.
157 *
158 * Returns a texttable_error.
159 */
160 extern int texttable_write(texttable *tt, FILE *file, char delimiter,
161 int hasnames);
162
163 /*
164 * Return the error message relative to texttable_error err.
165 */
166 extern char *texttable_strerror(int err);
167
168 /*
169 * Print an error message. Used by the CHECKTTSTATUS macro
170 */
171 extern void texttable_perror(int status, char *file, int line);
172
173 /* High level API */
174
175 /*
176 * Read some data columns from a file using the specified delimiter.
177 * If hasname !=0, the first line contains the names of the columns.
178 * type must point to an array of column types
179 * If name != NULL, point names to an array of the names of the columns.
180 * Point rows to the number of rows read.
181 *
182 * Returns a texttable_error.
183 */
184 extern int texttable_readtable(FILE *file, char delimiter, int hasnames,
185 size_t columns, texttable_type *type, char ***name, size_t *rows, ...);
186
187 /*
188 * Write some data columns to a file using the specified delimiter.
189 * columns is the number of columns to write.
190 * If name != NULL, write the names of the columns on the first line.
191 * rows is the number of rows to write.
192 *
193 * Returns a texttable_error.
194 */
195 extern int texttable_writetable(FILE *file, char delimiter, size_t columns,
196 texttable_type *type, char **name, size_t rows, ...);
197
198 #if defined(__cplusplus)
199 }
200 #endif
201
202 /*
203 * Assume status is a variable containing a texttable_error.
204 * If an error occured, print it and and exit.
205 */
206 #define CHECKTTSTATUS if(status != TEXTTABLE_NOERR) texttable_perror(status, \
207 __FILE__, __LINE__)
208
209 #endif /* _TEXTTABLE_ */

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22