summaryrefslogtreecommitdiff
path: root/tests/examplefiles/FakeFile.pike
blob: 48f3ea64a5d272c31fbaa94c16b21161884b09cc (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#pike __REAL_VERSION__

//! A string wrapper that pretends to be a @[Stdio.File] object
//! in addition to some features of a @[Stdio.FILE] object.


//! This constant can be used to distinguish a FakeFile object
//! from a real @[Stdio.File] object.
constant is_fake_file = 1;

protected string data;
protected int ptr;
protected int(0..1) r;
protected int(0..1) w;
protected int mtime;

protected function read_cb;
protected function read_oob_cb;
protected function write_cb;
protected function write_oob_cb;
protected function close_cb;

//! @seealso
//!   @[Stdio.File()->close()]
int close(void|string direction) {
  direction = lower_case(direction||"rw");
  int cr = has_value(direction, "r");
  int cw = has_value(direction, "w");

  if(cr) {
    r = 0;
  }

  if(cw) {
    w = 0;
  }

  // FIXME: Close callback
  return 1;
}

//! @decl void create(string data, void|string type, void|int pointer)
//! @seealso
//!   @[Stdio.File()->create()]
void create(string _data, void|string type, int|void _ptr) {
  if(!_data) error("No data string given to FakeFile.\n");
  data = _data;
  ptr = _ptr;
  mtime = time();
  if(type) {
    type = lower_case(type);
    if(has_value(type, "r"))
      r = 1;
    if(has_value(type, "w"))
      w = 1;
  }
  else
    r = w = 1;
}

protected string make_type_str() {
  string type = "";
  if(r) type += "r";
  if(w) type += "w";
  return type;
}

//! @seealso
//!   @[Stdio.File()->dup()]
this_program dup() {
  return this_program(data, make_type_str(), ptr);
}

//! Always returns 0.
//! @seealso
//!   @[Stdio.File()->errno()]
int errno() { return 0; }

//! Returns size and the creation time of the string.
Stdio.Stat stat() {
  Stdio.Stat st = Stdio.Stat();
  st->size = sizeof(data);
  st->mtime=st->ctime=mtime;
  st->atime=time();
  return st;
}

//! @seealso
//!   @[Stdio.File()->line_iterator()]
String.SplitIterator line_iterator(int|void trim) {
  if(trim)
    return String.SplitIterator( data-"\r", '\n' );
  return String.SplitIterator( data, '\n' );
}

protected mixed id;

//! @seealso
//!   @[Stdio.File()->query_id()]
mixed query_id() { return id; }

//! @seealso
//!   @[Stdio.File()->set_id()]
void set_id(mixed _id) { id = _id; }

//! @seealso
//!   @[Stdio.File()->read_function()]
function(:string) read_function(int nbytes) {
  return lambda() { return read(nbytes); };
}

//! @seealso
//!   @[Stdio.File()->peek()]
int(-1..1) peek(int|float|void timeout) {
  if(!r) return -1;
  if(ptr >= sizeof(data)) return 0;
  return 1;
}

//! Always returns 0.
//! @seealso
//!   @[Stdio.File()->query_address()]
string query_address(void|int(0..1) is_local) { return 0; }

//! @seealso
//!   @[Stdio.File()->read()]
string read(void|int(0..) len, void|int(0..1) not_all) {
  if(!r) return 0;
  if (len < 0) error("Cannot read negative number of characters.\n");
  int start=ptr;
  ptr += len;
  if(zero_type(len) || ptr>sizeof(data))
    ptr = sizeof(data);

  // FIXME: read callback
  return data[start..ptr-1];
}

//! @seealso
//!   @[Stdio.FILE()->gets()]
string gets() {
  if(!r) return 0;
  string ret;
  sscanf(data,"%*"+(string)ptr+"s%[^\n]",ret);
  if(ret)
  {
    ptr+=sizeof(ret)+1;
    if(ptr>sizeof(data))
    {
      ptr=sizeof(data);
      if(!sizeof(ret))
	ret = 0;
    }
  }

  // FIXME: read callback
  return ret;
}

//! @seealso
//!   @[Stdio.FILE()->getchar()]
int getchar() {
  if(!r) return 0;
  int c;
  if(catch(c=data[ptr]))
    c=-1;
  else
    ptr++;

  // FIXME: read callback
  return c;
}

//! @seealso
//!   @[Stdio.FILE()->unread()]
void unread(string s) {
  if(!r) return;
  if(data[ptr-sizeof(s)..ptr-1]==s)
    ptr-=sizeof(s);
  else
  {
    data=s+data[ptr..];
    ptr=0;
  }
}

//! @seealso
//!   @[Stdio.File()->seek()]
int seek(int pos, void|int mult, void|int add) {
  if(mult)
    pos = pos*mult+add;
  if(pos<0)
  {
    pos = sizeof(data)+pos;
    if( pos < 0 )
	pos = 0;
  }
  ptr = pos;
  if( ptr > strlen( data ) )
      ptr = strlen(data);
  return ptr;
}

//! Always returns 1.
//! @seealso
//!   @[Stdio.File()->sync()]
int(1..1) sync() { return 1; }

//! @seealso
//!   @[Stdio.File()->tell()]
int tell() { return ptr; }

//! @seealso
//!   @[Stdio.File()->truncate()]
int(0..1) truncate(int length) {
  data = data[..length-1];
  return sizeof(data)==length;
}

//! @seealso
//!   @[Stdio.File()->write()]
int(-1..) write(string|array(string) str, mixed ... extra) {
  if(!w) return -1;
  if(arrayp(str)) str=str*"";
  if(sizeof(extra)) str=sprintf(str, @extra);

  if(ptr==sizeof(data)) {
    data += str;
    ptr = sizeof(data);
  }
  else if(sizeof(str)==1)
    data[ptr++] = str[0];
  else {
    data = data[..ptr-1] + str + data[ptr+sizeof(str)..];
    ptr += sizeof(str);
  }

  // FIXME: write callback
  return sizeof(str);
}

//! @seealso
//!   @[Stdio.File()->set_blocking]
void set_blocking() {
  close_cb = 0;
  read_cb = 0;
  read_oob_cb = 0;
  write_cb = 0;
  write_oob_cb = 0;
}

//! @seealso
//!   @[Stdio.File()->set_blocking_keep_callbacks]
void set_blocking_keep_callbacks() { }

//! @seealso
//!   @[Stdio.File()->set_blocking]
void set_nonblocking(function rcb, function wcb, function ccb,
		     function rocb, function wocb) {
  read_cb = rcb;
  write_cb = wcb;
  close_cb = ccb;
  read_oob_cb = rocb;
  write_oob_cb = wocb;
}

//! @seealso
//!   @[Stdio.File()->set_blocking_keep_callbacks]
void set_nonblocking_keep_callbacks() { }


//! @seealso
//!   @[Stdio.File()->set_close_callback]
void set_close_callback(function cb) { close_cb = cb; }

//! @seealso
//!   @[Stdio.File()->set_read_callback]
void set_read_callback(function cb) { read_cb = cb; }

//! @seealso
//!   @[Stdio.File()->set_read_oob_callback]
void set_read_oob_callback(function cb) { read_oob_cb = cb; }

//! @seealso
//!   @[Stdio.File()->set_write_callback]
void set_write_callback(function cb) { write_cb = cb; }

//! @seealso
//!   @[Stdio.File()->set_write_oob_callback]
void set_write_oob_callback(function cb) { write_oob_cb = cb; }


//! @seealso
//!   @[Stdio.File()->query_close_callback]
function query_close_callback() { return close_cb; }

//! @seealso
//!   @[Stdio.File()->query_read_callback]
function query_read_callback() { return read_cb; }

//! @seealso
//!   @[Stdio.File()->query_read_oob_callback]
function query_read_oob_callback() { return read_oob_cb; }

//! @seealso
//!   @[Stdio.File()->query_write_callback]
function query_write_callback() { return write_cb; }

//! @seealso
//!   @[Stdio.File()->query_write_oob_callback]
function query_write_oob_callback() { return write_oob_cb; }

string _sprintf(int t) {
  return t=='O' && sprintf("%O(%d,%O)", this_program, sizeof(data),
			   make_type_str());
}


// FakeFile specials.

//! A FakeFile can be casted to a string.
mixed cast(string to) {
  switch(to) {
  case "string": return data;
  case "object": return this;
  }
  error("Can not cast object to %O.\n", to);
}

//! Sizeof on a FakeFile returns the size of its contents.
int(0..) _sizeof() {
  return sizeof(data);
}

//! @ignore

#define NOPE(X) mixed X (mixed ... args) { error("This is a FakeFile. %s is not available.\n", #X); }
NOPE(assign);
NOPE(async_connect);
NOPE(connect);
NOPE(connect_unix);
NOPE(open);
NOPE(open_socket);
NOPE(pipe);
NOPE(tcgetattr);
NOPE(tcsetattr);

// Stdio.Fd
NOPE(dup2);
NOPE(lock); // We could implement this
NOPE(mode); // We could implement this
NOPE(proxy); // We could implement this
NOPE(query_fd);
NOPE(read_oob);
NOPE(set_close_on_exec);
NOPE(set_keepalive);
NOPE(trylock); // We could implement this
NOPE(write_oob);

//! @endignore