]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_netChat.cxx
Add optional attribute condition to "copyProperties".
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 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> // for strdup
29
30 namespace  simgear {
31
32 void
33 NetChat::setTerminator (const char* t)
34 {
35   if (terminator) delete[] terminator;
36   terminator = strdup(t);
37 }
38
39 const char*
40 NetChat::getTerminator (void)
41 {
42   return terminator;
43 }
44
45 // return the size of the largest prefix of needle at the end
46 // of haystack
47
48 #define MAX(a,b) (((a)>(b))?(a):(b))
49
50 static int
51 find_prefix_at_end (const NetBuffer& haystack, const char* needle)
52 {
53   const char* hd = haystack.getData();
54   int hl = haystack.getLength();
55   int nl = strlen(needle);
56   
57   for (int i = MAX (nl-hl, 0); i < nl; i++) {
58     //if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) {
59     if (memcmp(needle, &hd[hl-(nl-i)], nl-i) == 0) {
60       return (nl-i);
61     }
62   }
63   return 0;
64 }
65
66 static int
67 find_terminator (const NetBuffer& haystack, const char* needle)
68 {
69   if (needle && *needle)
70   {
71     const char* data = haystack.getData();
72     const char* ptr = strstr(data,needle);
73     if (ptr != NULL)
74       return(ptr-data);
75   }
76   return -1;
77 }
78
79 bool NetChat::push (const char* s)
80 {
81   return bufferSend ( s, strlen(s) ) ;
82 }
83
84 void
85 NetChat::handleBufferRead (NetBuffer& in_buffer)
86 {
87   // Continue to search for terminator in in_buffer,
88   // while calling collect_incoming_data.  The while loop is
89   // necessary because we might read several data+terminator combos
90   // with a single recv().
91   
92   while (in_buffer.getLength()) {
93
94     // special case where we're not using a terminator
95     if (terminator == 0 || *terminator == 0) {
96       collectIncomingData (in_buffer.getData(),in_buffer.getLength());
97       in_buffer.remove ();
98       return;
99     }
100     
101     int terminator_len = strlen(terminator);
102     
103     int index = find_terminator ( in_buffer, terminator ) ;
104     
105     // 3 cases:
106     // 1) end of buffer matches terminator exactly:
107     //    collect data, transition
108     // 2) end of buffer matches some prefix:
109     //    collect data to the prefix
110     // 3) end of buffer does not match any prefix:
111     //    collect data
112     
113     if (index != -1) {
114       // we found the terminator
115       collectIncomingData ( in_buffer.getData(), index ) ;
116       in_buffer.remove (0, index + terminator_len);
117       foundTerminator();
118     } else {
119       // check for a prefix of the terminator
120       int num = find_prefix_at_end (in_buffer, terminator);
121       if (num) {
122         int bl = in_buffer.getLength();
123         // we found a prefix, collect up to the prefix
124         collectIncomingData ( in_buffer.getData(), bl - num ) ;
125         in_buffer.remove (0, bl - num);
126         break;
127       } else {
128         // no prefix, collect it all
129         collectIncomingData (in_buffer.getData(), in_buffer.getLength());
130         in_buffer.remove();
131       }
132     }
133   }
134 }
135
136 } // of namespace simgear
137
138