source: git/src/pos.cc @ 04078a7

faster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 04078a7 was 8c4cefb, checked in by Olly Betts <olly@…>, 17 months ago

Pass station name to export code as wxString

This means we don't force a conversion to UTF8 for formats where
the name isn't actually used, and also means we can pass the name
in to the cross method too without worrying about extra costs.

This fixes poor handling of equated stations in SVG export where
previously we'd write out the same station name for each equated
station. SVG export is also more efficient than before.

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[775752c6]1/* pos.cc
[df121c2d]2 * Export from Aven as Survex .pos or .csv.
[775752c6]3 */
[8553bdb]4/* Copyright (C) 2001,2002,2011,2013,2014,2015,2018 Olly Betts
[775752c6]5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include "pos.h"
26
27#include "export.h" // For LABELS, etc
28
29#include <algorithm>
30#include <stdio.h>
31#include <string.h>
32
33#include "message.h"
34#include "namecompare.h"
35#include "osalloc.h"
36#include "useful.h"
37
38using namespace std;
39
[df121c2d]40static void
41csv_quote(const char* s, FILE* fh)
42{
43    size_t i = 0;
44    while (true) {
45        switch (s[i]) {
46            case '\0':
47                fputs(s, fh);
48                return;
49            case ',':
50            case '"':
51            case '\r':
52            case '\n':
53                break;
54        }
55        ++i;
56    }
57    PUTC('"', fh);
58    fwrite(s, i, 1, fh);
59    while (s[i]) {
60        // Double up any " in the string to escape them.
61        if (s[i] == '"')
62            PUTC(s[i], fh);
63        PUTC(s[i], fh);
64        ++i;
65    }
66    PUTC('"', fh);
67}
68
[775752c6]69POS::~POS()
70{
71    vector<pos_label*>::const_iterator i;
72    for (i = todo.begin(); i != todo.end(); ++i) {
[8553bdb]73        free(*i);
[775752c6]74    }
75    todo.clear();
76}
77
78const int *
79POS::passes() const
80{
81    static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, 0 };
82    return default_passes;
83}
84
85void POS::header(const char *, const char *, time_t,
86                 double, double, double, double, double, double)
87{
[df121c2d]88    if (csv) {
89        bool comma = false;
90        for (int msgno : { /*Easting*/378,
91                           /*Northing*/379,
92                           /*Altitude*/335,
93                           /*Station Name*/100 }) {
94            if (comma) PUTC(',', fh);
95            csv_quote(msg(msgno), fh);
96            comma = true;
97        }
98        PUTC('\n', fh);
99    } else {
100        /* TRANSLATORS: Heading line for .pos file.  Please try to ensure the
101         * “,”s (or at least the columns) are in the same place */
102        fputsnl(msg(/*( Easting, Northing, Altitude )*/195), fh);
103    }
[775752c6]104}
105
106void
[8c4cefb]107POS::label(const img_point *p, const wxString& str, bool /*fSurface*/, int /*type*/)
[775752c6]108{
[8c4cefb]109    const char* s = str.utf8_str();
[775752c6]110    size_t len = strlen(s);
[8553bdb]111    pos_label * l = (pos_label*)malloc(offsetof(pos_label, name) + len + 1);
112    if (l == NULL)
113        throw std::bad_alloc();
[775752c6]114    l->x = p->x;
115    l->y = p->y;
116    l->z = p->z;
117    memcpy(l->name, s, len + 1);
118    todo.push_back(l);
119}
120
[6936694]121class pos_label_ptr_cmp {
122    char separator;
123
124  public:
[fd4f6ff]125    explicit pos_label_ptr_cmp(char separator_) : separator(separator_) { }
[6936694]126
[775752c6]127    bool operator()(const POS::pos_label* a, const POS::pos_label* b) {
[6936694]128        return name_cmp(a->name, b->name, separator) < 0;
[775752c6]129    }
130};
131
132void
133POS::footer()
134{
[6936694]135    sort(todo.begin(), todo.end(), pos_label_ptr_cmp(separator));
[775752c6]136    vector<pos_label*>::const_iterator i;
137    for (i = todo.begin(); i != todo.end(); ++i) {
[df121c2d]138        if (csv) {
139            fprintf(fh, "%.2f,%.2f,%.2f,", (*i)->x, (*i)->y, (*i)->z);
140            csv_quote((*i)->name, fh);
141            PUTC('\n', fh);
142        } else {
143            fprintf(fh, "(%8.2f, %8.2f, %8.2f ) %s\n",
144                    (*i)->x, (*i)->y, (*i)->z, (*i)->name);
145        }
[775752c6]146    }
147}
Note: See TracBrowser for help on using the repository browser.