home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 May / PCWK5A99.ISO / Os2 / WINCARDS.ZIP / SOURCE / cardcpp.h next >
Encoding:
C/C++ Source or Header  |  1997-02-21  |  5.1 KB  |  179 lines

  1. #include <fstream.h>
  2. #include <iostream.h>
  3. #include <stdlib.h>
  4. #ifndef cardfile_h
  5. #include "cardfile.h"
  6. #endif
  7.  
  8. ifstream fin;
  9. ofstream fout;
  10. int numcheck(void);
  11. int fileopen(char*);
  12. int verify (void);
  13. Linkedlist readcardfile(int);
  14. int writecardfile (Linkedlist cardlist, int numcards, char* filename);
  15.  
  16. int fileopen(char* filename)
  17. {
  18.     char checkfile[3];
  19.  
  20.     /*      Open the file and perform a cursory check on the file type      */
  21.     /* Get the first 3 characters of the file -- they should be MGC         */
  22.  
  23.     fin.open(filename, ios::in | ios::binary);
  24.     if (!fin.good()) exit(1);
  25.     fin.get(checkfile,4);
  26.     fin.close();
  27.     if (strcmp(checkfile, "MGC"))
  28.         exit(2);
  29.     return 0;
  30. }
  31.  
  32. /*      Get number of cards                                             */
  33.  
  34. int verify(char* filename)
  35. {
  36.     int checksum;
  37.     int numcards;
  38.     int offset;
  39.  
  40.     fin.open(filename, ios::in | ios::binary);
  41.     /*      Read in the number of cards                                     */
  42.     fin.seekg (3, ios::beg);
  43.     fin.read((int *)&numcards, sizeof numcards);
  44.  
  45.     /*      Read in number of cards in binary & convert to number of cards; */
  46.     /*      Check once more to make sure file is valid                      */
  47.  
  48.     fin.seekg (11, ios::beg);
  49.     fin.read ((int*) &offset, sizeof offset);
  50.     fin.close();
  51.     checksum = ((offset - 57)/52) + 1;
  52.     if (checksum != numcards) exit (9999);
  53.  
  54.     return numcards;
  55. }
  56.  
  57. /*      Read the cards into the linked list                             */
  58.  
  59. Linkedlist readcardfile(int numcards, char* filename)
  60. {
  61.     int cardnum = 0;
  62.     static int newoffset = 0;
  63.     int cards, namesize = 41;
  64.     static short numchars;
  65.     char index[namesize];
  66.     char * varinfo;
  67.     Linkedlist cardlist;
  68.     Card anothercard;
  69.     int offset = ((numcards-1)*52)+57;
  70.  
  71.     /*                                                                      */
  72.     /*      Start reading in cards here                                     */
  73.     /*                                                                      */
  74. fin.open(filename, ios::in | ios::binary);
  75.  
  76. fin.seekg (16, ios::beg);
  77.  
  78.     for (int i=1; (i < (numcards+1)) ; i++)
  79.     {
  80.         fin.get(index, namesize);
  81.  
  82.         fin.seekg (7, ios::cur);
  83.         cardnum++;
  84.  
  85.         /*      Get the number of characters in the data field;                 */
  86.         /*      the last card has no data on stored info. -- it goes to EOF.    */
  87.  
  88.         if (i != numcards)
  89.         {
  90.             fin.read((int*)&cards, sizeof cards);
  91.             newoffset = cards;
  92.         }
  93.  
  94.         fin.seekg(offset+2, ios::beg);
  95.  
  96.         fin.read((short*)&numchars, sizeof numchars);
  97.         varinfo = new char[numchars+1];
  98.         fin.read(varinfo,numchars+1);
  99.  
  100.         /*      Go back and get more data                                       */
  101.  
  102.         fin.seekg((68+(52*(cardnum-1))), ios::beg);
  103.         offset=newoffset;
  104.  
  105.         /*      Use the information to enter the card                           */
  106.         /*      Add the extra char beyond numchars to make sure that the        */
  107.         /*      end-of-line gets written into the array                         */
  108.  
  109.         anothercard.Wincard(index, numchars+1, varinfo);
  110.         cardlist.insertcard(anothercard);
  111.  
  112.         /*      Delete needed because varinfo was created with new              */
  113.         delete varinfo;
  114.     }
  115.     fin.close();
  116.  
  117.     fin.clear();
  118.  
  119.     return cardlist;
  120. }
  121.  
  122. /*      Note -- Linkedlist is passed by value because 1) structure is   */
  123. /*      small and 2) use of the movecard() function means the topcard   */
  124. /*      pointer would have to be reset to the correct card after use    */
  125. /*      of this function; the current setup is probably a little slower */
  126. /*      but is easy to program                                          */
  127.  
  128. int writecardfile (Linkedlist cardlist, int numcards, char* filename)
  129. {
  130.  
  131.     static int newoffset = 0;
  132.     int namesize = 41;
  133.     int id = 0;
  134.     static short numchars;
  135.     int offset = ((numcards - 1) * 52) + 57;
  136.  
  137.     while (! ((cardlist.showcard(-1) > cardlist.showcard(0)) || (id < numcards)))
  138.     {
  139.         cardlist.movecard(1);
  140.         id++;
  141.     }
  142.  
  143.     fout.open (filename, ios::out | ios::trunc | ios::binary);
  144.  
  145.     fout << "MGC";
  146.  
  147.     fout.write ((int*) &numcards, sizeof numcards);
  148.  
  149.     fout.seekp (11, ios::beg);
  150.  
  151.     fout.write ((int*) &offset, sizeof offset);
  152.     fout.seekp (16, ios::beg);
  153.  
  154.     for (int i=0; (i < numcards); i++)
  155.     {
  156.         for (int j=0; j < namesize; j++)
  157.             fout << cardlist.showcard(i).showindexinfo()[j];
  158.     fout.seekp (6, ios::cur);
  159.  
  160.         numchars = cardlist.showcard(i).shownumcardchars() - 1;
  161.         if (numchars == -1) numchars = 0;
  162.         newoffset = offset + numchars + 4;
  163.         if (i < (numcards - 1))
  164.             fout.write ((int*) &newoffset, sizeof newoffset);
  165.  
  166. fout.seekp (offset + 2, ios::beg);
  167.  
  168.         fout.write ((short*) &numchars, sizeof numchars);
  169.  
  170.         fout << cardlist.showcard(i).showcardinfo();
  171.  
  172.         fout.seekp(68+(52*i), ios::beg);
  173.         offset = newoffset;
  174.     }
  175.  
  176.     fout.close();
  177.     return 0;
  178. }
  179.