Add MainFrm::GetCSProj() getter method
[survex.git] / src / 3dtopos.c
blobdf56d9dd2f1f7effa812427946e96f3663a67696
1 /* 3dtopos.c */
2 /* Produce a .pos file from a .3d file */
3 /* Copyright (C) 2001,2002,2011,2013,2014 Olly Betts
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 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <ctype.h>
25 #include <math.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "cmdline.h"
31 #include "filelist.h"
32 #include "filename.h"
33 #include "img_hosted.h"
34 #include "message.h"
35 #include "namecmp.h"
36 #include "osalloc.h"
38 static 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}
46 #define short_opts "s:"
48 static struct help_msg help[] = {
49 /* <-- */
50 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
51 {0, 0, 0}
54 typedef struct {
55 const char *name;
56 img_point pt;
57 } station;
59 static int separator;
61 static int
62 cmp_station(const void *a, const void *b)
64 return name_cmp(((const station *)a)->name, ((const station *)b)->name,
65 separator);
68 static station *stns;
69 static OSSIZE_T c_stns = 0;
71 int
72 main(int argc, char **argv)
74 char *fnm, *fnm_out;
75 FILE *fh_out;
76 img *pimg;
77 int result;
78 OSSIZE_T c_labels = 0;
79 OSSIZE_T c_totlabel = 0;
80 char *p, *p_end;
81 const char *survey = NULL;
83 msg_init(argv);
85 /* TRANSLATORS: Part of 3dtopos --help */
86 cmdline_set_syntax_message(/*3D_FILE [POS_FILE]*/217, 0, NULL);
87 cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
88 while (1) {
89 int opt = cmdline_getopt();
90 if (opt == EOF) break;
91 if (opt == 's') survey = optarg;
94 fnm = argv[optind++];
95 if (argv[optind]) {
96 fnm_out = argv[optind];
97 } else {
98 char *base = baseleaf_from_fnm(fnm);
99 fnm_out = add_ext(base, EXT_SVX_POS);
100 osfree(base);
103 pimg = img_open_survey(fnm, survey);
104 if (!pimg) fatalerror(img_error2msg(img_error()), fnm);
105 separator = pimg->separator;
107 fh_out = safe_fopen(fnm_out, "w");
109 /* Output headings line */
110 /* TRANSLATORS: Heading line for .pos file. Please try to ensure the “,”s
111 * (or at least the columns) are in the same place */
112 fputsnl(msg(/*( Easting, Northing, Altitude )*/195), fh_out);
114 do {
115 img_point pt;
116 result = img_read_item(pimg, &pt);
117 switch (result) {
118 case img_MOVE:
119 case img_LINE:
120 break;
121 case img_LABEL:
122 c_labels++;
123 c_totlabel += strlen(pimg->label) + 1;
124 break;
125 case img_BAD:
126 img_close(pimg);
127 fatalerror(img_error2msg(img_error()), fnm);
129 } while (result != img_STOP);
131 /* + 1 to allow for reading coordinates of legs after the last label */
132 stns = osmalloc(ossizeof(station) * (c_labels + 1));
133 p = osmalloc(c_totlabel);
134 p_end = p + c_totlabel;
136 img_rewind(pimg);
138 do {
139 result = img_read_item(pimg, &(stns[c_stns].pt));
140 switch (result) {
141 case img_MOVE:
142 case img_LINE:
143 break;
144 case img_LABEL:
145 if (c_stns < c_labels) {
146 OSSIZE_T len = strlen(pimg->label) + 1;
147 if (p + len <= p_end) {
148 memcpy(p, pimg->label, len);
149 stns[c_stns++].name = p;
150 p += len;
151 break;
154 /* FALLTHRU */
155 case img_BAD:
156 img_close(pimg);
157 fatalerror(/*Bad 3d image file “%s”*/106, fnm);
159 } while (result != img_STOP);
161 if (c_stns != c_labels || p != p_end) {
162 img_close(pimg);
163 fatalerror(/*Bad 3d image file “%s”*/106, fnm);
166 img_close(pimg);
168 qsort(stns, c_stns, sizeof(station), cmp_station);
171 OSSIZE_T i;
172 for (i = 0; i < c_stns; i++) {
173 fprintf(fh_out, "(%8.2f, %8.2f, %8.2f ) %s\n",
174 stns[i].pt.x, stns[i].pt.y, stns[i].pt.z, stns[i].name);
178 safe_fclose(fh_out);
180 return EXIT_SUCCESS;