]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_netChat.cxx
Drop explicit SDK setting on Mac
[simgear.git] / simgear / io / sg_netChat.cxx
index fcced48eca9abb0963e8c23e13faf7dec6c008f3..3baecdf8b64b564dc0fed0aa0e239d91a921c56b 100644 (file)
@@ -16,7 +16,7 @@
  
      You should have received a copy of the GNU Library General Public
      License along with this library; if not, write to the Free Software
-     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
  
      For further information visit http://plib.sourceforge.net
 
 
 #include <simgear/io/sg_netChat.hxx>
 
-#include <cstring> // for strdup
+#include <cstring>
+#include <cstdlib>
+#include <algorithm>
 
 namespace  simgear {
 
 void
-NetChat::setTerminator (const char* t)
+NetChat::setTerminator(const std::string& t)
 {
-  if (terminator) delete[] terminator;
-  terminator = strdup(t);
+  terminator = t;
+  bytesToCollect = -1;
 }
 
 const char*
-NetChat::getTerminator (void)
+NetChat::getTerminator() const
 {
-  return terminator;
+  return terminator.c_str();
+}
+
+
+void
+NetChat::setByteCount(int count)
+{
+    terminator.clear();
+    bytesToCollect = count;
 }
 
 // return the size of the largest prefix of needle at the end
@@ -48,15 +58,15 @@ NetChat::getTerminator (void)
 #define MAX(a,b) (((a)>(b))?(a):(b))
 
 static int
-find_prefix_at_end (const NetBuffer& haystack, const char* needle)
+find_prefix_at_end(const NetBuffer& haystack, const std::string& needle)
 {
   const char* hd = haystack.getData();
   int hl = haystack.getLength();
-  int nl = strlen(needle);
+  int nl = needle.length();
   
   for (int i = MAX (nl-hl, 0); i < nl; i++) {
     //if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) {
-    if (memcmp(needle, &hd[hl-(nl-i)], nl-i) == 0) {
+    if (memcmp(needle.c_str(), &hd[hl-(nl-i)], nl-i) == 0) {
       return (nl-i);
     }
   }
@@ -64,12 +74,12 @@ find_prefix_at_end (const NetBuffer& haystack, const char* needle)
 }
 
 static int
-find_terminator (const NetBuffer& haystack, const char* needle)
+find_terminator(const NetBuffer& haystack, const std::string& needle)
 {
-  if (needle && *needle)
+  if( !needle.empty() )
   {
     const char* data = haystack.getData();
-    const char* ptr = strstr(data,needle);
+    const char* ptr = strstr(data,needle.c_str());
     if (ptr != NULL)
       return(ptr-data);
   }
@@ -89,17 +99,25 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
   // necessary because we might read several data+terminator combos
   // with a single recv().
   
-  while (in_buffer.getLength()) {
-
+  while (in_buffer.getLength()) {      
     // special case where we're not using a terminator
-    if (terminator == 0 || *terminator == 0) {
-      collectIncomingData (in_buffer.getData(),in_buffer.getLength());
-      in_buffer.remove ();
-      return;
+    if ( terminator.empty() ) {
+        if ( bytesToCollect > 0) {
+            const int toRead = std::min(in_buffer.getLength(), bytesToCollect);
+            collectIncomingData(in_buffer.getData(), toRead);
+            in_buffer.remove(0, toRead);
+            bytesToCollect -= toRead;
+            if (bytesToCollect ==  0) { // read all requested bytes
+                foundTerminator();
+            }
+        } else { // read the whole lot
+            collectIncomingData (in_buffer.getData(),in_buffer.getLength());
+            in_buffer.remove ();
+        }
+        
+        continue;
     }
     
-    int terminator_len = strlen(terminator);
-    
     int index = find_terminator ( in_buffer, terminator ) ;
     
     // 3 cases:
@@ -113,7 +131,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
     if (index != -1) {
       // we found the terminator
       collectIncomingData ( in_buffer.getData(), index ) ;
-      in_buffer.remove (0, index + terminator_len);
+      in_buffer.remove (0, index + terminator.length());
       foundTerminator();
     } else {
       // check for a prefix of the terminator