source: git/src/sorterr.c @ 45af761

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 45af761 was 45af761, checked in by Olly Betts <olly@…>, 13 years ago

lib/,src/: Extract all the help strings in --help output and make
them available for translation.

git-svn-id: file:///home/survex-svn/survex/trunk@3741 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/* sorterr.c */
2/* Sort a survex .err file */
3/* Copyright (C) 2001,2002,2005,2010,2011 Olly Betts
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <ctype.h>
25#include <math.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "cmdline.h"
31#include "filename.h"
32#include "message.h"
33#include "osalloc.h"
34#include "whichos.h"
35
36static const struct option long_opts[] = {
37   /* const char *name; int has_arg (0 no_argument, 1 required_*, 2 optional_*); int *flag; int val; */
38   {"horizontal", no_argument, 0, 'h'},
39   {"vertical", no_argument, 0, 'v'},
40   {"percentage", no_argument, 0, 'p'},
41   {"per-leg", no_argument, 0, 'l'},
42   {"replace", no_argument, 0, 'r'},
43   {"help", no_argument, 0, HLP_HELP},
44   {"version", no_argument, 0, HLP_VERSION},
45   {0, 0, 0, 0}
46};
47
48#define short_opts "hvplr"
49
50static struct help_msg help[] = {
51/*                              <-- */
52   {HLP_ENCODELONG(0),        /*sort by horizontal error factor*/179, 0},
53   {HLP_ENCODELONG(1),        /*sort by vertical error factor*/180, 0},
54   {HLP_ENCODELONG(2),        /*sort by percentage error*/181, 0},
55   {HLP_ENCODELONG(3),        /*sort by error per leg*/182, 0},
56   {HLP_ENCODELONG(4),        /*replace .err file with resorted version*/183, 0},
57   {0, 0, 0}
58};
59
60typedef struct {
61   double err;
62   long fpos;
63} trav;
64
65static void
66skipline(const char *fnm, FILE *fh)
67{
68   int ch;
69   do {
70      ch = GETC(fh);
71   } while (ch != '\n' && ch != EOF);
72
73   if (ch == EOF) {
74      if (ferror(fh))
75         fatalerror_in_file(fnm, 0, /*Error reading file*/18);
76      fatalerror_in_file(fnm, 0, /*Couldn't parse .err file*/112);
77   }
78}
79
80static void
81printline(const char *fnm, FILE *fh, FILE *fh_out)
82{
83   int ch;
84   do {
85      ch = GETC(fh);
86      if (ch != EOF && ch != '\r' && ch != '\n') PUTC(ch, fh_out);
87   } while (ch != '\n' && ch != EOF);
88   PUTC('\n', fh_out);
89
90   if (ch == EOF) {
91      if (ferror(fh))
92         fatalerror_in_file(fnm, 0, /*Error reading file*/18);
93      fatalerror_in_file(fnm, 0, /*Couldn't parse .err file*/112);
94   }
95}
96
97static int
98cmp_trav(const void *a, const void *b)
99{
100   double diff = ((const trav *)a)->err - ((const trav *)b)->err;
101   if (diff < 0) return -1;
102   if (diff > 0) return 1;
103   return 0;
104}
105
106int
107main(int argc, char **argv)
108{
109   char *fnm;
110   FILE *fh;
111   char sortby = 'A';
112   size_t len = 1024;
113   trav *blk = osmalloc(1024 * sizeof(trav));
114   size_t next = 0;
115   size_t howmany = 0;
116   FILE *fh_out = stdout;
117   char *fnm_out = NULL;
118
119   msg_init(argv);
120
121   cmdline_set_syntax_message("ERR_FILE [HOW MANY]", NULL); /* TRANSLATE */
122   cmdline_init(argc, argv, short_opts, long_opts, NULL, help, 1, 2);
123   while (1) {
124      int opt = cmdline_getopt();
125      if (opt == EOF) break;
126      switch (opt) {
127       case 'h': case 'v': case 'p': case 'l':
128         sortby = toupper(opt);
129         break;
130       case 'r':
131         fh_out = NULL;
132         break;
133      }
134   }
135
136   fnm = argv[optind++];
137   if (argv[optind]) howmany = atoi(argv[optind]);
138
139   fh = fopen(fnm, "rb");
140   if (!fh) fatalerror(/*Couldn't open file `%s'*/93, fnm);
141
142   /* 4 line paragraphs, separated by blank lines...
143    * 041.verhall.12 - 041.verhall.13
144    * Original length   2.97m (  1 legs), moved   0.04m ( 0.04m/leg). Error   1.19%
145    * 0.222332
146    * H: 0.224749 V: 0.215352
147    *
148    */
149   while (1) {
150      int ch;
151      if (next == len) {
152         len += len;
153         blk = osrealloc(blk, len * ossizeof(trav));
154      }
155      blk[next].fpos = ftell(fh);
156      ch = GETC(fh);
157      if (ch == EOF) break;
158      skipline(fnm, fh);
159      switch (sortby) {
160       case 'A':
161         skipline(fnm, fh);
162         if (fscanf(fh, "%lf", &blk[next].err) != 1) {
163            baderrfile:
164            fatalerror_in_file(fnm, 0, /*Couldn't parse .err file*/112);
165         }
166         skipline(fnm, fh);
167         skipline(fnm, fh);
168         break;
169       case 'H': case 'V':
170         skipline(fnm, fh);
171         skipline(fnm, fh);
172         do {
173            ch = GETC(fh);
174            if (ch == '\n' || ch == EOF) goto baderrfile;
175         } while (ch != sortby);
176         if (fscanf(fh, ":%lf", &blk[next].err) != 1) goto baderrfile;
177         skipline(fnm, fh);
178         break;
179       case 'P':
180         do {
181            ch = GETC(fh);
182            if (ch == '\n' || ch == EOF) goto baderrfile;
183         } while (ch != ')');
184         do {
185            ch = GETC(fh);
186            if (ch == '\n' || ch == EOF) goto baderrfile;
187         } while (ch != ')');
188         do {
189            ch = GETC(fh);
190            if (ch == '\n' || ch == EOF) goto baderrfile;
191         } while (!isdigit(ch));
192         ungetc(ch, fh);
193         if (fscanf(fh, "%lf", &blk[next].err) != 1) goto baderrfile;
194         skipline(fnm, fh);
195         skipline(fnm, fh);
196         skipline(fnm, fh);
197         break;
198       case 'L':
199         do {
200            ch = GETC(fh);
201            if (ch == '\n' || ch == EOF) goto baderrfile;
202         } while (ch != ')');
203         do {
204            ch = GETC(fh);
205            if (ch == '\n' || ch == EOF) goto baderrfile;
206         } while (ch != '(');
207         if (fscanf(fh, "%lf", &blk[next].err) != 1) goto baderrfile;
208         skipline(fnm, fh);
209         skipline(fnm, fh);
210         skipline(fnm, fh);
211         break;
212      }
213      skipline(fnm, fh);
214      next++;
215   }
216
217   if (next == 0) {
218      /* no entries - nothing more to do whether -r is specified or not */
219      exit(EXIT_SUCCESS);
220   }
221
222   qsort(blk, next, sizeof(trav), cmp_trav);
223
224   if (fh_out == NULL) {
225      char *base = base_from_fnm(fnm);
226      fnm_out = add_ext(base, "tmp");
227      osfree(base);
228      fh_out = safe_fopen(fnm_out, "w");
229   }
230
231   do {
232      --next;
233      if (fseek(fh, blk[next].fpos, SEEK_SET) == -1)
234         fatalerror_in_file(fnm, 0, /*Error reading file*/18);
235
236      printline(fnm, fh, fh_out);
237      printline(fnm, fh, fh_out);
238      printline(fnm, fh, fh_out);
239      printline(fnm, fh, fh_out);
240      PUTC('\n', fh_out);
241      if (howmany && --howmany == 0) break;
242   } while (next);
243   fclose(fh);
244
245   if (fnm_out) {
246      safe_fclose(fh_out);
247#if OS_WIN32
248      /* UNIX rename atomically replaces, so doesn't need this.
249       * WIN32 won't overwrite (from tests) so needs this code.
250       */
251      remove(fnm);
252#endif
253      rename(fnm_out, fnm);
254   }
255
256   return 0;
257}
Note: See TracBrowser for help on using the repository browser.