source: git/src/3dtopos.c @ dc42b8e

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since dc42b8e was 8ca8c26, checked in by Olly Betts <olly@…>, 22 years ago

Fixed compiler warnings.

git-svn-id: file:///home/survex-svn/survex/trunk@1930 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/* 3dtopos.c */
2/* Produce a .pos file from a .3d file */
3/* Copyright (C) 2001 Olly Betts
4 *
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.
9 *
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.
14 *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <ctype.h>
25#include <math.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "cmdline.h"
31#include "filelist.h"
32#include "filename.h"
33#include "img.h"
34#include "message.h"
35#include "namecmp.h"
36#include "osalloc.h"
37
38static const struct option long_opts[] = {
39   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
40   {"survey", required_argument, 0, 's'},
41   {"help", no_argument, 0, HLP_HELP},
42   {"version", no_argument, 0, HLP_VERSION},
43   {0, 0, 0, 0}
44};
45
46#define short_opts "s:"
47
48static struct help_msg help[] = {
49/*                              <-- */
50   {HLP_ENCODELONG(0),          "only load the sub-survey with this prefix"},
51   {0, 0}
52};
53
54typedef struct {
55   const char *name;
56   img_point pt;
57} station;
58
59static int
60cmp_station(const void *a, const void *b)
61{
62   return name_cmp(((const station *)a)->name, ((const station *)b)->name);
63}
64
65static station *stns;
66static OSSIZE_T c_stns = 0;
67
68int
69main(int argc, char **argv)
70{
71   char *fnm, *fnm_out;
72   FILE *fh_out;
73   img *pimg;
74   int result;
75   OSSIZE_T c_labels = 0;
76   OSSIZE_T c_totlabel = 0;
77   char *p, *p_end;
78   const char *survey = NULL;
79
80   msg_init(argv);
81
82   cmdline_set_syntax_message("3D_FILE [POS_FILE]", NULL); /* TRANSLATE */
83   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
84   while (1) {
85      int opt = cmdline_getopt();
86      if (opt == EOF) break;
87      if (opt == 's') survey = optarg;
88   }
89
90   fnm = argv[optind++];
91   if (argv[optind]) {
92      fnm_out = argv[optind];
93   } else {
94      char *base = baseleaf_from_fnm(fnm);
95      fnm_out = add_ext(base, EXT_SVX_POS);
96      osfree(base);
97   }
98
99   pimg = img_open_survey(fnm, survey);
100   if (!pimg) fatalerror(img_error(), fnm);
101
102   fh_out = safe_fopen(fnm_out, "w");
103
104   /* Output headings line */
105   fputsnl(msg(/*( Easting, Northing, Altitude )*/195), fh_out);
106
107   do {
108      img_point pt;
109      result = img_read_item(pimg, &pt);
110      switch (result) {
111       case img_MOVE:
112       case img_LINE:
113         break;
114       case img_LABEL:
115         c_labels++;
116         c_totlabel += strlen(pimg->label) + 1;
117         break;
118       case img_BAD:
119         img_close(pimg);
120         fatalerror(img_error(), fnm);
121      }
122   } while (result != img_STOP);
123
124   /* + 1 to allow for reading coordinates of legs after the last label */
125   stns = osmalloc(ossizeof(station) * (c_labels + 1));
126   p = osmalloc(c_totlabel);
127   p_end = p + c_totlabel;
128
129   img_rewind(pimg);
130
131   do {
132      result = img_read_item(pimg, &(stns[c_stns].pt));
133      switch (result) {
134       case img_MOVE:
135       case img_LINE:
136         break;
137       case img_LABEL:
138         if (c_stns < c_labels) {
139            OSSIZE_T len = strlen(pimg->label) + 1;
140            if (p + len <= p_end) {
141               memcpy(p, pimg->label, len);
142               stns[c_stns++].name = p;
143               p += len;
144               break;
145            }
146         }
147         /* FALLTHRU */
148       case img_BAD:
149         img_close(pimg);
150         fatalerror(/*Bad 3d image file `%s'*/106, fnm);
151      }
152   } while (result != img_STOP);
153
154   if (c_stns != c_labels || p != p_end) {
155      img_close(pimg);
156      fatalerror(/*Bad 3d image file `%s'*/106, fnm);
157   }
158
159   img_close(pimg);
160
161   qsort(stns, c_stns, sizeof(station), cmp_station);
162
163   {
164      OSSIZE_T i;
165      for (i = 0; i < c_stns; i++) {
166         fprintf(fh_out, "(%8.2f, %8.2f, %8.2f ) %s\n",
167                 stns[i].pt.x, stns[i].pt.y, stns[i].pt.z, stns[i].name);
168      }
169   }
170
171   safe_fclose(fh_out);
172
173   return EXIT_SUCCESS;
174}
Note: See TracBrowser for help on using the repository browser.