aboutsummaryrefslogtreecommitdiffstats
path: root/filesocket.cpp
blob: 3ba37e648dae64e9a4f9040c54f6e3ccaca1ed9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// filesocket.cpp
// Revision 9-jan-2005

//#include "filesocket.h"
#include "file.h"

#include "socket.h"
#include "util.h"

#include "trace.h"

namespace blassic {

namespace file {

class BlFileSocket : public BlFile {
public:
	BlFileSocket (const std::string & host, short port);
	~BlFileSocket ();
	bool isfile () const { return true; }
	void getline (std::string & str, bool endline= true);
	bool eof ();
	void flush ();
	std::string read (size_t n);
private:
	void outstring (const std::string & str);
	void outchar (char c);

	class Internal;
	Internal * pin;
};

//***********************************************
//              BlFileSocket::Internal
//***********************************************

class BlFileSocket::Internal
{
	TcpSocketClient socket;
public:
	Internal (const std::string & host, short port);
	~Internal ();
	void getline (std::string & str, bool endline);
	bool eof ();
	void flush ();
	std::string read (size_t n);
	void outstring (const std::string & str);
	void outchar (char c);
private:
	Internal (const Internal &); // Forbidden
	void operator = (const Internal &); // Forbidden
};

BlFileSocket::Internal::Internal (const std::string & host, short port) :
	socket (host, port)
{
}

BlFileSocket::Internal::~Internal ()
{
	TRACEFUNC (tr, "BlFileSocke::Internal::~Internal");
}

bool BlFileSocket::Internal::eof ()
{
	return socket.eof ();
}

void BlFileSocket::Internal::flush ()
{
	// There is no work to do.
}

void BlFileSocket::Internal::getline (std::string & str, bool)
{
	str= socket.readline ();
}

std::string BlFileSocket::Internal::read (size_t n)
{
	util::auto_buffer <char> buf (n);
	int r= socket.read (buf, n);
	std::string result;
	if (r > 0)
		result.assign (buf, r);
	return result;
}

void BlFileSocket::Internal::outstring (const std::string & str)
{
	socket.write (str);
}

void BlFileSocket::Internal::outchar (char c)
{
	socket.write (& c, 1);
}

//***********************************************
//              BlFileSocket
//***********************************************

BlFile * newBlFileSocket (const std::string & host, short port)
{
	return new BlFileSocket (host, port);
}

BlFileSocket::BlFileSocket (const std::string & host, short port) :
	BlFile (OpenMode (Input | Output) ),
	pin (new Internal (host, port) )
{ }

BlFileSocket::~BlFileSocket ()
{
	delete pin;
}

bool BlFileSocket::eof ()
{
	return pin->eof ();
}

void BlFileSocket::flush ()
{
	pin->flush ();
}

void BlFileSocket::getline (std::string & str, bool endline)
{
	pin->getline (str, endline);
}

std::string BlFileSocket::read (size_t n)
{
	return pin->read (n);
}

void BlFileSocket::outstring (const std::string & str)
{
	pin->outstring (str);
}

void BlFileSocket::outchar (char c)
{
	pin->outchar (c);
}

} // namespace file

} // namespace blassic

// End of filesocket.cpp
Un proyecto texto-plano.xyz