]> 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 597bb47e19c6dc589c2f36fa371ae0e7007669da..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) free(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)
 {
-    if (terminator) free(terminator);
-    terminator = NULL;
+    terminator.clear();
     bytesToCollect = count;
 }
 
@@ -58,15 +58,15 @@ NetChat::setByteCount(int count)
 #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);
     }
   }
@@ -74,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);
   }
@@ -101,7 +101,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
   
   while (in_buffer.getLength()) {      
     // special case where we're not using a terminator
-    if (terminator == 0 || *terminator == 0) {        
+    if ( terminator.empty() ) {
         if ( bytesToCollect > 0) {
             const int toRead = std::min(in_buffer.getLength(), bytesToCollect);
             collectIncomingData(in_buffer.getData(), toRead);
@@ -114,12 +114,10 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
             collectIncomingData (in_buffer.getData(),in_buffer.getLength());
             in_buffer.remove ();
         }
-      
-      return;
+        
+        continue;
     }
     
-    int terminator_len = strlen(terminator);
-    
     int index = find_terminator ( in_buffer, terminator ) ;
     
     // 3 cases:
@@ -133,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