Exemplo n.º 1
0
bool SIDStil::GetField(PString &result, PString buffer, int32 tuneNo, STILField field)
{
	int32 start, firstTuneNo, temp, temp2 = -1;

	// Clean out the result buffer first
	result.MakeEmpty();

	// Position pointer to the first char beyond the file designation
	start = buffer.Find('\n');
	start++;

	// Check whether this is a NULL entry or not
	if (start == 0)
		return (false);

	// Is this a multitune entry?
	firstTuneNo = buffer.Find("(#", start);

	// This is a tune designation only if the previous char was
	// a newline (ie. if the "(#" is on the beginning of a line).
	if ((firstTuneNo >= 1) && (buffer.GetAt(firstTuneNo - 1) != '\n'))
		firstTuneNo = -1;

	if (firstTuneNo == -1)
	{
		//-------------------//
		// SINGLE TUNE ENTRY //
		//-------------------//
		//
		// Is the first thing in this STIL entry the COMMENT?
		temp = buffer.Find(_COMMENT_STR, start);

		// Search for other potential fields beyond the COMMENT
		if (temp == start)
		{
			temp2 = buffer.Find(_NAME_STR, start);
			if (temp2 == -1)
			{
				temp2 = buffer.Find(_AUTHOR_STR, start);
				if (temp2 == -1)
				{
					temp2 = buffer.Find(_TITLE_STR, start);
					if (temp2 == -1)
						temp2 = buffer.Find(_ARTIST_STR, start);
				}
			}
		}

		if (temp == start)
		{
			// Yes. So it's assumed to be a file-global comment
			if ((tuneNo == 0) && ((field == all) || ((field == comment) && (temp2 == -1))))
			{
				// Simply copy the stuff in
				result = buffer.Mid(start);
				return (true);
			}
			else if ((tuneNo == 0) && (field == comment))
			{
				// Just copy the comment
				result = buffer.Mid(start, temp2 - start);
				return (true);
			}
			else if ((tuneNo == 1) && (temp2 != -1))
			{
				// A specific field was asked for
				return (GetOneField(result, buffer.Mid(temp2), field));
			}
			else
			{
				// Anything else is invalid as of v2.00
				return (false);
			}
		}
		else
		{
			// No. Handle it as a regular entry
			if ((field == all) && ((tuneNo == 0) || (tuneNo == 1)))
			{
				// The complete entry was asked for. Simply copy the stuff in
				result = buffer.Mid(start);
				return (true);
			}
			else if (tuneNo == 1)
			{
				// A specific field was asked for
				return (GetOneField(result, buffer.Mid(start), field));
			}
			else
			{
				// Anything else is invalid as of v2.00
				return (false);
			}
		}
	}
	else
	{
		//-----------------//
		// MULTITUNE ENTRY //
		//-----------------//
		int32 myTuneNo, nextTuneNo;
		PString tuneNoStr;

		// Was the complete entry asked for?
		if (tuneNo == 0)
		{
			switch (field)
			{
				case all:
				{
					// Yes. Simply copy the stuff in
					result = buffer.Mid(start);
					return (true);
				}

				case comment:
				{
					// Only the file-global comment field was asked for
					if (firstTuneNo != start)
						return (GetOneField(result, buffer.Mid(start, firstTuneNo - start), comment));
					else
						return (false);
				}

				default:
				{
					// If a specific field other than a comment is
					// asked for tuneNo=0, this is illegal
					return (false);
				}
			}
		}

		// Search for the requested tune number
		tuneNoStr.Format("(#%d)", tuneNo);
		myTuneNo = buffer.Find(tuneNoStr, start);

		if (myTuneNo != -1)
		{
			// We found the requested tune number.
			// Set the pointer beyond it
			myTuneNo = buffer.Find('\n', myTuneNo) + 1;

			// Where is the next one?
			nextTuneNo = buffer.Find("\n(#", myTuneNo);
			if (nextTuneNo == -1)
			{
				// There is no next one - set pointer to the end of entry
				nextTuneNo = buffer.GetLength();
			}
			else
			{
				// The search included the \n - go beyond it
				nextTuneNo++;
			}

			// Put the desired fields into the result (which may be 'all')
			PString tempResult;
			bool retVal;

			retVal = GetOneField(tempResult, buffer.Mid(myTuneNo, nextTuneNo - myTuneNo), field);
			result += tempResult;
			return (retVal);
		}
		else
			return (false);
	}
}
Exemplo n.º 2
0
bool SIDStil::GetOneField(PString &result, PString buffer, STILField field)
{
	int32 temp = -1;

	// Sanity check
	if (buffer.GetAt(buffer.GetLength() - 1) != '\n')
	{
		result.MakeEmpty();
		return (false);
	}

	switch (field)
	{
		case all:
			result += buffer;
			return (true);

		case name:
			temp = buffer.Find(_NAME_STR);
			break;

		case author:
			temp = buffer.Find(_AUTHOR_STR);
			break;

		case title:
			temp = buffer.Find(_TITLE_STR);
			break;

		case artist:
			temp = buffer.Find(_ARTIST_STR);
			break;

		case comment:
			temp = buffer.Find(_COMMENT_STR);
			break;

		default:
			break;
	}

	// If the field was not found or it is not in between 'start'
	// and 'end', it is declared a failure
	if (temp == -1)
	{
		result.MakeEmpty();
		return (false);
	}

	// Search for the end of this field. This is done by finding
	// where the next field starts
	int32 nextName, nextAuthor, nextTitle, nextArtist, nextComment, nextField;

	nextName    = buffer.Find(_NAME_STR, temp + 1);
	nextAuthor  = buffer.Find(_AUTHOR_STR, temp + 1);
	nextTitle   = buffer.Find(_TITLE_STR, temp + 1);
	nextArtist  = buffer.Find(_ARTIST_STR, temp + 1);
	nextComment = buffer.Find(_COMMENT_STR, temp + 1);

	// Now determine which one is the closest to our field - that one
	// will mark the end of the required field
	nextField = nextName;

	if (nextField == -1)
		nextField = nextAuthor;
	else if ((nextAuthor != -1) && (nextAuthor < nextField))
		nextField = nextAuthor;

	if (nextField == -1)
		nextField = nextTitle;
	else if ((nextTitle != -1) && (nextTitle < nextField))
		nextField = nextTitle;

	if (nextField == -1)
		nextField = nextArtist;
	else if ((nextArtist != -1) && (nextArtist < nextField))
		nextField = nextArtist;

	if (nextField == -1)
		nextField = nextComment;
	else if ((nextComment != -1) && (nextComment < nextField))
		nextField = nextComment;

	if (nextField == -1)
		nextField = buffer.GetLength();

	// Now nextField points to the last+1 char that should be copied to
	// result. Do that
	result += buffer.Mid(temp, nextField - temp);
	return (true);
}