home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / hl10osrc.zoo / Ascii2Libr / Ascii2Libr.cc < prev    next >
C/C++ Source or Header  |  2009-11-06  |  7KB  |  297 lines

  1. /* -*- Mode: C -*- */
  2. /* Ascii2Libr.cc - Read in an ASCII text file representing a library
  3.  *           into a libr file
  4.  * Created by Robert Heller on Mon Dec  9 19:04:45 1991
  5.  *
  6.  * ------------------------------------------------------------------
  7.  * Home Libarian by Deepwoods Software
  8.  * Progam to read in an Ascii text file describing a library into a
  9.  * Library Card Catalog file.
  10.  * ------------------------------------------------------------------
  11.  * Modification History:
  12.  * ------------------------------------------------------------------
  13.  * Contents:
  14.  * ------------------------------------------------------------------
  15.  * 
  16.  * 
  17.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  18.  *        All Rights Reserved
  19.  * 
  20.  */
  21.  
  22. #include <stream.h>
  23. #include <vBTree.h>
  24. #include <ctype.h>
  25. #ifdef MESSYDOS
  26. #include <CardRec.h>
  27. #include <ListRec.h>
  28. #else
  29. #include <CardRecord.h>
  30. #include <ListRecord.h>
  31. #endif
  32. #define VERSION "V1.0Beta"
  33. #ifdef MESSYDOS
  34. #include "Asc2Libr.h"
  35. #else
  36. #include "Ascii2Libr.h"
  37. #endif
  38.  
  39. const initbuffsize = 2048;
  40. const buffergrowth = 2048;
  41.  
  42. char* ReadQuotedString(istream& in)
  43. {
  44.     char* outbuffer;
  45.     char* newbuffer;
  46.     char* p;
  47.     char  ch;
  48.     int   buffsize, charsleft, chindex;
  49.  
  50.     //cerr << "*** Entering ReadQuotedString\n";
  51.     do {
  52.         in.get(ch);
  53.         //cerr << "*** Skiping '" << ch << "'\n";
  54.     } while (ch != '"');
  55.     outbuffer = new char[initbuffsize];
  56.     buffsize = initbuffsize;
  57.     charsleft = initbuffsize;
  58.     chindex = 0;
  59.     p = outbuffer;
  60.     while (true) {
  61.         in.get(ch);
  62.         if (ch == '"') break;
  63.         else if (ch == '\\') {
  64.             in >> ch;
  65.             //cerr << "*** Inserting '" << ch << "' (was escaped)\n";
  66.             *p++ = ch;
  67.             charsleft--;
  68.             chindex++;
  69.         } else {
  70.             //cerr << "*** Inserting '" << ch << "'\n";
  71.             *p++ = ch;
  72.             charsleft--;
  73.             chindex++;
  74.         }
  75.         if (charsleft < 1) {
  76.             newbuffer = new char[buffsize+buffergrowth];
  77.             strcpy(newbuffer,outbuffer);
  78.             delete outbuffer;
  79.             outbuffer = newbuffer;
  80.             buffsize += buffergrowth;
  81.             p = outbuffer+chindex;
  82.             charsleft = buffergrowth;
  83.         }
  84.     }
  85.     *p = 0;
  86.     return outbuffer;
  87. }
  88.  
  89. main(int argc,char** argv)
  90. {
  91.     int numpages,numkeys;
  92.     char* p;
  93.     Key key;
  94.     char ch;
  95.     static char wordbuffer[80];
  96.     static char* vector[2048];
  97.     int ielt;
  98.     Card tempcard(Other,0,0,0,0,0,0,0);
  99.     static Ascii2Libr args(argc,argv);
  100.     cin >> numpages;
  101.     if (args.minpages_passed && args.minpages > numpages)
  102.         numpages = args.minpages;
  103.     vBTree outtree(args.outfile,(OpenMode)(ReadWrite|Create),numpages);
  104.     switch (outtree.OpenStat()) {
  105.         case failure: int error = errno;
  106.                   cerr << "Could not create " << args.outfile
  107.                    << ": " << strerror(error) << "\n";
  108.                   exit(error);
  109.         case openold: cerr << args.outfile << " already exists!\n";
  110.                   exit(1);
  111.     }                  
  112.     cin >> numkeys;
  113.     for (int i = 0;i < numkeys;i++) {
  114.         p = ReadQuotedString(cin);
  115.         strncpy(key,p,36);
  116.         key[35] = 0;
  117.         delete p;
  118.         //cerr << "*** Key = |" << key << "|\n";
  119.         do {
  120.             cin >> ch;
  121.         } while (ch != '#');
  122.         cin >> ch;
  123.         if (ch != 'C') {
  124.             cerr << form("Syntax error reading Card %d: expected a C, saw a %c\n",
  125.                      i,ch);
  126.             exit(1);
  127.         }
  128.         cin >> ch;
  129.         if (ch != '(') {
  130.             cerr << form("Syntax error reading Card %d: expected a (, saw a %c\n",
  131.                      i,ch);
  132.             exit(1);
  133.         }
  134.         while (true) {
  135.             while (true) {
  136.                 cin >> ch;
  137.                 //cerr << "*** ch = '" << ch << "'\n";
  138.                 if (ch == ')') break;
  139.                 else if (ch > ' ') {
  140.                     //cerr << "*** putting back '" << ch << "'\n";
  141.                     cin.putback(ch);
  142.                     break;
  143.                 }
  144.             }
  145.             if (ch == ')') break;
  146.             cin >> wordbuffer;
  147.             //cerr << "*** wordbuffer = |" << wordbuffer << "|\n";
  148.             for (p = wordbuffer;*p != 0;p++) {
  149.                 ch = *p;
  150.                 if (islower(ch)) ch = toupper(ch);
  151.                 *p = ch;
  152.             }
  153.             if (strcmp(wordbuffer,":TYPE") == 0) {
  154.                 cin >> wordbuffer;
  155.                 tempcard.type = NameType(wordbuffer);
  156.             } else if (strcmp(wordbuffer,":AUTHOR") == 0) {
  157.                 tempcard.author = ReadQuotedString(cin);
  158.             } else if (strcmp(wordbuffer,":TITLE") == 0) {
  159.                 tempcard.title = ReadQuotedString(cin);
  160.             } else if (strcmp(wordbuffer,":PUBLISHER") == 0) {
  161.                 tempcard.publisher = ReadQuotedString(cin);
  162.             } else if (strcmp(wordbuffer,":CITY") == 0) {
  163.                 tempcard.city = ReadQuotedString(cin);
  164.             } else if (strcmp(wordbuffer,":DESCRIPTION") == 0) {
  165.                 tempcard.description = ReadQuotedString(cin);
  166.             } else if (strcmp(wordbuffer,":VOLUME") == 0) {
  167.                 cin >> tempcard.vol;
  168.             } else if (strcmp(wordbuffer,":YEAR") == 0) {
  169.                 cin >> tempcard.year;
  170.             } else {
  171.                 cerr << form("Bad key in Card #%d: %s\n",i,wordbuffer);
  172.             }
  173.         }
  174.         CardRecord crec(&tempcard);
  175.         Record rawrec = crec;
  176.         outtree.InsertId(key,&rawrec);
  177.         if (tempcard.author != 0) {
  178.             delete tempcard.author;
  179.             tempcard.author = 0;
  180.         }
  181.         if (tempcard.title != 0) {
  182.             delete tempcard.title;
  183.             tempcard.title = 0;
  184.         }
  185.         if (tempcard.publisher != 0) {
  186.             delete tempcard.publisher;
  187.             tempcard.publisher = 0;
  188.         }
  189.         if (tempcard.city != 0) {
  190.             delete tempcard.city;
  191.             tempcard.city = 0;
  192.         }
  193.         if (tempcard.description != 0) {
  194.             delete tempcard.description;
  195.             tempcard.description = 0;
  196.         }
  197.     }
  198.     cin >> numkeys;
  199.     for (i = 0;i < numkeys;i++) {
  200.         p = ReadQuotedString(cin);
  201.         strncpy(key,p,36);
  202.         key[35] = 0;
  203.         delete p;
  204.         do {
  205.             cin >> ch;
  206.         } while (ch != '#');
  207.         cin >> ch;
  208.         if (ch != '(') {
  209.             cerr << form("Syntax error reading Title %d: expected a (, saw a %c\n",
  210.                      i,ch);
  211.             exit(1);
  212.         }
  213.         for (ielt = 0;ielt < 2048;ielt++) {
  214.             while (true) {
  215.                 cin >> ch;
  216.                 if (ch == ')') break;
  217.                 else if (ch > ' ') {
  218.                     cin.putback(ch);
  219.                     break;
  220.                 }
  221.             }
  222.             if (ch == ')') break;
  223.             vector[ielt] = ReadQuotedString(cin);
  224.         }
  225.         ListRecord lrec(ielt,vector);
  226.         Record rawrec = lrec;
  227.         outtree.InsertTitle(key,&rawrec);
  228.         for (;ielt > 0;ielt--) delete vector[ielt-1];
  229.     }
  230.     cin >> numkeys;
  231.     for (i = 0;i < numkeys;i++) {
  232.         p = ReadQuotedString(cin);
  233.         strncpy(key,p,36);
  234.         key[35] = 0;
  235.         delete p;
  236.         do {
  237.             cin >> ch;
  238.         } while (ch != '#');
  239.         cin >> ch;
  240.         if (ch != '(') {
  241.             cerr << form("Syntax error reading Author %d: expected a (, saw a %c\n",
  242.                      i,ch);
  243.             exit(1);
  244.         }
  245.         for (ielt = 0;ielt < 2048;ielt++) {
  246.             while (true) {
  247.                 cin >> ch;
  248.                 if (ch == ')') break;
  249.                 else if (ch > ' ') {
  250.                     cin.putback(ch);
  251.                     break;
  252.                 }
  253.             }
  254.             if (ch == ')') break;
  255.             vector[ielt] = ReadQuotedString(cin);
  256.         }
  257.         ListRecord lrec(ielt,vector);
  258.         Record rawrec = lrec;
  259.         outtree.InsertAuthor(key,&rawrec);
  260.         for (;ielt > 0;ielt--) delete vector[ielt-1];
  261.     }
  262.     cin >> numkeys;
  263.     for (i = 0;i < numkeys;i++) {
  264.         p = ReadQuotedString(cin);
  265.         strncpy(key,p,36);
  266.         key[35] = 0;
  267.         delete p;
  268.         do {
  269.             cin >> ch;
  270.         } while (ch != '#');
  271.         cin >> ch;
  272.         if (ch != '(') {
  273.             cerr << form("Syntax error reading Subject list %d: expected a (, saw a %c\n",
  274.                      i,ch);
  275.             exit(1);
  276.         }
  277.         for (ielt = 0;ielt < 2048;ielt++) {
  278.             while (true) {
  279.                 cin >> ch;
  280.                 if (ch == ')') break;
  281.                 else if (ch > ' ') {
  282.                     cin.putback(ch);
  283.                     break;
  284.                 }
  285.             }
  286.             if (ch == ')') break;
  287.             vector[ielt] = ReadQuotedString(cin);
  288.         }
  289.         ListRecord lrec(ielt,vector);
  290.         Record rawrec = lrec;
  291.         outtree.InsertSubj(key,&rawrec);
  292.         for (;ielt > 0;ielt--) delete vector[ielt-1];
  293.     }
  294. }
  295.  
  296.  
  297.