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

  1. /* --------------------------------------------------------------------------
  2.  * $RCSfile: eatool.cpp,v $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1996/05/22 20:59:55 $
  5.  * $Author: Bablok $
  6.  * --------------------------------------------------------------------------
  7.  * Synopsis:
  8.  *
  9.  * Simple commandline EA tool.
  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: eatool.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 <iomanip.h>
  36. #include <istring.hpp>
  37. #include <iexcbase.hpp>
  38. #include "EA.hpp"
  39. #include "EAList.hpp"
  40.  
  41. #define INDENT_DELTA 3
  42.  
  43. void usage(const char* pgmName);
  44. void dumpEA(const EA& ea, int indent);
  45. void dumpEAList(const EAList& eaList, int indent);
  46.  
  47. int main(int argc, char *argv[]) {
  48.  
  49.    if (argc < 2 || argc > 5 || *argv[1] != '-' ||
  50.        (*(argv[1]+1) != 'r' && *(argv[1]+1) != 'w' && *(argv[1]+1) != 'd') ||
  51.                                         (*(argv[1]+1) == 'r' && argc >  4) ||
  52.                                         (*(argv[1]+1) == 'w' && argc != 5) ||
  53.                                         (*(argv[1]+1) == 'd' && argc >  4)   )
  54.       usage(argv[0]);
  55.    try {
  56.       switch (*(argv[1]+1)) {
  57.          case 'r':
  58.             if (argc == 3) {
  59.                EAList list(argv[2]);
  60.                dumpEAList(list,0);
  61.             } else {
  62.                EA ea(argv[3]);
  63.                ea.read(argv[2]);
  64.                dumpEA(ea,0);
  65.             }
  66.             return 0;
  67.          case 'w': {
  68.             IString name(argv[3]), value(argv[4]);
  69.             EA ea(name,value);
  70.             ea.write(argv[2]);
  71.             return 0;
  72.          } case 'd':
  73.             if (argc == 3) {
  74.                EAList list;
  75.                list.remove(argv[2],false);
  76.             } else {
  77.                EA ea(argv[3]);
  78.                ea.remove(argv[2]);
  79.             }
  80.             return 0;
  81.          default:
  82.             usage(argv[0]);
  83.            break;
  84.       }
  85.       return 0;
  86.    }
  87.    catch (IException& exc) {
  88.       cerr << "Error:" << endl;
  89.       for (int i=0; i<exc.textCount(); ++i)
  90.          cerr << "   " << exc.text(i) << endl;
  91.       return 3;
  92.    }
  93. }
  94.  
  95. ///////////////////////////////////////////////////////////////////////////////
  96. // dumpEA(): Formatted output of EA
  97. //
  98. void dumpEA(const EA& ea, int indent) {
  99.    if (ea.value() == "")
  100.       return;
  101.  
  102.    cout.setf(ios::right,ios::adjustfield);
  103.    cout << setw(7+indent) << "name:  ";
  104.    cout.setf(ios::left,ios::adjustfield);
  105.    cout << ea.name() << endl;
  106.  
  107.    cout.setf(ios::right,ios::adjustfield);
  108.    cout << setw(7+indent) << "type:  ";
  109.    cout.setf(ios::left,ios::adjustfield);
  110.    cout << ea.typeAsString();
  111.    if (ea.flag() == FEA_NEEDEA)
  112.       cout << " (EA is critical)";
  113.    cout << endl;
  114.  
  115.    cout.setf(ios::right,ios::adjustfield);
  116.    cout << setw(7+indent) << "value: ";
  117.    cout.setf(ios::left,ios::adjustfield);
  118.    if (ea.type() != EAT_MVMT && ea.type() != EAT_MVST)
  119.       if (ea.type() == EAT_ASCII)
  120.          cout << ea.value() << endl;
  121.       else
  122.          cout << IString::c2x(ea.value()) << endl;
  123.    else {
  124.       cout << endl;
  125.       EAList list(ea.name(),ea);
  126.       dumpEAList(list,indent+INDENT_DELTA);
  127.    }
  128.    return;
  129. }
  130.  
  131.  
  132. ///////////////////////////////////////////////////////////////////////////////
  133. // dumpEAList(): Formatted output of EAList
  134. //
  135. void dumpEAList(const EAList& eaList, int indent) {
  136.    EAList::Cursor current(eaList);
  137.    forCursor(current) {
  138.       dumpEA(current.element(),indent);
  139.       cout << endl;
  140.    }
  141.    return;
  142. }
  143.  
  144. ///////////////////////////////////////////////////////////////////////////////
  145. // usage(): Show syntax
  146. //
  147. void usage(const char* pgmName) {
  148.    cerr << "EATool (c) by Bernhard Bablok, 1996\n\n"
  149.            "This program is distributed in the hope that it will be\n"
  150.            "useful, but WITHOUT ANY WARRANTY; without even the implied\n"
  151.            "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
  152.  
  153.            "Usage: " << pgmName << " -[rwd] fileName [eaName] [eaValue]\n"
  154.            "\tr: Read   (all EAs or the EA with name eaName) \n"
  155.            "\tw: Write  (sets value of eaName to eaValue)\n"
  156.            "\td: Delete (all EAs or EA with name eaName)" << endl;
  157.   exit(3);
  158. }
  159.