feisty meow concerns codebase  2.140
cgi_nechung.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : CGI nechung *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 1997-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
16 
17 #include "nechung_oracle.h"
18 
19 #include <basis/astring.h>
20 #include <basis/environment.h>
21 #include <basis/guards.h>
22 #include <filesystem/filename.h>
23 #include <loggers/console_logger.h>
26 
27 #include <stdio.h>
28 
29 //using namespace application;
30 using namespace basis;
31 using namespace filesystem;
32 using namespace loggers;
33 
34 #undef LOG
35 #define LOG(s) program_wide_logger::get().log(s, 0)
36 
38 //hmmm: need this back when things are ready!
39 
40 #define DEFAULT_FORTUNE_FILE "fortunes.dat"
41 
42 int main(int argc, char *argv[])
43 {
45 
46  astring name;
47  astring index;
48  if (argc > 1) {
49  // use the first command line argument.
50  name = argv[1];
51  } else {
52  // if nothing on the command line, then use our defaults.
53  name = environment::get("NECHUNG");
54  // first try the environment variable.
55  if (!name) name = DEFAULT_FORTUNE_FILE;
56  // next, use the hardwired default.
57  }
58 
59  if (name.length() < 5) {
60  LOG(astring("nechung:: file name is too short (") + name + ").");
61  return 1;
62  }
63  filename index_file_name(name);
64  astring extension(index_file_name.extension());
65  int end = index_file_name.raw().end();
66 #ifdef DEBUG_NECHUNG
67  LOG(astring("fortune filename is ") + name);
68  LOG(astring("extension is ") + extension);
69 #endif
70  astring tmp = index_file_name;
71  tmp.zap( (end + 1) - extension.length(), end);
72  tmp += "idx";
73  astring base_part = filename(tmp).basename();
74  index_file_name = environment::TMP() + "/" + base_part;
75 #ifdef DEBUG_NECHUNG
76  LOG(astring("index file is ") + index_file_name);
77 #endif
78  index = index_file_name;
79 
80  nechung_oracle some_fortunes(name, index);
81  // send the header for html text.
82  printf("content-type: text/html\n\n");
83  // send the preliminary gunk.
84  printf("<body>\n");
85 //text=\"#00ee00\" bgcolor=\"#000000\" link=\"#66ff99\" "
86 // "vlink=\"#cc33cc\" alink=\"#ff9900\">\n");
87  printf("<tt style=\"font-weight: bold;\">\n");
88  printf("<font size=\"+1\">\n");
89 
90  astring to_show = some_fortunes.pick_random();
91  int line_posn = 0;
92  for (int i = 0; i < to_show.length(); i++) {
93  if (to_show[i] == ' ') {
94  // spaces get translated to one non-breaking space.
95  printf("&nbsp;");
96  line_posn++;
97  } else if (to_show[i] == '\t') {
98  // tabs get translated to positioning at tab stops based on eight.
99  int to_add = 8 - line_posn % 8;
100  for (int j = 0; j < to_add; j++) printf("&nbsp;");
101  line_posn += to_add;
102  } else if (to_show[i] == '\r')
103  continue;
104  else if (to_show[i] == '\n') {
105  printf("<br>%c", to_show[i]);
106  line_posn = 0;
107  } else {
108  printf("%c", to_show[i]);
109  line_posn++;
110  }
111  }
112  printf("\n");
113  printf("</font>\n");
114  printf("</tt>\n");
115  printf("</body>\n");
116  printf("</html>\n");
117  return 0;
118 }
119 
120 #ifdef __BUILD_STATIC_APPLICATION__
121  // static dependencies found by buildor_gen_deps.sh:
123  #include <basis/astring.cpp>
124  #include <basis/common_outcomes.cpp>
125  #include <basis/environment.cpp>
126  #include <basis/mutex.cpp>
127  #include <basis/utf_conversion.cpp>
134  #include <filesystem/byte_filer.cpp>
135  #include <filesystem/directory.cpp>
136  #include <filesystem/filename.cpp>
137  #include <filesystem/file_time.cpp>
138  #include <loggers/combo_logger.cpp>
139  #include <loggers/console_logger.cpp>
140  #include <loggers/critical_events.cpp>
141  #include <loggers/file_logger.cpp>
143  #include <structures/bit_vector.cpp>
144  #include <structures/checksums.cpp>
148  #include <structures/string_table.cpp>
150  #include <textual/byte_formatter.cpp>
151  #include <textual/parser_bits.cpp>
153  #include <timely/earth_time.cpp>
154  #include <timely/time_stamp.cpp>
155 #endif // __BUILD_STATIC_APPLICATION__
156 
int main(int argc, char *argv[])
Definition: cgi_nechung.cpp:42
#define LOG(s)
Definition: cgi_nechung.cpp:35
#define DEFAULT_FORTUNE_FILE
Definition: cgi_nechung.cpp:40
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
virtual void zap(int start, int end)
Deletes the characters between "start" and "end" inclusively.
Definition: astring.cpp:521
int end() const
returns the index of the last (non-null) character in the string.
Definition: astring.h:86
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
static astring TMP()
provides a single place to get the temporary directory.
Definition: environment.cpp:39
static astring get(const astring &variable_name)
looks up the "variable_name" in the current environment variables.
Definition: environment.cpp:57
Provides operations commonly needed on file names.
Definition: filename.h:64
basis::astring extension() const
returns the extension for the file, if one is present.
Definition: filename.cpp:299
const basis::astring & raw() const
returns the astring that we're holding onto for the path.
Definition: filename.cpp:97
filename basename() const
returns the base of the filename; no directory.
Definition: filename.cpp:385
basis::astring pick_random()
#define SETUP_CONSOLE_LOGGER
< a macro that retasks the program-wide logger as a console_logger.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.