bool CDatum::CreateFromFile (const CString &sFilespec, ESerializationFormats iFormat, CDatum *retdDatum, CString *retsError) // CreateFromFile // // Loads a datum from a file. { // Open the file CFileBuffer theFile; if (!theFile.OpenReadOnly(sFilespec)) { *retsError = strPattern(ERR_CANT_OPEN_FILE, sFilespec); return false; } // If unknown format, see if we can detect the format if (iFormat == formatUnknown) { if (!DetectFileFormat(sFilespec, theFile, &iFormat, retsError)) return false; // If format unknown, then error. if (iFormat == formatUnknown) { *retsError = strPattern(ERR_UNKNOWN_FORMAT, sFilespec); return false; } } // Parse it if (!Deserialize(iFormat, theFile, retdDatum)) { *retsError = strPattern(ERR_DESERIALIZE_ERROR, sFilespec); return false; } // Done return true; }
bool CBlackBox::ReadRecent (const CString &sPath, const CString &sFind, int iLines, TArray<CString> *retLines) // ReadRecent // // Returns the most recent set of lines. { // First we make a list of log files at the given path. TArray<CString> Files; if (!fileGetFileList(sPath, NULL_STR, CString("*.log"), 0, &Files)) return false; // Now sort them in reverse chronological order (we can do this because we // have encoded the date in the name). Files.Sort(DescendingSort); // Now loop until we have filled all the lines (or until we run out of log // files). int iLogFile = 0; int iLinesLeft = iLines; while (iLogFile < Files.GetCount() && iLinesLeft > 0) { // Open the log file for read-only CFileBuffer LogFile; if (!LogFile.OpenReadOnly(Files[iLogFile])) return false; // Parse backwards until we reach the number of lines that we want // or until we reach the beginning of the file. char *pBoF = LogFile.GetPointer(); char *pEoF = pBoF + LogFile.GetLength(); char *pStart = pEoF; while (pStart > pBoF && iLinesLeft > 0) { // Remember the end of the line. char *pLineEnd = pStart; // Go backwards until we hit a line ending while (pStart > pBoF && pStart[-1] != '\n') pStart--; // We're at the beginning of the line so get the whole thing. If // this is a line that we want, then add it to the result. CString sLine(pStart, (int)(pLineEnd - pStart)); if (!sLine.IsEmpty() && (sFind.IsEmpty() || strFind(sLine, sFind) != -1)) { // We add at the beginning because we are reading backwards retLines->Insert(sLine, 0); // We got a line iLinesLeft--; } // Now move backwards to skip the line ending if (pStart > pBoF && pStart[-1] == '\n') pStart--; if (pStart > pBoF && pStart[-1] == '\r') pStart--; } // Next file LogFile.Close(); iLogFile++; } // Done return true; }