/* TrackBackgroundSaver.c */
/*****************************************************************************/
/*                                                                           */
/*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
/*    Copyright (C) 1994  Thomas R. Lawrence                                 */
/*                                                                           */
/*    This program is free software; you can redistribute it and/or modify   */
/*    it under the terms of the GNU General Public License as published by   */
/*    the Free Software Foundation; either version 2 of the License, or      */
/*    (at your option) any later version.                                    */
/*                                                                           */
/*    This program is distributed in the hope that it will be useful,        */
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/*    GNU General Public License for more details.                           */
/*                                                                           */
/*    You should have received a copy of the GNU General Public License      */
/*    along with this program; if not, write to the Free Software            */
/*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
/*                                                                           */
/*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
/*                                                                           */
/*****************************************************************************/

#include "MiscInfo.h"
#include "Audit.h"
#include "Debug.h"
#include "Definitions.h"

#include "TrackBackgroundSaver.h"
#include "BufferedFileInput.h"
#include "BufferedFileOutput.h"
#include "Memory.h"
#include "Array.h"
#include "TrackObject.h"


/* Background Display Subblock Format: */
/*   4-byte little endian number of tracks in the background of this track */
/*   for each of those: */
/*     4-byte index of the track to be in the background */
/*       the index is respective to the order in which the tracks were loaded */
/*       from the file, so 0 is the first track, 1 is the second, and so on. */


/* save the information describing which tracks are in the background (greyed) */
FileLoadingErrors		SaveBackgroundTrackInfo(struct TrackObjectRec* TrackObj,
											struct BufferedOutputRec* Output,
											struct ArrayRec* OrderedListOfAllTracks)
	{
		ArrayRec*					Backgrounds;
		long							Scan;
		long							Limit;

		CheckPtrExistence(TrackObj);
		CheckPtrExistence(Output);
		CheckPtrExistence(OrderedListOfAllTracks);

		Backgrounds = TrackObjectGetBackgroundList(TrackObj); /* actual thing */
		CheckPtrExistence(Backgrounds);

		Limit = ArrayGetLength(Backgrounds);
		if (!WriteBufferedUnsignedLongLittleEndian(Output,Limit))
			{
				return eFileLoadDiskError;
			}

		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				long							Index;

				Index = ArrayFindElement(OrderedListOfAllTracks,
					ArrayGetElement(Backgrounds,Scan));
				ERROR(Index < 0,PRERR(ForceAbort,
					"SaveBackgroundTrackInfo:  track not on master list"));
				if (!WriteBufferedUnsignedLongLittleEndian(Output,Index))
					{
						return eFileLoadDiskError;
					}
			}

		return eFileLoadNoError;
	}


/* load information describing which tracks are in the background */
FileLoadingErrors		LoadBackgroundTrackInfo(struct TrackObjectRec* TrackObj,
											struct BufferedInputRec* Input,
											struct ArrayRec* OrderedListOfAllTracks)
	{
		unsigned long			Limit;
		unsigned long			Scan;
		long							TotalTrackCount;

		CheckPtrExistence(TrackObj);
		CheckPtrExistence(Input);
		CheckPtrExistence(OrderedListOfAllTracks);

		if (!ReadBufferedUnsignedLongLittleEndian(Input,&Limit))
			{
				return eFileLoadDiskError;
			}

		TotalTrackCount = ArrayGetLength(OrderedListOfAllTracks);
		for (Scan = 0; Scan < Limit; Scan += 1)
			{
				unsigned long			Value;
				TrackObjectRec*		OneToBackground;

				if (!ReadBufferedUnsignedLongLittleEndian(Input,&Value))
					{
						return eFileLoadDiskError;
					}
				if ((Value < 0) || (Value >= TotalTrackCount))
					{
						return eFileLoadBadFormat;
					}
				OneToBackground = (TrackObjectRec*)ArrayGetElement(OrderedListOfAllTracks,Value);
				CheckPtrExistence(OneToBackground);
				if (-1 != ArrayFindElement(TrackObjectGetBackgroundList(TrackObj),
					OneToBackground))
					{
						return eFileLoadBadFormat; /* can't have multiple ones */
					}
				if (!TrackObjectAddBackgroundObj(TrackObj,OneToBackground))
					{
						return eFileLoadOutOfMemory;
					}
			}

		return eFileLoadNoError;
	}
