This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
THMemoryFile.cpp
689 lines (614 loc) · 26.8 KB
/
THMemoryFile.cpp
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#include <TH/THMemoryFile.h>
#include <TH/THStorageFunctions.hpp>
#include <TH/THFilePrivate.h>
#include <TH/THDiskFile.h>
#include <stdint.h>
#ifndef _WIN32
#include <sys/types.h>
#endif
typedef struct THMemoryFile__
{
THFile file;
THCharStorage *storage;
ssize_t size;
ssize_t position;
int longSize;
} THMemoryFile;
static int THMemoryFile_isOpened(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
return (mfself->storage != NULL);
}
static int8_t *THMemoryFile_strnextspace(int8_t *str_, int8_t *c_)
{
int8_t c;
while( (c = *str_) )
{
if( (c != ' ') && (c != '\n') && (c != ':') && (c != ';') )
break;
str_++;
}
while( (c = *str_) )
{
if( (c == ' ') || (c == '\n') || (c == ':') || (c == ';') )
{
*c_ = c;
*str_ = '\0';
return(str_);
}
str_++;
}
return NULL;
}
static void THMemoryFile_grow(THMemoryFile *self, ssize_t size)
{
ssize_t missingSpace;
if(size <= self->size)
return;
else
{
if(size < self->storage->numel()) /* note the "<" and not "<=" */
{
self->size = size;
THCharStorage_data(self->storage)[self->size] = '\0';
return;
}
}
missingSpace = size-self->storage->numel()+1; /* +1 for the '\0' */
THCharStorage_resize(self->storage, (self->storage->numel()/2 > missingSpace ?
self->storage->numel() + (self->storage->numel()/2)
: self->storage->numel() + missingSpace));
}
static int THMemoryFile_mode(const char *mode, int *isReadable, int *isWritable)
{
*isReadable = 0;
*isWritable = 0;
if(strlen(mode) == 1)
{
if(*mode == 'r')
{
*isReadable = 1;
return 1;
}
else if(*mode == 'w')
{
*isWritable = 1;
return 1;
}
}
else if(strlen(mode) == 2)
{
if(mode[0] == 'r' && mode[1] == 'w')
{
*isReadable = 1;
*isWritable = 1;
return 1;
}
}
return 0;
}
/********************************************************/
#define READ_WRITE_METHODS(TYPE, TYPEC, ASCII_READ_ELEM, ASCII_WRITE_ELEM, INSIDE_SPACING) \
static ssize_t THMemoryFile_read##TYPEC(THFile *self, TYPE *data, ssize_t n) \
{ \
THMemoryFile *mfself = (THMemoryFile*)self; \
ssize_t nread = 0; \
\
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file"); \
THArgCheck(mfself->file.isReadable, 1, "attempt to read in a write-only file"); \
\
if (n == 0) \
return 0; \
\
if(mfself->file.isBinary) \
{ \
ssize_t nByte = sizeof(TYPE)*n; \
ssize_t nByteRemaining = (mfself->position + nByte <= mfself->size ? nByte : mfself->size-mfself->position); \
nread = nByteRemaining/sizeof(TYPE); \
memmove(data, THCharStorage_data(mfself->storage)+mfself->position, nread*sizeof(TYPE)); \
mfself->position += nread*sizeof(TYPE); \
} \
else \
{ \
ssize_t i; \
for(i = 0; i < n; i++) \
{ \
ssize_t nByteRead = 0; \
int8_t spaceChar = 0; \
int8_t *spacePtr = THMemoryFile_strnextspace(THCharStorage_data(mfself->storage)+mfself->position, &spaceChar); \
ASCII_READ_ELEM; \
if(ret == EOF) \
{ \
while(THCharStorage_data(mfself->storage)[mfself->position]) \
mfself->position++; \
} \
else \
mfself->position += nByteRead; \
if(spacePtr) \
*spacePtr = spaceChar; \
} \
if(mfself->file.isAutoSpacing && (n > 0)) \
{ \
if( (mfself->position < mfself->size) && (THCharStorage_data(mfself->storage)[mfself->position] == '\n') ) \
mfself->position++; \
} \
} \
\
if(nread != n) \
{ \
mfself->file.hasError = 1; /* shouldn't we put hasError to 0 all the time ? */ \
if(!mfself->file.isQuiet) \
THError("read error: read %d blocks instead of %d", nread, n); \
} \
\
return nread; \
} \
\
static ssize_t THMemoryFile_write##TYPEC(THFile *self, TYPE *data, ssize_t n) \
{ \
THMemoryFile *mfself = (THMemoryFile*)self; \
\
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file"); \
THArgCheck(mfself->file.isWritable, 1, "attempt to write in a read-only file"); \
\
if (n == 0) \
return 0; \
\
if(mfself->file.isBinary) \
{ \
ssize_t nByte = sizeof(TYPE)*n; \
THMemoryFile_grow(mfself, mfself->position+nByte); \
memmove(THCharStorage_data(mfself->storage)+mfself->position, data, nByte); \
mfself->position += nByte; \
if(mfself->position > mfself->size) \
{ \
mfself->size = mfself->position; \
THCharStorage_data(mfself->storage)[mfself->size] = '\0'; \
} \
} \
else \
{ \
ssize_t i; \
for(i = 0; i < n; i++) \
{ \
ssize_t nByteWritten; \
while (1) \
{ \
ASCII_WRITE_ELEM; \
if( (nByteWritten > -1) && (nByteWritten < mfself->storage->numel()-mfself->position) ) \
{ \
mfself->position += nByteWritten; \
break; \
} \
THMemoryFile_grow(mfself, mfself->storage->numel() + (mfself->storage->numel()/2) + 2); \
} \
if(mfself->file.isAutoSpacing) \
{ \
if(i < n-1) \
{ \
THMemoryFile_grow(mfself, mfself->position+1); \
sprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, " "); \
mfself->position++; \
} \
if(i == n-1) \
{ \
THMemoryFile_grow(mfself, mfself->position+1); \
sprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, "\n"); \
mfself->position++; \
} \
} \
} \
if(mfself->position > mfself->size) \
{ \
mfself->size = mfself->position; \
THCharStorage_data(mfself->storage)[mfself->size] = '\0'; \
} \
} \
\
return n; \
}
void THMemoryFile_longSize(THFile *self, int size)
{
THMemoryFile *dfself = (THMemoryFile*)(self);
THArgCheck(size == 0 || size == 4 || size == 8, 1, "Invalid long size specified");
dfself->longSize = size;
}
THCharStorage *THMemoryFile_storage(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THCharStorage_resize(mfself->storage, mfself->size+1);
return mfself->storage;
}
static void THMemoryFile_synchronize(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
}
static void THMemoryFile_seek(THFile *self, ssize_t position)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THArgCheck(position >= 0, 2, "position must be positive");
if(position <= mfself->size)
mfself->position = position;
else
{
mfself->file.hasError = 1;
if(!mfself->file.isQuiet)
THError("unable to seek at position %zu", position);
}
}
static void THMemoryFile_seekEnd(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
mfself->position = mfself->size;
}
static ssize_t THMemoryFile_position(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
return mfself->position;
}
static void THMemoryFile_close(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THCharStorage_free(mfself->storage);
mfself->storage = NULL;
}
static void THMemoryFile_free(THFile *self)
{
THMemoryFile *mfself = (THMemoryFile*)self;
if(mfself->storage)
THCharStorage_free(mfself->storage);
THFree(mfself);
}
/* READ_WRITE_METHODS(bool, Bool, */
/* int value = 0; int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%d%n", &value, &nByteRead); data[i] = (value ? 1 : 0), */
/* int value = (data[i] ? 1 : 0); nByteWritten = snprintf(THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%d", value), */
/* 1) */
READ_WRITE_METHODS(uint8_t, Byte,
ssize_t ret = (mfself->position + n <= mfself->size ? n : mfself->size-mfself->position); \
if(spacePtr) *spacePtr = spaceChar; \
nByteRead = ret; \
nread = ret; \
i = n-1; \
memmove(data, THCharStorage_data(mfself->storage)+mfself->position, nByteRead),
nByteWritten = (n < mfself->storage->numel()-mfself->position ? n : -1); \
i = n-1; \
if(nByteWritten > -1)
memmove(THCharStorage_data(mfself->storage)+mfself->position, data, nByteWritten),
0)
/* DEBUG: we should check if %n is count or not as a element (so ret might need to be ret-- on some systems) */
/* Note that we do a trick for char */
READ_WRITE_METHODS(int8_t, Char,
ssize_t ret = (mfself->position + n <= mfself->size ? n : mfself->size-mfself->position); \
if(spacePtr) *spacePtr = spaceChar; \
nByteRead = ret; \
nread = ret; \
i = n-1; \
memmove(data, THCharStorage_data(mfself->storage)+mfself->position, nByteRead),
nByteWritten = (n < mfself->storage->numel()-mfself->position ? n : -1); \
i = n-1; \
if(nByteWritten > -1)
memmove(THCharStorage_data(mfself->storage)+mfself->position, data, nByteWritten),
0)
READ_WRITE_METHODS(int16_t, Short,
int nByteRead_; int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%hd%n", &data[i], &nByteRead_); nByteRead = nByteRead_; if(ret <= 0) break; else nread++,
nByteWritten = snprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%hd", data[i]),
1)
READ_WRITE_METHODS(int32_t, Int,
int nByteRead_; int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%d%n", &data[i], &nByteRead_); nByteRead = nByteRead_; if(ret <= 0) break; else nread++,
nByteWritten = snprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%d", data[i]),
1)
READ_WRITE_METHODS(float, Float,
int nByteRead_; int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%g%n", &data[i], &nByteRead_); nByteRead = nByteRead_; if(ret <= 0) break; else nread++,
nByteWritten = snprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%.9g", data[i]),
1)
READ_WRITE_METHODS(at::Half, Half,
int nByteRead_; float buf; \
int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%g%n", &buf, &nByteRead_); \
data[i] = static_cast<at::Half>(buf); nByteRead = nByteRead_; if(ret <= 0) break; else nread++,
nByteWritten = snprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%.9g", static_cast<float>(data[i])),
1)
READ_WRITE_METHODS(double, Double,
int nByteRead_; int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%lg%n", &data[i], &nByteRead_); nByteRead = nByteRead_; if(ret <= 0) break; else nread++,
nByteWritten = snprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%.17g", data[i]),
1)
static ssize_t THMemoryFile_readLong(THFile *self, int64_t *data, ssize_t n)
{
THMemoryFile *mfself = (THMemoryFile*)self;
ssize_t nread = 0L;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THArgCheck(mfself->file.isReadable, 1, "attempt to read in a write-only file");
if (n == 0)
return 0;
if(mfself->file.isBinary)
{
if(mfself->longSize == 0 || mfself->longSize == sizeof(int64_t))
{
ssize_t nByte = sizeof(int64_t)*n;
ssize_t nByteRemaining = (mfself->position + nByte <= mfself->size ? nByte : mfself->size-mfself->position);
nread = nByteRemaining/sizeof(int64_t);
memmove(data, THCharStorage_data(mfself->storage)+mfself->position, nread*sizeof(int64_t));
mfself->position += nread*sizeof(int64_t);
} else if(mfself->longSize == 4)
{
ssize_t nByte = 4*n;
ssize_t nByteRemaining = (mfself->position + nByte <= mfself->size ? nByte : mfself->size-mfself->position);
int32_t *storage = (int32_t *)(THCharStorage_data(mfself->storage) + mfself->position);
nread = nByteRemaining/4;
ssize_t i;
for(i = 0; i < nread; i++)
data[i] = storage[i];
mfself->position += nread*4;
}
else /* if(mfself->longSize == 8) */
{
int big_endian = !THDiskFile_isLittleEndianCPU();
ssize_t nByte = 8*n;
int32_t *storage = (int32_t *)(THCharStorage_data(mfself->storage) + mfself->position);
ssize_t nByteRemaining = (mfself->position + nByte <= mfself->size ? nByte : mfself->size-mfself->position);
nread = nByteRemaining/8;
ssize_t i;
for(i = 0; i < nread; i++)
data[i] = storage[2*i + big_endian];
mfself->position += nread*8;
}
}
else
{
ssize_t i;
for(i = 0; i < n; i++)
{
ssize_t nByteRead = 0;
int8_t spaceChar = 0;
int8_t *spacePtr = THMemoryFile_strnextspace(THCharStorage_data(mfself->storage)+mfself->position, &spaceChar);
int nByteRead_; int ret = sscanf((char*) THCharStorage_data(mfself->storage)+mfself->position, "%" PRId64 "%n", &data[i], &nByteRead_); nByteRead = nByteRead_; if(ret <= 0) break; else nread++;
if(ret == EOF)
{
while(THCharStorage_data(mfself->storage)[mfself->position])
mfself->position++;
}
else
mfself->position += nByteRead;
if(spacePtr)
*spacePtr = spaceChar;
}
if(mfself->file.isAutoSpacing && (n > 0))
{
if( (mfself->position < mfself->size) && (THCharStorage_data(mfself->storage)[mfself->position] == '\n') )
mfself->position++;
}
}
if(nread != n)
{
mfself->file.hasError = 1; /* shouldn't we put hasError to 0 all the time ? */
if(!mfself->file.isQuiet)
THError("read error: read %d blocks instead of %d", nread, n);
}
return nread;
}
static ssize_t THMemoryFile_writeLong(THFile *self, int64_t *data, ssize_t n)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THArgCheck(mfself->file.isWritable, 1, "attempt to write in a read-only file");
if (n == 0)
return 0;
if(mfself->file.isBinary)
{
if(mfself->longSize == 0 || mfself->longSize == sizeof(int64_t))
{
ssize_t nByte = sizeof(int64_t)*n;
THMemoryFile_grow(mfself, mfself->position+nByte);
memmove(THCharStorage_data(mfself->storage)+mfself->position, data, nByte);
mfself->position += nByte;
} else if(mfself->longSize == 4)
{
ssize_t nByte = 4*n;
THMemoryFile_grow(mfself, mfself->position+nByte);
int32_t *storage = (int32_t *)(THCharStorage_data(mfself->storage) + mfself->position);
ssize_t i;
for(i = 0; i < n; i++)
storage[i] = (int32_t) data[i];
mfself->position += nByte;
}
else /* if(mfself->longSize == 8) */
{
int big_endian = !THDiskFile_isLittleEndianCPU();
ssize_t nByte = 8*n;
THMemoryFile_grow(mfself, mfself->position+nByte);
int32_t *storage = (int32_t *)(THCharStorage_data(mfself->storage) + mfself->position);
ssize_t i;
for(i = 0; i < n; i++)
{
storage[2*i + !big_endian] = 0;
storage[2*i + big_endian] = (int32_t) data[i];
}
mfself->position += nByte;
}
if(mfself->position > mfself->size)
{
mfself->size = mfself->position;
THCharStorage_data(mfself->storage)[mfself->size] = '\0';
}
}
else
{
ssize_t i;
for(i = 0; i < n; i++)
{
ssize_t nByteWritten;
while (1)
{
nByteWritten = snprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, mfself->storage->numel()-mfself->position, "%" PRId64, data[i]);
if( (nByteWritten > -1) && (nByteWritten < mfself->storage->numel()-mfself->position) )
{
mfself->position += nByteWritten;
break;
}
THMemoryFile_grow(mfself, mfself->storage->numel() + (mfself->storage->numel()/2) + 2);
}
if(mfself->file.isAutoSpacing)
{
if(i < n-1)
{
THMemoryFile_grow(mfself, mfself->position+1);
sprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, " ");
mfself->position++;
}
if(i == n-1)
{
THMemoryFile_grow(mfself, mfself->position+1);
sprintf((char*) THCharStorage_data(mfself->storage)+mfself->position, "\n");
mfself->position++;
}
}
}
if(mfself->position > mfself->size)
{
mfself->size = mfself->position;
THCharStorage_data(mfself->storage)[mfself->size] = '\0';
}
}
return n;
}
static int8_t* THMemoryFile_cloneString(const int8_t *str, ssize_t size)
{
int8_t *cstr = static_cast<int8_t*>(THAlloc(size));
memcpy(cstr, str, size);
return cstr;
}
static ssize_t THMemoryFile_readString(THFile *self, const char *format, char **str_)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THArgCheck(mfself->file.isReadable, 1, "attempt to read in a write-only file");
THArgCheck((strlen(format) >= 2 ? (format[0] == '*') && (format[1] == 'a' || format[1] == 'l') : 0), 2, "format must be '*a' or '*l'");
if(mfself->position == mfself->size) /* eof ? */
{
mfself->file.hasError = 1;
if(!mfself->file.isQuiet)
THError("read error: read 0 blocks instead of 1");
*str_ = NULL;
return 0;
}
if(format[1] == 'a')
{
ssize_t str_size = mfself->size-mfself->position;
*str_ = (char*) THMemoryFile_cloneString(THCharStorage_data(mfself->storage)+mfself->position, str_size);
mfself->position = mfself->size;
return str_size;
}
else
{
int8_t *p = THCharStorage_data(mfself->storage)+mfself->position;
int eolFound = 0;
ssize_t posEol;
ssize_t i;
for(i = 0; i < mfself->size-mfself->position; i++)
{
if(p[i] == '\n')
{
posEol = i;
eolFound = 1;
break;
}
}
if(eolFound)
{
*str_ = (char*) THMemoryFile_cloneString(THCharStorage_data(mfself->storage)+mfself->position, posEol);
mfself->position += posEol+1;
return posEol;
}
else /* well, we read all! */
{
ssize_t str_size = mfself->size-mfself->position;
*str_ = (char*) THMemoryFile_cloneString(THCharStorage_data(mfself->storage)+mfself->position, str_size);
mfself->position = mfself->size;
return str_size;
}
}
*str_ = NULL;
return 0;
}
static ssize_t THMemoryFile_writeString(THFile *self, const char *str, ssize_t size)
{
THMemoryFile *mfself = (THMemoryFile*)self;
THArgCheck(mfself->storage != NULL, 1, "attempt to use a closed file");
THArgCheck(mfself->file.isWritable, 1, "attempt to write in a read-only file");
THMemoryFile_grow(mfself, mfself->position+size);
memmove(THCharStorage_data(mfself->storage)+mfself->position, str, size);
mfself->position += size;
if(mfself->position > mfself->size)
{
mfself->size = mfself->position;
THCharStorage_data(mfself->storage)[mfself->size] = '\0';
}
return size;
}
THFile *THMemoryFile_newWithStorage(THCharStorage *storage, const char *mode)
{
static struct THFileVTable vtable = {
THMemoryFile_isOpened,
THMemoryFile_readByte,
THMemoryFile_readChar,
THMemoryFile_readShort,
THMemoryFile_readInt,
THMemoryFile_readLong,
THMemoryFile_readFloat,
THMemoryFile_readDouble,
THMemoryFile_readHalf,
THMemoryFile_readString,
THMemoryFile_writeByte,
THMemoryFile_writeChar,
THMemoryFile_writeShort,
THMemoryFile_writeInt,
THMemoryFile_writeLong,
THMemoryFile_writeFloat,
THMemoryFile_writeDouble,
THMemoryFile_writeHalf,
THMemoryFile_writeString,
THMemoryFile_synchronize,
THMemoryFile_seek,
THMemoryFile_seekEnd,
THMemoryFile_position,
THMemoryFile_close,
THMemoryFile_free
};
THMemoryFile *mfself;
int isReadable;
int isWritable;
if(storage)
{
THArgCheck(THCharStorage_data(storage)[storage->numel()-1] == '\0', 1, "provided CharStorage must be terminated by 0");
THArgCheck(THMemoryFile_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w' or 'rw'");
THCharStorage_retain(storage);
}
else
{
THArgCheck(THMemoryFile_mode(mode, &isReadable, &isWritable), 2, "file mode should be 'r','w' or 'rw'");
storage = THCharStorage_newWithSize(1);
THCharStorage_data(storage)[0] = '\0';
}
mfself = static_cast<THMemoryFile*>(THAlloc(sizeof(THMemoryFile)));
mfself->storage = storage;
mfself->size = (storage ? storage->numel()-1 : 0);
mfself->position = 0;
mfself->longSize = 0;
mfself->file.vtable = &vtable;
mfself->file.isQuiet = 0;
mfself->file.isReadable = isReadable;
mfself->file.isWritable = isWritable;
mfself->file.isBinary = 0;
mfself->file.isAutoSpacing = 1;
mfself->file.hasError = 0;
return (THFile*)mfself;
}
THFile *THMemoryFile_new(const char *mode)
{
return THMemoryFile_newWithStorage(NULL, mode);
}