FileType SessionImpl::DeriveFileType (/*[in]*/ const char * lpszPath) { MIKTEX_ASSERT_STRING (lpszPath); RegisterFileTypes (); const char * lpszExt = GetFileNameExtension(lpszPath); for (int idx = 1; idx < fileTypes.size(); ++ idx) { if (lpszExt == 0) { if (StringCompare(fileTypes[idx].fileTypeString.c_str(), lpszPath) == 0) { return (fileTypes[idx].fileType); } } else { for (CSVList ext (fileTypes[idx].fileNameExtensions.c_str(), PATH_DELIMITER); ext.GetCurrent() != 0; ++ ext) { if (PathName::Compare(ext.GetCurrent(), lpszExt) == 0) { return (fileTypes[idx].fileType); } } } } return (FileType::None); }
bool MpView::AutoDecompressFile (const string &FileName, const string &TempDirectory, string &DecompressedTempFileName) { char tmpname[256], buf[256]; const char *filename = FileName.c_str(); const char *extension = GetFileNameExtension(filename); // remember if a temporary file has been created bool created_tmp = false, failure = false; // default: file without extension or with unrecognized extension // is assumed to be plain data file strcpy(tmpname,filename); // yes there is an extension: do corresponding decompression if (extension) for (int i = 0; DT[i].extension; i++) if ( ! strcmp( DT[i].extension, extension ) ) { // make temporary file name sprintf(tmpname,"%s/%s",TempDirectory.c_str(),TempFilePattern); mkstemp(tmpname); sprintf(buf,DT[i].action,filename,tmpname); failure = (SystemCall(string(buf)) != 0); created_tmp = true; break; } // return empty string on decompression error DecompressedTempFileName = failure ? "" : string(tmpname); return created_tmp; }
/*! Save the screenshot to the file with the specified filename and type. Note that any existing file with the same filename will be overwritten without warning. */ status_t Utility::Save(BBitmap** screenshot, const char* fileName, uint32 imageType) const { BString fileNameString(fileName); // Generate a default filename when none is given if (fileNameString.Compare("") == 0) { BPath homePath; if (find_directory(B_USER_DIRECTORY, &homePath) != B_OK) return B_ERROR; BEntry entry; int32 index = 1; BString extension = GetFileNameExtension(imageType); do { fileNameString.SetTo(homePath.Path()); fileNameString << "/" << B_TRANSLATE(sDefaultFileNameBase) << index++ << extension; entry.SetTo(fileNameString.String()); } while (entry.Exists()); } // Create the file BFile file(fileNameString, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY); if (file.InitCheck() != B_OK) return B_ERROR; // Write the screenshot bitmap to the file BBitmapStream stream(*screenshot); BTranslatorRoster* roster = BTranslatorRoster::Default(); roster->Translate(&stream, NULL, NULL, &file, imageType, B_TRANSLATOR_BITMAP); *screenshot = NULL; // Set the file MIME attribute BNodeInfo nodeInfo(&file); if (nodeInfo.InitCheck() != B_OK) return B_ERROR; nodeInfo.SetType(_GetMimeString(imageType)); return B_OK; }
bool SessionImpl::FindFileInternal (/*[in]*/ const char * lpszFileName, /*[in]*/ FileType fileType, /*[in]*/ bool firstMatchOnly, /*[in]*/ bool tryHard, /*[in]*/ bool create, /*[in]*/ bool renew, /*[out]*/ vector<PathName> & result) { MIKTEX_ASSERT (result.empty()); // try to derive the file type if (fileType == FileType::None) { fileType = DeriveFileType(lpszFileName); if (fileType == FileType::None) { trace_filesearch->WriteFormattedLine ( "core", T_("cannot derive file type from %s"), Q_(lpszFileName)); return (false); } } if (renew) { if (! TryCreateFile(lpszFileName, fileType)) { return (false); } } // construct the search vector PathNameArray vec = ConstructSearchVector(fileType); // get the file type information const InternalFileTypeInfo * pFileTypeInfo = GetInternalFileTypeInfo(fileType); MIKTEX_ASSERT (pFileTypeInfo != 0); // check to see whether the file name has a registered file name extension const char * lpszExtension = GetFileNameExtension(lpszFileName); bool hasRegisteredExtension = false; if (lpszExtension != 0) { for (CSVList ext (pFileTypeInfo->fileNameExtensions.c_str(), PATH_DELIMITER); ext.GetCurrent() != 0 && ! hasRegisteredExtension; ++ ext) { if (PathName::Compare(lpszExtension, ext.GetCurrent()) == 0) { hasRegisteredExtension = true; } } for (CSVList ext (pFileTypeInfo->alternateExtensions.c_str(), PATH_DELIMITER); ext.GetCurrent() != 0 && ! hasRegisteredExtension; ++ ext) { if (PathName::Compare(lpszExtension, ext.GetCurrent()) == 0) { hasRegisteredExtension = true; } } } vector<PathName> fileNamesToTry; // try each registered file name extension, if none was specified if (! hasRegisteredExtension) { for (CSVList ext (pFileTypeInfo->fileNameExtensions.c_str(), PATH_DELIMITER); ext.GetCurrent() != 0; ++ ext) { PathName fileName (lpszFileName); fileName.AppendExtension (ext.GetCurrent()); fileNamesToTry.push_back (fileName); } } // try it with the given file name fileNamesToTry.push_back (lpszFileName); // first round: use the fndb for (vector<PathName>::const_iterator it = fileNamesToTry.begin(); it != fileNamesToTry.end(); ++ it) { if (FindFileInternal(it->Get(), vec, firstMatchOnly, true, false, result) && firstMatchOnly) { return (true); } } // second round: don't use the fndb if (tryHard) { for (vector<PathName>::const_iterator it = fileNamesToTry.begin(); it != fileNamesToTry.end(); ++ it) { if (FindFileInternal(it->Get(), vec, firstMatchOnly, false, true, result) && firstMatchOnly) { return (true); } } } if (create) { if (result.empty()) { if (TryCreateFile(lpszFileName, fileType)) { FindFileInternal (lpszFileName, vec, firstMatchOnly, true, false, result); } } else if ((fileType == FileType::BASE || fileType == FileType::FMT || fileType == FileType::MEM) && GetConfigValue(MIKTEX_REGKEY_TEXMF, MIKTEX_REGVAL_RENEW_FORMATS_ON_UPDATE, true)) { PathName pathPackagesIniC ( GetSpecialPath(SpecialPath::CommonInstallRoot), MIKTEX_PATH_PACKAGES_INI, 0); PathName pathPackagesIniU ( GetSpecialPath(SpecialPath::UserInstallRoot), MIKTEX_PATH_PACKAGES_INI, 0); if (IsNewer(pathPackagesIniC, result[0]) || (pathPackagesIniU != pathPackagesIniC && IsNewer(pathPackagesIniU, result[0]))) { if (TryCreateFile(lpszFileName, fileType)) { result.clear (); FindFileInternal (lpszFileName, vec, firstMatchOnly, true, false, result); } } } } return (! result.empty()); }