home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_05 / 8n05056a < prev    next >
Text File  |  1990-04-18  |  3KB  |  142 lines

  1. *****Listing 5*****
  2.  
  3. const int forever = 1;
  4. class Editor {
  5. public:
  6.     Editor( char *fname) : b( fname), input(fname), output( "", "w")
  7.     {
  8.         strcpy( filename, fname);
  9.  
  10.         rp = ®
  11.     }
  12.  
  13.     void eval()
  14.     {
  15.         while( forever) {
  16.             int c = input.get();
  17.             (this->*action[c])( c);
  18.         }
  19.     }
  20.  
  21.     void go( int c)     { rp->go( b, output); rp = ® }
  22.  
  23.     void print( int c)  { rp->print( b, output); rp = ® }
  24.  
  25.     void insert( int c) { rp->insert( b, input); rp = ® }
  26.  
  27.     void del( int c)    { rp->del( b, kbuf, output); rp = ® }
  28.  
  29.     void put( int c)    { rp->put( b, kbuf); rp = ® }
  30.  
  31.     void info( int c)
  32.     {
  33.         char buf[128];
  34.         int point = b.lengthb();
  35.         b. go( point);
  36.         output.put( filename);
  37.         sprintf( buf, ":  point %d, length %d\n", point, b.length());
  38.         output.put( buf);
  39.     }
  40.  
  41.     void write( int c)
  42.     {
  43.         output.put( "file? ");
  44.         char buf[128];
  45.         char *p = buf;
  46.  
  47.         // candidate member function of File:  getline( char *buf)
  48.         while( (c = input.get()) != '\n' )
  49.             *p++ = c;
  50.         *p = 0;
  51.         if( !*p) 
  52.             p = filename;
  53.  
  54.         File f( p, "w" );
  55.         if( f.isok() )
  56.             for( ; !b.isend(); b.next() )
  57.                 f.put( b.geta() );
  58.         else
  59.             output.put( "cannot open file\n" );
  60.     }
  61.     
  62.     void quit( int c)   { exit(0); }
  63.  
  64.     void isinteger( int c)
  65.     {
  66.         char buf[64];
  67.         char *p = buf;
  68.  
  69.         for( *p = c; !input.iseof(); p++) {
  70.             c = input.get();
  71.             if( isdigit(c) )
  72.                 *p = c;
  73.             else {
  74.                 input.unget( c);
  75.                 break;
  76.             }
  77.         }
  78.  
  79.         *p = 0;  // null termination of string
  80.         ireg.load( atoi( buf));
  81.         rp = &ireg;
  82.     }
  83.  
  84.     void eerror( int c) { output.put( "?\n"); } // error function
  85.  
  86.     void donothing( int c)    {}
  87. private:
  88.     Buffer b;
  89.     File input;
  90.     File output;
  91.     char filename[128];
  92.  
  93.     Register Editor::*rp;
  94.     Register reg;
  95.     Iregister ireg;
  96.     Buffer kbuf;  // kill buffer
  97.     static void (Editor::*action[128])( int character);
  98. };
  99.  
  100. // here we initialize the action[] array
  101. // most entries are &Editor::eerror.  To save space, we show the 
  102. // non eerror entries
  103.  
  104. void (Editor::*action[128])( int character) = {
  105.     &Editor::eerror,  // '\0'
  106.     ...
  107.     &Editor::donothing,  // tab
  108.     &Editor::donothing,  // newline
  109.     ...
  110.     &Editor::donothing,  // 0x20  blank
  111.     ...
  112.     &Editor::isinteger,  // 0x30 '0'
  113.     &Editor::isinteger,  // '1'
  114.     &Editor::isinteger,  // '2'
  115.     &Editor::isinteger,  // '3'
  116.     &Editor::isinteger,  // '4'
  117.     &Editor::isinteger,  // '5'
  118.     &Editor::isinteger,  // '6'
  119.     &Editor::isinteger,  // '7'
  120.     &Editor::isinteger,  // '8'
  121.     &Editor::isinteger,  // '9'
  122.     ...
  123.     &Editor::info,    // '?'
  124.     ...
  125.     &Editor::del,    // 'd'
  126.     &Editor::eerror,
  127.     &Editor::eerror,
  128.     &Editor::go,     // 'g'
  129.     &Editor::eerror,
  130.     &Editor::insert, // 'i'
  131.     ...
  132.     &Editor::print,  // 0x70 'p'
  133.     &Editor::quit,   // 'q'
  134.     ...
  135.     &Editor::write,  // 'w'
  136.     &Editor::eerror,
  137.     &Editor::put,    // 'y'
  138.     ...
  139.     &Editor::eerror  // 0x80
  140. };
  141.  
  142.