X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_netChat.cxx;h=3baecdf8b64b564dc0fed0aa0e239d91a921c56b;hb=70c5d605641b628039f75cb8761ce783a17a5bdf;hp=f007e2856ec079f2979a52f3c93860c5b2582abf;hpb=04dc28cb3307a2d78b024f533011628dbe5e5a52;p=simgear.git diff --git a/simgear/io/sg_netChat.cxx b/simgear/io/sg_netChat.cxx index f007e285..3baecdf8 100644 --- a/simgear/io/sg_netChat.cxx +++ b/simgear/io/sg_netChat.cxx @@ -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 @@ -25,31 +25,30 @@ #include -#include // for strdup +#include #include +#include 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; } @@ -59,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); } } @@ -75,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); } @@ -102,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); @@ -115,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: @@ -134,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