[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 | |
---|
| 38 | using namespace std; |
---|
| 39 | |
---|
[df121c2d] | 40 | static void |
---|
| 41 | csv_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] | 69 | POS::~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 | |
---|
| 78 | const int * |
---|
| 79 | POS::passes() const |
---|
| 80 | { |
---|
| 81 | static const int default_passes[] = { LABELS|ENTS|FIXES|EXPORTS, 0 }; |
---|
| 82 | return default_passes; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void 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 | |
---|
| 106 | void |
---|
| 107 | POS::label(const img_point *p, const char *s, bool /*fSurface*/, int /*type*/) |
---|
| 108 | { |
---|
| 109 | size_t len = strlen(s); |
---|
[8553bdb] | 110 | pos_label * l = (pos_label*)malloc(offsetof(pos_label, name) + len + 1); |
---|
| 111 | if (l == NULL) |
---|
| 112 | throw std::bad_alloc(); |
---|
[775752c6] | 113 | l->x = p->x; |
---|
| 114 | l->y = p->y; |
---|
| 115 | l->z = p->z; |
---|
| 116 | memcpy(l->name, s, len + 1); |
---|
| 117 | todo.push_back(l); |
---|
| 118 | } |
---|
| 119 | |
---|
[6936694] | 120 | class pos_label_ptr_cmp { |
---|
| 121 | char separator; |
---|
| 122 | |
---|
| 123 | public: |
---|
[fd4f6ff] | 124 | explicit pos_label_ptr_cmp(char separator_) : separator(separator_) { } |
---|
[6936694] | 125 | |
---|
[775752c6] | 126 | bool operator()(const POS::pos_label* a, const POS::pos_label* b) { |
---|
[6936694] | 127 | return name_cmp(a->name, b->name, separator) < 0; |
---|
[775752c6] | 128 | } |
---|
| 129 | }; |
---|
| 130 | |
---|
| 131 | void |
---|
| 132 | POS::footer() |
---|
| 133 | { |
---|
[6936694] | 134 | sort(todo.begin(), todo.end(), pos_label_ptr_cmp(separator)); |
---|
[775752c6] | 135 | vector<pos_label*>::const_iterator i; |
---|
| 136 | for (i = todo.begin(); i != todo.end(); ++i) { |
---|
[df121c2d] | 137 | if (csv) { |
---|
| 138 | fprintf(fh, "%.2f,%.2f,%.2f,", (*i)->x, (*i)->y, (*i)->z); |
---|
| 139 | csv_quote((*i)->name, fh); |
---|
| 140 | PUTC('\n', fh); |
---|
| 141 | } else { |
---|
| 142 | fprintf(fh, "(%8.2f, %8.2f, %8.2f ) %s\n", |
---|
| 143 | (*i)->x, (*i)->y, (*i)->z, (*i)->name); |
---|
| 144 | } |
---|
[775752c6] | 145 | } |
---|
| 146 | } |
---|