[775752c6] | 1 | /* pos.cc |
---|
[df121c2d] | 2 | * Export from Aven as Survex .pos or .csv. |
---|
[775752c6] | 3 | */ |
---|
[371385f] | 4 | /* Copyright (C) 2001-2024 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 "useful.h" |
---|
| 34 | |
---|
| 35 | using namespace std; |
---|
| 36 | |
---|
[df121c2d] | 37 | static void |
---|
| 38 | csv_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); |
---|
[87c9067] | 55 | FWRITE_(s, i, 1, fh); |
---|
[df121c2d] | 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 | |
---|
[775752c6] | 66 | POS::~POS() |
---|
| 67 | { |
---|
| 68 | vector<pos_label*>::const_iterator i; |
---|
| 69 | for (i = todo.begin(); i != todo.end(); ++i) { |
---|
[8553bdb] | 70 | free(*i); |
---|
[775752c6] | 71 | } |
---|
| 72 | todo.clear(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | const int * |
---|
| 76 | POS::passes() const |
---|
| 77 | { |
---|
| 78 | static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, 0 }; |
---|
| 79 | return default_passes; |
---|
| 80 | } |
---|
| 81 | |
---|
[696db01] | 82 | void POS::header(const char *, time_t, |
---|
[775752c6] | 83 | double, double, double, double, double, double) |
---|
| 84 | { |
---|
[df121c2d] | 85 | if (csv) { |
---|
| 86 | bool comma = false; |
---|
| 87 | for (int msgno : { /*Easting*/378, |
---|
| 88 | /*Northing*/379, |
---|
| 89 | /*Altitude*/335, |
---|
| 90 | /*Station Name*/100 }) { |
---|
| 91 | if (comma) PUTC(',', fh); |
---|
| 92 | csv_quote(msg(msgno), fh); |
---|
| 93 | comma = true; |
---|
| 94 | } |
---|
| 95 | } else { |
---|
| 96 | /* TRANSLATORS: Heading line for .pos file. Please try to ensure the |
---|
| 97 | * “,”s (or at least the columns) are in the same place */ |
---|
[b632a67] | 98 | fputs(msg(/*( Easting, Northing, Altitude )*/195), fh); |
---|
[df121c2d] | 99 | } |
---|
[b632a67] | 100 | PUTC('\n', fh); |
---|
[775752c6] | 101 | } |
---|
| 102 | |
---|
| 103 | void |
---|
[371385f] | 104 | POS::label(const img_point *p, const wxString& str, int /*sflags*/, int /*type*/) |
---|
[775752c6] | 105 | { |
---|
[8c4cefb] | 106 | const char* s = str.utf8_str(); |
---|
[775752c6] | 107 | size_t len = strlen(s); |
---|
[8553bdb] | 108 | pos_label * l = (pos_label*)malloc(offsetof(pos_label, name) + len + 1); |
---|
| 109 | if (l == NULL) |
---|
| 110 | throw std::bad_alloc(); |
---|
[775752c6] | 111 | l->x = p->x; |
---|
| 112 | l->y = p->y; |
---|
| 113 | l->z = p->z; |
---|
| 114 | memcpy(l->name, s, len + 1); |
---|
| 115 | todo.push_back(l); |
---|
| 116 | } |
---|
| 117 | |
---|
[6936694] | 118 | class pos_label_ptr_cmp { |
---|
| 119 | char separator; |
---|
| 120 | |
---|
| 121 | public: |
---|
[fd4f6ff] | 122 | explicit pos_label_ptr_cmp(char separator_) : separator(separator_) { } |
---|
[6936694] | 123 | |
---|
[775752c6] | 124 | bool operator()(const POS::pos_label* a, const POS::pos_label* b) { |
---|
[6936694] | 125 | return name_cmp(a->name, b->name, separator) < 0; |
---|
[775752c6] | 126 | } |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | void |
---|
| 130 | POS::footer() |
---|
| 131 | { |
---|
[6936694] | 132 | sort(todo.begin(), todo.end(), pos_label_ptr_cmp(separator)); |
---|
[775752c6] | 133 | vector<pos_label*>::const_iterator i; |
---|
| 134 | for (i = todo.begin(); i != todo.end(); ++i) { |
---|
[df121c2d] | 135 | if (csv) { |
---|
| 136 | fprintf(fh, "%.2f,%.2f,%.2f,", (*i)->x, (*i)->y, (*i)->z); |
---|
| 137 | csv_quote((*i)->name, fh); |
---|
| 138 | PUTC('\n', fh); |
---|
| 139 | } else { |
---|
| 140 | fprintf(fh, "(%8.2f, %8.2f, %8.2f ) %s\n", |
---|
| 141 | (*i)->x, (*i)->y, (*i)->z, (*i)->name); |
---|
| 142 | } |
---|
[775752c6] | 143 | } |
---|
| 144 | } |
---|