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
33 #include "img_hosted.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
},
46 #define short_opts "s:"
48 static struct help_msg help
[] = {
50 {HLP_ENCODELONG(0), /*only load the sub-survey with this prefix*/199, 0},
62 cmp_station(const void *a
, const void *b
)
64 return name_cmp(((const station
*)a
)->name
, ((const station
*)b
)->name
,
69 static OSSIZE_T c_stns
= 0;
72 main(int argc
, char **argv
)
78 OSSIZE_T c_labels
= 0;
79 OSSIZE_T c_totlabel
= 0;
81 const char *survey
= NULL
;
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);
89 int opt
= cmdline_getopt();
90 if (opt
== EOF
) break;
91 if (opt
== 's') survey
= optarg
;
96 fnm_out
= argv
[optind
];
98 char *base
= baseleaf_from_fnm(fnm
);
99 fnm_out
= add_ext(base
, EXT_SVX_POS
);
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
);
116 result
= img_read_item(pimg
, &pt
);
123 c_totlabel
+= strlen(pimg
->label
) + 1;
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
;
139 result
= img_read_item(pimg
, &(stns
[c_stns
].pt
));
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
;
157 fatalerror(/*Bad 3d image file “%s”*/106, fnm
);
159 } while (result
!= img_STOP
);
161 if (c_stns
!= c_labels
|| p
!= p_end
) {
163 fatalerror(/*Bad 3d image file “%s”*/106, fnm
);
168 qsort(stns
, c_stns
, sizeof(station
), cmp_station
);
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
);