]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_netChat.cxx
Don't use object returned from vector::end()
[simgear.git] / simgear / io / sg_netChat.cxx
1 /*
2     Copied from PLIB into SimGear
3     
4      PLIB - A Suite of Portable Game Libraries
5      Copyright (C) 1998,2002  Steve Baker
6  
7      This library is free software; you can redistribute it and/or
8      modify it under the terms of the GNU Library General Public
9      License as published by the Free Software Foundation; either
10      version 2 of the License, or (at your option) any later version.
11  
12      This library is distributed in the hope that it will be useful,
13      but WITHOUT ANY WARRANTY; without even the implied warranty of
14      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15      Library General Public License for more details.
16  
17      You should have received a copy of the GNU Library General Public
18      License along with this library; if not, write to the Free Software
19      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
20  
21      For further information visit http://plib.sourceforge.net
22
23      $Id: NetChat.cxx 2117 2007-09-13 23:21:09Z fayjf $
24 */
25
26 #include <simgear/io/sg_netChat.hxx>
27
28 #include <cstring>
29 #include <cstdlib>
30 #include <algorithm>
31
32 namespace  simgear {
33
34 void
35 NetChat::setTerminator(const std::string& t)
36 {
37   terminator = t;
38   bytesToCollect = -1;
39 }
40
41 const char*
42 NetChat::getTerminator() const
43 {
44   return terminator.c_str();
45 }
46
47
48 void
49 NetChat::setByteCount(int count)
50 {
51     terminator.clear();
52     bytesToCollect = count;
53 }
54
55 // return the size of the largest prefix of needle at the end
56 // of haystack
57
58 #define MAX(a,b) (((a)>(b))?(a):(b))
59
60 static int
61 find_prefix_at_end(const NetBuffer& haystack, const std::string& needle)
62 {
63   const char* hd = haystack.getData();
64   int hl = haystack.getLength();
65   int nl = needle.length();
66   
67   for (int i = MAX (nl-hl, 0); i < nl; i++) {
68     //if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) {
69     if (memcmp(needle.c_str(), &hd[hl-(nl-i)], nl-i) == 0) {
70       return (nl-i);
71     }
72   }
73   return 0;
74 }
75
76 static int
77 find_terminator(const NetBuffer& haystack, const std::string& needle)
78 {
79   if( !needle.empty() )
80   {
81     const char* data = haystack.getData();
82     const char* ptr = strstr(data,needle.c_str());
83     if (ptr != NULL)
84       return(ptr-data);
85   }
86   return -1;
87 }
88
89 bool NetChat::push (const char* s)
90 {
91   return bufferSend ( s, strlen(s) ) ;
92 }
93
94 void
95 NetChat::handleBufferRead (NetBuffer& in_buffer)
96 {
97   // Continue to search for terminator in in_buffer,
98   // while calling collect_incoming_data.  The while loop is
99   // necessary because we might read several data+terminator combos
100   // with a single recv().
101   
102   while (in_buffer.getLength()) {      
103     // special case where we're not using a terminator
104     if ( terminator.empty() ) {
105         if ( bytesToCollect > 0) {
106             const int toRead = std::min(in_buffer.getLength(), bytesToCollect);
107             collectIncomingData(in_buffer.getData(), toRead);
108             in_buffer.remove(0, toRead);
109             bytesToCollect -= toRead;
110             if (bytesToCollect ==  0) { // read all requested bytes
111                 foundTerminator();
112             }
113         } else { // read the whole lot
114             collectIncomingData (in_buffer.getData(),in_buffer.getLength());
115             in_buffer.remove ();
116         }
117         
118         continue;
119     }
120     
121     int index = find_terminator ( in_buffer, terminator ) ;
122     
123     // 3 cases:
124     // 1) end of buffer matches terminator exactly:
125     //    collect data, transition
126     // 2) end of buffer matches some prefix:
127     //    collect data to the prefix
128     // 3) end of buffer does not match any prefix:
129     //    collect data
130     
131     if (index != -1) {
132       // we found the terminator
133       collectIncomingData ( in_buffer.getData(), index ) ;
134       in_buffer.remove (0, index + terminator.length());
135       foundTerminator();
136     } else {
137       // check for a prefix of the terminator
138       int num = find_prefix_at_end (in_buffer, terminator);
139       if (num) {
140         int bl = in_buffer.getLength();
141         // we found a prefix, collect up to the prefix
142         collectIncomingData ( in_buffer.getData(), bl - num ) ;
143         in_buffer.remove (0, bl - num);
144         break;
145       } else {
146         // no prefix, collect it all
147         collectIncomingData (in_buffer.getData(), in_buffer.getLength());
148         in_buffer.remove();
149       }
150     }
151   }
152 }
153
154 } // of namespace simgear
155
156