void file_bstore::safe_read_record(off_t offset, char req_prefix, std::vector<char>& record) { char prefix; bool r = read_record(offset, prefix, record); if (!r) throw io_exception("EOF in read of data"); if (prefix != req_prefix) throw io_exception("Mismatched prefix"); }
void file_bstore::add_file(const std::string& name) { file_info fi; fi.name = m_dir + "/" + name; file_io* file = new file_io(fi.name); fi.io = file; char prefix; std::vector<char> record; file->seek(0); if (!read_record(file, prefix, record)) throw io_exception("File has no header data"); if (prefix != 'S') throw io_exception("File has incorrect header data"); vector_reader vr(record); off_t start_loc; deserialize(vr, start_loc); m_slabs.insert(std::make_pair(start_loc, fi)); file->seek_end(); off_t file_size = file->get_offset(); if (start_loc + file_size > m_size) { m_cur_slab = file; m_size = start_loc + file_size; } }
/* IO */ static void _exc_to_str_io(char *buf, int n) { snprintf(buf, n, "io_exception: errno=%d, function '%s', filename '%s'", io_exception(_exception_ptr)->err, io_exception(_exception_ptr)->function, io_exception(_exception_ptr)->filename ); }
void open(const open_params_type& params) { if ( _Base::m_handle != _Impl::invalid_handle ) throw io_exception("roast::basic_io::open() Already opened."); _Base::m_handle = _Impl::open(params); if ( _Base::m_handle == _Impl::invalid_handle ) throw io_exception("roast::basic_io::open() open failed."); }
void heightmap::init(const string & fname) { if (!io::file::exists(fname)) throw io_exception(L"%ls does not exist!", fname.w_string()); surface = IMG_Load(fname.c_string()); if (!surface) throw io_exception(L"Unable to load %ls!", fname.w_string()); flip_texture(surface); width = surface->w; height = surface->h; } // heightmap::init()
static void _exc_del_io(struct exception *e) { struct io_exception *dd = io_exception(e); if (dd == NULL) return; if (dd->function) free(dd->function); if (dd->filename) free(dd->filename); }
void file_reader::close() { if (m_user_file || m_file == NULL) return; if (fclose(m_file) != 0) throw io_exception("Unable to close file"); m_file = NULL; }
file_writer::file_writer(const std::string& filename) : m_user_file(false) { m_file = fopen(filename.c_str(), "wb"); if (!m_file) throw io_exception("Unable to open file for writing"); }
file_reader::file_reader(const std::string& filename) : m_user_file(false) { m_file = fopen(filename.c_str(), "rb"); if (!m_file) throw io_exception("Unable to open file for reading: " + filename); }
off_t file_io::get_offset() { off_t r = ftello(m_file); if (r == -1) throw io_exception("Error during get_offset"); return r; }
bool file_bstore::read_record(off_t offset, char& prefix, std::vector<char>& record) { if (offset == m_size) return false; slabs_t::iterator it = m_slabs.upper_bound(offset); if (it == m_slabs.begin()) throw io_exception(printstring("Invalid offset on read: %d", (int) offset)); it--; file_io* file = it->second.io; file->seek(offset - it->first); bool r = read_record(file, prefix, record); if (r == false && file != m_slabs.rbegin()->second.io) throw io_exception("End of file in inter-slab region"); return true; }
void file_bstore::write_root(const std::vector<char>& record) { if (m_cur_slab == NULL) throw io_exception("file_store is not open"); lock_t lock(m_mutex); m_root = write_record('R', record); //printf("Writing root record: %d\n", (int) m_root); }
virtual std::size_t write(const byte_buffer& buffer) { boost::system::error_code error; std::size_t result = socket_->write_some(asio_buffer(buffer), boost::ref(error)); if(error) { boost::throw_exception(io_exception("Write socket error "+error.message())); } return result; }
int string_input_stream::read() throw(io_exception) { if (eof()) { throw io_exception("string_input_stream::read(): read past end of stream"); } char c; c = str_[pointer_]; pointer_++; return (int)c; }
inline void validate_io(ssize_t res, const char* msg) { if(res < 0 ) { boost::system::error_code ec(errno, boost::system::system_category()); std::string smsg(msg); smsg.append(" "); smsg.append(ec.message()); boost::throw_exception(io_exception(smsg)); } }
bool file_bstore::read_record(file_io* file, char& prefix, std::vector<char>& record) { if (!file->read(&prefix, 1)) return false; size_t s; deserialize(*file, s); record.resize(s); if (file->read(record.data(), s) != s) throw io_exception("EOF in read of data"); return true; }
void file_bstore::read_root(std::vector<char>& record) { if (m_cur_slab == NULL) throw io_exception("file_store is not open"); lock_t lock(m_mutex); //printf("Reading root record: %d\n", (int) m_root); if (m_root == 0) record.clear(); else safe_read_record(m_root, 'R', record); }
void ft_stream::unget(wchar_t ch) { if (mode & FILE_OPEN_READ) { ::ungetwc(ch, static_cast<FILE *>(fp)); } else { throw io_exception(L"cannot read from a write-only file"); } } // ft_stream::unget()
inline void validate_io(BOOL expression, const char* failMessage) { if(!expression) { DWORD lastError = ::GetLastError(); if(ERROR_IO_PENDING != lastError) { std::string errMsg(failMessage); errMsg.append(" "); errMsg.append(last_error_str(lastError)); boost::throw_exception(io_exception(errMsg)); } } }
// Throws io_exception if an error occurs void write_file(const std::string& fname, const std::string& data) { //const util::scoped_resource<FILE*,close_FILE> file(fopen(fname.c_str(),"wb")); const util::scoped_FILE file(fopen(fname.c_str(),"wb")); if(file.get() == NULL) { throw io_exception("Could not open file for writing: '" + fname + "'"); } const size_t block_size = 4096; char buf[block_size]; for(size_t i = 0; i < data.size(); i += block_size) { const size_t bytes = std::min<size_t>(block_size,data.size() - i); std::copy(data.begin() + i, data.begin() + i + bytes,buf); const size_t res = fwrite(buf,1,bytes,file.get()); if(res != bytes) { throw io_exception("Error writing to file: '" + fname + "'"); } } }
virtual std::size_t read(byte_buffer& buffer) { boost::system::error_code error; size_t result = socket_->read_some(asio_buffer(buffer), boost::ref(error)); if(error == boost::asio::error::eof) { return 0; } else if(error) { boost::throw_exception(io_exception("Read socket error "+error.message())); } buffer.move(result); return result; }
wchar_t ft_stream::get() { if (mode & FILE_OPEN_READ) { wchar_t ch = ::fgetwc(static_cast<FILE *>(fp)); return ch; } else { throw io_exception(L"cannot read from a write-only file"); } } // ft_stream::get()
wchar_t ft_stream::peek() { if (mode & FILE_OPEN_READ) { wchar_t ch = ::fgetwc(static_cast<FILE *>(fp)); ::ungetwc(ch, static_cast<FILE *>(fp)); return ch; } else { throw io_exception(L"Cannot read from write-only stream %ls", fname.w_string()); } } // ft_stream::peek()
off_t file_bstore::write_node(const std::vector<char>& record) { if (m_cur_slab == NULL) throw io_exception("file_store is not open"); //unsigned int outlen = 101 * record.size() / 100 + 600; //std::vector<char> outbuf(4 + outlen); //*((uint32_t *) (outbuf.data())) = htonl(record.size()); //BZ2_bzBuffToBuffCompress(&outbuf[4], &outlen, const_cast<char*>(&record[0]), record.size(), 1, 0, 0); //outbuf.resize(4 + outlen); lock_t lock(m_mutex); //return write_record('N', outbuf); return write_record('N', record); }
void file_bstore::read_node(off_t which, std::vector<char>& record) { if (m_cur_slab == NULL) throw io_exception("file_store is not open"); //std::vector<char> inbuf; //{ lock_t lock(m_mutex); //safe_read_record(which, 'N', inbuf); safe_read_record(which, 'N', record); //} //unsigned int destlen = ntohl(*((uint32_t *) (inbuf.data()))); //record.resize(destlen); //BZ2_bzBuffToBuffDecompress(&record[0], &destlen, &inbuf[4], inbuf.size() - 4, 0, 0); }
static void read_variable(readable& src, IntType& x) { x = 0; int shift = 0; while(true) { unsigned char c; if (src.read((char *) &c, 1) != 1) throw io_exception("Invalid number end"); x |= IntType(c & 0x7f) << shift; if ((c & 0x80) == 0) return; shift += 7; } }
texture_impl::texture_impl(const string & fname, const int & format, const GLenum opengl_texture_unit, const bool compress) : shared_object(), name(fname), format(format), compress(compress), opengl_texture_unit(opengl_texture_unit), opengl_id(0), buffer(0) { if (!file::exists(fname)) throw io_exception(L"Texture file %ls not found.", fname.w_string()); SDL_Surface *surface = IMG_Load(fname.c_string()); if (!surface) throw runtime_exception(L"Unable to load texture file %ls: %hs", fname.w_string(), IMG_GetError()); buffer = new rgba_buffer(surface); SDL_FreeSurface(surface); } // texture_impl::texture_impl()
gsgl::index_t ft_stream::read(wchar_t *buf, const gsgl::index_t num) { if (mode & FILE_OPEN_READ) { /// \todo Implement UTF-8 for file streams. data::smart_pointer<char, true> cbuf(new char[num+1]); int num_read = (gsgl::index_t) ::fread(cbuf, 1, num, static_cast<FILE *>(fp)); for (int i = 0; i < num_read; ++i) buf[i] = cbuf[i]; return num_read; } else { throw io_exception(L"cannot read from a write-only file"); } } // ft_stream::read()
gsgl::index_t ft_stream::write(const wchar_t *buf, const gsgl::index_t num) { if (mode & FILE_OPEN_WRITE) { /// \todo Implement UTF-8 for file streams. data::smart_pointer<char, true> cbuf (new char[num+1]); for (int i = 0; i < num; ++i) cbuf[i] = (char) buf[i]; return (gsgl::index_t) ::fwrite(cbuf, 1, num, static_cast<FILE *>(fp)); } else { throw io_exception(L"cannot write to a read-only file"); } } // ft_stream::write()
void read () { if (_pd) { error_code ec; ssize_t n = _pd->read(& _value, sizeof(char_type), ec); if (n < 0) PFS_THROW(io_exception(ec)); if (n == 0) { _value = 0; _pd = 0; _flag |= ATEND_FLAG; } } }