//================================================== //! Load a file from the VFS into memory, can throw a FileIOException //================================================== void System::VFS::LoadRaw( const string& strFilename, vector<byte>& data ) { // Check to see if it exists if( !PHYSFS_exists(strFilename.c_str()) ) { THROW_EXCEPTION( FileReadException() << FileIOExceptionFilename(strFilename) ); } // Open the file shared_ptr<PHYSFS_file> pFile( PHYSFS_openRead(strFilename.c_str()), PHYSFS_close ); if( pFile.get() == NULL ) { THROW_EXCEPTION( FileReadException() << FileIOExceptionFilename(strFilename) ); } // Try to read the number of bytes, fail if we can't PHYSFS_sint64 nBytes= PHYSFS_fileLength( pFile.get() ); if( nBytes < 0 ) { THROW_EXCEPTION( FileReadException() << FileIOExceptionFilename(strFilename) ); } data.resize( unsigned(nBytes) ); // Try to read the data PHYSFS_sint64 nBytesRead= PHYSFS_read( pFile.get(), &data[0], sizeof(byte), data.size() ); // Check for unexpected length if( nBytesRead != nBytes ) { data.resize( unsigned(nBytesRead) ); THROW_EXCEPTION( FileReadException() << FileIOExceptionFilename(strFilename) ); } }
void WriteFile::write(const void* buffer, size_t objsize, size_t objcount) { PHYSFS_sint64 objswritten = PHYSFS_write(file, buffer, objsize, objcount); if(objswritten != (PHYSFS_sint64) objcount) throw FileReadException(objswritten, objcount, "write not possible (disk full)?"); }
void open(std::string const& cmd_or_file, std::string const& mode, Type type) { if (this->proc_or_file || this->string != "") throw HandleNotClosedException("handle not closed - cannot open", __FILE__, __LINE__); this->proc_or_file = NULL; this->string = ""; switch (type) { case TYPE_STRING: this->string = cmd_or_file; break; case TYPE_COMMAND: this->proc_or_file = popen(cmd_or_file.c_str(), mode.c_str()); break; case TYPE_FILE: this->proc_or_file = fopen(cmd_or_file.c_str(), mode.c_str()); break; default: throw LogicException("unexpected type given"); } if (this->proc_or_file || type == TYPE_STRING) this->type = type; else throw FileReadException("Cannot read the file/cmd: " + cmd_or_file, __FILE__, __LINE__); }
void FileInputStream::read(void* data, std::size_t /*typeAlignment*/, std::size_t size, std::size_t count) { if (std::fread(data, size, count, _fp) < count) { // WARNING: The data buffer may not be in a valid state after this. throw FileReadException(); } }
fileRead::fileRead(string fName) { // Check if can open the file m_ifs.open(fName.c_str(),ifstream::in); if ( !m_ifs.is_open() ) throw FileReadException("Cannot open file. Check the file name."); m_EOS = false; m_LastWordReturnedEOS = false; m_readEOS = false; m_readChar = false; };
fileRead::fileRead(string fName) { m_stream = fopen(fName.c_str(),"r"); // Check if can open the file if ( m_stream == NULL ) throw FileReadException("Cannot open file. Check the file name."); m_EOS = false; m_LastWordReturnedEOS=false; m_readEOS = false; m_readChar = false; };
void SettingsFile::LoadFileToString(std::ifstream& file, SettingsFile::StringVector& lines, const std::string& file_name) { // First, tell the file not to throw any exceptions. file.exceptions(std::ios_base::goodbit); // Now load each line from the file into a string. while(file.good() == true) { std::string line; std::getline(file, line); lines.push_back(line); } // If an error occurred while reading (other than reaching EOF), then throw a FileReadException. if(file.fail() == true && file.eof() == false) { file.close(); throw FileReadException(file_name); } }
QVector<Target> TargetFileHandler::readFile(const QString& fileName){ QFile file(fileName); // Open the file if (!file.open(QIODevice::ReadOnly)) { // TODO throw different errors depending on why // ie. file doesn't exist throw FileReadException(); } // Read the file QVector<Target> targets; QTextStream in(&file); while(!in.atEnd()) { Target t; in >> t; targets.append(t); } file.close(); return targets; }
/** Read file into memory. * @param filename file path * @return memory location of file content, free after done */ char * WebServer::read_file(const char *filename) { FILE *f = fopen(filename, "rb"); if (! f) { throw CouldNotOpenFileException(filename, errno); } long size = 0; if ((fseek(f, 0, SEEK_END) != 0) || ((size = ftell(f)) == 1)) { fclose(f); throw Exception("Cannot determine file size of %s", filename); } fseek(f, 0, SEEK_SET); if ( size == 0 ) { fclose(f); throw Exception("File %s has zero length", filename); } else if (size > 1024 * 1024) { // keys or certs should not be that long... fclose(f); throw Exception("File %s is unexpectedly large", filename); } char *rv = (char *)malloc(size); if (fread(rv, size, 1, f) != 1) { int terrno = errno; fclose(f); free(rv); throw FileReadException(filename, terrno); } fclose(f); return rv; }
void ReadFile::read(void* buffer, size_t objsize, size_t objcount) { PHYSFS_sint64 objsread = PHYSFS_read(file, buffer, objsize, objcount); if(objsread != (PHYSFS_sint64) objcount) throw FileReadException(objsread, objcount, "eof while reading"); }