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);
}
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; }
#ifndef MSP_IO_BASE_H_
#define MSP_IO_BASE_H_
+#include <cstddef>
#include <sigc++/sigc++.h>
#include <msp/core/mutex.h>
#include <msp/core/noncopyable.h>
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,
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);
}
}
-unsigned Buffered::do_read(char *data, unsigned size)
+size_t Buffered::do_read(char *data, size_t size)
{
set_op(M_READ);
{
// 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;
if(size<buf_size)
{
// Fill the buffer and serve the rest of the request from it
- unsigned len = below.read(end, buf+buf_size-end);
+ size_t len = below.read(end, buf+buf_size-end);
end += len;
- len = min(static_cast<unsigned>(end-begin), size);
+ len = min<size_t>(end-begin, size);
memcpy(data, begin, len);
begin += len;
ret += len;
}
}
-unsigned Buffered::put(char c)
+size_t Buffered::put(char c)
{
set_op(M_WRITE);
cur_op = op;
}
-unsigned Buffered::get_current_size() const
+size_t Buffered::get_current_size() const
{
return end-begin;
}
{
private:
Base &below;
- unsigned buf_size;
+ std::size_t buf_size;
char *buf;
char *begin;
char *end;
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();
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
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();
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);
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);
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();
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;
}
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();
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();
#ifndef MSP_IO_HANDLE_H_
#define MSP_IO_HANDLE_H_
+#include <cstddef>
+
namespace Msp {
namespace IO {
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
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);
}
init(b, e, m);
}
-Memory::Memory(const char *cd, unsigned s)
+Memory::Memory(const char *cd, size_t s)
{
char *d = const_cast<char *>(cd);
init(d, d+s, M_READ);
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<unsigned>(size, end-pos);
+ size = min<size_t>(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);
return 0;
}
- size = min<unsigned>(size, end-pos);
+ size = min<size_t>(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);
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);
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();
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;
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();
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);
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;
}
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);
#include "handle.h"
#include "handle_private.h"
+using namespace std;
+
namespace Msp {
namespace IO {
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)
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)
+#define NOMINMAX
+#include <limits>
#include <msp/core/systemerror.h>
#include "handle.h"
#include "handle_private.h"
+using namespace std;
+
namespace Msp {
namespace IO {
{
}
-unsigned sys_read(Handle &handle, char *buf, unsigned size)
+size_t sys_read(Handle &handle, char *buf, size_t size)
{
+ if(size>numeric_limits<DWORD>::max())
+ throw invalid_argument("read");
+
DWORD ret;
if(ReadFile(*handle, buf, size, &ret, 0)==0)
throw system_error("ReadFile");
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<DWORD>::max())
+ throw invalid_argument("write");
+
DWORD ret;
if(WriteFile(*handle, buf, size, &ret, 0)==0)
throw system_error("WriteFile");
#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(processed<size)
{
- unsigned free_in = (in_buffer+buffer_size-priv->stream.next_in);
+ size_t free_in = (in_buffer+buffer_size-priv->stream.next_in);
if(free_in<size && priv->stream.next_in>in_buffer)
{
// Not all of the data fits in the buffer, so make some more room
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;
}
// 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<char *>(out_buffer), priv->stream.next_out-out_buffer);
if(len>0)
{
- if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
+ if(len<static_cast<size_t>(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;
#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(processed<size)
{
if(priv->stream.next_out>out_buffer)
{
// We have some pending output, give it out first
- unsigned len = min<unsigned>(priv->stream.next_out-out_buffer, size-processed);
+ size_t len = min<size_t>(priv->stream.next_out-out_buffer, size-processed);
copy(out_buffer, out_buffer+len, data+processed);
processed += len;
- if(len<static_cast<unsigned>(priv->stream.next_out-out_buffer))
+ if(len<static_cast<size_t>(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;
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<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
+ size_t len = below.read(reinterpret_cast<char *>(priv->stream.next_in), in_buffer+buffer_size-priv->stream.next_in);
priv->stream.avail_in += len;
if(!len && below.eof())
stream_end = true;
struct Private;
Base &below;
- unsigned buffer_size;
+ std::size_t buffer_size;
unsigned char *in_buffer;
unsigned char *out_buffer;
bool stream_end;
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
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);
};