]> git.saurik.com Git - apt.git/commitdiff
fileutl: simple_buffer: Add write() and full() methods
authorJulian Andres Klode <jak@debian.org>
Sun, 27 Dec 2015 14:35:51 +0000 (15:35 +0100)
committerJulian Andres Klode <jak@debian.org>
Sun, 27 Dec 2015 14:43:13 +0000 (15:43 +0100)
These can be used to implement write buffering

Gbp-Dch: ignore

apt-pkg/contrib/fileutl.cc

index 38ae0246ead05a1498c20f225a4f1b2cd6da636f..99c0a34170923ca71d8ba9b6d2ad764e008cbeac 100644 (file)
@@ -928,6 +928,7 @@ struct APT_HIDDEN simple_buffer {                                                   /*{{{*/
    const char *get() const { return buffer + bufferstart; }
    char *get() { return buffer + bufferstart; }
    bool empty() const { return bufferend <= bufferstart; }
+   bool full() const { return bufferend == buffersize_max; }
    unsigned long long size() const { return bufferend-bufferstart; }
    void reset() { bufferend = bufferstart = 0; }
    ssize_t read(void *to, unsigned long long requested_size) APT_MUSTCHECK
@@ -940,6 +941,16 @@ struct APT_HIDDEN simple_buffer {                                                  /*{{{*/
         bufferstart = bufferend = 0;
       return requested_size;
    }
+   ssize_t write(const void *from, unsigned long long requested_size) APT_MUSTCHECK
+   {
+      if (buffersize_max - size() < requested_size)
+        requested_size = buffersize_max - size();
+      memcpy(buffer + bufferend, from, requested_size);
+      bufferend += requested_size;
+      if (bufferstart == bufferend)
+        bufferstart = bufferend = 0;
+      return requested_size;
+   }
 };
                                                                        /*}}}*/