Simple data buffer splitter and re-assembler.

Revision:
6:094ed29a8bf7
Parent:
4:221c8a56a80e
Child:
7:6baecc15cb26
--- a/SplitterAssembler.cpp	Tue Feb 17 06:59:49 2015 +0000
+++ b/SplitterAssembler.cpp	Wed Feb 18 06:57:57 2015 +0000
@@ -128,11 +128,11 @@
  // add a fragment to assemble later
  int SplitterAssembler::add(uint8_t *fragment,int fragment_length)
  {
-     if (this->m_num_fragments < (MAX_FRAGMENTS-1)) {
+     if (this->m_num_fragments < MAX_FRAGMENTS) {
          int length = fragment_length;
          if (length > DEF_FRAGMENT_LENGTH) {
              length = DEF_FRAGMENT_LENGTH;
-             //DBG("WARNING: Truncating input fragment in add() fragment_length=%d\r\n",fragment_length);
+             DBG("WARNING: Truncating input fragment in add() fragment_length=%d\r\n",fragment_length);
          }
          memcpy(this->m_fragments[this->m_num_fragments],fragment,length);
          ++this->m_num_fragments;
@@ -145,8 +145,11 @@
  }
  
  // assemble all input fragments
- void SplitterAssembler::assemble(uint8_t *buffer,int buffer_length,bool reset_after_assemble)
+ int SplitterAssembler::assemble(uint8_t *buffer,int buffer_length,bool reset_after_assemble)
  {
+     // DEBUG
+     //this->dump();
+     
      // calculate the final assembly length
      int length = this->calculateAssemblyLength(buffer_length);
      if (length > 0) {   
@@ -175,12 +178,19 @@
          }
          
          // DEBUG
-         //DBG("assemble(): buffer=[%s] length=%d\r\n",buffer,buffer_length);
+         DBG("assemble(): buffer=[%s] length=%d\r\n",buffer,buffer_length);
+         
+         // if desired, we re-set after we assemble
+         if (reset_after_assemble) this->reset();
      }
      else {
          // unable to assemble... 
-         DBG("ERROR: Unable to assemble. calculateAssemblyLength() failed (%d)\r\n",length);
+         length = -1;
+         DBG("ERROR: Unable to assemble. calculateAssemblyLength() failed (length=%d)\r\n",length);
      }
+     
+     // return our length
+     return length;
  }
  
  // calculate the assembled packet length
@@ -189,8 +199,9 @@
      
      for(int i=0;i<this->m_num_fragments;++i) {
         if (i < (this->m_num_fragments-1)) length += DEF_FRAGMENT_LENGTH;
-        else length += this->m_last_fragment_length;
+        else this->m_last_fragment_length += strlen((char *)this->m_fragments[i]);
      }
+     length += this->m_last_fragment_length; 
      
      // sanity check
      if (length > buffer_length) {
@@ -202,6 +213,23 @@
      return length;
  }
  
+ // see if any of the collected fragments contains a special character
+ bool SplitterAssembler::hasCollectedCharacter(char special_char)
+ {
+     int found = false;
+     
+     for(int i=0;i<this->m_num_fragments && !found;++i) {
+         char *fragment = (char *)this->m_fragments[i];
+         int fragment_length = strlen(fragment);
+         for(int j=0;j<fragment_length && !found;++j) {
+             if (fragment[j] == special_char) 
+                found = true;
+         }
+     }
+     
+     return found;
+ }
+ 
  // dump the state of the Fragmenter/Assembler
  void SplitterAssembler::dump(void)
  {