Add support for errors with a range of columns
[survex.git] / src / img.h
blob57505cbce561d019886fda84fdac6f89bb73a539
1 /* img.h
2 * Header file for routines to read and write Survex ".3d" image files
3 * Copyright (C) Olly Betts 1993,1994,1997,2001,2002,2003,2004,2005,2006,2010,2011,2012,2013,2014,2016
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifndef IMG_H
21 # define IMG_H
23 /* Define IMG_API_VERSION if you want more recent versions of the img API.
25 * 0 (default) The old API. date1 and date2 give the survey date as time_t.
26 * Set to 0 for "unknown".
27 * 1 days1 and days2 give survey dates as days since 1st Jan 1900.
28 * Set to -1 for "unknown".
30 #ifndef IMG_API_VERSION
31 # define IMG_API_VERSION 0
32 #elif IMG_API_VERSION > 1
33 # error IMG_API_VERSION > 1 too new
34 #endif
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 #include <stdio.h>
41 #include <time.h> /* for time_t */
43 # define img_BAD -2
44 # define img_STOP -1
45 # define img_MOVE 0
46 # define img_LINE 1
47 /* NB: img_CROSS is never output and ignored on input.
48 * Put crosses where labels are. */
49 /* # define img_CROSS 2 */
50 # define img_LABEL 3
51 # define img_XSECT 4
52 # define img_XSECT_END 5
53 /* Loop closure information for the *preceeding* traverse (img_MOVE + one or
54 * more img_LINEs). */
55 # define img_ERROR_INFO 6
57 /* Leg flags */
58 # define img_FLAG_SURFACE 0x01
59 # define img_FLAG_DUPLICATE 0x02
60 # define img_FLAG_SPLAY 0x04
62 /* Station flags */
63 # define img_SFLAG_SURFACE 0x01
64 # define img_SFLAG_UNDERGROUND 0x02
65 # define img_SFLAG_ENTRANCE 0x04
66 # define img_SFLAG_EXPORTED 0x08
67 # define img_SFLAG_FIXED 0x10
68 # define img_SFLAG_ANON 0x20
69 # define img_SFLAG_WALL 0x40
71 /* File-wide flags */
72 # define img_FFLAG_EXTENDED 0x80
74 /* When writing img_XSECT, img_XFLAG_END in pimg->flags means this is the last
75 * img_XSECT in this tube:
77 # define img_XFLAG_END 0x01
79 # define img_STYLE_UNKNOWN -1
80 # define img_STYLE_NORMAL 0
81 # define img_STYLE_DIVING 1
82 # define img_STYLE_CARTESIAN 2
83 # define img_STYLE_CYLPOLAR 3
84 # define img_STYLE_NOSURVEY 4
86 /* 3D coordinates (in metres) */
87 typedef struct {
88 double x, y, z;
89 } img_point;
91 typedef struct {
92 /* Members you can access when reading (don't touch when writing): */
93 char *label;
94 int flags;
95 char *title;
96 /* If the coordinate system was specified, this contains a PROJ4 string
97 * describing it. If not, this member will be NULL.
99 char *cs;
100 /* Older .3d format versions stored a human readable datestamp string.
101 * Format versions >= 8 versions store a string consisting of "@" followed
102 * by the number of seconds since midnight UTC on 1/1/1970. Some foreign
103 * formats contain a human readable string, others no date information
104 * (which results in "?" being returned).
106 char *datestamp;
107 /* The datestamp as a time_t (or (time_t)-1 if not available).
109 * For 3d format versions >= 8, this is a reliable value and in UTC. Older
110 * 3d format versions store a human readable time, which img will attempt
111 * to decode, but it may fail, particularly with handling timezones. Even
112 * if it does work, beware that times in local time where DST applies are
113 * inherently ambiguous around when the clocks go back.
115 * CMAP XYZ files contain a timestamp. It's probably in localtime (but
116 * without any timezone information) and the example files are all pre-2000
117 * and have two digit years. We do our best to turn these into a useful
118 * time_t value.
120 time_t datestamp_numeric;
121 char separator; /* character used to separate survey levels ('.' usually) */
123 /* Members that can be set when writing: */
124 #if IMG_API_VERSION == 0
125 time_t date1, date2;
126 #else /* IMG_API_VERSION == 1 */
127 int days1, days2;
128 #endif
129 double l, r, u, d;
131 /* Error information - valid when img_ERROR_INFO is returned: */
132 int n_legs;
133 double length;
134 double E, H, V;
136 /* The filename actually opened (e.g. may have ".3d" added): */
137 char * filename_opened;
139 /* Non-zero if reading an extended elevation: */
140 int is_extended_elevation;
142 /* Members that can be set when writing: */
143 /* The style of the data - one of the img_STYLE_* constants above */
144 int style;
146 /* All other members are for internal use only: */
147 FILE *fh; /* file handle of image file */
148 char *label_buf;
149 size_t buf_len;
150 size_t label_len;
151 int fRead; /* 1 for reading, 0 for writing */
152 long start;
153 /* version of file format:
154 * -4 => CMAP .xyz file, shot format
155 * -3 => CMAP .xyz file, station format
156 * -2 => Compass .plt file
157 * -1 => .pos file
158 * 0 => 0.01 ascii
159 * 1 => 0.01 binary,
160 * 2 => byte actions and flags
161 * 3 => prefixes for legs; compressed prefixes
162 * 4 => survey date
163 * 5 => LRUD info
164 * 6 => error info
165 * 7 => more compact dates with wider range
166 * 8 => lots of changes
168 int version;
169 char *survey;
170 size_t survey_len;
171 int pending; /* for old style text format files and survey filtering */
172 img_point mv;
173 #if IMG_API_VERSION == 0
174 time_t olddate1, olddate2;
175 #else /* IMG_API_VERSION == 1 */
176 int olddays1, olddays2;
177 #endif
178 int oldstyle;
179 } img;
181 /* Which version of the file format to output (defaults to newest) */
182 extern unsigned int img_output_version;
184 /* Minimum supported value for img_output_version: */
185 #define IMG_VERSION_MIN 1
187 /* Maximum supported value for img_output_version: */
188 #define IMG_VERSION_MAX 8
190 /* Open a .3d file for reading
191 * fnm is the filename
192 * Returns pointer to an img struct or NULL
194 #define img_open(F) img_open_survey((F), NULL)
196 /* Open a .3d file for reading
197 * fnm is the filename
198 * Returns pointer to an img struct or NULL
199 * survey points to a survey name to restrict reading to (or NULL for all
200 * survey data in the file)
202 img *img_open_survey(const char *fnm, const char *survey);
204 /* Open a .3d file for output
205 * fnm is the filename
206 * title is the title
207 * flags contains a bitwise-or of any file-wide flags - currently only one
208 * is available: img_FFLAG_EXTENDED. (The third parameter used to be
209 * 'fBinary', but has been ignored for many years, so the parameter has
210 * been repurposed for flags - for this reason, img.c deliberately ignores bit
211 * 1 being set, but callers should be written/updated not to set it).
213 * Returns pointer to an img struct or NULL for error (check img_error()
214 * for details)
216 #define img_open_write(F, T, S) img_open_write_cs(F, T, NULL, S)
218 /* Open a .3d file for output in a specified coordinate system
219 * fnm is the filename
220 * title is the title
221 * cs is a PROJ4 string describing the coordinate system (or NULL)
222 * flags contains a bitwise-or of any file-wide flags - currently only one
223 * is available: img_FFLAG_EXTENDED.
225 * Returns pointer to an img struct or NULL for error (check img_error()
226 * for details)
228 img *img_open_write_cs(const char *fnm, const char *title, const char * cs,
229 int flags);
231 /* Read an item from a .3d file
232 * pimg is a pointer to an img struct returned by img_open()
233 * coordinates are returned in p
234 * flags and label name are returned in fields in pimg
235 * Returns img_XXXX as #define-d above
237 int img_read_item(img *pimg, img_point *p);
239 /* Write a item to a .3d file
240 * pimg is a pointer to an img struct returned by img_open_write()
241 * code is one of the img_XXXX #define-d above
242 * flags is the leg, station, or xsect flags
243 * (meaningful for img_LINE, img_LABEL, and img_XSECT respectively)
244 * s is the label (only meaningful for img_LABEL)
245 * x, y, z are the coordinates
247 void img_write_item(img *pimg, int code, int flags, const char *s,
248 double x, double y, double z);
250 /* Write error information for the current traverse
251 * n_legs is the number of legs in the traverse
252 * length is the traverse length (in m)
253 * E is the ratio of the observed misclosure to the theoretical one
254 * H is the ratio of the observed horizontal misclosure to the theoretical one
255 * V is the ratio of the observed vertical misclosure to the theoretical one
257 void img_write_errors(img *pimg, int n_legs, double length,
258 double E, double H, double V);
260 /* rewind a .3d file opened for reading so the data can be read in
261 * several passes
262 * pimg is a pointer to an img struct returned by img_open()
263 * Returns: non-zero for success, zero for error (check img_error() for
264 * details)
266 int img_rewind(img *pimg);
268 /* Close a .3d file
269 * pimg is a pointer to an img struct returned by img_open() or
270 * img_open_write()
271 * Returns: non-zero for success, zero for error (check img_error() for
272 * details)
274 int img_close(img *pimg);
276 /* Codes returned by img_error */
277 typedef enum {
278 IMG_NONE = 0, IMG_FILENOTFOUND, IMG_OUTOFMEMORY,
279 IMG_CANTOPENOUT, IMG_BADFORMAT, IMG_DIRECTORY,
280 IMG_READERROR, IMG_WRITEERROR, IMG_TOONEW
281 } img_errcode;
283 /* Read the error code
284 * If img_open(), img_open_survey() or img_open_write() returns NULL, or
285 * img_rewind() or img_close() returns 0, or img_read_item() returns img_BAD
286 * then you can call this function to discover why.
288 img_errcode img_error(void);
290 #ifdef __cplusplus
292 #endif
294 #endif