source: git/src/pos.cc

main v1.4.21
Last change on this file was c3bd62a, checked in by Olly Betts <olly@…>, 3 weeks ago

Allow exporting/not exporting anonymous stations

The default is chosen as most appropriate for each format.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/* pos.cc
2 * Export from Aven as Survex .pos or .csv.
3 */
4/* Copyright (C) 2001-2026 Olly Betts
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, see
18 * <https://www.gnu.org/licenses/>.
19 */
20
21#include <config.h>
22
23#include "pos.h"
24
25#include "export.h" // For LABELS, etc
26
27#include <algorithm>
28#include <stdio.h>
29#include <string.h>
30
31#include "message.h"
32#include "namecompare.h"
33#include "useful.h"
34
35using namespace std;
36
37static void
38csv_quote(const char* s, FILE* fh)
39{
40    size_t i = 0;
41    while (true) {
42        switch (s[i]) {
43            case '\0':
44                fputs(s, fh);
45                return;
46            case ',':
47            case '"':
48            case '\r':
49            case '\n':
50                break;
51        }
52        ++i;
53    }
54    PUTC('"', fh);
55    FWRITE_(s, i, 1, fh);
56    while (s[i]) {
57        // Double up any " in the string to escape them.
58        if (s[i] == '"')
59            PUTC(s[i], fh);
60        PUTC(s[i], fh);
61        ++i;
62    }
63    PUTC('"', fh);
64}
65
66POS::~POS()
67{
68    vector<pos_label*>::const_iterator i;
69    for (i = todo.begin(); i != todo.end(); ++i) {
70        free(*i);
71    }
72    todo.clear();
73}
74
75const int *
76POS::passes() const
77{
78    static const int default_passes[] = {
79        LABELS|ENTS|FIXES|EXPORTS,
80        ANON_STNS,
81        0
82    };
83    return default_passes;
84}
85
86void POS::header(const char *, time_t,
87                 double, double, double, double, double, double)
88{
89    if (csv) {
90        bool comma = false;
91        for (int msgno : { /*Easting*/378,
92                           /*Northing*/379,
93                           /*Altitude*/335,
94                           /*Station Name*/100 }) {
95            if (comma) PUTC(',', fh);
96            csv_quote(msg(msgno), fh);
97            comma = true;
98        }
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        fputs(msg(/*( Easting, Northing, Altitude )*/195), fh);
103    }
104    PUTC('\n', fh);
105}
106
107void
108POS::start_pass(int pass)
109{
110    if (pass == ANON_STNS && !todo.empty()) {
111        // Sort and write out all the named stations first.
112        footer();
113    }
114}
115
116void
117POS::label(const img_point *p, const wxString& str, int /*sflags*/, int /*type*/)
118{
119    // Named station.
120    const char* s = str.utf8_str();
121    size_t len = strlen(s);
122    pos_label * l = (pos_label*)malloc(offsetof(pos_label, name) + len + 1);
123    if (l == NULL)
124        throw std::bad_alloc();
125    l->x = p->x;
126    l->y = p->y;
127    l->z = p->z;
128    memcpy(l->name, s, len + 1);
129    todo.push_back(l);
130}
131
132void
133POS::cross(const img_point *p, const wxString&, int /*sflags*/)
134{
135    // Anonymous station.
136    if (csv) {
137        fprintf(fh, "%.2f,%.2f,%.2f,\n", p->x, p->y, p->z);
138    } else {
139        fprintf(fh, "(%8.2f, %8.2f, %8.2f )\n", p->x, p->y, p->z);
140    }
141}
142
143class pos_label_ptr_cmp {
144    char separator;
145
146  public:
147    explicit pos_label_ptr_cmp(char separator_) : separator(separator_) { }
148
149    bool operator()(const POS::pos_label* a, const POS::pos_label* b) {
150        return name_cmp(a->name, b->name, separator) < 0;
151    }
152};
153
154void
155POS::footer()
156{
157    sort(todo.begin(), todo.end(), pos_label_ptr_cmp(separator));
158    for (auto&& i = todo.begin(); i != todo.end(); ++i) {
159        if (csv) {
160            fprintf(fh, "%.2f,%.2f,%.2f,", (*i)->x, (*i)->y, (*i)->z);
161            csv_quote((*i)->name, fh);
162            PUTC('\n', fh);
163        } else {
164            fprintf(fh, "(%8.2f, %8.2f, %8.2f ) %s\n",
165                    (*i)->x, (*i)->y, (*i)->z, (*i)->name);
166        }
167    }
168    todo.clear();
169}
Note: See TracBrowser for help on using the repository browser.