source: git/src/pos.cc @ d3fa693

faster-cavernloglog-selectstereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since d3fa693 was 4c83f84, checked in by Olly Betts <olly@…>, 15 months ago

Don't check HAVE_CONFIG_H in most cases

This check is only useful for img.c, which is intended to be usable
outside of Survex (and had fallbacks for functions which may not be
available which will get used if built in a non-autotools project).
For all the other source files it's just useless boilerplate.

  • 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
[4c83f84]21#include <config.h>
[775752c6]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 "osalloc.h"
34#include "useful.h"
35
36using namespace std;
37
[df121c2d]38static void
39csv_quote(const char* s, FILE* fh)
40{
41    size_t i = 0;
42    while (true) {
43        switch (s[i]) {
44            case '\0':
45                fputs(s, fh);
46                return;
47            case ',':
48            case '"':
49            case '\r':
50            case '\n':
51                break;
52        }
53        ++i;
54    }
55    PUTC('"', fh);
56    fwrite(s, i, 1, fh);
57    while (s[i]) {
58        // Double up any " in the string to escape them.
59        if (s[i] == '"')
60            PUTC(s[i], fh);
61        PUTC(s[i], fh);
62        ++i;
63    }
64    PUTC('"', fh);
65}
66
[775752c6]67POS::~POS()
68{
69    vector<pos_label*>::const_iterator i;
70    for (i = todo.begin(); i != todo.end(); ++i) {
[8553bdb]71        free(*i);
[775752c6]72    }
73    todo.clear();
74}
75
76const int *
77POS::passes() const
78{
79    static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, 0 };
80    return default_passes;
81}
82
83void POS::header(const char *, const char *, time_t,
84                 double, double, double, double, double, double)
85{
[df121c2d]86    if (csv) {
87        bool comma = false;
88        for (int msgno : { /*Easting*/378,
89                           /*Northing*/379,
90                           /*Altitude*/335,
91                           /*Station Name*/100 }) {
92            if (comma) PUTC(',', fh);
93            csv_quote(msg(msgno), fh);
94            comma = true;
95        }
96        PUTC('\n', fh);
97    } else {
98        /* TRANSLATORS: Heading line for .pos file.  Please try to ensure the
99         * “,”s (or at least the columns) are in the same place */
100        fputsnl(msg(/*( Easting, Northing, Altitude )*/195), fh);
101    }
[775752c6]102}
103
104void
[8c4cefb]105POS::label(const img_point *p, const wxString& str, bool /*fSurface*/, int /*type*/)
[775752c6]106{
[8c4cefb]107    const char* s = str.utf8_str();
[775752c6]108    size_t len = strlen(s);
[8553bdb]109    pos_label * l = (pos_label*)malloc(offsetof(pos_label, name) + len + 1);
110    if (l == NULL)
111        throw std::bad_alloc();
[775752c6]112    l->x = p->x;
113    l->y = p->y;
114    l->z = p->z;
115    memcpy(l->name, s, len + 1);
116    todo.push_back(l);
117}
118
[6936694]119class pos_label_ptr_cmp {
120    char separator;
121
122  public:
[fd4f6ff]123    explicit pos_label_ptr_cmp(char separator_) : separator(separator_) { }
[6936694]124
[775752c6]125    bool operator()(const POS::pos_label* a, const POS::pos_label* b) {
[6936694]126        return name_cmp(a->name, b->name, separator) < 0;
[775752c6]127    }
128};
129
130void
131POS::footer()
132{
[6936694]133    sort(todo.begin(), todo.end(), pos_label_ptr_cmp(separator));
[775752c6]134    vector<pos_label*>::const_iterator i;
135    for (i = todo.begin(); i != todo.end(); ++i) {
[df121c2d]136        if (csv) {
137            fprintf(fh, "%.2f,%.2f,%.2f,", (*i)->x, (*i)->y, (*i)->z);
138            csv_quote((*i)->name, fh);
139            PUTC('\n', fh);
140        } else {
141            fprintf(fh, "(%8.2f, %8.2f, %8.2f ) %s\n",
142                    (*i)->x, (*i)->y, (*i)->z, (*i)->name);
143        }
[775752c6]144    }
145}
Note: See TracBrowser for help on using the repository browser.