Skip to content
Newer
Older
100644 153 lines (122 sloc) 2.76 KB
a9461cd initializing repo from version 0.10.2 minus the example files
Julian authored
1 // filesocket.cpp
2 // Revision 9-jan-2005
3
4 //#include "filesocket.h"
5 #include "file.h"
6
7 #include "socket.h"
8 #include "util.h"
9
10 #include "trace.h"
11
12 namespace blassic {
13
14 namespace file {
15
16 class BlFileSocket : public BlFile {
17 public:
18 BlFileSocket (const std::string & host, short port);
19 ~BlFileSocket ();
20 bool isfile () const { return true; }
21 void getline (std::string & str, bool endline= true);
22 bool eof ();
23 void flush ();
24 std::string read (size_t n);
25 private:
26 void outstring (const std::string & str);
27 void outchar (char c);
28
29 class Internal;
30 Internal * pin;
31 };
32
33 //***********************************************
34 // BlFileSocket::Internal
35 //***********************************************
36
37 class BlFileSocket::Internal
38 {
39 TcpSocketClient socket;
40 public:
41 Internal (const std::string & host, short port);
42 ~Internal ();
43 void getline (std::string & str, bool endline);
44 bool eof ();
45 void flush ();
46 std::string read (size_t n);
47 void outstring (const std::string & str);
48 void outchar (char c);
49 private:
50 Internal (const Internal &); // Forbidden
51 void operator = (const Internal &); // Forbidden
52 };
53
54 BlFileSocket::Internal::Internal (const std::string & host, short port) :
55 socket (host, port)
56 {
57 }
58
59 BlFileSocket::Internal::~Internal ()
60 {
61 TRACEFUNC (tr, "BlFileSocke::Internal::~Internal");
62 }
63
64 bool BlFileSocket::Internal::eof ()
65 {
66 return socket.eof ();
67 }
68
69 void BlFileSocket::Internal::flush ()
70 {
71 // There is no work to do.
72 }
73
74 void BlFileSocket::Internal::getline (std::string & str, bool)
75 {
76 str= socket.readline ();
77 }
78
79 std::string BlFileSocket::Internal::read (size_t n)
80 {
81 util::auto_buffer <char> buf (n);
82 int r= socket.read (buf, n);
83 std::string result;
84 if (r > 0)
85 result.assign (buf, r);
86 return result;
87 }
88
89 void BlFileSocket::Internal::outstring (const std::string & str)
90 {
91 socket.write (str);
92 }
93
94 void BlFileSocket::Internal::outchar (char c)
95 {
96 socket.write (& c, 1);
97 }
98
99 //***********************************************
100 // BlFileSocket
101 //***********************************************
102
103 BlFile * newBlFileSocket (const std::string & host, short port)
104 {
105 return new BlFileSocket (host, port);
106 }
107
108 BlFileSocket::BlFileSocket (const std::string & host, short port) :
109 BlFile (OpenMode (Input | Output) ),
110 pin (new Internal (host, port) )
111 { }
112
113 BlFileSocket::~BlFileSocket ()
114 {
115 delete pin;
116 }
117
118 bool BlFileSocket::eof ()
119 {
120 return pin->eof ();
121 }
122
123 void BlFileSocket::flush ()
124 {
125 pin->flush ();
126 }
127
128 void BlFileSocket::getline (std::string & str, bool endline)
129 {
130 pin->getline (str, endline);
131 }
132
133 std::string BlFileSocket::read (size_t n)
134 {
135 return pin->read (n);
136 }
137
138 void BlFileSocket::outstring (const std::string & str)
139 {
140 pin->outstring (str);
141 }
142
143 void BlFileSocket::outchar (char c)
144 {
145 pin->outchar (c);
146 }
147
148 } // namespace file
149
150 } // namespace blassic
151
152 // End of filesocket.cpp
Something went wrong with that request. Please try again.