]> git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
compilation fix
[wxWidgets.git] / src / common / protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: protocol.cpp
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 07/07/1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "protocol.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/protocol/protocol.h"
24 #include "wx/url.h"
25 #include "wx/module.h"
26
27 #include <stdlib.h>
28
29 /////////////////////////////////////////////////////////////////
30 // wxProtoInfo
31 /////////////////////////////////////////////////////////////////
32
33 /*
34 * --------------------------------------------------------------
35 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
36 * --------------------------------------------------------------
37 */
38
39 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
40 const bool need_host1, wxClassInfo *info)
41 {
42 m_protoname = name;
43 m_servname = serv;
44 m_cinfo = info;
45 m_needhost = need_host1;
46 next = wxURL::ms_protocols;
47 wxURL::ms_protocols = this;
48 }
49
50 /////////////////////////////////////////////////////////////////
51 // wxProtocol ///////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////
53
54 #if wxUSE_SOCKETS
55 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
56 #else
57 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
58 #endif
59
60 wxProtocol::wxProtocol()
61 #if wxUSE_SOCKETS
62 : wxSocketClient()
63 #endif
64 {
65 }
66
67 #if wxUSE_SOCKETS
68 bool wxProtocol::Reconnect()
69 {
70 wxIPV4address addr;
71
72 if (!GetPeer(addr))
73 {
74 Close();
75 return FALSE;
76 }
77 if (!Close())
78 return FALSE;
79 if (!Connect(addr))
80 return FALSE;
81
82 return TRUE;
83 }
84
85 // ----------------------------------------------------------------------------
86 // Read a line from socket
87 // ----------------------------------------------------------------------------
88
89 // TODO ReadLine() should use buffers private to wxProtocol for efficiency!
90
91 // static
92 wxProtocolError wxProtocol::ReadLine(wxSocketBase *socket, wxString& result)
93 {
94 result.Empty();
95 char ch, chLast = '\0';
96 while ( !socket->Read(&ch, sizeof(ch)).Error() )
97 {
98 switch ( ch )
99 {
100 case '\r':
101 // remember it, if the following is '\n', we're done
102 chLast = '\r';
103 break;
104
105 case '\n':
106 // only ends line if the previous character was '\r'
107 if ( chLast == '\r' )
108 {
109 // EOL found
110 return wxPROTO_NOERR;
111 }
112 //else: fall through
113
114 default:
115 // normal char
116 if ( chLast )
117 {
118 result += chLast;
119 chLast = '\0';
120 }
121
122 result += ch;
123 }
124 }
125
126 return wxPROTO_NETERR;
127 }
128
129 wxProtocolError wxProtocol::ReadLine(wxString& result)
130 {
131 return ReadLine(this, result);
132 }
133
134 // old function which only chops '\n' and not '\r\n'
135 wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
136 #define PROTO_BSIZE 2048
137 size_t avail, size;
138 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
139 char *ret;
140 bool found;
141
142 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
143 if (sock->Error() || avail == 0)
144 return wxPROTO_NETERR;
145
146 memcpy(tmp_str, tmp_buf, avail);
147
148 // Not implemented on all systems
149 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
150 found = FALSE;
151 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
152 if (*ret == '\n') {
153 found = TRUE;
154 break;
155 }
156
157 if (!found)
158 return wxPROTO_PROTERR;
159 *ret = 0;
160
161 result = tmp_str;
162 result = result.Left(result.Length()-1);
163
164 size = ret-tmp_str+1;
165 sock->Unread(&tmp_buf[size], avail-size);
166 return wxPROTO_NOERR;
167 #undef PROTO_BSIZE
168 }
169 #endif