source: git/src/survexport.cc @ 3ebfd6a

debug-ci-sanitisersfaster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 3ebfd6a was a901cea, checked in by Olly Betts <olly@…>, 17 months ago

Eliminate all uses of strcasecmp()

There weren't many uses and most can be done more efficiently.

  • Property mode set to 100644
File size: 14.1 KB
RevLine 
[79b32a95]1/* survexport.cc
2 * Convert a processed survey data file to another format.
[bd30612]3 */
4
[a901cea]5/* Copyright (C) 1994-2024 Olly Betts
[bd30612]6 * Copyright (C) 2004 John Pybus (SVG Output code)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 */
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
[9990aab]27#define MSG_SETUP_PROJ_SEARCH_PATH 1
28
[a901cea]29#include <ctype.h>
[bd30612]30#include <math.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "export.h"
36#include "mainfrm.h"
37
38#include "cmdline.h"
39#include "filename.h"
40#include "img_hosted.h"
41#include "message.h"
42#include "str.h"
43#include "useful.h"
44
[a8d2349]45#include <iostream>
[f7fb2ec]46#include <string>
47
48using namespace std;
49
[bd30612]50int
51main(int argc, char **argv)
52{
53   double pan = 0;
54   double tilt = -90.0;
[4d3c8915]55   export_format format = FMT_MAX_PLUS_ONE_;
[bd30612]56   int show_mask = 0;
57   const char *survey = NULL;
58   double grid = 0.0; /* grid spacing (or 0 for no grid) */
59   double text_height = DEFAULT_TEXT_HEIGHT; /* for station labels */
60   double marker_size = DEFAULT_MARKER_SIZE; /* for station markers */
61   double scale = 500.0;
[1a46879]62   SurveyFilter* filter = NULL;
[bd30612]63
[b349e7e2]64   {
65       /* Default to .pos output if installed as 3dtopos. */
66       char* progname = baseleaf_from_fnm(argv[0]);
[a901cea]67       for (char * p = progname; *p; ++p) {
68           *p = tolower((unsigned char)*p);
69       }
70       if (strcmp(progname, "3dtopos") == 0) {
[b349e7e2]71           format = FMT_POS;
72       }
73       osfree(progname);
74   }
75
[4d3c8915]76   const int OPT_FMT_BASE = 20000;
[02ba4c9]77   enum {
78       OPT_SCALE = 0x100, OPT_BEARING, OPT_TILT, OPT_PLAN, OPT_ELEV,
79       OPT_LEGS, OPT_SURF, OPT_SPLAYS, OPT_CROSSES, OPT_LABELS, OPT_ENTS,
80       OPT_FIXES, OPT_EXPORTS, OPT_XSECT, OPT_WALLS, OPT_PASG,
[32a040e]81       OPT_CENTRED, OPT_FULL_COORDS, OPT_CLAMP_TO_GROUND, OPT_DEFAULTS
[02ba4c9]82   };
[bd30612]83   static const struct option long_opts[] = {
84        /* const char *name; int has_arg (0 no_argument, 1 required, 2 options_*); int *flag; int val */
85        {"survey", required_argument, 0, 's'},
[02ba4c9]86        {"scale", required_argument, 0, OPT_SCALE},
87        {"bearing", required_argument, 0, OPT_BEARING},
88        {"tilt", required_argument, 0, OPT_TILT},
89        {"plan", no_argument, 0, OPT_PLAN},
90        {"elevation", no_argument, 0, OPT_ELEV},
[f7fb2ec]91        {"legs", no_argument, 0, OPT_LEGS},
[6e89e5d]92        {"surface-legs", no_argument, 0, OPT_SURF},
[02ba4c9]93        {"splays", no_argument, 0, OPT_SPLAYS},
[f7fb2ec]94        {"crosses", no_argument, 0, OPT_CROSSES},
95        {"station-names", no_argument, 0, OPT_LABELS},
[02ba4c9]96        {"entrances", no_argument, 0, OPT_ENTS},
97        {"fixes", no_argument, 0, OPT_FIXES},
98        {"exports", no_argument, 0, OPT_EXPORTS},
99        {"cross-sections", no_argument, 0, OPT_XSECT},
100        {"walls", no_argument, 0, OPT_WALLS},
101        {"passages", no_argument, 0, OPT_PASG},
102        {"origin-in-centre", no_argument, 0, OPT_CENTRED},
103        {"full-coordinates", no_argument, 0, OPT_FULL_COORDS},
[32a040e]104        {"clamp-to-ground", no_argument, 0, OPT_CLAMP_TO_GROUND},
[f7fb2ec]105        {"defaults", no_argument, 0, OPT_DEFAULTS},
[bd30612]106        {"grid", optional_argument, 0, 'g'},
107        {"text-height", required_argument, 0, 't'},
108        {"marker-size", required_argument, 0, 'm'},
[355df41]109        {"3d", no_argument, 0, OPT_FMT_BASE + FMT_3D},
[df121c2d]110        {"csv", no_argument, 0, OPT_FMT_BASE + FMT_CSV},
[4d3c8915]111        {"dxf", no_argument, 0, OPT_FMT_BASE + FMT_DXF},
112        {"eps", no_argument, 0, OPT_FMT_BASE + FMT_EPS},
113        {"gpx", no_argument, 0, OPT_FMT_BASE + FMT_GPX},
114        {"hpgl", no_argument, 0, OPT_FMT_BASE + FMT_HPGL},
115        {"json", no_argument, 0, OPT_FMT_BASE + FMT_JSON},
116        {"kml", no_argument, 0, OPT_FMT_BASE + FMT_KML},
117        {"plt", no_argument, 0, OPT_FMT_BASE + FMT_PLT},
118        {"skencil", no_argument, 0, OPT_FMT_BASE + FMT_SK},
119        {"pos", no_argument, 0, OPT_FMT_BASE + FMT_POS},
120        {"svg", no_argument, 0, OPT_FMT_BASE + FMT_SVG},
[bd30612]121        {"help", no_argument, 0, HLP_HELP},
122        {"version", no_argument, 0, HLP_VERSION},
[02ba4c9]123        // US spelling:
124        {"origin-in-center", no_argument, 0, OPT_CENTRED},
125        // Abbreviation:
126        {"full-coords", no_argument, 0, OPT_FULL_COORDS},
[bd30612]127        {0,0,0,0}
128   };
129
[02ba4c9]130#define short_opts "s:g::t:m:"
[bd30612]131
132   static struct help_msg help[] = {
133        /*                      <-- */
134        {HLP_ENCODELONG(0),   /*only load the sub-survey with this prefix*/199, 0},
[c0e4f18]135        /* TRANSLATORS: These example input values should not be translated. */
[02ba4c9]136        {HLP_ENCODELONG(1),   /*scale (50, 0.02, 1:50 and 2:100 all mean 1:50)*/217, 0},
[c0e4f18]137        /* TRANSLATORS: These example input values should not be translated. */
[02ba4c9]138        {HLP_ENCODELONG(2),   /*bearing (90, 90d, 100g all mean 90°)*/460, 0},
[c0e4f18]139        /* TRANSLATORS: These example input values should not be translated. */
[02ba4c9]140        {HLP_ENCODELONG(3),   /*tilt (45, 45d, 50g, 100% all mean 45°)*/461, 0},
[c0e4f18]141        /* TRANSLATORS: Don't translate example command line option --tilt=-90 */
[02ba4c9]142        {HLP_ENCODELONG(4),   /*plan view (equivalent to --tilt=-90)*/462, 0},
[c0e4f18]143        /* TRANSLATORS: Don't translate example command line option --tilt=0 */
[02ba4c9]144        {HLP_ENCODELONG(5),   /*elevation view (equivalent to --tilt=0)*/463, 0},
[f7fb2ec]145        {HLP_ENCODELONG(6),   /*underground survey legs*/476, 0},
[02ba4c9]146        {HLP_ENCODELONG(7),   /*surface survey legs*/464, 0},
147        {HLP_ENCODELONG(8),   /*splay legs*/465, 0},
[f7fb2ec]148        {HLP_ENCODELONG(9),   /*station markers*/474, 0},
149        {HLP_ENCODELONG(10),  /*station labels*/475, 0},
[02ba4c9]150        {HLP_ENCODELONG(11),  /*entrances*/466, 0},
151        {HLP_ENCODELONG(12),  /*fixed points*/467, 0},
152        {HLP_ENCODELONG(13),  /*exported stations*/468, 0},
153        {HLP_ENCODELONG(14),  /*cross-sections*/469, 0},
154        {HLP_ENCODELONG(15),  /*walls*/470, 0},
155        {HLP_ENCODELONG(16),  /*passages*/471, 0},
156        {HLP_ENCODELONG(17),  /*origin in centre*/472, 0},
157        {HLP_ENCODELONG(18),  /*full coordinates*/473, 0},
[32a040e]158        {HLP_ENCODELONG(19),  /*clamp to ground*/478, 0},
159        {HLP_ENCODELONG(20),  /*include items exported by default*/155, 0},
160        {HLP_ENCODELONG(21),  /*generate grid (default %sm)*/148, STRING(DEFAULT_GRID_SPACING)},
161        {HLP_ENCODELONG(22),  /*station labels text height (default %s)*/149, STRING(DEFAULT_TEXT_HEIGHT)},
162        {HLP_ENCODELONG(23),  /*station marker size (default %s)*/152, STRING(DEFAULT_MARKER_SIZE)},
[355df41]163        {HLP_ENCODELONG(24),  /*produce Survex 3d output*/487, 0},
164        {HLP_ENCODELONG(25),  /*produce CSV output*/102, 0},
165        {HLP_ENCODELONG(26),  /*produce DXF output*/156, 0},
166        {HLP_ENCODELONG(27),  /*produce EPS output*/454, 0},
167        {HLP_ENCODELONG(28),  /*produce GPX output*/455, 0},
168        {HLP_ENCODELONG(29),  /*produce HPGL output*/456, 0},
169        {HLP_ENCODELONG(30),  /*produce JSON output*/457, 0},
170        {HLP_ENCODELONG(31),  /*produce KML output*/458, 0},
[bd30612]171        /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
172         * so should not be translated. */
[355df41]173        {HLP_ENCODELONG(32),  /*produce Compass PLT output for Carto*/159, 0},
[4d3c8915]174        /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
175         * translated. */
[355df41]176        {HLP_ENCODELONG(33),  /*produce Skencil output*/158, 0},
177        {HLP_ENCODELONG(34),  /*produce Survex POS output*/459, 0},
178        {HLP_ENCODELONG(35),  /*produce SVG output*/160, 0},
[bd30612]179        {0, 0, 0}
180   };
181
182   msg_init(argv);
183
[f7fb2ec]184   string optmap[sizeof(show_mask) * CHAR_BIT];
185
186   int long_index;
187   bool always_include_defaults = false;
188   cmdline_init(argc, argv, short_opts, long_opts, &long_index, help, 1, 2);
[bd30612]189   while (1) {
[f7fb2ec]190      long_index = -1;
[bd30612]191      int opt = cmdline_getopt();
192      if (opt == EOF) break;
[f7fb2ec]193      int bit = 0;
[bd30612]194      switch (opt) {
[02ba4c9]195       case OPT_LEGS:
[f7fb2ec]196         bit = LEGS;
[bd30612]197         break;
[02ba4c9]198       case OPT_SURF:
[f7fb2ec]199         bit = SURF;
[02ba4c9]200         break;
201       case OPT_SPLAYS:
[f7fb2ec]202         bit = SPLAYS;
[02ba4c9]203         break;
204       case OPT_CROSSES:
[f7fb2ec]205         bit = STNS;
[bd30612]206         break;
[02ba4c9]207       case OPT_LABELS:
[f7fb2ec]208         bit = LABELS;
[bd30612]209         break;
[02ba4c9]210       case OPT_ENTS:
[f7fb2ec]211         bit = ENTS;
[02ba4c9]212         break;
213       case OPT_FIXES:
[f7fb2ec]214         bit = FIXES;
[02ba4c9]215         break;
216       case OPT_EXPORTS:
[f7fb2ec]217         bit = EXPORTS;
[02ba4c9]218         break;
219       case OPT_XSECT:
[f7fb2ec]220         bit = XSECT;
[02ba4c9]221         break;
222       case OPT_WALLS:
[f7fb2ec]223         bit = WALLS;
[02ba4c9]224         break;
225       case OPT_PASG:
[f7fb2ec]226         bit = PASG;
[02ba4c9]227         break;
228       case OPT_CENTRED:
[f7fb2ec]229         bit = CENTRED;
[02ba4c9]230         break;
231       case OPT_FULL_COORDS:
[f7fb2ec]232         bit = FULL_COORDS;
233         break;
[32a040e]234       case OPT_CLAMP_TO_GROUND:
235         bit = CLAMP_TO_GROUND;
236         break;
[f7fb2ec]237       case OPT_DEFAULTS:
238         always_include_defaults = true;
[bd30612]239         break;
240       case 'g': /* Grid */
241         if (optarg) {
242            grid = cmdline_double_arg();
243         } else {
244            grid = (double)DEFAULT_GRID_SPACING;
245         }
[f7fb2ec]246         bit = GRID;
[bd30612]247         break;
[02ba4c9]248       case OPT_SCALE: {
249         char* colon = strchr(optarg, ':');
250         if (!colon) {
251             /* --scale=1000 => 1:1000 => scale = 1000 */
252             scale = cmdline_double_arg();
253             if (scale < 1.0) {
254                 /* --scale=0.001 => 1:1000 => scale = 1000 */
255                 scale = 1.0 / scale;
256             }
257         } else if (colon - optarg == 1 && optarg[0] == '1') {
258             /* --scale=1:1000 => 1:1000 => scale = 1000 */
259             optarg += 2;
260             scale = cmdline_double_arg();
[f7fb2ec]261             optarg -= 2;
[02ba4c9]262         } else {
263             /* --scale=2:1000 => 1:500 => scale = 500 */
264             *colon = '\0';
265             scale = cmdline_double_arg();
266             optarg = colon + 1;
267             scale = cmdline_double_arg() / scale;
[f7fb2ec]268             *colon = ':';
[02ba4c9]269         }
[f7fb2ec]270         bit = SCALE;
[02ba4c9]271         break;
272       }
273       case OPT_BEARING: {
274         int units = 0;
275         size_t len = strlen(optarg);
276         if (len > 0) {
277             char ch = optarg[len - 1];
278             switch (ch) {
279                 case 'd':
280                 case 'g':
281                     units = ch;
282                     optarg[len - 1] = '\0';
283                     break;
284             }
[f7fb2ec]285             pan = cmdline_double_arg();
286             optarg[len - 1] = ch;
287         } else {
288             pan = cmdline_double_arg();
[02ba4c9]289         }
290         if (units == 'g') {
291             pan *= 0.9;
292         }
[40623c5]293         bit = ORIENTABLE;
[02ba4c9]294         break;
295       }
296       case OPT_TILT: {
297         int units = 0;
298         size_t len = strlen(optarg);
299         if (len > 0) {
300             char ch = optarg[len - 1];
301             switch (ch) {
302                 case '%':
303                 case 'd':
304                 case 'g':
305                     units = ch;
306                     optarg[len - 1] = '\0';
307                     break;
308             }
[f7fb2ec]309             tilt = cmdline_double_arg();
310             optarg[len - 1] = ch;
311         } else {
312             tilt = cmdline_double_arg();
[02ba4c9]313         }
314         if (units == 'g') {
315             tilt *= 0.9;
316         } else if (units == '%') {
317             tilt = deg(atan(tilt * 0.01));
318         }
[40623c5]319         bit = ORIENTABLE;
[02ba4c9]320         break;
321       }
322       case OPT_PLAN:
323         tilt = -90.0;
[40623c5]324         bit = ORIENTABLE;
[02ba4c9]325         break;
326       case OPT_ELEV:
327         tilt = 0.0;
[40623c5]328         bit = ORIENTABLE;
[bd30612]329         break;
330       case 't': /* Text height */
331         text_height = cmdline_double_arg();
[f7fb2ec]332         bit = TEXT_HEIGHT;
[bd30612]333         break;
334       case 'm': /* Marker size */
335         marker_size = cmdline_double_arg();
[f7fb2ec]336         bit = MARKER_SIZE;
[bd30612]337         break;
338       case 's':
[1a46879]339         if (survey) {
340             if (!filter) {
341                 filter = new SurveyFilter();
342                 filter->add(survey);
343             }
344             filter->add(optarg);
345         } else {
346             survey = optarg;
347         }
[bd30612]348         break;
[4d3c8915]349       default:
350         if (opt >= OPT_FMT_BASE && opt < OPT_FMT_BASE + FMT_MAX_PLUS_ONE_) {
351             format = export_format(opt - OPT_FMT_BASE);
352         }
[bd30612]353      }
[f7fb2ec]354      if (bit) {
355          show_mask |= bit;
356          int i = 0;
357          while (((bit >> i) & 1) == 0) ++i;
358
359          if (!optmap[i].empty()) optmap[i] += ' ';
360
361          // Reconstruct what the command line option was.
362          if (long_index < 0) {
363              optmap[i] += '-';
364              optmap[i] += char(opt);
365              if (optarg) {
366                  if (optarg == argv[optind - 1]) {
367                      optmap[i] += ' ';
368                  }
369                  optmap[i] += optarg;
370              }
371          } else {
372              optmap[i] += "--";
373              optmap[i] += long_opts[long_index].name;
374              if (optarg) {
375                  if (optarg == argv[optind - 1]) {
376                      optmap[i] += ' ';
377                  } else {
378                      optmap[i] += '=';
379                  }
380                  optmap[i] += optarg;
381              }
382          }
383      }
[bd30612]384   }
385
[1a46879]386   // A single --survey is handled by img at load-time.  Multiple --survey are
387   // handled via a SurveyFilter at export time.
388   if (filter) survey = NULL;
389
[bd30612]390   const char* fnm_in = argv[optind++];
391   const char* fnm_out = argv[optind];
392   if (fnm_out) {
[4d3c8915]393      if (format == FMT_MAX_PLUS_ONE_) {
394         // Select format based on extension.
[bd30612]395         size_t len = strlen(fnm_out);
[a901cea]396         // Length of longest extension of interest.
397         constexpr size_t MAX_EXT_LEN = 4;
398         char ext[MAX_EXT_LEN + 2];
399         for (size_t i = 0; i < MAX_EXT_LEN + 2; ++i) {
400             ext[i] = tolower((unsigned char)fnm_out[len - (MAX_EXT_LEN + 1) + i]);
401         }
[4d3c8915]402         for (size_t i = 0; i < FMT_MAX_PLUS_ONE_; ++i) {
403            const auto& info = export_format_info[i];
404            size_t l = strlen(info.extension);
[bd30612]405            if (len > l + 1 &&
[a901cea]406                strcmp(ext + MAX_EXT_LEN + 1 - l, info.extension) == 0) {
[bd30612]407               format = export_format(i);
408               break;
409            }
410         }
[4d3c8915]411         if (format == FMT_MAX_PLUS_ONE_) {
412            fatalerror(/*Export format not specified and not known from output file extension*/252);
413         }
[bd30612]414      }
415   } else {
[4d3c8915]416      if (format == FMT_MAX_PLUS_ONE_) {
417         fatalerror(/*Export format not specified*/253);
418      }
[bd30612]419      char *baseleaf = baseleaf_from_fnm(fnm_in);
420      /* note : memory allocated by fnm_out gets leaked in this case... */
[4d3c8915]421      fnm_out = add_ext(baseleaf, export_format_info[format].extension);
[bd30612]422      osfree(baseleaf);
423   }
424
[f7fb2ec]425   const auto& format_info_mask = export_format_info[format].mask;
426   unsigned not_allowed = show_mask &~ format_info_mask;
427   if (not_allowed) {
428       printf("warning: The following options are not supported for this export format and will be ignored:\n");
429       int i = 0;
[7e7433c]430       unsigned bit = 1;
[f7fb2ec]431       while (not_allowed) {
432           if (not_allowed & bit) {
433               // E.g. --walls maps to two bits in show_mask, but the options
434               // are only put on the least significant in such cases.
435               if (!optmap[i].empty())
436                   printf("%s\n", optmap[i].c_str());
437               not_allowed &= ~bit;
438           }
439           ++i;
440           bit <<= 1;
441       }
442       show_mask &= format_info_mask;
443   }
444
445   if (always_include_defaults || show_mask == 0) {
446       show_mask |= export_format_info[format].defaults;
447   }
448
[40623c5]449   if (!(format_info_mask & ORIENTABLE)) {
[f7fb2ec]450       pan = 0.0;
451       tilt = -90.0;
452   }
453
[bd30612]454   Model model;
455   int err = model.Load(fnm_in, survey);
456   if (err) fatalerror(err, fnm_in);
[1a46879]457   if (filter) filter->SetSeparator(model.GetSeparator());
[bd30612]458
[2907665]459   try {
460       if (!Export(fnm_out, model.GetSurveyTitle(),
461                   model.GetDateString(),
462                   model, filter,
463                   pan, tilt, show_mask, format,
464                   grid, text_height, marker_size,
465                   scale)) {
466          fatalerror(/*Couldn’t write file “%s”*/402, fnm_out);
467       }
468   } catch (const wxString & m) {
[a8d2349]469       wxString r = msg_appname();
470       r += ": ";
471       r += wmsg(/*error*/93);
472       r += ": ";
473       r += m;
474       wcerr << r.c_str() << '\n';
[bd30612]475   }
[2907665]476
[bd30612]477   return 0;
478}
Note: See TracBrowser for help on using the repository browser.