home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / hl10osrc.lzh / PrintLabels / PrintLabels.cc < prev    next >
C/C++ Source or Header  |  1994-04-23  |  6KB  |  238 lines

  1. /* -*- Mode: C -*- */
  2. /* PrintLabels.cc - Main program
  3.  * Created by Robert Heller on Sat Feb 29 16:24:09 1992
  4.  *
  5.  * ------------------------------------------------------------------
  6.  * Home Libarian by Deepwoods Software
  7.  * Label printing program
  8.  * ------------------------------------------------------------------
  9.  * Modification History:
  10.  * ------------------------------------------------------------------
  11.  * Contents:
  12.  * ------------------------------------------------------------------
  13.  * 
  14.  * 
  15.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  16.  *        All Rights Reserved
  17.  * 
  18.  */
  19.  
  20. #include <stream.h>
  21. #include <vBTree.h>
  22. #include <ctype.h>
  23. #include <fstream.h>
  24. #ifdef MESSYDOS
  25. #include <CardRec.h>
  26. #include <ListRec.h>
  27. #else
  28. #include <CardRecord.h>
  29. #include <ListRecord.h>
  30. #endif
  31. #include <PTree.h>
  32. #define VERSION "V1.0Beta"
  33. #ifdef MESSYDOS
  34. #include "PrtLabls.h"
  35. #else
  36. #include "PrintLabels.h"
  37. #endif
  38.  
  39. extern Tree* ParseOnly(char*);
  40. extern Boolean EvalExpr(Card*, Tree*);
  41. static vBTree *lTree;
  42. static Tree* onlyexpr = 0;
  43. static ifstream tfile;
  44. static char *tfilename;
  45.  
  46. enum PrintFields {PrintType, PrintAuthor, PrintTitle, PrintPublisher,
  47.           PrintCity, PrintVolume, PrintYear, PrintIdent,
  48.           PrintDescription, IllField };
  49.  
  50. static const struct {
  51.     const char* n;
  52.     PrintFields f;
  53. } FieldNames[] = {
  54.     { "TYPE", PrintType },
  55.     { "AUTHOR", PrintAuthor },
  56.     { "TITLE", PrintTitle },
  57.     { "PUBLISHER", PrintPublisher },
  58.     { "CITY", PrintCity },
  59.     { "VOLUME", PrintVolume },
  60.     { "YEAR", PrintYear },
  61.     { "ID", PrintIdent },
  62.     { "DESCRIPTION", PrintDescription }
  63. };
  64.  
  65. const NumFields = sizeof(FieldNames) / sizeof(FieldNames[0]);
  66.  
  67. void PrintCardRecord(CoreItem* item,int level)
  68. {
  69.     if (item->data.size <= 0) return;
  70.     CardRecord rec(&item->data);
  71.     register Card* c = &rec;
  72.     if (onlyexpr != 0) {
  73.         if (!EvalExpr(c,onlyexpr)) return;
  74.     }
  75.     tfile.open(tfilename,ios::in);
  76.     if (!tfile.is_open()) {
  77.         int error = errno;
  78.         perror("PrintLabels: template file open");
  79.         cerr << "PrintLabels: could not open " << tfilename << "\n";
  80.         exit(errno);
  81.     }
  82.  
  83.     //cerr << "*** card: " << item->key << "\n";
  84.     //cerr << "*** tfile.eof() = " << tfile.eof() <<
  85.     //    ", tfile.fail() = " << tfile.fail() << "\n";
  86.     while (!tfile.eof() && !tfile.fail()) {
  87.         char ch;
  88.         tfile.get(ch);
  89.         //cerr << "*** ch = " << ((int) ch) << "\n";
  90.         if (ch == EOF) break;
  91.         else if (ch != '%') cout << ch;
  92.         else {
  93.             tfile.get(ch);
  94.             if (ch == '%') cout << ch;
  95.             else if (ch == EOF) break;
  96.             else if (isalpha(ch)) {
  97.                 static char word[256];
  98.                 register char* p = word;
  99.                 register PrintFields field = IllField;
  100.                 do {
  101.                     if (islower(ch)) ch = toupper(ch);
  102.                     *p++ = ch;
  103.                     tfile.get(ch);
  104.                 } while (isalpha(ch));
  105.                 if (ch != EOF) tfile.putback(ch);
  106.                 *p = 0;
  107.                 for (int i = 0; i < NumFields; i++) {
  108.                     if (strcmp(word,FieldNames[i].n) == 0) {
  109.                         field = FieldNames[i].f;
  110.                         break;
  111.                     }
  112.                 }
  113.                 if (field == IllField) cerr << "Bad field name " << word << " ignored.\n";
  114.                 else {
  115.                     switch (field) {
  116.                         case PrintType:
  117.                             cout << TypeName(c->type);
  118.                             break;
  119.                         case PrintAuthor:
  120.                             cout << c->author;
  121.                             break;
  122.                         case PrintTitle:
  123.                             cout << c->title;
  124.                             break;
  125.                         case PrintPublisher:
  126.                             cout << c->publisher;
  127.                             break;
  128.                         case PrintCity:
  129.                             cout << c->city;
  130.                             break;
  131.                         case PrintVolume:
  132.                             cout << c->vol;
  133.                             break;
  134.                         case PrintYear:
  135.                             cout << c->year;
  136.                             break;
  137.                         case PrintIdent:
  138.                             cout << item->key;
  139.                             break;
  140.                         case PrintDescription:
  141.                             cout << c->description;
  142.                             break;
  143.                     }
  144.                 }
  145.             } else {
  146.                 cerr << "Bad field name char " << ch << " ignored.\n";
  147.             }
  148.         }
  149.     }
  150.     tfile.close();
  151. }
  152.  
  153. static Boolean strequ(char* a,char* b)
  154. {
  155.     Boolean eqp;
  156.     char aa, bb;
  157.     do {
  158.         aa = *a++;
  159.         bb = *b++;
  160.         if (islower(aa)) aa = toupper(aa);
  161.         if (islower(bb)) bb = toupper(bb);
  162.         eqp = aa == bb;
  163.     } while (eqp && aa != 0 && bb != 0);
  164.     return(eqp);
  165. }
  166.  
  167. void PrintListRecord(CoreItem *item,int level)
  168. {
  169.     if (item->data.size > 0) {
  170.         ListRecord rec(&item->data);
  171.         int numitems = rec.ElementCount();
  172. #ifdef ILLEGAL_INSTR
  173.         static CoreItem temp;
  174.         for (int i = 0;i < numitems; i++) {
  175.             if (lTree->SearchId(rec[i],&temp) &&
  176.                 strlen(rec[i]) == strlen(temp.key)) 
  177.                     PrintCardRecord(&temp,level);
  178.         }
  179. #else
  180.         CoreItem* temp = new CoreItem;
  181.         for (int i = 0;i < numitems; i++) {
  182.             if (lTree->SearchId(rec[i],temp) &&
  183.                 strlen(rec[i]) == strlen(temp->key)) 
  184.                     PrintCardRecord(temp,level);
  185.         }
  186.         delete temp;
  187. #endif
  188.     }
  189. }
  190.  
  191. static void ErrorHandler(ErrKind err,char* msg)
  192. {
  193.     if (err == sysErr) {
  194.         int error = errno;
  195.         cerr << form("Error: System:%d %s %s\n",error,strerror(error),
  196.                 msg);
  197.     } else {
  198.         cerr << form("Error: %s %s\n",
  199.             (err == termErr ? "Terminal" : "Memory"),
  200.             msg);
  201.     }
  202. }
  203.     
  204. int main(int argc,char **argv)
  205. {
  206.     PrintLabels args(argc,argv);
  207.     vBTree vtree(args.infile,(OpenMode)(ReadOnly));
  208.     if (vtree.OpenStat() == failure) {
  209.         int error = errno;
  210.         cerr << "Could not open " << args.infile
  211.              << ": " << strerror(error) << "\n";
  212.         exit(error);
  213.     }
  214.     vtree.ErrorFun() = ErrorHandler;
  215.     lTree = &vtree;
  216.     
  217.     if (args.only_passed) {
  218.         onlyexpr = ParseOnly(args.only);
  219.         if (onlyexpr == 0) exit(1);
  220.     }
  221.             
  222.     tfilename = args.templatefile;
  223.  
  224.     if (args.by_passed) {
  225.         if (strequ(args.by,"ID")) vtree.TraverseId(PrintCardRecord);
  226.         else if (strequ(args.by,"TITLE")) vtree.TraverseTitle(PrintListRecord);
  227.         else if (strequ(args.by,"AUTHOR")) vtree.TraverseAuthor(PrintListRecord);
  228.         else if (strequ(args.by,"SUBJECT")) vtree.TraverseSubj(PrintListRecord);
  229.         else {
  230.             cout << "Bad -by value: " << args.by << "\n";
  231.             args.Usage();
  232.             exit(1);
  233.         }
  234.     } else vtree.TraverseId(PrintCardRecord);
  235. }
  236.  
  237.  
  238.