]> git.donarmstrong.com Git - bamtools.git/blobdiff - src/api/internal/io/RollingBuffer_p.cpp
Stablized HTTP access on all platforms. (issue #54, issue #11)
[bamtools.git] / src / api / internal / io / RollingBuffer_p.cpp
index ab29253370036d0fb69f836d8c0e798b6d251fda..c712b57440578be99bbf4b0088146311c5534773 100644 (file)
@@ -1,9 +1,20 @@
+// ***************************************************************************
+// RollingBuffer_p.cpp (c) 2011 Derek Barnett
+// Marth Lab, Department of Biology, Boston College
+// ---------------------------------------------------------------------------
+// Last modified: 8 December 2011 (DB)
+// ---------------------------------------------------------------------------
+// Provides a dynamic I/O FIFO byte queue, which removes bytes as they are
+// read from the front of the buffer and grows to accept bytes being written
+// to buffer end.
+//
+// implementation note: basically a 'smart' wrapper around 1..* ByteArrays
+// ***************************************************************************
+
 #include "api/internal/io/RollingBuffer_p.h"
 using namespace BamTools;
 using namespace BamTools::Internal;
 
-#include <iostream> // for debug
-
 #include <climits>
 #include <cstring>
 #include <algorithm>
@@ -156,6 +167,10 @@ void RollingBuffer::Free(size_t n) {
 
 size_t RollingBuffer::IndexOf(char c) const {
 
+    // skip processing if empty buffer
+    if ( IsEmpty() )
+        return string::npos;
+
     size_t index(0);
 
     // iterate over byte arrays
@@ -222,7 +237,7 @@ size_t RollingBuffer::ReadLine(char* dest, size_t max) {
         bytesReadSoFar += bytesToRead;
         Free(bytesToRead);
 
-        if ( !((bytesReadSoFar < index+1)&&(bytesReadSoFar < max-1)) )
+        if ( !((bytesReadSoFar < index+1) && (bytesReadSoFar < max-1)) )
             finished = true;
     }
 
@@ -231,42 +246,6 @@ size_t RollingBuffer::ReadLine(char* dest, size_t max) {
     return bytesReadSoFar;
 }
 
-string RollingBuffer::ReadLine(size_t max) {
-
-    ByteArray result;
-    result.Resize(max);
-
-    size_t numBytesRead = 0;
-
-    // if max not provided, we need to read incrementally
-    if ( max == 0 ) {
-        max = UINT_MAX;
-
-        // make sure we leave room for null terminator
-        result.Resize(1);
-
-        size_t readResult;
-        do {
-            result.Resize(std::min(max, result.Size()+m_bufferGrowth));
-            readResult = ReadLine(result.Data() + numBytesRead, result.Size() - numBytesRead);
-            if ( readResult > 0 || numBytesRead == 0 )
-                numBytesRead += readResult;
-        } while ( readResult == m_bufferGrowth && result[numBytesRead-1] != '\n');
-    }
-
-    // otherwise read line with provided max
-    else numBytesRead = ReadLine(result.Data(), result.Size());
-
-    // adjust byte array depending on numBytesRead
-    if ( numBytesRead == 0 )
-        result.Clear();
-    else
-        result.Resize(numBytesRead);
-
-    // return string from byte array
-    return string(result.ConstData(), result.Size());
-}
-
 const char* RollingBuffer::ReadPointer(void) const {
 
     // return null if empty buffer
@@ -295,7 +274,7 @@ char* RollingBuffer::Reserve(size_t n) {
     if ( (m_tail + n) <= m_data.at(m_tailBufferIndex).Size() ) {
 
         // fetch write pointer at current 'tail', increment tail by @n & return
-        char* ptr = m_data[m_tailBufferIndex].Data() + m_tail;
+        char* ptr = m_data[m_tailBufferIndex].Data(); //+ m_tail;
         m_tail += n;
         return ptr;
     }
@@ -307,7 +286,7 @@ char* RollingBuffer::Reserve(size_t n) {
         m_data[m_tailBufferIndex].Resize(m_tail + n);
 
         // fetch write pointer at current 'tail', increment tail by @n & return
-        char* ptr = m_data[m_tailBufferIndex].Data() + m_tail;
+        char* ptr = m_data[m_tailBufferIndex].Data(); //+ m_tail;
         m_tail += n;
         return ptr;
     }