1*ab8db090SAndroid Build Coastguard Worker #ifndef MARISA_GRIMOIRE_IO_READER_H_ 2*ab8db090SAndroid Build Coastguard Worker #define MARISA_GRIMOIRE_IO_READER_H_ 3*ab8db090SAndroid Build Coastguard Worker 4*ab8db090SAndroid Build Coastguard Worker #include <cstdio> 5*ab8db090SAndroid Build Coastguard Worker #include <iostream> 6*ab8db090SAndroid Build Coastguard Worker 7*ab8db090SAndroid Build Coastguard Worker #include "marisa/base.h" 8*ab8db090SAndroid Build Coastguard Worker 9*ab8db090SAndroid Build Coastguard Worker namespace marisa { 10*ab8db090SAndroid Build Coastguard Worker namespace grimoire { 11*ab8db090SAndroid Build Coastguard Worker namespace io { 12*ab8db090SAndroid Build Coastguard Worker 13*ab8db090SAndroid Build Coastguard Worker class Reader { 14*ab8db090SAndroid Build Coastguard Worker public: 15*ab8db090SAndroid Build Coastguard Worker Reader(); 16*ab8db090SAndroid Build Coastguard Worker ~Reader(); 17*ab8db090SAndroid Build Coastguard Worker 18*ab8db090SAndroid Build Coastguard Worker void open(const char *filename); 19*ab8db090SAndroid Build Coastguard Worker void open(std::FILE *file); 20*ab8db090SAndroid Build Coastguard Worker void open(int fd); 21*ab8db090SAndroid Build Coastguard Worker void open(std::istream &stream); 22*ab8db090SAndroid Build Coastguard Worker 23*ab8db090SAndroid Build Coastguard Worker template <typename T> read(T * obj)24*ab8db090SAndroid Build Coastguard Worker void read(T *obj) { 25*ab8db090SAndroid Build Coastguard Worker MARISA_THROW_IF(obj == NULL, MARISA_NULL_ERROR); 26*ab8db090SAndroid Build Coastguard Worker read_data(obj, sizeof(T)); 27*ab8db090SAndroid Build Coastguard Worker } 28*ab8db090SAndroid Build Coastguard Worker 29*ab8db090SAndroid Build Coastguard Worker template <typename T> read(T * objs,std::size_t num_objs)30*ab8db090SAndroid Build Coastguard Worker void read(T *objs, std::size_t num_objs) { 31*ab8db090SAndroid Build Coastguard Worker MARISA_THROW_IF((objs == NULL) && (num_objs != 0), MARISA_NULL_ERROR); 32*ab8db090SAndroid Build Coastguard Worker MARISA_THROW_IF(num_objs > (MARISA_SIZE_MAX / sizeof(T)), 33*ab8db090SAndroid Build Coastguard Worker MARISA_SIZE_ERROR); 34*ab8db090SAndroid Build Coastguard Worker read_data(objs, sizeof(T) * num_objs); 35*ab8db090SAndroid Build Coastguard Worker } 36*ab8db090SAndroid Build Coastguard Worker 37*ab8db090SAndroid Build Coastguard Worker void seek(std::size_t size); 38*ab8db090SAndroid Build Coastguard Worker 39*ab8db090SAndroid Build Coastguard Worker bool is_open() const; 40*ab8db090SAndroid Build Coastguard Worker 41*ab8db090SAndroid Build Coastguard Worker void clear(); 42*ab8db090SAndroid Build Coastguard Worker void swap(Reader &rhs); 43*ab8db090SAndroid Build Coastguard Worker 44*ab8db090SAndroid Build Coastguard Worker private: 45*ab8db090SAndroid Build Coastguard Worker std::FILE *file_; 46*ab8db090SAndroid Build Coastguard Worker int fd_; 47*ab8db090SAndroid Build Coastguard Worker std::istream *stream_; 48*ab8db090SAndroid Build Coastguard Worker bool needs_fclose_; 49*ab8db090SAndroid Build Coastguard Worker 50*ab8db090SAndroid Build Coastguard Worker void open_(const char *filename); 51*ab8db090SAndroid Build Coastguard Worker void open_(std::FILE *file); 52*ab8db090SAndroid Build Coastguard Worker void open_(int fd); 53*ab8db090SAndroid Build Coastguard Worker void open_(std::istream &stream); 54*ab8db090SAndroid Build Coastguard Worker 55*ab8db090SAndroid Build Coastguard Worker void read_data(void *buf, std::size_t size); 56*ab8db090SAndroid Build Coastguard Worker 57*ab8db090SAndroid Build Coastguard Worker // Disallows copy and assignment. 58*ab8db090SAndroid Build Coastguard Worker Reader(const Reader &); 59*ab8db090SAndroid Build Coastguard Worker Reader &operator=(const Reader &); 60*ab8db090SAndroid Build Coastguard Worker }; 61*ab8db090SAndroid Build Coastguard Worker 62*ab8db090SAndroid Build Coastguard Worker } // namespace io 63*ab8db090SAndroid Build Coastguard Worker } // namespace grimoire 64*ab8db090SAndroid Build Coastguard Worker } // namespace marisa 65*ab8db090SAndroid Build Coastguard Worker 66*ab8db090SAndroid Build Coastguard Worker #endif // MARISA_GRIMOIRE_IO_READER_H_ 67