GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


io.hh

    
#ifndef ALREADY_INCLUDED_IO_HH
#define ALREADY_INCLUDED_IO_HH

//#ifndef null
//#define null  (0)
//#endif /* null */

#include "../noallegro/my-allegro.hh"
#include "string.hh"

#ifndef PRINT
#define PRINT(x) \
do { cout << #x << " = " << (x) << endl; } while (false)
#endif /* PRINT */

#ifndef PRINT_LINE
#define PRINT_LINE(x) \
do { cout << "----------------------------------------\n"; } while (false)
#endif /* PRINT_LINE */

#ifndef TELL
#define TELL(x) \
do { cout << "* COMMAND: " << #x << endl; x; } while (false)
#endif /* TELL */

/* The following macro is used by the Allegro games library: */
#ifndef END_OF_MAIN
#define END_OF_MAIN
#endif /* END_OF_MAIN */

//using namespace std;          // bad form to import an entire namespace
//#define cerr  (std::cerr)
//#define cout  (std::cout)
//#define endl  (std::endl)
//#define ostream std::ostream


// STRINGS_EQUAL FUNCTION! inline bool strings_equal(const char* s1, const char* s2) { if ((s1 == null) && (s2 == null)) { return true; } else if ((s1 == null) || (s2 == null)) { return false; } else { return (0 == strcmp(s1,s2)); } }
// WRITER CLASS! ABSTRACT class Writer { public: virtual ~Writer() {} private: virtual void write(char ch) = null; virtual void write(int i) = null; virtual void write(double d) = null; virtual void write(const char* s) = null; virtual string get_file_name() = null; public: Writer& operator << (char ch) { this->write(ch); return *this; } Writer& operator << (int i) { this->write(i); return *this; } Writer& operator << (double d) { this->write(d); return *this; } Writer& operator << (const char* s) { this->write(s); return *this; } Writer& operator << (const string& s) { this->write(s.const_char_star()); return *this; } //Writer& operator << (long long mylong) };
// FILE_WRITER CLASS! class File_Writer : public Writer { private: FILE* f; bool ours_to_close; string file_name; public: File_Writer(string file_name) { this->f = fopen(file_name.const_char_star(),"wb"); ASSERT(f != null); this->ours_to_close = true; this->file_name = file_name; } File_Writer(FILE* f) { ASSERT(f != null); this->f = f; this->ours_to_close = false; this->file_name = "<FILE>"; } ~File_Writer() { if (ours_to_close) { fclose(f); } } public: File_Writer& operator << (char ch) { fprintf(this->f,"%c",ch); return *this; } File_Writer& operator << (int i) { fprintf(this->f,"%d",i); return *this; } File_Writer& operator << (double d) { fprintf(this->f,"%g",d); return *this; } File_Writer& operator << (const char* s) { fprintf(this->f,"%s",s); return *this; } File_Writer& operator << (const string& s) { *this << s.const_char_star(); return *this; } // EXTRA: string get_file_name() { return file_name; } private: virtual void write(char ch) { *this << ch; } virtual void write(int i) { *this << i; } virtual void write(double d) { *this << d; } virtual void write(const char* s) { *this << s; } };
// COUT CIN! extern File_Writer cout; extern File_Writer cerr; const char endl = '\n';
// STRING_BUFFER CLASS! /* * Class for efficient string building inspired by Java's * StringBuffer class. The class features efficient * appending to existing string_buffer object. */ class string_buffer : public string, public Writer { public: string_buffer() {} void set_char_at(int i, char ch); string_buffer& operator << (char ch); string_buffer& operator << (int i); string_buffer& operator << (double d); string_buffer& operator << (const char* s); string_buffer& operator << (const string& s) { *this << s.const_char_star(); return *this; } // EXTRA: string get_file_name() { return "<string_buffer>"; } private: virtual void write(char ch) { *this << ch; } virtual void write(int i) { *this << i; } virtual void write(double d) { *this << d; } virtual void write(const char* s) { *this << s; } };
// STRING_BUFFER2 CLASS! /* * Class for efficient string concatenation The purpose of * this class is to execute a clever trick that allows for * efficient string concatenation. */ class string_buffer2 : public string_buffer { private: string_buffer2() {} public: friend string_buffer2 operator + (const string& a, const string& b) { //cout << "** BEGIN string_buffer2 operator + (const string&, const string&)\n"; //PRINT(a); //PRINT(b); string_buffer2 result; result << a; result << b; //cout << "** END string_buffer2 operator + (const string&, const string&)\n"; return result; } friend string_buffer2 operator + (const string& a, const char ch) { string_buffer2 result; result << a; result << ch; return result; } friend string_buffer2 operator + (const string& a, const int i) { string_buffer2 result; result << a; result << i; return result; } friend string_buffer2 operator + (const string& a, const double d) { string_buffer2 result; result << a; result << d; return result; } friend string_buffer2 operator + (const string& a, const char* s) { string_buffer2 result; result << a; result << s; return result; } /// /// NOT ALLOWED: /// //friend string_buffer2 operator + (const char* s1, const char* s2) friend string_buffer2& operator + (const string_buffer2& csb, const string& s) { //cout << "** BEGIN string_buffer2& operator + (string_buffer2&, const string&)\n"; //PRINT(s); string_buffer2& sb = const_cast<string_buffer2&>(csb); sb << s; //cout << "** END string_buffer2& operator + (string_buffer2&, const string&)\n"; return sb; } friend string_buffer2& operator + (const string_buffer2& csb, char ch) { string_buffer2& sb = const_cast<string_buffer2&>(csb); sb << ch; return sb; } friend string_buffer2& operator + (const string_buffer2& csb, int i) { string_buffer2& sb = const_cast<string_buffer2&>(csb); sb << i; return sb; } friend string_buffer2& operator + (const string_buffer2& csb, double d) { string_buffer2& sb = const_cast<string_buffer2&>(csb); sb << d; return sb; } friend string_buffer2& operator + (const string_buffer2& csb, const char* s) { string_buffer2& sb = const_cast<string_buffer2&>(csb); sb << s; return sb; } };
// PACK_FILE_WRITER CLASS! // class Pack_File_Writer : public Writer // { // private: // PACKFILE* f; // bool ours_to_close; // string file_name; // public: // Pack_File_Writer(string file_name) // { // this->f = pack_fopen(file_name.const_char_star(),"wp"); // ASSERT(f != null); // this->ours_to_close = true; // this->file_name = file_name; // } // Pack_File_Writer(PACKFILE* f) // { // ASSERT(f != null); // this->f = f; // this->ours_to_close = false; // this->file_name = "<PACKFILE>"; // } // ~Pack_File_Writer() // { // if (ours_to_close) { // pack_fclose(f); // } // } // // public: // Pack_File_Writer& operator << (char ch) // { string_buffer sb; sb << ch; pack_fputs(sb.const_char_star(),f); return *this; } // Pack_File_Writer& operator << (int i) // { string_buffer sb; sb << i; pack_fputs(sb.const_char_star(),f); return *this; } // Pack_File_Writer& operator << (double d) // { string_buffer sb; sb << d; pack_fputs(sb.const_char_star(),f); return *this; } // Pack_File_Writer& operator << (const char* s) // { pack_fputs(s,f); return *this; } // Pack_File_Writer& operator << (const string& s) // { pack_fputs(s.const_char_star(),f); return *this; } // // // EXTRA: // string get_file_name() // { // return file_name; // } // // private: // virtual void write(char ch) { *this << ch; } // virtual void write(int i) { *this << i; } // virtual void write(double d) { *this << d; } // virtual void write(const char* s) { *this << s; } // }; //
// TO-string FUNCTION! template<class T> string to_string(const T* t) { string_buffer sb; if (t == null) { sb << "null"; } else { sb << *t; } return sb; }
// GULPER CLASS! ABSTRACT class Gulper { protected: string file_name; int file_line; int current_char; public: Gulper(string file_name) { this->file_name = file_name; this->file_line = 1; this->current_char = EOF; } virtual ~Gulper() { /* DO NOTHING! */ } string get_file_name() { return file_name; } int get_file_line() { return file_line; } int get_current_char() { return current_char; } virtual void gulp_char() = null; };
// FILE_GULPER CLASS! class File_Gulper : public Gulper { private: FILE* f; bool saw_line_end; public: File_Gulper(string file_name); ~File_Gulper() { fclose(f); } virtual void gulp_char(); };
// STRING_GULPER CLASS! class String_Gulper : public Gulper { private: string internal; int index; bool saw_line_end; public: String_Gulper(string internal); ~String_Gulper() { /* DO NOTHING! */ } virtual void gulp_char(); };
// READER_CORE CLASS! ABSTRACT class Reader_Core { private: enum Return_Values { Type_Char = 1, Type_Int, Type_Double, Type_Ident, Type_String, Type_Eof }; char last_char; int last_int; double last_double; string last_ident; string last_string; bool last_bool; Gulper* gulper; Return_Values current_type; void skip_whitespace(); int scan_int(); double scan_fraction(); string scan_identifier(); string scan_quoted_string(); void wrong_type(string tname); virtual void pure_virtual_not_used() = null; // NOTE: Ensures class is abstract public: bool currently_char() { return current_type == Type_Char; } bool currently_int() { return current_type == Type_Int; } bool currently_double() { return current_type == Type_Double; } bool currently_identifier() { return current_type == Type_Ident; } bool currently_string() { return current_type == Type_String; } bool currently_eof() { return current_type == Type_Eof; } char peek_char() { if (current_type != Type_Char) wrong_type("char"); return last_char; } int peek_int() { if (current_type != Type_Int) wrong_type("int"); return last_int; } double peek_double() { if (current_type != Type_Double) wrong_type("double"); return last_double; } string peek_identifier() { if (current_type != Type_Ident) wrong_type("identifier"); return last_ident; } string peek_string() { if (current_type != Type_String) wrong_type("string"); return last_string; } void next_token(); int get_file_line() { return gulper->get_file_line(); } string get_file_name() { return gulper->get_file_name(); } void error_and_halt(string s); string debug_say_symbol(); protected: Reader_Core(Gulper* gulper) { this->gulper = gulper; this->last_char = 0; this->last_int = 0; this->last_double = 0; this->last_ident = ""; this->last_string = ""; next_token(); } virtual ~Reader_Core() { delete gulper; } };
// READER CLASS! ABSTRACT class Reader : public Reader_Core { protected: Reader(Gulper* gulper) : Reader_Core(gulper) { /* DO NOTHING! */ } public: virtual ~Reader() { /* DO NOTHING! */ } public: /* PEEKING: */ bool looking_at(char ch) { if (ch != '(' && ch != ')' && ch != '\'') { const char* error_message = "Reader::looking_at must be given one of '(',')','\\''"; #ifdef ALLEGRO_MESSAGE_AND_EXIT ALLEGRO_MESSAGE_AND_EXIT((error_message)); #else printf(error_message); exit(EXIT_FAILURE); #endif } return (currently_char() && peek_char() == ch); } bool looking_at(string s) { return (currently_identifier() && peek_identifier().equals(s)); } /* READING LITERALS: */ friend Reader& operator >> (Reader& r, const char& ch); friend Reader& operator >> (Reader& r, const char* s); /* GULPING DATA: */ friend Reader& operator >> (Reader& r, char& ch); friend Reader& operator >> (Reader& r, int& i); friend Reader& operator >> (Reader& r, double& d); friend Reader& operator >> (Reader& r, string& s); string gulp_quoted_string(); };
// FILE_READER CLASS! class File_Reader : public Reader { public: File_Reader(string file_name) : Reader(new File_Gulper(file_name)) { /* DO NOTHING! */ } virtual void pure_virtual_not_used() { ASSERT(false); } };
// STRING_READER CLASS! class String_Reader : public Reader { public: String_Reader(string readme) : Reader(new String_Gulper(readme)) { /* DO NOTHING! */ } virtual void pure_virtual_not_used() { ASSERT(false); } }; #include "temp-blit.hh" #include "safe-allegro.hh" #endif /* ALREADY_INCLUDED_IO_HH */
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:09:05 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to