Rewrite BufferedFile as a standalone class
[libs/core.git] / source / io / file.cpp
index 90694324aa1d6e52f8c59b9f649135725926e031..666b1edc784b9fc9d0bc76cf29d77b82ff80b826 100644 (file)
@@ -81,5 +81,66 @@ const Handle &File::get_handle(Mode m)
        return handle;
 }
 
+
+BufferedFile::BufferedFile(const string &fn, Mode m, File::CreateMode cm):
+       file(fn, m, cm),
+       buffer(file),
+       position(0)
+{
+       mode = m;
+       file.signal_end_of_file.connect(sigc::mem_fun(this, &BufferedFile::set_eof));
+}
+
+unsigned BufferedFile::do_write(const char *buf, unsigned size)
+{
+       unsigned ret = buffer.write(buf, size);
+       position += ret;
+       return ret;
+}
+
+unsigned BufferedFile::do_read(char *buf, unsigned size)
+{
+       unsigned ret = buffer.read(buf, size);
+       position += ret;
+       return ret;
+}
+
+unsigned BufferedFile::put(char c)
+{
+       unsigned ret = buffer.put(c);
+       position += ret;
+       return ret;
+}
+
+bool BufferedFile::getline(string &line)
+{
+       bool ret = buffer.getline(line);
+       if(ret)
+               position += line.size();
+       return ret;
+}
+
+int BufferedFile::get()
+{
+       int ret = buffer.get();
+       if(ret>=0)
+               ++position;
+       return ret;
+}
+
+SeekOffset BufferedFile::seek(SeekOffset offset, SeekType type)
+{
+       if(type==S_CUR)
+       {
+               offset += position;
+               type = S_BEG;
+       }
+
+       signal_flush_required.emit();
+       position = file.seek(offset, type);
+       eof_flag = false;
+       return position;
+}
+
 } // namespace IO
 } // namespace Msp