home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / eaclib.zip / tdrive.cpp < prev    next >
C/C++ Source or Header  |  1996-05-22  |  7KB  |  208 lines

  1. /* --------------------------------------------------------------------------
  2.  * $RCSfile: tdrive.cpp,v $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1996/05/22 20:59:55 $
  5.  * $Author: Bablok $
  6.  * --------------------------------------------------------------------------
  7.  * Synopsis:
  8.  *
  9.  * Test driver program for class EA and EAList.
  10.  *
  11.  * This file is part of the EA classlib package.
  12.  * Copyright Bernhard Bablok, 1996
  13.  *
  14.  * The EA classlib package is distributed in the hope that it will be
  15.  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  16.  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17.  *
  18.  * You may use the classes in the package to any extend you wish. You are
  19.  * allowed to change and copy the source of the classes, as long as you keep
  20.  * the copyright notice intact and as long as you document the changes you made.
  21.  *
  22.  * You are not allowed to sell the EA classlib package or a modified version
  23.  * thereof, but you may charge for costs of distribution media.
  24.  *
  25.  * --------------------------------------------------------------------------
  26.  * Change-Log:
  27.  *
  28.  * $Log: tdrive.cpp,v $
  29.  * Revision 1.1  1996/05/22 20:59:55  Bablok
  30.  * Initial revision
  31.  *
  32.  * -------------------------------------------------------------------------- */
  33.  
  34. #include <fstream.h>
  35. #include <istring.hpp>
  36. #include <iexcbase.hpp>
  37. #include "EA.hpp"
  38. #include "EAList.hpp"
  39.  
  40. void dumpEA(const EA& ea);
  41. void dumpEAList(const EAList& eaList);
  42.  
  43. int main(int argc, char *argv[]) {
  44.  
  45.    IString basename, name, value, file, dumpFile;
  46.    fstream stream;
  47.    EAList eaList;
  48.    EA     ea("dummy",IString("dummy"));
  49.    if (argc > 1)
  50.       file = argv[1];
  51.  
  52.    cout << "Test driver for class EA:\n\n";
  53.    while (1) {
  54.       char answer;
  55.       cout << "Select:\n\n"
  56.               "1 filename       8 write  EA   b write  list\n"
  57.               "2 create EA      9 remove EA   c remove list\n"
  58.               "3 create EAList  a read   EA   d remove all\n"
  59.               "4 EAList->EA                   e read list\n"
  60.               "5 EA->EAList                   f read all\n"
  61.               "6 print EA                     g dump to file\n"
  62.               "7 print EAList                 h read from file\n"
  63.               "q quit"
  64.            << endl;
  65.       cin >> answer; cin.ignore(80,'\n');
  66.       try {
  67.          switch (answer) {
  68.             case 'q':
  69.             case 'Q':
  70.                return 0;
  71.             case '1':                                     // set file
  72.                cout << "Enter filename:";
  73.                cin >> file;
  74.                break;
  75.             case '2':                                     // define EA
  76.                cout << "Enter name of EA:";
  77.                cin >> name;
  78.                cout << "Enter value of EA:";
  79.                value = IString::lineFrom(cin);
  80.                ea.setName(name);
  81.                ea.setValue(value);
  82.                break;
  83.             case '3':                                     // create EAList
  84.                eaList.removeAll();
  85.                while (1) {
  86.                   cout << "Enter name of EA:";
  87.                   name = IString::lineFrom(cin);
  88.                   if (name == "")
  89.                      break;
  90.                   cout << "Enter value of EA:";
  91.                   value = IString::lineFrom(cin);
  92.                   eaList.addOrReplaceElementWithKey(EA(name,value));
  93.                }
  94.                break;
  95.             case '4':
  96.                ea = EA("list",eaList);
  97.                break;
  98.             case '5':
  99.                cout << "Enter basename for EAList:";
  100.                basename = IString::lineFrom(cin);
  101.                eaList = EAList(basename,ea);
  102.                break;
  103.             case '6':
  104.                dumpEA(ea);
  105.                break;
  106.             case '7':
  107.                dumpEAList(eaList);
  108.                break;
  109.             case '8':
  110.                ea.write(file);
  111.                cout << "EA successfully written!" << endl;
  112.                dumpEA(ea);
  113.                break;
  114.             case '9':
  115.                ea.remove(file);
  116.                cout << "EA successfully removed!" << endl;
  117.                dumpEA(ea);
  118.                break;
  119.             case 'a':
  120.                ea.read(file);
  121.                cout << "EA successfully read!" << endl;
  122.                dumpEA(ea);
  123.                break;
  124.             case 'b':
  125.                eaList.write(file);
  126.                cout << "EAList successfully written!" << endl;
  127.                dumpEAList(eaList);
  128.                break;
  129.             case 'c':
  130.                eaList.remove(file);
  131.                cout << "EAs from list successfully removed!" << endl;
  132.                dumpEAList(eaList);
  133.                break;
  134.             case 'd':
  135.                eaList.remove(file,false);
  136.                cout << "All EAs successfully removed!" << endl;
  137.                dumpEAList(eaList);
  138.                break;
  139.             case 'e':
  140.                eaList.read(file);
  141.                cout << "EAs read from list:" << endl;
  142.                dumpEAList(eaList);
  143.                break;
  144.             case 'f':
  145.                eaList.read(file,false);
  146.                cout << "All EAs from file:" << endl;
  147.                dumpEAList(eaList);
  148.                break;
  149.             case 'g':
  150.                cout << "Enter filename to dump to:" << endl;
  151.                dumpFile = IString::lineFrom(cin);
  152.                stream.open(dumpFile,ios::bin|ios::out);
  153.                if (stream) {
  154.                   stream << eaList;
  155.                   stream.close();
  156.                   cout << "EAList written to file " << dumpFile << endl;
  157.                } else
  158.                   cout << "Open failed for file " << dumpFile << endl;
  159.                break;
  160.             case 'h':
  161.                cout << "Enter filename to read from:" << endl;
  162.                dumpFile = IString::lineFrom(cin);
  163.                stream.open(dumpFile,ios::bin|ios::in);
  164.                if (stream) {
  165.                   stream >> eaList;
  166.                   stream.close();
  167.                   cout << "EAList from file " << dumpFile << ":" << endl;
  168.                   dumpEAList(eaList);
  169.                } else
  170.                   cout << "Open failed for file " << dumpFile << endl;
  171.                break;
  172.             default:
  173.               break;
  174.          }  // endswitch
  175.       } catch (IException& exc) { }
  176.    }  // end while(1)
  177. }
  178.  
  179.  
  180. void dumpEA(const EA& ea) {
  181.    switch (ea.type()) {
  182.       case EAT_ASCII:
  183.          cout << ea.name() <<": >" << ea.value() << "<" << endl;
  184.          break;
  185.       case EAT_MVMT:
  186.       case EAT_MVST:
  187.           try {
  188.              cout << ea.name() << " [" << endl;
  189.              EAList list("value",ea);
  190.              dumpEAList(list);
  191.              cout << "]" << endl;
  192.           }
  193.           catch (IInvalidRequest& exc) {
  194.             cout << ea.name() <<": >" << IString::c2x(ea.value()) << "<" << endl;
  195.           }
  196.          break;
  197.       default:
  198.          cout << ea.name() <<": >" << IString::c2x(ea.value()) << "<" << endl;
  199.         break;
  200.    }
  201. }
  202.  
  203. void dumpEAList(const EAList& eaList) {
  204.    EAList::Cursor current(eaList);
  205.    forCursor(current)
  206.       dumpEA(current.element());
  207. }
  208.