source: git/src/survexport.cc @ 938d05e

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 938d05e was c0e4f18, checked in by Olly Betts <olly@…>, 5 years ago

Add TRANSLATORS comments for survexport help

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