bool process () { bool error = false; // Make sure the template file exists if (! m_templateFile.existsAsFile()) { std::cout << name () << " The template file doesn't exist!\n\n"; error = true; } if (!error) { // Prepare to write output to a temporary file. std::cout << " Building: " << m_targetFile.getFullPathName() << "...\n"; TemporaryFile temp (m_targetFile); ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream (1024 * 128)); if (out == 0) { std::cout << " \n!! ERROR - couldn't write to the target file: " << temp.getFile().getFullPathName() << "\n\n"; return false; } out->setNewLineString ("\n"); if (! parseFile (m_targetFile.getParentDirectory(), m_targetFile, *out, m_templateFile, m_alreadyIncludedFiles, m_includesToIgnore, m_wildcards, 0, false)) { return false; } out = 0; if (calculateFileHashCode (m_targetFile) == calculateFileHashCode (temp.getFile())) { std::cout << " -- No need to write - new file is identical\n"; return true; } if (! temp.overwriteTargetFileWithTemporary()) { std::cout << " \n!! ERROR - couldn't write to the target file: " << m_targetFile.getFullPathName() << "\n\n"; return false; } } return error; }
//============================================================================== static bool munge (const File& templateFile, const File& targetFile, const String& wildcard, StringArray& alreadyIncludedFiles, const StringArray& includesToIgnore) { if (! templateFile.existsAsFile()) { std::cout << " The template file doesn't exist!\n\n"; return false; } StringArray wildcards; wildcards.addTokens (wildcard, ";,", "'\""); wildcards.trim(); wildcards.removeEmptyStrings(); std::cout << "Building: " << targetFile.getFullPathName() << "...\n"; TemporaryFile temp (targetFile); ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream (1024 * 128)); if (out == 0) { std::cout << "\n!! ERROR - couldn't write to the target file: " << temp.getFile().getFullPathName() << "\n\n"; return false; } out->setNewLineString ("\n"); if (! parseFile (targetFile.getParentDirectory(), targetFile, *out, templateFile, alreadyIncludedFiles, includesToIgnore, wildcards, true, false)) { return false; } out = 0; if (calculateFileHashCode (targetFile) == calculateFileHashCode (temp.getFile())) { std::cout << " -- No need to write - new file is identical\n"; return true; } if (! temp.overwriteTargetFileWithTemporary()) { std::cout << "\n!! ERROR - couldn't write to the target file: " << targetFile.getFullPathName() << "\n\n"; return false; } return true; }
bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, int numBytes) { if (file.getSize() == numBytes) { MemoryInputStream newStream (data, numBytes, false); if (calculateStreamHashCode (newStream) == calculateFileHashCode (file)) return true; } TemporaryFile temp (file); return temp.getFile().appendData (data, numBytes) && temp.overwriteTargetFileWithTemporary(); }
void FileLogger::trimFileSize (int64 maxFileSizeBytes) const { if (maxFileSizeBytes <= 0) { logFile.deleteFile(); } else { const int64 fileSize = logFile.getSize(); if (fileSize > maxFileSizeBytes) { TemporaryFile tempFile (logFile); { FileOutputStream out (tempFile.getFile()); FileInputStream in (logFile); if (! (out.openedOk() && in.openedOk())) return; in.setPosition (fileSize - maxFileSizeBytes); for (;;) { const char c = in.readByte(); if (c == 0) return; if (c == '\n' || c == '\r') { out << c; break; } } out.writeFromInputStream (in, -1); } tempFile.overwriteTargetFileWithTemporary(); } } }
bool Utils::writeStringToFile(const String& s, const File& f) { TemporaryFile temp (f); ScopedPointer <FileOutputStream> out (temp.getFile().createOutputStream()); if (out != nullptr) { out->write(s.toUTF8(), s.getNumBytesAsUTF8()); out = nullptr; // (deletes the stream) bool succeeded = temp.overwriteTargetFileWithTemporary(); return succeeded; } else { return false; } }
bool ResourceFile::write (const File& cppFile) { TemporaryFile tempH (cppFile.withFileExtension (".h"), TemporaryFile::useHiddenFile); TemporaryFile tempCpp (cppFile, TemporaryFile::useHiddenFile); ScopedPointer <FileOutputStream> cppOut (tempCpp.getFile().createOutputStream (32768)); ScopedPointer <FileOutputStream> hppOut (tempH.getFile().createOutputStream (32768)); if (cppOut != nullptr && hppOut != nullptr) { if (write (cppFile, *cppOut, *hppOut)) { cppOut = nullptr; hppOut = nullptr; return (tempCpp.getFile().hasIdenticalContentTo (tempCpp.getTargetFile()) || tempCpp.overwriteTargetFileWithTemporary()) && (tempH.getFile().hasIdenticalContentTo (tempH.getTargetFile()) || tempH.overwriteTargetFileWithTemporary()); } } return false; }
bool OwlNestSettings::downloadFromServer(CommandID commandID) { String nodeString, optionString, warningString; PropertySet* props = ApplicationConfiguration::getApplicationProperties(); switch (commandID){ case ApplicationCommands::checkForFirmwareUpdates: warningString = "Beware that this procedure can make your OWL unresponsive!"; nodeString = "firmwares"; optionString = props->getValue("firmware-dfu-options"); break; case ApplicationCommands::checkForBootloaderUpdates: warningString = "Beware that this procedure can brick your OWL!"; nodeString = "bootloaders"; optionString = props->getValue("bootloader-dfu-options"); break; default: return false; } String xmlFilename ("updates.xml"); URL url(props->getValue("owl-updates-dir-url")+xmlFilename); ScopedPointer<XmlElement> xmlUpdates; if(url.isWellFormed()) xmlUpdates = url.readEntireXmlStream(0); if(xmlUpdates == NULL) { AlertWindow::showMessageBoxAsync(AlertWindow::WarningIcon, "Connection Error", "Server connection failed"); return false; } XmlElement* filesNode = xmlUpdates->getChildByName(nodeString); StringArray names; XmlElement* child = filesNode->getFirstChildElement(); while(child != nullptr){ names.add(child->getStringAttribute("name")); child = child->getNextElement(); } AlertWindow popup("Download File From Server", "Choose file:", juce::AlertWindow::InfoIcon); popup.addButton("Cancel", 0, juce::KeyPress(), juce::KeyPress()); popup.addButton("Download", 1, juce::KeyPress(), juce::KeyPress()); popup.addComboBox("box", names); if(popup.runModalLoop()==0) return false; popup.setVisible(false); String selectedFilename(popup.getComboBoxComponent("box")->getText()); URL fwUrl(props->getValue("owl-updates-dir-url")+selectedFilename); ScopedPointer<InputStream> strm; strm = fwUrl.createInputStream(0); if(strm == NULL){ AlertWindow::showMessageBoxAsync(AlertWindow::WarningIcon, "Connection Error", "File unavailable", "Continue"); return false; } File theTargetFile(ApplicationConfiguration::getApplicationDirectory().getChildFile(selectedFilename)); FileChooser chooser("Save As", theTargetFile, "*.bin"); bool succeeded = false; if(!chooser.browseForFileToSave(true)) return false; theTargetFile = chooser.getResult(); TemporaryFile temp (theTargetFile); ScopedPointer<FileOutputStream> out(temp.getFile().createOutputStream()); if(out != nullptr) { out->writeFromInputStream(*strm, strm->getTotalLength()); out = nullptr; // deletes the stream succeeded = temp.overwriteTargetFileWithTemporary(); } if(!succeeded){ AlertWindow::showMessageBoxAsync(AlertWindow::WarningIcon, "File Error", "Failed to save file", "Continue"); return false; } if(AlertWindow::showOkCancelBox(AlertWindow::QuestionIcon, "Update Device", "Would you like to update your OWL with this binary now? "+warningString, "Yes", "No")) { DBG("pathName"<< theTargetFile.getFullPathName()); DBG("optionString " << optionString); return deviceFirmwareUpdate(theTargetFile, optionString); } return true; }