Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 289 lines (257 sloc) 6.892 kb
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
1 // winxedxx_handle.cxx
38b4bf9 @NotFound implement read, print and eof in StringHandle
authored
2 // (C) 2011-2012 Julián Albo
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
3
4 #include "winxedxx_types.h"
5 #include "winxedxx_object.h"
6 #include "winxedxx_default.h"
7 #include "winxedxx_integer.h"
8 #include "winxedxx_handle.h"
9
f30e094 @NotFound missing include
authored
10 #include <stdio.h>
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
11 #include <stdlib.h>
12
13 namespace WinxedXX
14 {
15
16 //*************************************************************
17
18 WxxFileHandle::WxxFileHandle(int predef) : WxxDefault("FileHandle")
19 {
20 switch (predef) {
21 case 1:
22 f = stdin;
23 break;
24 case 2:
25 f = stdout;
26 break;
27 case 3:
28 f = stderr;
29 break;
30 default:
31 f = 0;
32 }
33 }
34
35 WxxFileHandle::~WxxFileHandle()
36 {
37 //std::cerr << "~WxxFileHandle\n";
38 if (f && f != stdin && f != stdout && f != stderr)
39 fclose(f);
40 }
41
fd64b59 @NotFound refactor builtin open
authored
42 WxxObject *WxxFileHandle::open(const std::string &name)
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
43 {
fd64b59 @NotFound refactor builtin open
authored
44 f = fopen(name.c_str(), "r");
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
45 return this;
46 }
47
fd64b59 @NotFound refactor builtin open
authored
48 WxxObject *WxxFileHandle::open(const std::string & name,
49 const std::string &mode)
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
50 {
51 if (f)
52 throw wxx_error("FileHandle is already open");
fd64b59 @NotFound refactor builtin open
authored
53 for (size_t i = 0; i < mode.length(); ++i) {
54 switch (mode[i]) {
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
55 case 'r':
56 case 'w':
57 case 'a':
58 case 'b':
59 break;
60 default:
61 throw wxx_error("Invalid mode in open");
62 }
63 }
fd64b59 @NotFound refactor builtin open
authored
64 f = fopen(name.c_str(), mode.c_str());
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
65 return this;
66 }
67
fd64b59 @NotFound refactor builtin open
authored
68 WxxObject *WxxFileHandle::open(WxxObjectPtr name)
69 {
70 return open(name.get_string());
71 }
72
73 WxxObject *WxxFileHandle::open(WxxObjectPtr name, WxxObjectPtr mode)
74 {
75 return open(name.get_string(), mode.get_string());
76 }
77
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
78 WxxObjectPtr WxxFileHandle::close()
79 {
80 int r;
81 if (f) {
82 r = fclose(f);
83 f = 0;
84 }
85 else
86 r = -1;
87 return r;
88 }
89
90 void WxxFileHandle::print(WxxObjectPtr obj)
91 {
92 if (! f)
93 throw wxx_error("FileHandle is closed");
94 fputs(obj.get_string().c_str(), f);
95 }
96
97 WxxObjectPtr WxxFileHandle::read(int n)
98 {
99 if (! f)
100 throw wxx_error("FileHandle is closed");
101 char *buf = (char *)malloc(n);
102 *buf = '\0';
103 size_t r = fread(buf, 1, n, f);
104 //std::cerr << "\nread " << r << "\n";
105 std::string result = r > 0 ? std::string(buf, r) : std::string();
106 free(buf);
48831b9 @NotFound use less object ptr implicit initialization with string
authored
107 return WxxObjectPtr(new WxxString(result));
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
108 }
109
110 WxxObjectPtr WxxFileHandle::readline()
111 {
112 if (! f)
113 throw wxx_error("FileHandle is closed");
114 char buffer[1024];
115 const char *r = fgets(buffer, 1024, f);
116 if (! r)
117 r = "";
48831b9 @NotFound use less object ptr implicit initialization with string
authored
118 return WxxObjectPtr(new WxxString(std::string(r)));
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
119 }
120
121 WxxObjectPtr WxxFileHandle::call_method(const std::string &methname, WxxObjectArray &args)
122 {
123 if (methname == "readline") {
124 if (args.elements() > 0)
125 throw wxx_error("too many positional arguments in readline");
126 return readline();
127 }
0768ce2 @NotFound fix FileHandle.print
authored
128 if (methname == "print") {
129 if (args.elements() != 1)
130 throw wxx_error("wrong number of positional arguments in print");
131 print(args.get_pmc_keyed(0));
132 return winxedxxnull;
133 }
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
134 if (methname == "read") {
135 if (args.elements() != 1)
136 throw wxx_error("wrong number of positional arguments in read");
137 return read(args.get_pmc_keyed(0));
138 }
139 if (methname == "open") {
140 switch (args.elements()) {
141 case 1:
142 return open(args.get_pmc_keyed(0));
143 case 2:
144 return open(args.get_pmc_keyed(0), args.get_pmc_keyed(1));
145 default:
146 throw wxx_error("too many positional arguments in close");
147 }
148 }
149 if (methname == "close") {
150 if (args.elements() > 0)
151 throw wxx_error("too many positional arguments in close");
152 return close();
153 }
154 if (methname == "eof") {
155 if (args.elements() > 0)
156 throw wxx_error("too many positional arguments in close");
157 return feof(f);
158 }
159 else
160 return WxxDefault::call_method(methname, args);
161 }
162
163 //*************************************************************
164
7f00fa7 @NotFound incomplete implementation of StringHandle and fake-implement new Boolean...
authored
165 WxxStringHandle::WxxStringHandle() : WxxDefault("StringHandle"),
166 pos(-1)
167 {
168 }
169
170 WxxStringHandle::~WxxStringHandle()
171 {
172 }
173
174 WxxObject *WxxStringHandle::open(const std::string &name)
175 {
176 pos = 0;
177 return this;
178 }
179
180 WxxObject *WxxStringHandle::open(const std::string & name,
181 const std::string &mode)
182 {
183 if (pos >= 0)
184 throw wxx_error("StringHandle is already open");
185 for (size_t i = 0; i < mode.length(); ++i) {
186 switch (mode[i]) {
187 case 'r':
188 case 'w':
189 case 'a':
190 case 'b':
191 break;
192 default:
193 throw wxx_error("Invalid mode in open");
194 }
195 }
196 pos = 0;
197 return this;
198 }
199
200 WxxObject *WxxStringHandle::open(WxxObjectPtr name)
201 {
202 return open(name.get_string());
203 }
204
205 WxxObject *WxxStringHandle::open(WxxObjectPtr name, WxxObjectPtr mode)
206 {
207 return open(name.get_string(), mode.get_string());
208 }
209
210 WxxObjectPtr WxxStringHandle::close()
211 {
212 int r = 0;
213 if (pos >= 0) {
214 pos = -1;
215 }
216 else
217 r = -1;
218 return r;
219 }
220
38b4bf9 @NotFound implement read, print and eof in StringHandle
authored
221 bool WxxStringHandle::eof() const
222 {
223 return pos < 0 || pos >= int(s.length());
224 }
225
7f00fa7 @NotFound incomplete implementation of StringHandle and fake-implement new Boolean...
authored
226 void WxxStringHandle::print(WxxObjectPtr obj)
227 {
228 if (pos < 0)
229 throw wxx_error("StringHandle is closed");
38b4bf9 @NotFound implement read, print and eof in StringHandle
authored
230 s += obj.get_string();
7f00fa7 @NotFound incomplete implementation of StringHandle and fake-implement new Boolean...
authored
231 }
232
233 WxxObjectPtr WxxStringHandle::read(int n)
234 {
235 if (pos < 0)
236 throw wxx_error("StringHandle is closed");
38b4bf9 @NotFound implement read, print and eof in StringHandle
authored
237 if (eof())
238 return winxedxxnull;
239
240 std::string result = s.substr(pos, n);
241 pos += result.length();
7f00fa7 @NotFound incomplete implementation of StringHandle and fake-implement new Boolean...
authored
242 return WxxObjectPtr(new WxxString(result));
243 }
244
245 WxxObjectPtr WxxStringHandle::call_method(const std::string &methname, WxxObjectArray &args)
246 {
247 if (methname == "print") {
248 if (args.elements() != 1)
249 throw wxx_error("wrong number of positional arguments in print");
250 print(args.get_pmc_keyed(0));
251 return winxedxxnull;
252 }
253 if (methname == "read") {
254 if (args.elements() != 1)
255 throw wxx_error("wrong number of positional arguments in read");
256 return read(args.get_pmc_keyed(0));
257 }
258 if (methname == "open") {
259 switch (args.elements()) {
260 case 1:
261 return open(args.get_pmc_keyed(0));
262 case 2:
263 return open(args.get_pmc_keyed(0), args.get_pmc_keyed(1));
264 default:
265 throw wxx_error("too many positional arguments in close");
266 }
267 }
268 if (methname == "close") {
269 if (args.elements() > 0)
270 throw wxx_error("too many positional arguments in close");
271 return close();
272 }
273 if (methname == "eof") {
274 if (args.elements() > 0)
275 throw wxx_error("too many positional arguments in close");
38b4bf9 @NotFound implement read, print and eof in StringHandle
authored
276 return eof();
7f00fa7 @NotFound incomplete implementation of StringHandle and fake-implement new Boolean...
authored
277 }
278 if (methname == "encoding")
279 return winxedxxnull;
280 else
281 return WxxDefault::call_method(methname, args);
282 }
283
284 //*************************************************************
285
0d193c2 @NotFound move FileHandle out of winxedxx.h
authored
286 } // namespace WinxedXX
287
288 // End
Something went wrong with that request. Please try again.