cgi_nechung.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : CGI nechung                                                       *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1997-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00016 
00017 #include "nechung_oracle.h"
00018 
00019 #include <basis/guards.h>
00020 #include <basis/istring.h>
00021 #include <loggers/console_logger.h>
00022 #include <opsystem/filename.h>
00023 #include <data_struct/static_memory_gremlin.h>
00024 
00025 #include <stdio.h>
00026 
00027 #undef LOG
00028 #define LOG(s) program_wide_logger().log(s)
00029 
00030 HOOPLE_STARTUP_CODE;
00031 
00032 #define DEFAULT_FORTUNE_FILE "fortunes.dat"
00033 
00034 int main(int argc, char *argv[])
00035 {
00036   SET_DEFAULT_CONSOLE_LOGGER;
00037   istring name;
00038   istring index;
00039   if (argc > 1) {
00040     // use the first command line argument.
00041     name = argv[1];
00042   } else {
00043     // if nothing on the command line, then use our defaults.
00044     name = portable::env_string("NECHUNG");
00045       // first try the environment variable.
00046     if (!name) name = DEFAULT_FORTUNE_FILE;
00047       // next, use the hardwired default.
00048   }
00049 
00050   if (name.length() < 5) {
00051     LOG(istring("nechung:: file name is too short (") + name + ").");
00052     return 1;
00053   }
00054   filename index_file_name(name);
00055   istring extension(index_file_name.extension());
00056   int end = index_file_name.raw().end();
00057 #ifdef DEBUG_NECHUNG
00058   LOG(istring("fortune filename is ") + name);
00059   LOG(istring("extension is ") + extension);
00060 #endif
00061   istring tmp = index_file_name;
00062   tmp.zap( (end + 1) - extension.length(), end);
00063   tmp += "idx";
00064   istring base_part = filename(tmp).basename();
00065   index_file_name = portable::env_string("TMP") + "/" + base_part;
00066 #ifdef DEBUG_NECHUNG
00067   LOG(istring("index file is ") + index_file_name);
00068 #endif
00069   index = index_file_name;
00070 
00071   nechung_oracle some_fortunes(name, index);
00072   // send the header for html text.
00073   printf("content-type: text/html\n\n");
00074   // send the preliminary gunk.
00075   printf("<body text=\"#00ee00\" bgcolor=\"#000000\" link=\"#66ff99\" "
00076       "vlink=\"#cc33cc\" alink=\"#ff9900\">\n");
00077 //old text color #33ccff
00078   printf("<tt style=\"font-weight: bold;\">\n");
00079   printf("<font size=\"+1\">\n");
00080 
00081   istring to_show = some_fortunes.pick_random();
00082   int line_posn = 0;
00083   for (int i = 0; i < to_show.length(); i++) {
00084     if (to_show[i] == ' ') {
00085       // spaces get translated to one non-breaking space.
00086       printf("&nbsp;");
00087       line_posn++;
00088     } else if (to_show[i] == '\t') {
00089       // tabs get translated to positioning at tab stops based on eight.
00090       int to_add = 8 - line_posn % 8;
00091       for (int j = 0; j < to_add; j++) printf("&nbsp;");
00092       line_posn += to_add;
00093     } else if (to_show[i] == '\r')
00094       continue;
00095     else if (to_show[i] == '\n') {
00096       printf("<br>%c", to_show[i]);
00097       line_posn = 0;
00098     } else {
00099       printf("%c", to_show[i]);
00100       line_posn++;
00101     }
00102   }
00103   printf("\n");
00104   printf("</font>\n");
00105   printf("</tt>\n");
00106   printf("</body>\n");
00107   return 0;
00108 }
00109 
00110 #ifdef __BUILD_STATIC_APPLICATION__
00111   // static dependencies found by buildor_gen_deps.sh:
00112   #include <basis/array.cpp>
00113   #include <basis/byte_array.cpp>
00114   #include <basis/callstack_tracker.cpp>
00115   #include <basis/chaos.cpp>
00116   #include <basis/convert_utf.cpp>
00117   #include <basis/definitions.cpp>
00118   #include <basis/earth_time.cpp>
00119   #include <basis/guards.cpp>
00120   #include <basis/istring.cpp>
00121   #include <basis/log_base.cpp>
00122   #include <basis/memory_checker.cpp>
00123   #include <basis/mutex.cpp>
00124   #include <basis/object_base.cpp>
00125   #include <basis/outcome.cpp>
00126   #include <basis/packable.cpp>
00127   #include <basis/portable.cpp>
00128   #include <basis/sequence.cpp>
00129   #include <basis/set.cpp>
00130   #include <basis/utility.cpp>
00131   #include <basis/version_record.cpp>
00132   #include <data_struct/amorph.cpp>
00133   #include <data_struct/bit_vector.cpp>
00134   #include <data_struct/byte_hasher.cpp>
00135   #include <data_struct/configurator.cpp>
00136   #include <data_struct/hash_table.cpp>
00137   #include <data_struct/pointer_hash.cpp>
00138   #include <data_struct/stack.cpp>
00139   #include <data_struct/static_memory_gremlin.cpp>
00140   #include <data_struct/string_hash.cpp>
00141   #include <data_struct/string_hasher.cpp>
00142   #include <data_struct/string_table.cpp>
00143   #include <data_struct/symbol_table.cpp>
00144   #include <data_struct/table_configurator.cpp>
00145   #include <loggers/console_logger.cpp>
00146   #include <loggers/file_logger.cpp>
00147   #include <loggers/locked_logger.cpp>
00148   #include <loggers/null_logger.cpp>
00149   #include <loggers/program_wide_logger.cpp>
00150   #include <opsystem/byte_filer.cpp>
00151   #include <opsystem/command_line.cpp>
00152   #include <opsystem/critical_events.cpp>
00153   #include <opsystem/directory.cpp>
00154   #include <opsystem/filename.cpp>
00155   #include <opsystem/filetime.cpp>
00156   #include <opsystem/ini_config.cpp>
00157   #include <opsystem/ini_parser.cpp>
00158   #include <opsystem/path_configuration.cpp>
00159   #include <opsystem/rendezvous.cpp>
00160   #include <textual/byte_format.cpp>
00161   #include <textual/parser_bits.cpp>
00162   #include <textual/string_manipulation.cpp>
00163   #include <textual/tokenizer.cpp>
00164 #endif // __BUILD_STATIC_APPLICATION__
00165 

Generated on Thu Nov 20 04:28:44 2008 for HOOPLE Libraries by  doxygen 1.5.1