void create_if_not_exists(const NString &name, const NString &license, bool force) { NFile outf; NDataStream content; if (NFile::exists(name) && !force) { NMessage::print() << "File " << name << " already exists."; return; } outf.setFileName(name); outf.open(NIODevice::Truncate); if (license != "") { content = content + license + "\n"; } content = content + "// Source code file created by nproj " + N_TOOLS_VERSION + " with NUS version " + N_VERSION + "\n"; outf.write(content); outf.closeDevice(); }
//============================================================================ // NFileUtilities::GetDirectorySize : Get a directory size. //---------------------------------------------------------------------------- uint64_t NFileUtilities::GetDirectorySize(const NFile &theDirectory) { NFileIterator fileIterator; NFileList theFiles; NFile theFile; uint64_t theSize; NFileListConstIterator theIter; // Validate our parameters NN_ASSERT(theDirectory.IsDirectory()); // Get the state we need theFiles = fileIterator.GetFiles(theDirectory); theSize = 0; // Get the size for (theIter = theFiles.begin(); theIter != theFiles.end(); theIter++) { theFile = *theIter; if (theFile.IsFile()) theSize += theFile.GetSize(); } return(theSize); }
//============================================================================ // NFileUtilities::SetFileData : Set a file to data. //---------------------------------------------------------------------------- NStatus NFileUtilities::SetFileData(const NFile &theFile, const NData &theData) { uint64_t theSize, numWritten; NFile mutableFile; NStatus theErr; // Get the state we need mutableFile = theFile; theSize = theData.GetSize(); // Open the file theErr = mutableFile.Open(kNPermissionUpdate, true); if (theErr != kNoErr) return(theErr); // Write the data theErr = mutableFile.SetSize(0); theErr |= mutableFile.Write(theSize, theData.GetData(), numWritten); NN_ASSERT_NOERR(theErr); NN_ASSERT(numWritten == theSize); // Clean up mutableFile.Close(); return(theErr); }
//============================================================================ // NFileUtilities::UnmountVolume : Unmount a volume. //---------------------------------------------------------------------------- NStatus NFileUtilities::UnmountVolume(const NFile &theFile) { // Validate our parameters NN_ASSERT(theFile.IsDirectory()); // Unmount the volume return(NTargetFile::UnmountVolume(theFile.GetPath())); }
bool SaveAdapter::save( const VariantMap& options, const vfs::Path& filename ) { std::string data = Json::serialize( options.toVariant(), " " ); if( !data.empty() ) { NFile f = NFile::open( filename, Entity::fmWrite ); f.write( data.c_str(), data.size() ); f.flush(); } return true; }
//============================================================================ // NDBHandlePool::Open : Open the database. //---------------------------------------------------------------------------- NStatus NDBHandlePool::Open(const NFile &theFile, NDBFlags theFlags, const NString &theVFS) { NDBHandlePtr dbHandle; NStatus theErr; // Validate our parameters and state NN_ASSERT(theFile.IsValid()); NN_ASSERT(!IsOpen()); // Update our state mFile = theFile; mFlags = theFlags; mVFS = theVFS; // Create the initial connection // // The initial connection allows us to test that we can actually open the // database, and may be all that we need if we're only to connect once. theErr = CreateConnection(dbHandle); NN_ASSERT_NOERR(theErr); mIsOpen = (dbHandle != NULL); if (mIsOpen) mPool.PushBack(dbHandle); return(theErr); }
//============================================================================ // NFileUtilities::GetFileData : Get a file as data. //---------------------------------------------------------------------------- NData NFileUtilities::GetFileData(const NFile &theFile) { uint64_t theSize, numRead; NFile mutableFile; NData theData; NStatus theErr; // Get the state we need mutableFile = theFile; theSize = mutableFile.GetSize(); NN_ASSERT( ((int64_t) theSize) <= kInt32Max); // Resize the buffer if (!theData.SetSize((NIndex) theSize)) return(theData); // Open the file theErr = mutableFile.Open(); if (theErr != kNoErr) { theData.Clear(); return(theData); } // Read the data theErr = mutableFile.Read(theSize, theData.GetData(), numRead); theData.SetSize((NIndex) numRead); NN_ASSERT_NOERR(theErr); NN_ASSERT(numRead == theSize); NN_ASSERT(((int64_t) numRead) <= kInt32Max); // Clean up mutableFile.Close(); return(theData); }
void initialize(ClimateType climate) { VariantMap climateArchives = config::load( SETTINGS_RC_PATH( climateModel ) ); std::string optName; if( climate == central ) { optName = CAESARIA_STR_A(central); } else if( climate == northen ) { optName = "north"; } else if( climate == desert ) { optName = "south"; } StringArray archives = climateArchives.get( optName ).toStringArray(); for( auto& str : archives ) { Path archivePath = str; Directory dir = archivePath.directory(); archivePath = dir.find( archivePath.baseName(), Path::ignoreCase ); ArchivePtr archive = FileSystem::instance().mountArchive( archivePath ); if( archive.isNull() ) { Logger::warning( "ClimateManager: can't load file " + archivePath.toString() ); continue; } ResourceLoader rc; NFile atlasInfo = archive->createAndOpenFile( "info" ); if( atlasInfo.isOpen() ) { rc.loadAtlases( atlasInfo, false ); } else { rc.loadFiles( archive ); } } }
void ResourceLoader::loadFiles(ArchivePtr archive) { const vfs::Entries::Items& files = archive->entries()->items(); gfx::PictureBank& pb = gfx::PictureBank::instance(); std::string basename; basename.reserve( 256 ); for( auto& entry : files ) { NFile file = archive->createAndOpenFile( entry.name ); if( file.isOpen() ) { gfx::Picture pic = PictureLoader::instance().load( file ); if( pic.isValid() ) { basename = entry.name.baseName().toString(); pb.setPicture( basename, pic ); } } } }
int main(void) { NFile tmp; tmp.setFileName("testfile.tmp"); if (tmp.exists()) { NMessage::print() << "File " << tmp.getFileName() << " does not exist"; } try { // Opening a file may throw a NIOException exception // Put file in read-only mode to see it happening tmp.open(NIODevice::ReadWrite); // If the file was openned, write something to it tmp.write("test"); } catch (NIOException &e) { NMessage::print() << "Error: " << e.getDescription(); if (e.getResource() == NIOException::FILE) { NMessage::print() << "On file: " << e.getResourceName(); } if (e.getFlag() == NIOException::IO_ACCESS_DENIED) { NWarning::print() << "Access Denied"; } exit(1); } return 0; }
static void DumpBinary(void) { NDataEncoder theEncoder; NFile theFile; NString theText; NData theData; // Create the binary .plist theFile = NFileUtilities::GetTemporaryFile("binary.plist"); NFileUtilities::SetFileText(theFile, kPropertyListXML); NTask::Execute("/usr/bin/plutil", "-convert", "binary1", theFile.GetPath().GetUTF8()); // Dump it theData = NFileUtilities::GetFileData(theFile); theText = theEncoder.Encode(theData, kNDataEncodingHex); theFile.Delete(); NN_LOG("\nstatic const NString kPropertyListBinary = \"%@\";\n", theText); }
//============================================================================ // NFileUtilities::GetDirectory : Get a standard directory. //---------------------------------------------------------------------------- NFile NFileUtilities::GetDirectory(NDirectoryLocation theLocation, const NString &fileName, NDirectoryDomain theDomain, bool canCreate) { NFile theFile; // Get the directory theFile = NTargetFile::GetDirectory(theDomain, theLocation); theFile.ResolveTarget(); if (!fileName.IsEmpty() && theFile.IsValid()) { theFile = theFile.GetChild(fileName); theFile.ResolveTarget(); } // Create it if necessary if (!theFile.Exists() && canCreate) theFile.CreateDirectory(); return(theFile); }
//============================================================================ // NFileUtilities::GetUniqueFile : Get a uniquely-named file. //---------------------------------------------------------------------------- NFile NFileUtilities::GetUniqueFile(const NFile &theDirectory, const NString &fileName) { NString nameChild, nameFile, nameExt; NRange extBreak; NFile theFile; NIndex n; // Validate our parameters NN_ASSERT(theDirectory.IsDirectory()); // Get the state we need if (fileName.IsEmpty()) { nameFile = "Temp"; nameExt = ".tmp"; } else { extBreak = fileName.Find(".", kNStringBackwards); if (extBreak.GetSize() == 1) { nameFile = fileName.GetLeft( extBreak.GetLocation()); nameExt = fileName.GetString(extBreak.GetLocation()); } else { nameFile = fileName; nameExt = ""; } } // Generate a unique name n = 0; while (n < 10000) { // Build the name if (n == 0) nameChild = nameFile; else nameChild.Format("%@ %ld", nameFile, n); if (!nameExt.IsEmpty()) nameChild += nameExt; // Check for the file theFile = theDirectory.GetChild(nameChild); if (!theFile.Exists()) return(theFile); n++; } // Handle failure NN_LOG("Unable to create a unique name"); theFile.Clear(); return(theFile); }
unsigned long NFile::size(vfs::Path filename) { NFile file = NFile::open( filename ); return file.size(); }