home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / muzsrc1.zip / DATABASE.CPP < prev    next >
C/C++ Source or Header  |  1992-07-22  |  9KB  |  320 lines

  1. // **********************************************
  2. // File: DATABASE.CPP
  3. // Database management module
  4.  
  5. #include "muzika.h"
  6. #include <dir.h>
  7. #include <string.h>
  8.  
  9. BOOL melodyExists = FALSE;        // Becomes TRUE as soon as a melody is loaded
  10.  
  11. // **********************************************
  12. // Following are the various classes' member function definitions.
  13.  
  14. // **********************************************
  15. // Melody::Melody is the default constructor of the Melody class.
  16.  
  17. Melody :: Melody() {}             // No default constructor for Melody
  18.  
  19. // **********************************************
  20. // Melody::~Melody is the default destructor of the Melody class.
  21.  
  22. Melody :: ~Melody()
  23. {
  24.   // Destroy the melody parts one by one
  25.   while (part.number())
  26.     part.destroyAt(part.number()-1);
  27. }
  28.  
  29. // **********************************************
  30. // MelodyParameters::LoadFrom loads the melody parameters from a file,
  31. // returning TRUE after success, otherwise calling the LoadError function.
  32.  
  33. BOOL MelodyParameters :: LoadFrom(istream &in, void (*LoadError)())
  34. {
  35.   // Read the staff width from the file
  36.   in.read((char *) &staffWidth, sizeof(staffWidth));
  37.  
  38.   if (in.gcount() != sizeof(staffWidth)) {
  39.     // An error occured: call the LoadError function
  40.     (*LoadError)();
  41.     return FALSE;
  42.   }
  43.  
  44.   return TRUE;
  45. }
  46.  
  47. // **********************************************
  48. // MelodyParameters::printOn saves the melody parameters in a file.
  49.  
  50. void MelodyParameters :: printOn(ostream &out)
  51. {
  52.   // Write the staff width on the stream
  53.   out.write((char *) &staffWidth, sizeof(staffWidth));
  54. }
  55.  
  56. // **********************************************
  57. // Melody::LoadFrom loads an entire melody from a file, using
  58. // the Part class's LoadFrom function to load the parts one by one.
  59.  
  60. BOOL Melody :: LoadFrom(istream &in, void (*LoadError)())
  61. {
  62.   // Load the melody header
  63.   if (in.get() != MELODY) {
  64.     (*LoadError)();
  65.     return FALSE;
  66.   }
  67.  
  68.   // Load the melody parameters
  69.   if (!MelodyParameters :: LoadFrom(in, LoadError))
  70.     return FALSE;
  71.  
  72.   // Load the melody parts one by one
  73.   int i = 0;
  74.   while (!in.eof() && in.peek() > MELODY) {
  75.     Part *p;
  76.     part.insertAt(*(p = new Part), i++);
  77.     if (!p->LoadFrom(in, LoadError))
  78.       return FALSE;
  79.   }
  80.  
  81.   return TRUE;
  82. }
  83.  
  84. // **********************************************
  85. // Melody::printOn saves an entire melody in a file, using
  86. // the IndexedList class's printOn function which saves all parts.
  87.  
  88. void Melody :: printOn(ostream &out)
  89. {
  90.   // Write an ID mark
  91.   out.put(MELODY);
  92.  
  93.   // Write the melody parameters
  94.   MelodyParameters :: printOn(out);
  95.  
  96.   // Write the contents of the various parts
  97.   part.printOn(out);
  98. }
  99.  
  100. // **********************************************
  101. // Part::Part is the default constructor of the Part class.
  102.  
  103. Part :: Part() {}                 // No default constructor for Part
  104.  
  105. // **********************************************
  106. // Part::Part constructs an empty part
  107. // given its name and staff multiplicity.
  108.  
  109. Part :: Part(const char *name, int multiplicity)
  110. {
  111.   SetPartY(0);
  112.   strcpy(_name, name);
  113.   _multiplicity = multiplicity;
  114. }
  115.  
  116. // **********************************************
  117. // Part::~Part is the default destructor of the Part class.
  118.  
  119. Part :: ~Part()
  120. {
  121.   // Destroy the part staves one by one
  122.   while (staff.number())
  123.     staff.destroyAt(staff.number()-1);
  124. }
  125.  
  126. // **********************************************
  127. // PartParameters::LoadFrom loads the part parameters from a file,
  128. // returning TRUE after success, otherwise calling the LoadError function.
  129.  
  130. BOOL PartParameters :: LoadFrom(istream &in, void (*LoadError)())
  131. {
  132.   // Read the part name
  133.   in.read(_name, MAXPARTNAME+1);
  134.   if (in.gcount() != MAXPARTNAME+1) {
  135.     (*LoadError)();
  136.     return FALSE;
  137.   }
  138.  
  139.   // Read the part staff multiplicity
  140.   in.read((char *) &_multiplicity, sizeof(_multiplicity));
  141.   if (in.gcount() != sizeof(_multiplicity)) {
  142.     (*LoadError)();
  143.     return FALSE;
  144.   }
  145.  
  146.   // Set the part initial Y as 0 by default
  147.   SetPartY(0);
  148.   return TRUE;
  149. }
  150.  
  151. // **********************************************
  152. // PartParameters::printOn saves the part parameters in a file.
  153.  
  154. void PartParameters :: printOn(ostream &out)
  155. {
  156.   // Write the part parameters (name and multiplicity) to the stream
  157.   out.write(_name, MAXPARTNAME+1);
  158.   out.write((char *) &_multiplicity, sizeof(_multiplicity));
  159. }
  160.  
  161. // **********************************************
  162. // Part::LoadFrom loads an entire part from a file, using the Staff class's
  163. // LoadFrom function to load the staves one by one.
  164.  
  165. BOOL Part :: LoadFrom(istream &in, void (*LoadError)())
  166. {
  167.   // Load the part header
  168.   if (in.get() != PART) {
  169.     (*LoadError)();
  170.     return FALSE;
  171.   }
  172.   if (!PartParameters :: LoadFrom(in, LoadError))
  173.     return FALSE;
  174.  
  175.   // Load the part staves one by one
  176.   int i = 0;
  177.   while (!in.eof() && in.peek() > PART) {
  178.     Staff *s;
  179.     staff.insertAt(*(s = new Staff), i++);
  180.     if (!s->LoadFrom(in, LoadError))
  181.       return FALSE;
  182.   }
  183.  
  184.   return TRUE;
  185. }
  186.  
  187. // **********************************************
  188. // Part::printOn saves an entire part on a file, using the
  189. // IndexedList class's printOn function to save all the staves.
  190.  
  191. void Part :: printOn(ostream &out) const
  192. {
  193.   out.put(PART);
  194.   PartParameters :: printOn(out);
  195.   staff.printOn(out);
  196. }
  197.  
  198. // **********************************************
  199. // Staff::Staff is the default constructor of the Staff class.
  200.  
  201. Staff :: Staff() {}               // No default constructor for Staff
  202.  
  203. // **********************************************
  204. // Staff:Staff constructs an empty staff given its Y location.
  205.  
  206. Staff :: Staff(int Y)
  207. {
  208.   _X = 32;
  209.   _Y = Y;
  210.   _width = melody.GetStaffWidth();
  211. }
  212.  
  213. // **********************************************
  214. // Staff::~Staff is the default destructor of the Staff class.
  215.  
  216. Staff :: ~Staff()
  217. {
  218.   // Destroy the list of the point objects
  219.   while (pointObject.number())
  220.     pointObject.destroyAt(pointObject.number()-1);
  221.  
  222.   // Destroy the list of the continuous objects
  223.   while (continuousObject.number())
  224.     continuousObject.destroyAt(continuousObject.number()-1);
  225. }
  226.  
  227. // **********************************************
  228. // StaffParameters::LoadFrom loads the staff parameters from a file.
  229.  
  230. BOOL StaffParameters :: LoadFrom(istream &in, void (*LoadError)())
  231. {
  232.   // Load the staff X location
  233.   in.read((char *) &_X, sizeof(_X));
  234.   if (in.gcount() != sizeof(_X)) {
  235.     (*LoadError)();
  236.     return FALSE;
  237.   }
  238.  
  239.   // Load the staff Y location
  240.   in.read((char *) &_Y, sizeof(_Y));
  241.   if (in.gcount() != sizeof(_Y)) {
  242.     (*LoadError)();
  243.     return FALSE;
  244.   }
  245.  
  246.   // Load the staff width
  247.   in.read((char *) &_width, sizeof(_width));
  248.   if (in.gcount() != sizeof(_width)) {
  249.     (*LoadError)();
  250.     return FALSE;
  251.   }
  252.  
  253.   return TRUE;
  254. }
  255.  
  256. // **********************************************
  257. // StaffParameters::printOn saves the staff parameters in a file.
  258.  
  259. void StaffParameters :: printOn(ostream &out)
  260. {
  261.   // Write the staff parameters to the stream
  262.   out.write((char *) &_X, sizeof(_X));
  263.   out.write((char *) &_Y, sizeof(_Y));
  264.   out.write((char *) &_width, sizeof(_width));
  265. }
  266.  
  267. // **********************************************
  268. // Staff::LoadFrom loads an entire staff from a file, using the
  269. // PointObject and ContinuousObject classes' LoadFrom virtual functions.
  270.  
  271. BOOL Staff :: LoadFrom(istream &in, void (*LoadError)())
  272. {
  273.   // Load the staff header
  274.   if (in.get() != STAFF) {
  275.     (*LoadError)();
  276.     return FALSE;
  277.   }
  278.  
  279.   // Load the staff parameters
  280.   if (!StaffParameters :: LoadFrom(in, LoadError))
  281.     return FALSE;
  282.  
  283.   // Load the staff objects themselves
  284.   int p = 0, c = 0;
  285.   int type;
  286.   MusicalObject *m;
  287.  
  288.   // For every object loaded, insert it in the appropriate list
  289.   while (!in.eof() && in.peek() > STAFF)
  290.     if ((m = LoadObject(in, LoadError, &type)) != NULL)
  291.       switch(type) {
  292.         case POINTOBJECT:
  293.           pointObject.insertAt(*m, p++);
  294.           break;
  295.  
  296.         case CONTINUOUSOBJECT:
  297.           continuousObject.insertAt(*m, c++);
  298.           break;
  299.       }
  300.     else
  301.       return FALSE;
  302.  
  303.   return TRUE;
  304. }
  305.  
  306. // **********************************************
  307. // Staff::printOn saves an entire staff in a file, using the
  308. // IndexedList class's printOn function to save all the objects
  309. // contained in the staff.
  310.  
  311. void Staff :: printOn(ostream &out) const
  312. {
  313.   out.put(STAFF);
  314.   StaffParameters :: printOn(out);
  315.   pointObject.printOn(out);
  316.   continuousObject.printOn(out);
  317. }
  318.  
  319. Melody melody;                    // The actual melody object
  320.