From: Mikko Rasa Date: Sun, 29 Aug 2021 08:54:20 +0000 (+0300) Subject: Use size_t for sizes in I/O classes X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=c8bf2d6c15893ccc9dbc4e04611b7229029f4808;p=libs%2Fcore.git Use size_t for sizes in I/O classes --- diff --git a/source/fs/unix/filemonitor.cpp b/source/fs/unix/filemonitor.cpp index 70cfc4e..68cd7ae 100644 --- a/source/fs/unix/filemonitor.cpp +++ b/source/fs/unix/filemonitor.cpp @@ -36,13 +36,13 @@ void INotify::remove_watch(int wd) throw system_error("inotify_rm_watch"); } -unsigned INotify::do_write(const char *, unsigned) +size_t INotify::do_write(const char *, size_t) { check_access(IO::M_WRITE); return 0; } -unsigned INotify::do_read(char *buf, unsigned size) +size_t INotify::do_read(char *buf, size_t size) { return IO::sys_read(fd, buf, size); } diff --git a/source/fs/unix/filemonitor_platform.h b/source/fs/unix/filemonitor_platform.h index 31b728f..ed900bf 100644 --- a/source/fs/unix/filemonitor_platform.h +++ b/source/fs/unix/filemonitor_platform.h @@ -20,8 +20,8 @@ public: virtual void set_inherit(bool) { } protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: virtual const IO::Handle &get_handle(IO::Mode) { return fd; } diff --git a/source/io/base.h b/source/io/base.h index b6d1ff6..7bd5bd9 100644 --- a/source/io/base.h +++ b/source/io/base.h @@ -1,6 +1,7 @@ #ifndef MSP_IO_BASE_H_ #define MSP_IO_BASE_H_ +#include #include #include #include @@ -69,25 +70,25 @@ public: protected: void check_access(Mode) const; - virtual unsigned do_write(const char *, unsigned) = 0; - virtual unsigned do_read(char *, unsigned) = 0; + virtual std::size_t do_write(const char *, std::size_t) = 0; + virtual std::size_t do_read(char *, std::size_t) = 0; public: /** Writes data from a buffer. Subject to blocking. Returns the number of bytes written, which may be zero for a non-blockin operation. */ - unsigned write(const char *b, unsigned c) { return do_write(b, c); } + std::size_t write(const char *b, std::size_t c) { return do_write(b, c); } /** Writes a string. This is identical to calling write(s.data(), s.size()). */ - unsigned write(const std::string &s) { return do_write(s.data(), s.size()); } + std::size_t write(const std::string &s) { return do_write(s.data(), s.size()); } /** Writes a single character. This is identical to calling write(&c, 1). */ - virtual unsigned put(char c) { return do_write(&c, 1); } + virtual std::size_t put(char c) { return do_write(&c, 1); } /** Reads data into a buffer. Subject to blocking. Returns the number of bytes read, which may be zero for a non-blocking operation. */ - unsigned read(char *b, unsigned c) { return do_read(b, c); } + std::size_t read(char *b, std::size_t c) { return do_read(b, c); } /** Reads characters up to the next linefeed or end-of-file. The linefeed is not included in the line. Returns true if a line was successfully read, diff --git a/source/io/buffered.cpp b/source/io/buffered.cpp index 2836e4b..55c69a6 100644 --- a/source/io/buffered.cpp +++ b/source/io/buffered.cpp @@ -61,7 +61,7 @@ void Buffered::flush() begin = end = buf; } -unsigned Buffered::do_write(const char *data, unsigned size) +size_t Buffered::do_write(const char *data, size_t size) { set_op(M_WRITE); @@ -92,7 +92,7 @@ unsigned Buffered::do_write(const char *data, unsigned size) } } -unsigned Buffered::do_read(char *data, unsigned size) +size_t Buffered::do_read(char *data, size_t size) { set_op(M_READ); @@ -110,7 +110,7 @@ unsigned Buffered::do_read(char *data, unsigned size) { // Give out whatever is in the buffer already memcpy(data, begin, end-begin); - unsigned ret = end-begin; + size_t ret = end-begin; begin = end = buf; data += ret; @@ -119,10 +119,10 @@ unsigned Buffered::do_read(char *data, unsigned size) if(size(end-begin), size); + len = min(end-begin, size); memcpy(data, begin, len); begin += len; ret += len; @@ -137,7 +137,7 @@ unsigned Buffered::do_read(char *data, unsigned size) } } -unsigned Buffered::put(char c) +size_t Buffered::put(char c) { set_op(M_WRITE); @@ -190,7 +190,7 @@ void Buffered::set_op(Mode op) cur_op = op; } -unsigned Buffered::get_current_size() const +size_t Buffered::get_current_size() const { return end-begin; } diff --git a/source/io/buffered.h b/source/io/buffered.h index da917e0..cd742fe 100644 --- a/source/io/buffered.h +++ b/source/io/buffered.h @@ -11,7 +11,7 @@ class Buffered: public Base, public sigc::trackable { private: Base &below; - unsigned buf_size; + std::size_t buf_size; char *buf; char *begin; char *end; @@ -27,10 +27,10 @@ public: void flush(); protected: - unsigned do_write(const char *, unsigned); - unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: - unsigned put(char); + virtual std::size_t put(char); bool getline(std::string &); int get(); @@ -41,7 +41,7 @@ private: void set_op(Mode); public: Mode get_current_op() const { return cur_op; } - unsigned get_current_size() const; + std::size_t get_current_size() const; }; } // namespace IO diff --git a/source/io/console.cpp b/source/io/console.cpp index a7417ed..abb872d 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -28,18 +28,18 @@ void Console::set_inherit(bool i) sys_set_inherit(handle, i); } -unsigned Console::do_write(const char *buf, unsigned len) +size_t Console::do_write(const char *buf, size_t len) { check_access(M_WRITE); return sys_write(handle, buf, len); } -unsigned Console::do_read(char *buf, unsigned len) +size_t Console::do_read(char *buf, size_t len) { check_access(M_READ); - unsigned ret = sys_read(handle, buf, len); + size_t ret = sys_read(handle, buf, len); if(ret==0) set_eof(); diff --git a/source/io/console.h b/source/io/console.h index 8feccfd..1200086 100644 --- a/source/io/console.h +++ b/source/io/console.h @@ -54,8 +54,8 @@ public: void redirect(Base &); protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: virtual const Handle &get_handle(Mode); diff --git a/source/io/file.cpp b/source/io/file.cpp index 74b9f9f..5ead278 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -38,7 +38,7 @@ void File::set_inherit(bool i) sys_set_inherit(handle, i); } -unsigned File::do_write(const char *buf, unsigned size) +size_t File::do_write(const char *buf, size_t size) { check_access(M_WRITE); @@ -53,14 +53,14 @@ unsigned File::do_write(const char *buf, unsigned size) return sys_write(handle, buf, size); } -unsigned File::do_read(char *buf, unsigned size) +size_t File::do_read(char *buf, size_t size) { check_access(M_READ); if(size==0) return 0; - unsigned ret = sys_read(handle, buf, size); + size_t ret = sys_read(handle, buf, size); if(ret==0) set_eof(); @@ -107,23 +107,23 @@ void BufferedFile::set_inherit(bool) throw logic_error("BufferedFile::set_inherit"); } -unsigned BufferedFile::do_write(const char *buf, unsigned size) +size_t BufferedFile::do_write(const char *buf, size_t size) { - unsigned ret = buffer.write(buf, size); + size_t ret = buffer.write(buf, size); position += ret; return ret; } -unsigned BufferedFile::do_read(char *buf, unsigned size) +size_t BufferedFile::do_read(char *buf, size_t size) { - unsigned ret = buffer.read(buf, size); + size_t ret = buffer.read(buf, size); position += ret; return ret; } -unsigned BufferedFile::put(char c) +size_t BufferedFile::put(char c) { - unsigned ret = buffer.put(c); + size_t ret = buffer.put(c); position += ret; return ret; } diff --git a/source/io/file.h b/source/io/file.h index 85075d0..aeac6c8 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -61,8 +61,8 @@ public: virtual void set_inherit(bool); protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: virtual void sync(); @@ -97,10 +97,10 @@ public: virtual void set_inherit(bool); protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: - virtual unsigned put(char); + virtual std::size_t put(char); virtual bool getline(std::string &); virtual int get(); diff --git a/source/io/handle.h b/source/io/handle.h index 4b311f5..be2097e 100644 --- a/source/io/handle.h +++ b/source/io/handle.h @@ -1,6 +1,8 @@ #ifndef MSP_IO_HANDLE_H_ #define MSP_IO_HANDLE_H_ +#include + namespace Msp { namespace IO { @@ -30,8 +32,8 @@ public: void sys_set_blocking(Handle &, bool); void sys_set_inherit(Handle &, bool); -unsigned sys_read(Handle &, char *, unsigned); -unsigned sys_write(Handle &, const char *, unsigned); +std::size_t sys_read(Handle &, char *, std::size_t); +std::size_t sys_write(Handle &, const char *, std::size_t); void sys_close(Handle &); } // namespace IO diff --git a/source/io/memory.cpp b/source/io/memory.cpp index eaaaeb1..616150f 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -8,7 +8,7 @@ using namespace std; namespace Msp { namespace IO { -Memory::Memory(char *d, unsigned s, Mode m) +Memory::Memory(char *d, size_t s, Mode m) { init(d, d+s, m); } @@ -18,7 +18,7 @@ Memory::Memory(char *b, char *e, Mode m) init(b, e, m); } -Memory::Memory(const char *cd, unsigned s) +Memory::Memory(const char *cd, size_t s) { char *d = const_cast(cd); init(d, d+s, M_READ); @@ -47,17 +47,17 @@ void Memory::set_inherit(bool) throw logic_error("Memory::set_inherit"); } -unsigned Memory::do_write(const char *buf, unsigned size) +size_t Memory::do_write(const char *buf, size_t size) { check_access(M_WRITE); - size = min(size, end-pos); + size = min(size, end-pos); memcpy(pos, buf, size); pos += size; return size; } -unsigned Memory::do_read(char *buf, unsigned size) +size_t Memory::do_read(char *buf, size_t size) { check_access(M_READ); @@ -67,13 +67,13 @@ unsigned Memory::do_read(char *buf, unsigned size) return 0; } - size = min(size, end-pos); + size = min(size, end-pos); memcpy(buf, pos, size); pos += size; return size; } -unsigned Memory::put(char c) +size_t Memory::put(char c) { check_access(M_WRITE); diff --git a/source/io/memory.h b/source/io/memory.h index 4a6eca2..5b9961c 100644 --- a/source/io/memory.h +++ b/source/io/memory.h @@ -14,9 +14,9 @@ private: char *pos; public: - Memory(char *, unsigned, Mode = M_RDWR); + Memory(char *, std::size_t, Mode = M_RDWR); Memory(char *, char *, Mode = M_RDWR); - Memory(const char *, unsigned); + Memory(const char *, std::size_t); Memory(const char *, const char *); private: void init(char *, char *, Mode); @@ -26,10 +26,10 @@ public: virtual void set_inherit(bool); private: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: - virtual unsigned put(char); + virtual std::size_t put(char); virtual bool getline(std::string &); virtual int get(); diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 1301b3a..01bf20a 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -54,7 +54,7 @@ void Pipe::set_inherit(bool i) sys_set_inherit(write_handle, i); } -unsigned Pipe::do_write(const char *buf, unsigned size) +size_t Pipe::do_write(const char *buf, size_t size) { if(size==0) return 0; @@ -62,12 +62,12 @@ unsigned Pipe::do_write(const char *buf, unsigned size) return sys_write(write_handle, buf, size); } -unsigned Pipe::do_read(char *buf, unsigned size) +size_t Pipe::do_read(char *buf, size_t size) { if(size==0) return 0; - unsigned ret = reader.read(buf, size); + size_t ret = reader.read(buf, size); if(ret==0) set_eof(); diff --git a/source/io/pipe.h b/source/io/pipe.h index 098e119..5fc0b3d 100644 --- a/source/io/pipe.h +++ b/source/io/pipe.h @@ -27,8 +27,8 @@ public: virtual void set_inherit(bool); protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: virtual const Handle &get_handle(Mode); diff --git a/source/io/slice.cpp b/source/io/slice.cpp index 3fbfc57..4007a5e 100644 --- a/source/io/slice.cpp +++ b/source/io/slice.cpp @@ -57,39 +57,39 @@ unsigned Slice::prepare_op(unsigned size, Mode m) return size; } -unsigned Slice::do_write(const char *data, unsigned size) +size_t Slice::do_write(const char *data, size_t size) { Base::Synchronize sync(below); size = prepare_op(size, M_WRITE); if(!size) return 0; - unsigned len = below.write(data, size); + size_t len = below.write(data, size); position += len; return len; } -unsigned Slice::do_read(char *data, unsigned size) +size_t Slice::do_read(char *data, size_t size) { Base::Synchronize sync(below); size = prepare_op(size, M_READ); if(!size) return 0; - unsigned len = below.read(data, size); + size_t len = below.read(data, size); if(!len && below.eof()) set_eof(); position += len; return len; } -unsigned Slice::put(char c) +size_t Slice::put(char c) { Base::Synchronize sync(below); if(!prepare_op(1, M_WRITE)) return 0; - unsigned len = below.put(c); + size_t len = below.put(c); position += len; return len; } diff --git a/source/io/slice.h b/source/io/slice.h index d5f0f74..8ca4327 100644 --- a/source/io/slice.h +++ b/source/io/slice.h @@ -36,11 +36,11 @@ private: unsigned prepare_op(unsigned, Mode); protected: - virtual unsigned do_write(const char *, unsigned); - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); + virtual std::size_t do_read(char *, std::size_t); public: - virtual unsigned put(char); + virtual std::size_t put(char); virtual int get(); virtual const Handle &get_handle(Mode); diff --git a/source/io/unix/handle.cpp b/source/io/unix/handle.cpp index cbffa48..c9bf3b1 100644 --- a/source/io/unix/handle.cpp +++ b/source/io/unix/handle.cpp @@ -5,6 +5,8 @@ #include "handle.h" #include "handle_private.h" +using namespace std; + namespace Msp { namespace IO { @@ -20,9 +22,9 @@ void sys_set_inherit(Handle &handle, bool i) fcntl(*handle, F_SETFD, (flags&~O_CLOEXEC)|(i?O_CLOEXEC:0)); } -unsigned sys_read(Handle &handle, char *buf, unsigned size) +size_t sys_read(Handle &handle, char *buf, size_t size) { - int ret = read(*handle, buf, size); + ssize_t ret = read(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) @@ -34,9 +36,9 @@ unsigned sys_read(Handle &handle, char *buf, unsigned size) return ret; } -unsigned sys_write(Handle &handle, const char *buf, unsigned size) +size_t sys_write(Handle &handle, const char *buf, size_t size) { - int ret = write(*handle, buf, size); + ssize_t ret = write(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) diff --git a/source/io/windows/handle.cpp b/source/io/windows/handle.cpp index e59de27..1cdf8ce 100644 --- a/source/io/windows/handle.cpp +++ b/source/io/windows/handle.cpp @@ -1,7 +1,11 @@ +#define NOMINMAX +#include #include #include "handle.h" #include "handle_private.h" +using namespace std; + namespace Msp { namespace IO { @@ -13,8 +17,11 @@ void sys_set_inherit(Handle &, bool) { } -unsigned sys_read(Handle &handle, char *buf, unsigned size) +size_t sys_read(Handle &handle, char *buf, size_t size) { + if(size>numeric_limits::max()) + throw invalid_argument("read"); + DWORD ret; if(ReadFile(*handle, buf, size, &ret, 0)==0) throw system_error("ReadFile"); @@ -22,8 +29,11 @@ unsigned sys_read(Handle &handle, char *buf, unsigned size) return ret; } -unsigned sys_write(Handle &handle, const char *buf, unsigned size) +size_t sys_write(Handle &handle, const char *buf, size_t size) { + if(size>numeric_limits::max()) + throw invalid_argument("write"); + DWORD ret; if(WriteFile(*handle, buf, size, &ret, 0)==0) throw system_error("WriteFile"); diff --git a/source/io/zlibcompressed.cpp b/source/io/zlibcompressed.cpp index 3ad4f22..f477bdc 100644 --- a/source/io/zlibcompressed.cpp +++ b/source/io/zlibcompressed.cpp @@ -141,15 +141,15 @@ void ZlibCompressed::flush() #endif } -unsigned ZlibCompressed::do_write(const char *data, unsigned size) +size_t ZlibCompressed::do_write(const char *data, size_t size) { check_access(M_WRITE); - unsigned processed = 0; + size_t processed = 0; #ifdef WITH_ZLIB while(processedstream.next_in); + size_t free_in = (in_buffer+buffer_size-priv->stream.next_in); if(free_instream.next_in>in_buffer) { // Not all of the data fits in the buffer, so make some more room @@ -161,7 +161,7 @@ unsigned ZlibCompressed::do_write(const char *data, unsigned size) if(free_in) { // Copy as much data into the input buffer as possible - unsigned len = min(free_in, size-processed); + size_t len = min(free_in, size-processed); copy(data+processed, data+processed+len, priv->stream.next_in+priv->stream.avail_in); priv->stream.avail_in += len; processed += len; @@ -196,12 +196,12 @@ bool ZlibCompressed::compress_data(int flush_mode) } // Write compressed data into the underlying object - unsigned len = 0; + size_t len = 0; if(priv->stream.next_out>out_buffer) len = below.write(reinterpret_cast(out_buffer), priv->stream.next_out-out_buffer); if(len>0) { - if(len(priv->stream.next_out-out_buffer)) + if(len(priv->stream.next_out-out_buffer)) copy(out_buffer+len, priv->stream.next_out, out_buffer); priv->stream.avail_out += len; priv->stream.next_out -= len; @@ -217,23 +217,23 @@ bool ZlibCompressed::compress_data(int flush_mode) #endif } -unsigned ZlibCompressed::do_read(char *data, unsigned size) +size_t ZlibCompressed::do_read(char *data, size_t size) { check_access(M_READ); - unsigned processed = 0; + size_t processed = 0; #ifdef WITH_ZLIB while(processedstream.next_out>out_buffer) { // We have some pending output, give it out first - unsigned len = min(priv->stream.next_out-out_buffer, size-processed); + size_t len = min(priv->stream.next_out-out_buffer, size-processed); copy(out_buffer, out_buffer+len, data+processed); processed += len; - if(len(priv->stream.next_out-out_buffer)) + if(len(priv->stream.next_out-out_buffer)) copy(out_buffer+len, priv->stream.next_out, out_buffer); priv->stream.next_out -= len; priv->stream.avail_out += len; @@ -261,7 +261,7 @@ unsigned ZlibCompressed::do_read(char *data, unsigned size) copy(priv->stream.next_in, priv->stream.next_in+priv->stream.avail_in, in_buffer); priv->stream.next_in = in_buffer; - unsigned len = below.read(reinterpret_cast(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in); + size_t len = below.read(reinterpret_cast(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in); priv->stream.avail_in += len; if(!len && below.eof()) stream_end = true; diff --git a/source/io/zlibcompressed.h b/source/io/zlibcompressed.h index af75aec..e4ae6e0 100644 --- a/source/io/zlibcompressed.h +++ b/source/io/zlibcompressed.h @@ -34,7 +34,7 @@ private: struct Private; Base &below; - unsigned buffer_size; + std::size_t buffer_size; unsigned char *in_buffer; unsigned char *out_buffer; bool stream_end; @@ -62,7 +62,7 @@ public: void flush(); protected: - virtual unsigned do_write(const char *, unsigned); + virtual std::size_t do_write(const char *, std::size_t); private: /** Compresses data and writes it to the underlying object. Returns true if @@ -72,7 +72,7 @@ private: bool compress_data(int flush_mode); public: - virtual unsigned do_read(char *, unsigned); + virtual std::size_t do_read(char *, std::size_t); virtual const Handle &get_handle(Mode); };