PString SIDStil::GetEntry(PString relPathToEntry, int32 tuneNo, STILField field) { if (baseDir.IsEmpty()) return (""); // Fail if a section-global comment was asked for if (relPathToEntry.GetAt(relPathToEntry.GetLength() - 1) == '/') return (""); if (stilVersion < 2.59f) { tuneNo = 0; field = all; } // Find out whether we have this entry in the buffer if ((entryBuf.Left(relPathToEntry.GetLength()) != relPathToEntry) || ((entryBuf.Find('\n') != relPathToEntry.GetLength()) && (stilVersion > 2.59f))) { // The relative pathnames don't match or they're not the same length: // We don't have it in the buffer, so pull it in try { PDirectory tempDir; tempDir.SetDirectory(baseDir); tempDir.Append("DOCUMENTS"); stilFile->Open(tempDir.GetDirectory() + "STIL.txt", PFile::pModeRead | PFile::pModeShareRead); if (PositionToEntry(relPathToEntry, stilFile, stilDirs) == false) { // Copy the entry's name to the buffer entryBuf = relPathToEntry + "\n"; } else { entryBuf.MakeEmpty(); ReadEntry(stilFile, entryBuf); } stilFile->Close(); } catch(PFileException e) { // Failed reading from the STIL.txt file stilFile->Close(); return (""); } } // Put the requested field into the result string if (GetField(resultEntry, entryBuf, tuneNo, field) != true) return (""); return (resultEntry); }
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); } }
bool SIDStil::PositionToEntry(PString entryStr, PFile *inFile, PList<DirList *> &dirs) { DirList *item = NULL; int32 slashIndex; int32 startPos; int32 i, count; int32 pathLen, entryStrLen; PString line; bool foundIt = false; bool globComm = false; bool temp; PCharSet_MS_WIN_1252 charSet; try { // Seek to the start of the file inFile->SeekToBegin(); // Get the dirpath slashIndex = entryStr.ReverseFind('/'); // If no slash was found, something is screwed up in the entryStr if (slashIndex == -1) return (false); pathLen = slashIndex + 1; // Determine whether a section-global comment is asked for entryStrLen = entryStr.GetLength(); if (pathLen == entryStrLen) globComm = true; // Find it in the table count = dirs.CountItems(); for (i = 0; i < count; i++) { item = dirs.GetItem(i); if (entryStr.Left(pathLen) == item->dirName) { foundIt = true; break; } } if (!foundIt) { // The directory was not found return (false); } // Jump to the first entry of this section inFile->Seek(item->position, PFile::pSeekBegin); foundIt = false; // Now find the desired entry do { startPos = inFile->GetPosition(); line = inFile->ReadLine(&charSet); if (inFile->IsEOF()) break; // Check if it is the start of an entry if (!line.IsEmpty() && (line.GetAt(0) == '/')) { if (line.Left(pathLen) != item->dirName) { // We are outside the section - get out of the loop, // which will fail the search break; } // Check whether we need to find a section-global comment or // a specific entry if (globComm || (stilVersion > 2.59f)) temp = (line == entryStr); else { // To be compatible with older versions of STIL, which may have // the tune designation on the first line of a STIL entry // together with the pathname temp = (line.Left(entryStrLen) == entryStr); } if (temp) { // Found it! foundIt = true; } } } while (!foundIt); if (foundIt) { // Reposition the file pointer back to the start of the entry inFile->Seek(startPos, PFile::pSeekBegin); return (true); } } catch(PFileException e) { ; } return (false); }
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); }