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); }
PString SIDStil::GetGlobalComment(PString relPathToEntry) { PString dir; int32 lastSlash, pathLen; int32 temp; if (baseDir.IsEmpty()) return (""); // Save the dirpath lastSlash = relPathToEntry.ReverseFind('/'); if (lastSlash == -1) return (""); pathLen = lastSlash + 1; dir = relPathToEntry.Left(pathLen); // Find out whether we have this global comment in the buffer. // If the baseDir was changed, we'll have to read it in again, // even if it might be in the buffer already if ((globalBuf.Left(dir.GetLength()) != dir) || ((globalBuf.Find('\n') != pathLen) && (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(dir, stilFile, stilDirs) == false) { // Copy the dirname to the buffer globalBuf = dir + "\n"; } else { globalBuf.MakeEmpty(); ReadEntry(stilFile, globalBuf); } stilFile->Close(); } catch(PFileException e) { // Failed reading from the STIL.txt file stilFile->Close(); return (""); } } // Position the index to the global comment field temp = globalBuf.Find('\n'); temp++; // Check whether this is a NULL entry or not if (temp == globalBuf.GetLength()) return (""); return (globalBuf.Mid(temp)); }