diff --git a/simgear/io/CMakeLists.txt b/simgear/io/CMakeLists.txt index 54fdb963..7a32073c 100644 --- a/simgear/io/CMakeLists.txt +++ b/simgear/io/CMakeLists.txt @@ -8,6 +8,7 @@ set(HEADERS raw_socket.hxx sg_binobj.hxx sg_file.hxx + sg_mmap.hxx sg_netBuffer.hxx sg_netChannel.hxx sg_netChat.hxx @@ -29,6 +30,7 @@ set(SOURCES raw_socket.cxx sg_binobj.cxx sg_file.cxx + sg_mmap.cxx sg_netBuffer.cxx sg_netChannel.cxx sg_netChat.cxx diff --git a/simgear/io/sg_mmap.cxx b/simgear/io/sg_mmap.cxx index 0ca6dc40..3868c22e 100644 --- a/simgear/io/sg_mmap.cxx +++ b/simgear/io/sg_mmap.cxx @@ -180,6 +180,23 @@ int SGMMapFile::read( char *buf, int length ) { return result; } +const char* SGMMapFile::advance(size_t len) { + if (len >= size - offset) + return nullptr; + + const char *ptr = buffer + offset; + offset += len; + + return ptr; +} + +int SGMMapFile::read( char *buf, int length, int num ) { + if (length == 0) + return 0; + + size_t size = num*length; + return read(buf, size)/length; +} // read a line of data, length is max size of input buffer int SGMMapFile::readline( char *buf, int length ) { diff --git a/simgear/io/sg_mmap.hxx b/simgear/io/sg_mmap.hxx index df597c65..810e8f53 100644 --- a/simgear/io/sg_mmap.hxx +++ b/simgear/io/sg_mmap.hxx @@ -92,6 +92,9 @@ public: // read a block of data of specified size int read( char *buf, int length ); + // read a block of data of specified size + int read( char *buf, int length, int num ); + // read a line of data, length is max size of input buffer int readline( char *buf, int length ); @@ -99,7 +102,11 @@ public: // note: this really defeats the purpose of mmapping a file std::string read_all(); - inline const char* get() { return buffer; } + // get the pointer to the start of the buffer + inline const char *get() { return buffer; } + + // get the pointer at the current offset and increase the offset by len + const char* advance(size_t len); inline size_t get_size() { return size; }