File: tide.cc

package info (click to toggle)
xtide 2.6.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,996 kB
  • ctags: 2,617
  • sloc: cpp: 26,266; ansic: 8,105; makefile: 152; yacc: 113; sh: 54; lex: 54
file content (346 lines) | stat: -rw-r--r-- 9,528 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// $Id: tide.cc,v 1.5 2002/12/19 18:40:45 flaterco Exp $
/*  tide  Command-line client for dusty old TTYs and line printers.

    Copyright (C) 1998  David Flater.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "common.hh"

#define fmtbarf { \
        Dstr details ("Can't do format "); \
        details += format; \
        details += " in mode "; \
        details += mode; \
        barf (BAD_FORMAT, details); \
      }

void
do_location (const Dstr &name, TideContext *context, Interval step,
FILE *outfp, char mode, char format) {
  StationRef *sr;
  CommandLineSettings *cls = context->cls;
  Settings *settings = context->settings;
  sr = (*(context->stationIndex()))[name];
  if (sr) {
    Station *s = sr->load(context);

    // Install mark level, if applicable.
    if (!(cls->ml_isnull)) {
      s->markLevel = new PredictionValue (cls->ml);
      if (cls->ml.Units() != s->myUnits)
        s->markLevel->Units (s->myUnits);
    }

    // Find start and end times.
    // This needs to be done here due to the timezones.
    Timestamp startt, endt;
    if (cls->b.isNull())
      startt = Timestamp ((time_t)(time(NULL)));
    else {
      startt = Timestamp (cls->b, sr->timezone, context->settings);
      if (startt.isNull()) {
	Dstr details ("The offending input was ");
	details += cls->b;
	details += "\nin the time zone ";
	if (context->settings->z == "n")
	  details += sr->timezone;
	else
	  details += "UTC0";
	barf (MKTIME_FAILED, details);
      }
    }
    if (cls->e.isNull())
      endt = startt + Interval(defpredictinterval);
    else {
      endt = Timestamp (cls->e, sr->timezone, context->settings);
      if (endt.isNull()) {
	Dstr details ("The offending input was ");
	details += cls->e;
	details += "\nin the time zone ";
	if (context->settings->z == "n")
	  details += sr->timezone;
	else
	  details += "UTC0";
	barf (MKTIME_FAILED, details);
      }
    }
    if (startt > endt) {
      Timestamp tt = startt;
      startt = endt;
      endt = tt;
    }

    switch (mode) {
    case 'p':
      if (format != 't')
        fmtbarf;
      {
        Dstr text_out;
        context->textMode (s, startt, endt, text_out);
        fprintf (outfp, "%s", text_out.aschar());
      }
      break;

    case 's':
      if (format != 't')
        fmtbarf;
      {
        Dstr text_out;
        context->statsMode (s, startt, endt, text_out);
        fprintf (outfp, "%s", text_out.aschar());
      }
      break;

    case 'c':
    case 'C':
      switch (format) {
      case 'h':
      case 't':
        {
          Dstr text_out;
          context->calendarMode (s, startt, endt, text_out, (format=='h'),
            (mode=='C'));
          fprintf (outfp, "%s", text_out.aschar());
        }
        break;
      default:
        fmtbarf;
      }
      break;

    case 'r':
      if (format != 't')
        fmtbarf;
      {
        Dstr text_out;
        context->rareModes (s, startt, endt, step, text_out, 0);
        fprintf (outfp, "%s", text_out.aschar());
      }
      break;

    case 'm':
      if (format != 't')
        fmtbarf;
      {
        Dstr text_out;
        context->rareModes (s, startt, endt, step, text_out, 1);
        fprintf (outfp, "%s", text_out.aschar());
      }
      break;

    case 'b':
      if (format != 't')
        fmtbarf;
      {
        Dstr text_out;
        context->bannerMode (s, startt, endt, text_out);
        fprintf (outfp, "%s", text_out.aschar());
      }
      break;

    case 'g':
      switch (format) {
      case 'p':
        {
          RGBGraph g (settings->gw, settings->gh, context->colors);
          g.drawTides (s, startt);
          png_file_ptr = outfp;  // See externC.cc
	  g.writeAsPNG (file_write_data_fn);
        }
        break;
      case 't':
        {
          Dstr text_out;
          TTYGraph g (settings->tw, settings->th);
          g.drawTides (s, startt);
	  g.writeAsText (text_out);
          fprintf (outfp, "%s", text_out.aschar());
        }
        break;
      default:
        fmtbarf;
      }
      break;

    default:
      {
        Dstr details ("Unsupported mode: ");
        details += mode;
        barf (BAD_MODE, details);
      }
    }

    delete s;

  } else {
    Dstr details ("Could not find: ");
    details += name;
    barf (STATION_NOT_FOUND, details, 0);
  }
}

void
loop_locations (TideContext *context, FILE *outfp) {
  CommandLineSettings *cls = context->cls;
  int is_first = 1;
  struct CommandLineSettings::lnode *l = cls->l;

  Interval step (HOURSECONDS);
  if (!(cls->s.isNull())) {
    step = Interval (cls->s);
    if (step <= Interval (0)) {
      Dstr details ("You must specify a positive step.  You tried ");
      details += cls->s;
      barf (NUMBER_RANGE_ERROR, details);
    }
  }

  char mode, format;
  if (cls->m.isNull())
    mode = 'p';
  else
    mode = cls->m[0];
  if (cls->f.isNull())
    format = 't';
  else
    format = cls->f[0];

  if (mode != 'l') {
    if (!l)
      fprintf (stderr, "Warning:  No locations specified with -l; hence, no output.\n");
    while (l) {
      if (is_first)
	is_first = 0;
      else
	fprintf (outfp, "%s", stationsep);
      do_location (l->name, context, step, outfp, mode, format);
      l = l->next;
    }

  } else { // List mode
    switch (format) {
    case 't':
      {
	Dstr text_out;
	context->listModePlain (text_out);
	fprintf (outfp, "%s", text_out.aschar());
      }
      break;
    case 'h':
      {
	Dstr text_out;
	context->listModeHTML (text_out);
	fprintf (outfp, "%s", text_out.aschar());
      }
      break;
    default:
      fmtbarf;
    }
  }
}

int main (int argc, char **argv)
{
  // So much for ANSI.
  // sync_with_stdio();

  Settings settings;
  {
    ConfigDefaults cd;
    UserDefaults ud;
    CommandLineSettings cls (argc, argv);
    settings.supersedeBy (cd);
    settings.supersedeBy (ud);
    settings.supersedeBy (cls);
  }

  TideContext *context = new TideContext (argc, argv, new Colors (settings),
    &settings);

  struct stat buf;
  if (stat (context->disabledisclaimerfile.aschar(), &buf)) {
    fprintf (stderr, "\
-----------------------------------------------------------------------------\n\
             XTide   Copyright (C) 1998 David Flater.\n\
\n\
This software is provided under the terms of the GNU General Public\n\
License, either version 2 of the License, or (at your option) any later\n\
version.\n\
\n\
Although the package as a whole is GPL, some individual source files\n\
are public domain.  Consult their header comments for details.\n\
\n\
                        NOT FOR NAVIGATION\n\
\n\
This program is distributed in the hope that it will be useful, but\n\
WITHOUT ANY WARRANTY; without even the implied warranty of\n\
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  The author\n\
assumes no liability for damages arising from use of this program OR\n\
of any 'harmonics data' that might be distributed with it.  For details, see\n\
the verbose documentation at http://www.flaterco.com/xtide/.\n\
\n\
This obnoxious message will go away permanently if you click \"Don't show\n\
this again\" in the disclaimer window of the X windows client (xtide), or\n\
if you create a file in your home directory called \".disableXTidedisclaimer\".\n\
-----------------------------------------------------------------------------\n\
\n");
  }

  if (argc < 2 && context->cls->l_len == 0) {
    fprintf (stderr, "\
Minimal usage:  tide -l \"Location name\" (or set the environment variable\n\
  XTIDE_DEFAULT_LOCATION to \"Location name\")\n\
Other switches:\n\
  -b \"YYYY-MM-DD HH:MM\"\n\
      Specify the begin (start) time for predictions.\n\
  -e \"YYYY-MM-DD HH:MM\"\n\
      Specify the end (stop) time for predictions.\n\
  -f h|p|t\n\
      Specify the output format as HTML, PNG, or text.  The default is text.\n\
      Only calendar and list modes can do HTML; only graph mode can do PNG.\n\
  -m b|c|C|g|l|m|p|r|s\n\
      Specify mode to be banner, calendar, alt. calendar, graph, list, medium\n\
      rare, plain, raw, or stats.  The default is plain.\n\
  -o \"filename\"\n\
      Redirect output to the specified file (appends).\n\
  -s \"HH:MM\"\n\
      Specify the step interval, in hours and minutes, for raw\n\
      mode predictions.  The default is one hour.\n\
  -v\n\
      Print version string and exit.\n\
\n\
NOTE:  These are only the most important switches.  For information on\n\
all of the switches, please read the verbose documentation at:\n\
    http://www.flaterco.com/xtide/\n");
    exit (-1);
  }

  FILE *outfp = stdout;
  if (!(context->cls->o.isNull())) {
    if (!(outfp = fopen (context->cls->o.aschar(), "a"))) {
      Dstr details (context->cls->o);
      details += ": ";
      details += strerror (errno);
      details += ".";
      barf (CANT_OPEN_FILE, details);
    }
  }

  loop_locations (context, outfp);

  exit (0);
}