home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / db02_src.zip / idxinfo.cc < prev    next >
C/C++ Source or Header  |  1993-11-05  |  8KB  |  392 lines

  1. /**************************************************************************
  2.  * Source Id :
  3.  *
  4.  * $Id: idxinfo.cc,v 1.13 1993/06/23 05:21:22 kevinl Exp $
  5.  *-------------------------------------------------------------------------
  6.  * Project Notes :
  7.  *
  8.  *  Diamond Base
  9.  *  ============
  10.  *      A solid database implementation, spurred on by the continuing
  11.  *  Metal (Lead) Base saga.
  12.  *
  13.  *  Project Team :
  14.  *        A. Davison
  15.  *        K. Lentin
  16.  *        D. Platt
  17.  *
  18.  *    Project Commenced : 05-02-1993
  19.  *
  20.  *-------------------------------------------------------------------------
  21.  *  Module Notes :
  22.  *
  23.  *        Code to support the field and header strctures for the database 
  24.  *    files themselves.
  25.  *
  26.  *  Original Author : Andy
  27.  *
  28.  *-------------------------------------------------------------------------
  29.  * Revision History:
  30.  *
  31.  * $Log: idxinfo.cc,v $
  32. // Revision 1.13  1993/06/23  05:21:22  kevinl
  33. // Mallocs are now in angular brackets
  34. //
  35. // Revision 1.12  1993/05/26  01:01:39  kevinl
  36. // MALLOC_H_MISSING
  37. //
  38. // Revision 1.11  1993/05/06  04:00:19  kevinl
  39. // SASC define for malloc.h
  40. //
  41. // Revision 1.10  1993/05/03  23:35:47  kevinl
  42. // the MAGIC nums are const
  43. //
  44. // Revision 1.9  1993/05/03  01:35:37  kevinl
  45. // Got rid of externs on the consts
  46. //
  47. // Revision 1.8  1993/04/28  11:31:09  kevinl
  48. // Fixed up inlines
  49. //
  50. // Revision 1.7  1993/04/27  07:00:19  kevinl
  51. // Comments
  52. //
  53. // Revision 1.6  1993/04/15  06:06:38  davison
  54. // Fixed up inline not expanded errors etc
  55. //
  56. // Revision 1.5  1993/04/15  04:21:52  kevinl
  57. // Moved malloc.h
  58. //
  59. // Revision 1.4  1993/04/08  05:26:40  kevinl
  60. // FGixed some memory leaks
  61. //
  62. // Revision 1.3  1993/03/29  08:06:22  darrenp
  63. // Can't add to a pointer. Add to size of data instead
  64. // malloc lib.
  65. //
  66. // Revision 1.2  1993/03/15  19:09:15  davison
  67. // Implemented a few nice things, like more than 2 index fields, line number
  68. // reporting on syntax errors, and usage reporting.
  69. //
  70. // Just for you, Daz :-)
  71. //
  72. // Revision 1.1  1993/03/15  19:07:31  davison
  73. // Initial revision
  74. //
  75.  **************************************************************************/
  76.  
  77. #include <stdlib.h>
  78. #ifndef MALLOC_H_MISSING
  79. #include <malloc.h>
  80. #endif
  81.  
  82. #include <idxinfo.h>
  83.  
  84. //--------------------------------------------------------------
  85. // Constructor
  86. //
  87.  
  88. indexInfo::indexInfo(int type, TIndicies indicies )
  89. {
  90.     idxType = type;
  91.     for(int i=0;i<MAX_FIELDS_IN_INDEX;i++)
  92.     {
  93.         idxFields[i] = indicies[i];
  94.     }
  95.     nextIdx = 0;
  96. }
  97.  
  98. indexInfo::~indexInfo()
  99. {
  100.     if (nextIdx)
  101.         delete nextIdx;
  102. }
  103.  
  104. fieldInfo::fieldInfo(char* name, int ty, int sz)
  105. {
  106.     fldName = new char[strlen(name)+1];
  107.     strcpy(fldName,name);
  108.     fldType = ty;
  109.     fldSize = sz;
  110.  
  111.     nextFld = 0;
  112. }
  113.  
  114. fieldInfo::~fieldInfo()
  115. {
  116.     delete fldName;
  117.     if (nextFld)
  118.         delete nextFld;
  119. }
  120.  
  121. //--------------------------------------------------------------
  122. // Stream checking routines.
  123.  
  124. void checkStream(ifstream& s) 
  125. {
  126.     if (!s)
  127.     {
  128.         cerr << "Error : Problem reading relation file. Aborting." << endl;
  129.         exit(1);
  130.     }
  131. }
  132.  
  133. void checkStream(ofstream& s) 
  134. {
  135.     if (!s)
  136.     {
  137.         cerr << "Error : Problem writing to relation file. Aborting." << endl;
  138.         exit(1);
  139.     }
  140. }
  141.  
  142. //--------------------------------------------------------------
  143. // Standard stream insertors/extractors.
  144.  
  145. ostream& operator << (ostream& s, indexList& list)
  146. {
  147.     if (list.indicies!=0)
  148.         s << *(list.indicies);
  149.     return s;
  150. }
  151.  
  152.  
  153. ostream& operator << (ostream& s, indexInfo& info)
  154. {
  155.     s << "Indexed on field " << info.idxFields[0];
  156.     for(int i=1;i<MAX_FIELDS_IN_INDEX;i++)
  157.     {
  158.         if(info.idxFields[i] != -1)
  159.             s << ", " << info.idxFields[i];
  160.     }
  161.     if (info.idxType & IDX_NODUPLICATES)
  162.         s << " without duplicates." << endl;
  163.     else
  164.         s << " with duplicate indicies permitted." << endl;
  165.     if (info.nextIdx)
  166.         s << *(info.nextIdx);
  167.  
  168.     return s;
  169. }
  170.  
  171. ostream& operator << (ostream& s, fieldList& list)
  172. {
  173.     s << *(list.fields);
  174.     return s;
  175. }
  176.  
  177. ostream& operator << (ostream& s, fieldInfo& info)
  178. {
  179.     s << "Name : " << info.fldName << endl;
  180.     s << "Type : " << info.fldType << endl;
  181.     s << "Size : " << info.fldSize << " bytes" << endl;
  182.  
  183.     if (info.nextFld)
  184.         s << *(info.nextFld);
  185.     return s;
  186. }
  187. //----------------------------------------------------------------
  188. // File stream insertors/extractors.
  189.  
  190. ofstream& operator << (ofstream& s, indexList& list)
  191. {
  192.     s.write((const char*)&IDX_LIST_MAGIC,sizeof(int));
  193.     checkStream(s);
  194.     s.write((const char*)&list.numIndicies,sizeof(int));
  195.     checkStream(s);
  196.  
  197.     if(list.indicies != 0)
  198.         s << *(list.indicies);
  199.     return s;
  200. }
  201.  
  202. ofstream& operator << (ofstream& s, indexInfo& info)
  203. {
  204.     s.write((const char*)&IDX_INFO_MAGIC, sizeof(int));
  205.     checkStream(s);
  206.     s.write((char*)&info.idxType,sizeof(int));
  207.     checkStream(s);
  208.     s.write((char*)&info.idxFields[0],MAX_FIELDS_IN_INDEX*sizeof(int));
  209.     checkStream(s);
  210.  
  211.     if (info.nextIdx != 0)
  212.         s << *(info.nextIdx);
  213.     return s;
  214. }
  215.  
  216. ofstream& operator << (ofstream& s, fieldList& list)
  217. {
  218.     s.write((const char*)&FLD_LIST_MAGIC,sizeof(int));
  219.     checkStream(s);
  220. #ifdef DEBUG
  221.     cout << "Dumping :" << hex << FLD_LIST_MAGIC << dec << endl;
  222. #endif
  223.     s.write((char*)&list.numFields,sizeof(int));
  224. #ifdef DEBUG
  225.     cout << "Number of fields = " << list.numFields << endl;
  226. #endif
  227.     checkStream(s);
  228.  
  229.     if (list.fields != 0)
  230.         s << *(list.fields);
  231.     return s;
  232. }
  233.  
  234. ofstream& operator << (ofstream& s, fieldInfo& info)
  235. {
  236.     s.write((const char*)&FLD_INFO_MAGIC,sizeof(int));
  237.     checkStream(s);
  238.     s.write((char*)&info.fldType,sizeof(int));
  239.     checkStream(s);
  240.     s.write((char*)&info.fldSize,sizeof(int));
  241.     checkStream(s);
  242.     int len = strlen(info.fldName)+1;
  243.     s.write((char*)&len,sizeof(int));
  244.     checkStream(s);
  245.     s.write((char*)info.fldName,len);
  246.     checkStream(s);
  247.  
  248.     if (info.nextFld != 0)
  249.         s << *(info.nextFld);
  250.     
  251.     return s;
  252. }
  253.  
  254. // Extract the fieldlist from a stream
  255.  
  256. ifstream& operator >> (ifstream& s, fieldList& list)
  257. {
  258.     int temp;
  259.  
  260. #ifdef DEBUG
  261.     cout <<"Reading field list magic number." << endl;
  262. #endif
  263.     s.read((char*)&temp,sizeof(int));
  264.     checkStream(s);
  265.     if(temp != FLD_LIST_MAGIC)
  266.     {
  267.         cerr << "Bad field list in relation file. Aborting." << endl;
  268.         exit(1);
  269.     }
  270. #ifdef DEBUG
  271.     cout <<"Reading number of fields" << endl;
  272. #endif
  273.  
  274.     s.read((char*)&list.numFields,sizeof(int));
  275.     checkStream(s);
  276.  
  277.     fieldInfo* currentFld = 0;
  278.  
  279.     for(int i=0;i<list.numFields;i++)
  280.     {
  281.         int type,size,len;
  282.         char* name;
  283. #ifdef DEBUG
  284.     cout <<"Reading field info magic number." << endl;
  285. #endif
  286.  
  287.         s.read((char*)&temp,sizeof(int));
  288.         checkStream(s);
  289.         if(temp != FLD_INFO_MAGIC)
  290.         {
  291.             cerr << "Bad field info in relation file. Aborting."<< endl;
  292.             exit(1);
  293.         }
  294. #ifdef DEBUG
  295.     cout <<"Reading type of field." << endl;
  296. #endif
  297.         s.read((char*)&type,sizeof(int));
  298.         checkStream(s);
  299. #ifdef DEBUG
  300.     cout << "Reading size of field." << endl;
  301. #endif
  302.         s.read((char*)&size,sizeof(int));
  303.         checkStream(s);
  304. #ifdef DEBUG
  305.     cout <<"Reading length of field name." << endl;
  306. #endif
  307.         s.read((char*)&len,sizeof(int));
  308.         checkStream(s);
  309.         name = new char[len+1];
  310. #ifdef DEBUG
  311.     cout << "Reading field name." << endl;
  312. #endif
  313.         s.read((char*)name,len);
  314.         checkStream(s);
  315.  
  316.         fieldInfo* tInfo = new fieldInfo(name,type,size);
  317.         delete name;
  318.  
  319.         if(currentFld != 0)
  320.             currentFld->nextFld = tInfo;
  321.         else
  322.             list.fields = tInfo;
  323.         currentFld = tInfo;
  324.     }
  325. #ifdef DEBUG
  326.     cout << "Finished reading field information." << endl;
  327. #endif
  328.     return s;
  329. }
  330.  
  331. // Extract the indexlist from a stream
  332.  
  333. ifstream& operator >> (ifstream& s, indexList& list)
  334. {
  335.     int temp;
  336.  
  337.     s.read((char*)&temp,sizeof(int));
  338.     checkStream(s);
  339.     if(temp != IDX_LIST_MAGIC)
  340.     {
  341.         cerr << "Bad index list in relation file. Aborting." << endl;
  342.         exit(1);
  343.     }
  344.  
  345. #ifdef DEBUG
  346.     cout <<"Reading Number of indicies." << endl;
  347. #endif
  348.     s.read((char*)&list.numIndicies,sizeof(int));
  349.     checkStream(s);
  350.  
  351.     indexInfo* currentIdx = 0;
  352.  
  353.     for(int i=0;i<list.numIndicies;i++)
  354.     {
  355.         int type
  356.         //,field1,field2
  357.         ;
  358.  
  359. #ifdef DEBUG
  360.     cout <<"Reading Index Info Magic number." << endl;
  361. #endif
  362.         s.read((char*)&temp,sizeof(int));
  363.         checkStream(s);
  364.         if(temp != IDX_INFO_MAGIC)
  365.         {
  366.             cerr << "Bad index info in relation file. Aborting."<< endl;
  367.             exit(1);
  368.         }
  369.         TIndicies indicies;
  370. #ifdef DEBUG
  371.     cout <<"Reading type of index." << endl;
  372. #endif
  373.         s.read((char*)&type,sizeof(int));
  374.         checkStream(s);
  375. #ifdef DEBUG
  376.     cout << "Reading index field array." << endl;
  377. #endif
  378.         s.read((char*)&indicies,MAX_FIELDS_IN_INDEX*sizeof(int));
  379.         checkStream(s);
  380.  
  381.         indexInfo* tInfo = new indexInfo(type,indicies);
  382.         if(currentIdx != 0)
  383.             currentIdx->nextIdx = tInfo;
  384.         else
  385.             list.indicies = tInfo;
  386.             
  387.         currentIdx = tInfo;
  388.     }
  389.     return s;
  390. }
  391.  
  392.