00001 #ifndef NECHUNG_CLASS 00002 #define NECHUNG_CLASS 00003 00004 /*****************************************************************************\ 00005 * * 00006 * Name : nechung_oracle * 00007 * Author : Chris Koeritz * 00008 * * 00009 * Purpose: * 00010 * * 00011 * This is the root nechung functionality. It provides a means of * 00012 * randomly selecting an item out of a specially formatted file. If no index * 00013 * file has previously been built for the file, then one is created. The * 00014 * index file makes choosing a fortune randomly very quick; only a seek on * 00015 * the much smaller index is needed in order to find the position of the * 00016 * fortune to be shown. * 00017 * * 00018 ******************************************************************************* 00019 * Copyright (c) 1991-$now By Author. This program is free software; you can * 00020 * redistribute it and/or modify it under the terms of the GNU General Public * 00021 * License as published by the Free Software Foundation; either version 2 of * 00022 * the License or (at your option) any later version. This is online at: * 00023 * http://www.fsf.org/copyleft/gpl.html * 00024 * Please send any updates to: fred@gruntose.com * 00025 \*****************************************************************************/ 00026 00027 // Nechung works with a particular form of data file and will extract a random 00028 // and hopefully auspicious message out of that file. An example of the file 00029 // format is: 00030 // msg1 00031 // ~ 00032 // msg2 00033 // ~ 00034 // (... more messages and tildes...) 00035 // The tilde is the separation character mentioned below. 00036 00037 #include <basis/object_base.h> 00038 00039 // forward. 00040 class console_logger; 00041 00042 //#define DEBUG_NECHUNG 00043 // uncomment this to get the debugging version. it is used by several files 00044 // that are part of nechung. 00045 00046 const char NECHUNG_SEPARATION_CHARACTER = '~'; 00047 // this character separates the entries in the fortunes database. 00048 00049 class nechung_oracle 00050 { 00051 public: 00052 nechung_oracle(const istring &data_filename, const istring &index_filename); 00053 // the constructor needs the name of a nechung format data file in 00054 // "data_filename" and the name of the index file to be used for that data 00055 // file in "index_filename". 00056 00057 virtual ~nechung_oracle(); 00058 00059 IMPLEMENT_CLASS_NAME("nechung_oracle"); 00060 00061 istring pick_random(); 00062 // returns a randomly chosen fortune. 00063 00064 void display_random(); 00065 // selects an oracular pronouncement from the file and then shows it on 00066 // standard output. 00067 00068 private: 00069 chaos *_randomizer; // the random number generator we use. 00070 istring *_filename_held; // the data file's name. 00071 istring *_index_held; // the index file's name. 00072 int _number_of_fortunes; // how many fortunes exist in the file. 00073 00074 void parse_file(); 00075 // given the data file and index file, this will ensure that the index 00076 // file is made up to date. it creates, if necessary, the file that 00077 // contains the positions of fortunes in the data file. this is what 00078 // we'll use to find the start of each fortune. if the file already 00079 // exists, then it will just retrieve the number of fortunes from the index 00080 // file. after this method, the pick_random() and display_random() methods 00081 // are available. 00082 00083 // disallowed. 00084 nechung_oracle(const nechung_oracle &); 00085 nechung_oracle &operator =(const nechung_oracle &); 00086 }; 00087 00088 #endif 00089
1.5.1