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

  1. /* -*- Mode: C -*- */
  2. /* PrintCards.cc - Main program
  3.  * Created by Robert Heller on Tue Feb 25 18:53:40 1992
  4.  *
  5.  * ------------------------------------------------------------------
  6.  * Home Libarian by Deepwoods Software
  7.  * Print card records from a librarian data file
  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. #ifdef MESSYDOS
  24. #include <CardRec.h>
  25. #include <ListRec.h>
  26. #else
  27. #include <CardRecord.h>
  28. #include <ListRecord.h>
  29. #endif
  30. #include <PTree.h>
  31. #define VERSION "V1.0Beta"
  32. #ifdef MESSYDOS
  33. #include "PrtCards.h"
  34. #else
  35. #include "PrintCards.h"
  36. #endif
  37.  
  38. extern Tree* ParseOnly(char*);
  39. extern Boolean EvalExpr(Card*, Tree*);
  40. static Boolean Large_P = false;
  41. static vBTree *lTree;
  42. static Tree* onlyexpr = 0;
  43.  
  44. void PrintCardRecord3x5(CoreItem* item,int level,char* heading)
  45. {
  46.     int column;
  47.     int lines = 0;
  48.     cout << item->key;
  49.     for(int space = ((50-strlen(heading))/2) - strlen(item->key);space > 0;space--) cout << ' ';
  50.     cout << heading << "\n\n";
  51.     if (item->data.size <= 0) {
  52.         cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
  53.     } else {
  54.         CardRecord rec(&item->data);
  55.         register Card *c = &rec;
  56.         char* tn = TypeName(c->type);
  57.         cout << c->author << ", " << c->year << "\n";
  58.         cout << "    " << c->title << "\n";
  59.         cout << "    " << tn << "\n";
  60.         cout << "    Published by " << c->publisher <<
  61.             " " << c->city << "\n";
  62.         if (c->vol != 0) cout << "    Volume: " << c->vol;
  63.         cout << "\n\n"; column = 0;
  64.         for (char* p = c->description; lines < 10 && *p != 0; p++) {
  65.             if (*p != '\n' && column < 4) {
  66.                 cout << "    ";
  67.                 column = 4;
  68.             }
  69.             if (*p == '\n') {
  70.                 cout << "\n";
  71.                 lines++;
  72.                 column = 0;
  73.             } else if (column > 48) {
  74.                 cout << "\\\n";
  75.                 lines++;
  76.                 column = 0;
  77.                 p--;
  78.             } else if (*p == '\t') {
  79.                 do {
  80.                     cout << " ";
  81.                     column++;
  82.                 } while ((column % 8) != 0);
  83.             } else {
  84.                 cout << *p;
  85.                 column++;
  86.             }
  87.         }
  88.         if (column > 0) {
  89.             cout << "\n";
  90.             lines++;
  91.         }
  92.         while (lines++ < 10) cout << "\n";
  93.     }
  94. }
  95.  
  96. void PrintCardRecord5x8(CoreItem* item,int level,char* heading)
  97. {
  98.     int column;
  99.     int lines = 0;
  100.     cout << item->key;
  101.     for(int space = ((80-strlen(heading))/2) - strlen(item->key);space > 0;space--) cout << ' ';
  102.     cout << heading << "\n\n";
  103.     if (item->data.size <= 0) {
  104.         cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
  105.     } else {
  106.         CardRecord rec(&item->data);
  107.         register Card *c = &rec;
  108.         char* tn = TypeName(c->type);
  109.         cout << c->author << ", " << c->year << "\n";
  110.         cout << "    " << c->title << "\n";
  111.         cout << "    " << tn << "\n";
  112.         cout << "    Published by " << c->publisher <<
  113.             " " << c->city << "\n";
  114.         if (c->vol != 0) cout << "    Volume: " << c->vol;
  115.         cout << "\n\n"; column = 0;
  116.         for (char* p = c->description; lines < 22 && *p != 0; p++) {
  117.             if (*p != '\n' && column < 4) {
  118.                 cout << "    ";
  119.                 column = 4;
  120.             }
  121.             if (*p == '\n') {
  122.                 cout << "\n";
  123.                 lines++;
  124.                 column = 0;
  125.             } else if (column > 78) {
  126.                 cout << "\\\n";
  127.                 lines++;
  128.                 column = 0;
  129.                 p--;
  130.             } else if (*p == '\t') {
  131.                 do {
  132.                     cout << " ";
  133.                     column++;
  134.                 } while ((column % 8) != 0);
  135.             } else {
  136.                 cout << *p;
  137.                 column++;
  138.             }
  139.         }
  140.         if (column > 0) {
  141.             cout << "\n";
  142.             lines++;
  143.         }
  144.         while (lines++ < 22) cout << "\n";
  145.     }
  146. }
  147.  
  148. void PrintCardRecord1(CoreItem* item,int level,char* heading)
  149. {
  150.     if (item->data.size <= 0) return;
  151.     if (onlyexpr != 0) {
  152.         CardRecord rec(&item->data);
  153.         register Card* c = &rec;
  154.         if (!EvalExpr(c,onlyexpr)) return;
  155.     }
  156.     if (Large_P) PrintCardRecord5x8(item,level,heading);
  157.     else PrintCardRecord3x5(item,level,heading);
  158. }
  159.  
  160.  
  161. void PrintCardRecord(CoreItem* item,int level)
  162. {
  163.     PrintCardRecord1(item,level,"Shelf List");
  164. }
  165.  
  166. static Boolean strequ(char* a,char* b)
  167. {
  168.     Boolean eqp;
  169.     char aa, bb;
  170.     do {
  171.         aa = *a++;
  172.         bb = *b++;
  173.         if (islower(aa)) aa = toupper(aa);
  174.         if (islower(bb)) bb = toupper(bb);
  175.         eqp = aa == bb;
  176.     } while (eqp && aa != 0 && bb != 0);
  177.     return(eqp);
  178. }
  179.  
  180. void PrintListRecord(CoreItem *item,int level)
  181. {
  182.     if (item->data.size > 0) {
  183.         ListRecord rec(&item->data);
  184.         int numitems = rec.ElementCount();
  185. #ifdef ILLEGAL_INSTR
  186.         static CoreItem temp;
  187.         for (int i = 0;i < numitems; i++) {
  188.             if (lTree->SearchId(rec[i],&temp) &&
  189.                 strlen(rec[i]) == strlen(temp.key)) 
  190.                     PrintCardRecord1(&temp,level,item->key);
  191.         }
  192. #else
  193.         CoreItem *temp = new CoreItem;
  194.         for (int i = 0;i < numitems; i++) {
  195.             if (lTree->SearchId(rec[i],temp) &&
  196.                 strlen(rec[i]) == strlen(temp->key)) 
  197.                     PrintCardRecord1(temp,level,item->key);
  198.         }
  199.         delete temp;
  200. #endif
  201.     }
  202. }
  203.  
  204. static void ErrorHandler(ErrKind err,char* msg)
  205. {
  206.     if (err == sysErr) {
  207.         int error = errno;
  208.         cerr << form("Error: System:%d %s %s\n",error,strerror(error),
  209.                 msg);
  210.     } else {
  211.         cerr << form("Error: %s %s\n",
  212.             (err == termErr ? "Terminal" : "Memory"),
  213.             msg);
  214.     }
  215. }
  216.     
  217. int main(int argc,char **argv)
  218. {
  219.     PrintCards args(argc,argv);
  220.     vBTree vtree(args.infile,(OpenMode)(ReadOnly));
  221.     if (vtree.OpenStat() == failure) {
  222.         int error = errno;
  223.         cerr << "Could not open " << args.infile
  224.              << ": " << strerror(error) << "\n";
  225.         exit(error);
  226.     }
  227.     vtree.ErrorFun() = ErrorHandler;
  228.     if (args.largep_passed) Large_P = args.largep;
  229.     lTree = &vtree;
  230.     
  231.     if (args.only_passed) {
  232.         onlyexpr = ParseOnly(args.only);
  233.         if (onlyexpr == 0) exit(1);
  234.     }
  235.             
  236.     if (args.by_passed) {
  237.         if (strequ(args.by,"ID")) vtree.TraverseId(PrintCardRecord);
  238.         else if (strequ(args.by,"TITLE")) vtree.TraverseTitle(PrintListRecord);
  239.         else if (strequ(args.by,"AUTHOR")) vtree.TraverseAuthor(PrintListRecord);
  240.         else if (strequ(args.by,"SUBJECT")) vtree.TraverseSubj(PrintListRecord);
  241.         else {
  242.             cout << "Bad -by value: " << args.by << "\n";
  243.             args.Usage();
  244.             exit(1);
  245.         }
  246.     } else vtree.TraverseId(PrintCardRecord);
  247. }
  248.  
  249.     
  250.  
  251.  
  252.