[d06141c] | 1 | /* sorterr.c */ |
---|
| 2 | /* Sort a survex .err file */ |
---|
[736f7df] | 3 | /* Copyright (C) 2001,2002,2005,2010,2011,2014 Olly Betts |
---|
[d06141c] | 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d06141c] | 18 | */ |
---|
| 19 | |
---|
[4c83f84] | 20 | #include <config.h> |
---|
[d06141c] | 21 | |
---|
| 22 | #include <ctype.h> |
---|
| 23 | #include <math.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | |
---|
| 28 | #include "cmdline.h" |
---|
[fa42426] | 29 | #include "filename.h" |
---|
[bfe1242] | 30 | #include "message.h" |
---|
[d06141c] | 31 | #include "osalloc.h" |
---|
| 32 | |
---|
| 33 | static const struct option long_opts[] = { |
---|
| 34 | /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */ |
---|
| 35 | {"horizontal", no_argument, 0, 'h'}, |
---|
| 36 | {"vertical", no_argument, 0, 'v'}, |
---|
| 37 | {"percentage", no_argument, 0, 'p'}, |
---|
| 38 | {"per-leg", no_argument, 0, 'l'}, |
---|
[059d065] | 39 | {"replace", no_argument, 0, 'r'}, |
---|
[d06141c] | 40 | {"help", no_argument, 0, HLP_HELP}, |
---|
| 41 | {"version", no_argument, 0, HLP_VERSION}, |
---|
| 42 | {0, 0, 0, 0} |
---|
| 43 | }; |
---|
| 44 | |
---|
[059d065] | 45 | #define short_opts "hvplr" |
---|
[d06141c] | 46 | |
---|
| 47 | static struct help_msg help[] = { |
---|
| 48 | /* <-- */ |
---|
[736f7df] | 49 | /* TRANSLATORS: --help output for sorterr --horizontal option */ |
---|
[5a2d346] | 50 | {HLP_ENCODELONG(0), /*sort by horizontal error factor*/179, 0, 0}, |
---|
[736f7df] | 51 | /* TRANSLATORS: --help output for sorterr --vertical option */ |
---|
[5a2d346] | 52 | {HLP_ENCODELONG(1), /*sort by vertical error factor*/180, 0, 0}, |
---|
[736f7df] | 53 | /* TRANSLATORS: --help output for sorterr --percentage option */ |
---|
[5a2d346] | 54 | {HLP_ENCODELONG(2), /*sort by percentage error*/181, 0, 0}, |
---|
[736f7df] | 55 | /* TRANSLATORS: --help output for sorterr --per-leg option */ |
---|
[5a2d346] | 56 | {HLP_ENCODELONG(3), /*sort by error per leg*/182, 0, 0}, |
---|
[736f7df] | 57 | /* TRANSLATORS: --help output for sorterr --replace option */ |
---|
[5a2d346] | 58 | {HLP_ENCODELONG(4), /*replace .err file with re-sorted version*/183, 0, 0}, |
---|
| 59 | {0, 0, 0, 0} |
---|
[d06141c] | 60 | }; |
---|
| 61 | |
---|
| 62 | typedef struct { |
---|
| 63 | double err; |
---|
| 64 | long fpos; |
---|
| 65 | } trav; |
---|
| 66 | |
---|
| 67 | static void |
---|
[dcf6125] | 68 | skipline(const char *fnm, FILE *fh) |
---|
[d06141c] | 69 | { |
---|
| 70 | int ch; |
---|
| 71 | do { |
---|
[e02a6a6] | 72 | ch = GETC(fh); |
---|
[d06141c] | 73 | } while (ch != '\n' && ch != EOF); |
---|
[dcf6125] | 74 | |
---|
[d06141c] | 75 | if (ch == EOF) { |
---|
[c0f9e1e] | 76 | if (FERROR(fh)) |
---|
[dcf6125] | 77 | fatalerror_in_file(fnm, 0, /*Error reading file*/18); |
---|
[ee7511a] | 78 | fatalerror_in_file(fnm, 0, /*Couldn’t parse .err file*/112); |
---|
[d06141c] | 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | static void |
---|
[dcf6125] | 83 | printline(const char *fnm, FILE *fh, FILE *fh_out) |
---|
[d06141c] | 84 | { |
---|
| 85 | int ch; |
---|
| 86 | do { |
---|
[e02a6a6] | 87 | ch = GETC(fh); |
---|
| 88 | if (ch != EOF && ch != '\r' && ch != '\n') PUTC(ch, fh_out); |
---|
[d06141c] | 89 | } while (ch != '\n' && ch != EOF); |
---|
[e02a6a6] | 90 | PUTC('\n', fh_out); |
---|
[dcf6125] | 91 | |
---|
[d06141c] | 92 | if (ch == EOF) { |
---|
[c0f9e1e] | 93 | if (FERROR(fh)) |
---|
[dcf6125] | 94 | fatalerror_in_file(fnm, 0, /*Error reading file*/18); |
---|
[ee7511a] | 95 | fatalerror_in_file(fnm, 0, /*Couldn’t parse .err file*/112); |
---|
[d06141c] | 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | static int |
---|
| 100 | cmp_trav(const void *a, const void *b) |
---|
| 101 | { |
---|
| 102 | double diff = ((const trav *)a)->err - ((const trav *)b)->err; |
---|
| 103 | if (diff < 0) return -1; |
---|
| 104 | if (diff > 0) return 1; |
---|
| 105 | return 0; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | int |
---|
| 109 | main(int argc, char **argv) |
---|
| 110 | { |
---|
| 111 | char *fnm; |
---|
| 112 | FILE *fh; |
---|
| 113 | char sortby = 'A'; |
---|
| 114 | size_t len = 1024; |
---|
| 115 | trav *blk = osmalloc(1024 * sizeof(trav)); |
---|
| 116 | size_t next = 0; |
---|
| 117 | size_t howmany = 0; |
---|
[059d065] | 118 | FILE *fh_out = stdout; |
---|
| 119 | char *fnm_out = NULL; |
---|
[d06141c] | 120 | |
---|
[bdfe97f] | 121 | msg_init(argv); |
---|
[d06141c] | 122 | |
---|
[736f7df] | 123 | /* TRANSLATORS: Part of sorterr --help */ |
---|
[a7b5554] | 124 | cmdline_set_syntax_message(/*ERR_FILE [HOW_MANY]*/268, 0, NULL); |
---|
[d06141c] | 125 | cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2); |
---|
| 126 | while (1) { |
---|
| 127 | int opt = cmdline_getopt(); |
---|
| 128 | if (opt == EOF) break; |
---|
| 129 | switch (opt) { |
---|
| 130 | case 'h': case 'v': case 'p': case 'l': |
---|
| 131 | sortby = toupper(opt); |
---|
| 132 | break; |
---|
[059d065] | 133 | case 'r': |
---|
| 134 | fh_out = NULL; |
---|
| 135 | break; |
---|
[d06141c] | 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | fnm = argv[optind++]; |
---|
| 140 | if (argv[optind]) howmany = atoi(argv[optind]); |
---|
| 141 | |
---|
| 142 | fh = fopen(fnm, "rb"); |
---|
[7962c9d] | 143 | if (!fh) fatalerror(/*Couldn’t open file “%s”*/1, fnm); |
---|
[d06141c] | 144 | |
---|
| 145 | /* 4 line paragraphs, separated by blank lines... |
---|
| 146 | * 041.verhall.12 - 041.verhall.13 |
---|
| 147 | * Original length 2.97m ( 1 legs), moved 0.04m ( 0.04m/leg). Error 1.19% |
---|
| 148 | * 0.222332 |
---|
| 149 | * H: 0.224749 V: 0.215352 |
---|
| 150 | * |
---|
| 151 | */ |
---|
| 152 | while (1) { |
---|
| 153 | int ch; |
---|
| 154 | if (next == len) { |
---|
| 155 | len += len; |
---|
[dd5a89c] | 156 | blk = osrealloc(blk, len * ossizeof(trav)); |
---|
[d06141c] | 157 | } |
---|
[e03662e] | 158 | blk[next].fpos = ftell(fh); |
---|
[e02a6a6] | 159 | ch = GETC(fh); |
---|
[d06141c] | 160 | if (ch == EOF) break; |
---|
[dcf6125] | 161 | skipline(fnm, fh); |
---|
[d06141c] | 162 | switch (sortby) { |
---|
| 163 | case 'A': |
---|
[dcf6125] | 164 | skipline(fnm, fh); |
---|
[d06141c] | 165 | if (fscanf(fh, "%lf", &blk[next].err) != 1) { |
---|
| 166 | baderrfile: |
---|
[4340dae] | 167 | fatalerror_in_file(fnm, 0, /*Couldn’t parse .err file*/112); |
---|
[d06141c] | 168 | } |
---|
[dcf6125] | 169 | skipline(fnm, fh); |
---|
| 170 | skipline(fnm, fh); |
---|
[d06141c] | 171 | break; |
---|
| 172 | case 'H': case 'V': |
---|
[dcf6125] | 173 | skipline(fnm, fh); |
---|
| 174 | skipline(fnm, fh); |
---|
[d06141c] | 175 | do { |
---|
[e02a6a6] | 176 | ch = GETC(fh); |
---|
[d06141c] | 177 | if (ch == '\n' || ch == EOF) goto baderrfile; |
---|
| 178 | } while (ch != sortby); |
---|
| 179 | if (fscanf(fh, ":%lf", &blk[next].err) != 1) goto baderrfile; |
---|
[dcf6125] | 180 | skipline(fnm, fh); |
---|
[d06141c] | 181 | break; |
---|
| 182 | case 'P': |
---|
| 183 | do { |
---|
[e02a6a6] | 184 | ch = GETC(fh); |
---|
[d06141c] | 185 | if (ch == '\n' || ch == EOF) goto baderrfile; |
---|
| 186 | } while (ch != ')'); |
---|
| 187 | do { |
---|
[e02a6a6] | 188 | ch = GETC(fh); |
---|
[d06141c] | 189 | if (ch == '\n' || ch == EOF) goto baderrfile; |
---|
| 190 | } while (ch != ')'); |
---|
| 191 | do { |
---|
[e02a6a6] | 192 | ch = GETC(fh); |
---|
[d06141c] | 193 | if (ch == '\n' || ch == EOF) goto baderrfile; |
---|
| 194 | } while (!isdigit(ch)); |
---|
| 195 | ungetc(ch, fh); |
---|
| 196 | if (fscanf(fh, "%lf", &blk[next].err) != 1) goto baderrfile; |
---|
[dcf6125] | 197 | skipline(fnm, fh); |
---|
| 198 | skipline(fnm, fh); |
---|
| 199 | skipline(fnm, fh); |
---|
[d06141c] | 200 | break; |
---|
| 201 | case 'L': |
---|
| 202 | do { |
---|
[e02a6a6] | 203 | ch = GETC(fh); |
---|
[d06141c] | 204 | if (ch == '\n' || ch == EOF) goto baderrfile; |
---|
| 205 | } while (ch != ')'); |
---|
| 206 | do { |
---|
[e02a6a6] | 207 | ch = GETC(fh); |
---|
[d06141c] | 208 | if (ch == '\n' || ch == EOF) goto baderrfile; |
---|
| 209 | } while (ch != '('); |
---|
| 210 | if (fscanf(fh, "%lf", &blk[next].err) != 1) goto baderrfile; |
---|
[dcf6125] | 211 | skipline(fnm, fh); |
---|
| 212 | skipline(fnm, fh); |
---|
| 213 | skipline(fnm, fh); |
---|
[d06141c] | 214 | break; |
---|
| 215 | } |
---|
[dcf6125] | 216 | skipline(fnm, fh); |
---|
[d06141c] | 217 | next++; |
---|
| 218 | } |
---|
| 219 | |
---|
[5baea35] | 220 | if (next == 0) { |
---|
| 221 | /* no entries - nothing more to do whether -r is specified or not */ |
---|
| 222 | exit(EXIT_SUCCESS); |
---|
| 223 | } |
---|
| 224 | |
---|
[d06141c] | 225 | qsort(blk, next, sizeof(trav), cmp_trav); |
---|
| 226 | |
---|
[059d065] | 227 | if (fh_out == NULL) { |
---|
| 228 | char *base = base_from_fnm(fnm); |
---|
| 229 | fnm_out = add_ext(base, "tmp"); |
---|
| 230 | osfree(base); |
---|
[893cc20] | 231 | fh_out = safe_fopen(fnm_out, "w"); |
---|
[059d065] | 232 | } |
---|
| 233 | |
---|
[d06141c] | 234 | do { |
---|
| 235 | --next; |
---|
[908298d] | 236 | if (fseek(fh, blk[next].fpos, SEEK_SET) == -1) |
---|
| 237 | fatalerror_in_file(fnm, 0, /*Error reading file*/18); |
---|
| 238 | |
---|
[dcf6125] | 239 | printline(fnm, fh, fh_out); |
---|
| 240 | printline(fnm, fh, fh_out); |
---|
| 241 | printline(fnm, fh, fh_out); |
---|
| 242 | printline(fnm, fh, fh_out); |
---|
[e02a6a6] | 243 | PUTC('\n', fh_out); |
---|
[d06141c] | 244 | if (howmany && --howmany == 0) break; |
---|
| 245 | } while (next); |
---|
[e5d7e205] | 246 | fclose(fh); |
---|
[d06141c] | 247 | |
---|
[059d065] | 248 | if (fnm_out) { |
---|
[f1067a2] | 249 | safe_fclose(fh_out); |
---|
[07a6d16] | 250 | #ifdef _WIN32 |
---|
[1d89197] | 251 | /* UNIX rename atomically replaces, so doesn't need this. |
---|
| 252 | * WIN32 won't overwrite (from tests) so needs this code. |
---|
| 253 | */ |
---|
[893cc20] | 254 | remove(fnm); |
---|
| 255 | #endif |
---|
[059d065] | 256 | rename(fnm_out, fnm); |
---|
| 257 | } |
---|
| 258 | |
---|
[d06141c] | 259 | return 0; |
---|
| 260 | } |
---|