File: tide.cc

package info (click to toggle)
xtide 2.9.5-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,996 kB
  • ctags: 2,141
  • sloc: cpp: 20,379; sh: 1,044; makefile: 224; yacc: 114; lex: 58
file content (228 lines) | stat: -rw-r--r-- 7,286 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
// $Id: tide.cc 2832 2007-12-01 01:05:01Z flaterco $

/*  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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "common.hh"


// Separator between tide stations in text client output
// \f is form feed
static constString stationSeparator = "\f";


static void doLocation (const Dstr &name,
                        Interval step,
                        FILE *outfp,
                        Mode::Mode mode,
                        Format::Format form) {
  const StationRef *sr (Global::stationIndex().getStationRefByName(name));
  if (sr) {
    std::auto_ptr<Station> station (sr->load());
    station->step = step;

    // Install mark level, if applicable.
    {
      Configurable &cfbl = Global::settings["ml"];
      if (!cfbl.isNull) {
        station->markLevel = cfbl.p;
        if (cfbl.p.Units() != station->predictUnits())
          station->markLevel.Units (station->predictUnits());
      }
    }

    // Find start and end times.
    // This needs to be done here due to the timezones.
    Timestamp startTime, endTime;
    {
      Configurable &cfbl = Global::settings["b"];
      if (cfbl.isNull)
	startTime = (time_t)time(NULL);
      else {
	startTime = Timestamp (cfbl.s, sr->timezone);
	if (startTime.isNull())
	  Global::cant_mktime (cfbl.s, sr->timezone, Error::fatal);
      }
    }{
      Configurable &cfbl = Global::settings["e"];
      if (cfbl.isNull)
	endTime = startTime + Global::defaultPredictInterval;
      else {
	endTime = Timestamp (cfbl.s, sr->timezone);
	if (endTime.isNull())
	  Global::cant_mktime (cfbl.s, sr->timezone, Error::fatal);
      }
    }
    if (startTime > endTime) {
      static bool I_told_you_once (false);
      if (!I_told_you_once) {
        fprintf (stderr, "Tide Warning:  swapping begin and end times\n");
        I_told_you_once = true;
      }
      std::swap (startTime, endTime);
    }

    if (mode == Mode::graph && form == Format::PNG)
      station->graphModePNG (outfp, startTime);
    else {
      Dstr text_out;
      station->print (text_out, startTime, endTime, mode, form);
      fprintf (outfp, "%s", text_out.aschar());
    }

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


static void loopLocations (FILE *outfp) {
  bool isFirst (true);

  Interval step (Global::hour);
  {
    Configurable &cfbl = Global::settings["s"];
    if (!cfbl.isNull) {
      step = Interval (cfbl.s);
      if (step <= Global::zeroInterval) {
	Dstr details ("You must specify a positive step.  You tried ");
	details += cfbl.s;
	Global::barf (Error::NUMBER_RANGE_ERROR, details);
      }
    }
  }

  Mode::Mode mode;
  Format::Format form;
  {
    Configurable &cfbl = Global::settings["m"];
    if (cfbl.isNull)
      mode = Mode::plain;
    else
      mode = (Mode::Mode)cfbl.c;
  }{
    Configurable &cfbl = Global::settings["f"];
    if (cfbl.isNull)
      form = Format::text;
    else
      form = (Format::Format)cfbl.c;
  }

  if (mode != Mode::list) {
    Configurable &cfbl = Global::settings["l"];
    DstrVector::const_iterator it = cfbl.v.begin();
    DstrVector::const_iterator stop = cfbl.v.end();
    if (it == stop)
      fprintf (stderr, "Warning:  No locations specified with -l; hence, no output.\n");
    while (it != stop) {
      if (isFirst)
	isFirst = false;
      else
	fprintf (outfp, "%s", stationSeparator);
      doLocation (*it, step, outfp, mode, form);
      ++it;
    }
  } else {
    // List mode
    Dstr text_out;
    Global::stationIndex().print (text_out, form);
    fprintf (outfp, "%s", text_out.aschar());
  }
}


int main (int argc, char **argv) {
  srand (time (NULL));
  Global::initCodeset();
  Global::settings.applyUserDefaults();
  Global::settings.applyCommandLine (argc, argv);
  Global::settings.fixUpDeprecatedSettings();

  if (!Global::disclaimerDisabled()) {
    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 3 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 && Global::settings["l"].v.empty()) {
    fprintf (stderr, "\
Minimal usage:  tide -l \"Location name\" (or set the environment variable\n\
  XTIDE_DEFAULT_LOCATION to \"Location name\")\n\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 c|h|i|l|p|t\n\
      Specify the output format as CSV, HTML, iCalendar, LaTeX, PNG, or text.\n\
      The default is text.\n\
  -m a|b|c|C|g|l|m|p|r|s\n\
      Specify mode to be about, banner, calendar, alt. calendar, graph, list,\n\
      medium 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\
These are only the most important switches.  For information on all of\n\
the switches, please read the verbose documentation at:\n\
  http://www.flaterco.com/xtide/\n");
    exit (-1);
  }

  FILE *outfp = stdout;
  {
    Configurable &cfbl = Global::settings["o"];
    if (!cfbl.isNull)
      if (!(outfp = fopen (cfbl.s.aschar(), "ab")))
	Global::cantOpenFile (cfbl.s, Error::fatal);
  }

  loopLocations (outfp);

  exit (0);
}

// Cleanup2006 Done