-
Notifications
You must be signed in to change notification settings - Fork 7
/
io.h
867 lines (669 loc) · 19.2 KB
/
io.h
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#ifndef __IO_H__
#define __IO_H__
typedef struct file_descriptor {
FILE *f;
char *name;
#ifdef __VMS
void *fab;
#endif
int error;
short mode;
short state;
} file_descriptor;
typedef struct text_descriptor {
file_descriptor desc;
char record;
} text_descriptor;
#ifdef __cplusplus
extern "C" {
#endif
extern boolean pio_ignore_error;
void pio_initialize(int argc, char const* argv[]);
void pio_rewrite_file(file_descriptor* fd,
size_t record_size,
const char* file_name, const char* options,
integer* error_code);
void pio_open_file(file_descriptor* fd,
size_t record_size,
const char* file_name,
const char* history,
integer* error_code);
void pio_reset_file(file_descriptor* fd,
size_t record_size,
const char* file_name, const char* options,
integer* error_code);
void pio_get_record(file_descriptor* fd, void* record, size_t record_size);
void pio_put_record(file_descriptor* fd, void* record, size_t record_size);
void pio_access_record(file_descriptor* fd, void* record, size_t record_size);
void pio_store_record(file_descriptor* fd, void* record, size_t record_size,
void* src);
void pio_read_record(file_descriptor* fd, void* record, size_t record_size,
void* dst);
void pio_write_record(file_descriptor* fd, void* record, size_t record_size,
void* src);
void pio_copy_record(file_descriptor* src_fd, void* src_record,
file_descriptor* dst_fd, void* dst_record,
size_t record_size);
boolean pio_check_end_of_file(file_descriptor *fd);
boolean pio_check_end_of_line(text_descriptor *td);
integer pio_iostatus(file_descriptor *fd);
boolean pio_ioerror(file_descriptor *fd);
void pio_file_ignore_error(file_descriptor* fd);
void pio_close_file(file_descriptor* fd);
void pio_seek_file(file_descriptor* fd, void* record, size_t record_size,
integer position);
void pio_rename_file(file_descriptor* fd, const char* new_name);
void pio_flush_file(file_descriptor* fd);
void pio_delete_file(file_descriptor* fd);
#ifdef __VMS
integer pio_get_file_version(file_descriptor* fd);
#endif
long pio_get_file_size(file_descriptor* fd, size_t record_size);
void pio_input_real(text_descriptor* td, double* val);
void pio_input_integer(text_descriptor* td, int* val);
void pio_input_unsigned(text_descriptor* td, unsigned* val);
void pio_input_long(text_descriptor* td, long* val);
void pio_input_ulong(text_descriptor* td, unsigned long* val);
void pio_input_char(text_descriptor* td, char* val);
int pio_input_string(text_descriptor* td, char* buf, size_t len, int padding);
void pio_input_newline(text_descriptor* td);
void pio_output_real(text_descriptor* td, double val, const int* width,
const int* prec);
void pio_output_integer(text_descriptor* td, int val,
const int* width);
void pio_output_unsigned(text_descriptor* td, unsigned val,
const int* width);
void pio_output_long(text_descriptor* td, long val,
const int* width);
void pio_output_ulong(text_descriptor* td, unsigned long val,
const int* width);
void pio_output_char(text_descriptor* td, char val, const int* width);
void pio_output_boolean(text_descriptor* td, boolean val,
const int* width);
void pio_output_string(text_descriptor* td, const char* buf, size_t len,
const int* width);
void pio_output_newline(text_descriptor* td);
void pio_output_end_of_page(text_descriptor* td);
typedef struct { int dummy; } new_line_marker;
extern new_line_marker NL;
#ifdef __cplusplus
}
//
// Some compilers report error when operator-> returns non-stuctural type
// (for exmaple "char*"). When SPLIT_FILE_CLASS option is defined, "file" class
// is divided into two parts, one of which is used as common base for
// "file of XXX" and "text", and second contains operator-> for access file
// record.
//
#ifdef SPLIT_FILE_CLASS
#define file file_template
#endif
template<class T>
class file {
public:
file_descriptor desc;
T record;
#ifdef TURBO_PASCAL
friend void assign(file& f, const char* file_name) {
f.desc.f = NULL;
f.desc.name = strdup(file_name);
}
friend void BlockRead(file& f, void* buf, word size, word& read) {
read = fread(buf, 1, size, f.desc.f);
}
friend void BlockWrite(file& f, void const* buf, word size, word& written){
written = fwrite(buf, 1, size, f.desc.f);
}
#endif
friend void open(file& f, const char* file_name, const char* history,
integer& error, int buffer = 0)
{
pio_open_file(&f.desc, sizeof(T), file_name, history, &error);
}
friend void open(file& f, const char* file_name, const char* history)
{
pio_open_file(&f.desc, sizeof(T), file_name, history, NULL);
}
friend void rewrite(file& f, const char* file_name,
const char* options,
integer& error_code)
{
pio_rewrite_file(&f.desc, sizeof(T), file_name, options, &error_code);
}
friend void rewrite(file& f, const char* file_name, const char* options)
{
pio_rewrite_file(&f.desc, sizeof(T), file_name, options, NULL);
}
friend void rewrite(file& f)
{
pio_rewrite_file(&f.desc, sizeof(T), NULL, NULL, NULL);
}
friend void rewrite(file& f, const char* file_name)
{
pio_rewrite_file(&f.desc, sizeof(T), file_name, NULL, NULL);
}
friend void reset(file& f, const char* file_name,
const char* options, integer& error_code)
{
pio_reset_file(&f.desc, sizeof(T), file_name, options, &error_code);
}
friend void reset(file& f, const char* file_name, const char* options)
{
pio_reset_file(&f.desc, sizeof(T), file_name, options, NULL);
}
friend void reset(file& f, const char* file_name)
{
pio_reset_file(&f.desc, sizeof(T), file_name, NULL, NULL);
}
friend void reset(file& f)
{
pio_reset_file(&f.desc, sizeof(T), NULL, NULL, NULL);
}
friend void get(file& f)
{
pio_get_record(&f.desc, (void*)&f.record, sizeof(T));
}
friend void put(file& f)
{
pio_put_record(&f.desc, (void*)&f.record, sizeof(T));
}
friend boolean eof(file& f)
{
return pio_check_end_of_file(&f.desc);
}
friend integer iostatus(file& f)
{
return pio_iostatus(&f.desc);
}
friend boolean ioerror(file& f)
{
return pio_ioerror(&f.desc);
}
friend void close(file& f)
{
pio_close_file(&f.desc);
}
friend void seek(file& f, integer position)
{
pio_seek_file(&f.desc, (void*)&f.record, sizeof(T), position);
}
friend void rename(file& f, const char* new_name)
{
pio_rename_file(&f.desc, new_name);
}
friend void flush(file& f)
{
pio_flush_file(&f.desc);
}
friend void Delete(file& f)
{
pio_delete_file(&f.desc);
}
friend long filesize(file& f)
{
return pio_get_file_size(&f.desc, sizeof(T));
}
friend void noioerror(file& f)
{
pio_file_ignore_error(&f.desc);
}
T& operator*()
{
pio_access_record(&desc, (void*)&record, sizeof(T));
return record;
}
#ifndef SPLIT_FILE_CLASS
T* operator->()
{
pio_access_record(&desc, (void*)&record, sizeof(T));
return &record;
}
#endif
friend void store(file& f, T const& record)
{
pio_store_record(&f.desc, (void*)&f.record, sizeof(T), (void*)&record);
}
file& operator >> (T& x)
{
pio_read_record(&desc, (void*)&record, sizeof(T), (void*)&x);
return *this;
}
file& operator >> (file const& f)
{
pio_copy_record(&desc, (void*)&record,
(file_descriptor*)&f.desc, (void*)&f.record,
sizeof(T));
return *this;
}
file& operator << (T const& x)
{
pio_write_record(&desc, (void*)&record, sizeof(T), (void*)&x);
return *this;
}
friend boolean file_is_opened(file const& f)
{
return f.desc.f != NULL;
}
file() { desc.f = NULL; desc.name = NULL; }
~file() { pio_close_file(&desc); }
};
class text;
extern "C" text input, output;
class text : public file<char> {
public:
friend boolean eof()
{
return pio_check_end_of_file(&input.desc);
}
friend boolean eoln(text& t)
{
return pio_check_end_of_line((text_descriptor*)&t);
}
friend boolean eoln()
{
return pio_check_end_of_line((text_descriptor*)&input);
}
text& operator >> (float& x)
{
double tmp;
pio_input_real((text_descriptor*)this, &tmp);
x = (float)tmp;
return *this;
}
text& operator >> (double& x)
{
pio_input_real((text_descriptor*)this, &x);
return *this;
}
text& operator >> (int& x)
{
pio_input_integer((text_descriptor*)this, &x);
return *this;
}
text& operator >> (unsigned& x)
{
pio_input_unsigned((text_descriptor*)this, &x);
return *this;
}
text& operator >> (long& x)
{
pio_input_long((text_descriptor*)this, &x);
return *this;
}
text& operator >> (unsigned long& x)
{
pio_input_ulong((text_descriptor*)this, &x);
return *this;
}
text& operator >> (short& x)
{
int tmp;
pio_input_integer((text_descriptor*)this, &tmp);
x = (short)tmp;
return *this;
}
text& operator >> (unsigned short& x)
{
int tmp;
pio_input_integer((text_descriptor*)this, &tmp);
x = (unsigned short)tmp;
return *this;
}
text& operator >> (unsigned char& x)
{
int tmp;
pio_input_integer((text_descriptor*)this, &tmp);
x = (unsigned char)tmp;
return *this;
}
text& operator >> (signed char& x)
{
int tmp;
pio_input_integer((text_descriptor*)this, &tmp);
x = (signed char)tmp;
return *this;
}
text& operator >> (char& x)
{
pio_input_char((text_descriptor*)this, &x);
return *this;
}
text& operator >> (new_line_marker&)
{
pio_input_newline((text_descriptor*)this);
return *this;
}
text& operator << (double x)
{
pio_output_real((text_descriptor*)this, x, NULL, NULL);
return *this;
}
text& operator << (long x)
{
pio_output_long((text_descriptor*)this, x, NULL);
return *this;
}
text& operator << (unsigned long x)
{
pio_output_ulong((text_descriptor*)this, x, NULL);
return *this;
}
text& operator << (int x)
{
pio_output_integer((text_descriptor*)this, x, NULL);
return *this;
}
text& operator << (unsigned x)
{
pio_output_unsigned((text_descriptor*)this, x, NULL);
return *this;
}
friend char* btos(boolean val) // convert boolean to ascii string
{
return (char*)(val ? " TRUE" : "FALSE");
}
text& operator << (char x)
{
pio_output_char((text_descriptor*)this, x, NULL);
return *this;
}
text& operator << (unsigned char x)
{
pio_output_char((text_descriptor*)this, x, NULL);
return *this;
}
text& operator << (const char* x)
{
pio_output_string((text_descriptor*)this, x, strlen(x), NULL);
return *this;
}
text& operator << (new_line_marker&)
{
pio_output_newline((text_descriptor*)this);
return *this;
}
friend void page(text& t)
{
pio_output_end_of_page((text_descriptor*)&t);
}
friend void page()
{
pio_output_end_of_page((text_descriptor*)&output);
}
};
#ifdef SPLIT_FILE_CLASS
#undef file
template<class T>
class file : public file_template<T> {
public:
T* operator->()
{
pio_access_record(&desc, (void*)&record, sizeof(T));
return &record;
}
};
#endif
#ifdef TURBO_PASCAL
typedef text untyped_file;
#endif
template<int size>
class format_string {
private:
char* buf;
int width;
public:
friend text& operator << (text& t, format_string const& fs) {
pio_output_string((text_descriptor*)&t, fs.buf, size, &fs.width);
return t;
}
format_string(const char* str_buf, int str_width) {
buf = (char*)str_buf;
width = str_width;
}
};
#ifdef TURBO_PASCAL
class format_varying_string {
private:
char buf[256];
int size;
int width;
public:
friend text& operator << (text& t, format_varying_string const& fs) {
pio_output_string((text_descriptor*)&t, fs.buf, fs.size, &fs.width);
return t;
}
format_varying_string(const char* str_buf, int str_size, int str_width) {
memcpy(buf, str_buf, str_size);
size = str_size;
width = str_width;
}
};
#endif
class format_real {
private:
real val;
int width;
public:
friend text& operator << (text &t, format_real const& fr) {
pio_output_real((text_descriptor*)&t, fr.val, &fr.width, NULL);
return t;
}
format_real(real real_val, int real_width) {
val = real_val;
width = real_width;
}
};
inline format_real format(real val, int width) {
return format_real(val, width);
}
class format_real_wp {
private:
real val;
int width;
int prec;
public:
friend text& operator << (text &t, format_real_wp const& fr) {
pio_output_real((text_descriptor*)&t, fr.val, &fr.width, &fr.prec);
return t;
}
format_real_wp(real real_val, int real_width, int real_prec) {
val = real_val;
prec = real_prec;
width = real_width;
}
};
inline format_real_wp format(real val, int width, int prec) {
return format_real_wp(val, width, prec);
}
class format_integer {
private:
int val;
int width;
public:
friend text& operator << (text &t, format_integer const& fi) {
pio_output_integer((text_descriptor*)&t, fi.val, &fi.width);
return t;
}
format_integer(int int_val, int int_width) {
val = int_val;
width = int_width;
}
};
inline format_integer format(int val, int width) {
return format_integer(val, width);
}
class format_unsigned {
private:
unsigned val;
int width;
public:
friend text& operator << (text &t, format_unsigned const& fi) {
pio_output_unsigned((text_descriptor*)&t, fi.val, &fi.width);
return t;
}
format_unsigned(unsigned unsigned_val, int unsigned_width) {
val = unsigned_val;
width = unsigned_width;
}
};
inline format_unsigned format(unsigned val, int width) {
return format_unsigned(val, width);
}
class format_long {
private:
long val;
int width;
public:
friend text& operator << (text &t, format_long const& fi) {
pio_output_long((text_descriptor*)&t, fi.val, &fi.width);
return t;
}
format_long(long long_val, int long_width) {
val = long_val;
width = long_width;
}
};
inline format_long format(long val, int width) {
return format_long(val, width);
}
class format_ulong {
private:
unsigned long val;
int width;
public:
friend text& operator << (text &t, format_ulong const& fi) {
pio_output_ulong((text_descriptor*)&t, fi.val, &fi.width);
return t;
}
format_ulong(unsigned long ulong_val, int ulong_width) {
val = ulong_val;
width = ulong_width;
}
};
inline format_ulong format(unsigned long val, int width) {
return format_ulong(val, width);
}
class format_lpsz {
private:
const char* val;
int width;
public:
friend text& operator << (text &t, format_lpsz const& fs) {
pio_output_string((text_descriptor*)&t, fs.val, strlen(fs.val),
&fs.width);
return t;
}
format_lpsz(const char* lpsz_val, int lpsz_width) {
val = lpsz_val;
width = lpsz_width;
}
};
inline format_lpsz format(const char* val, int width) {
return format_lpsz(val, width);
}
class format_char {
private:
char val;
int width;
public:
friend text& operator << (text &t, format_char const& fc) {
pio_output_char((text_descriptor*)&t, fc.val, &fc.width);
return t;
}
format_char(char char_val, int char_width) {
val = char_val;
width = char_width;
}
};
inline format_char format(char val, int width) {
return format_char(val, width);
}
#else
#define file(type) struct { \
file_descriptor desc; \
type record; \
}
typedef text_descriptor text;
extern text input, output;
#define VOID_FILE {{0}}
#define rewrite(file_variable, file_name, options, error_code) \
pio_rewrite_file(&(file_variable).desc, \
sizeof((file_variable).record), \
file_name, options, error_code)
#define reset(file_variable, file_name, options, error_code) \
pio_reset_file(&(file_variable).desc, \
sizeof((file_variable).record), \
file_name, options, error_code)
#define get(file_variable) \
pio_get_record(&(file_variable).desc, (void*)&(file_variable).record, \
sizeof((file_variable).record))
#define put(file_variable) \
pio_put_record(&(file_variable).desc, (void*)&(file_variable).record, \
sizeof((file_variable).record))
#define sread(file_variable, value) \
pio_read_record(&(file_variable).desc, (void*)&(file_variable).record,\
sizeof((file_variable).record), (void*)&(value))
#define scopy(src_file, dst_file) \
pio_copy_record(&(src_file).desc, (void*)&(src_file).record,\
&(dst_file).desc, (void*)&(dst_file).record,\
sizeof((src_file).record))
#define swrite(file_variable, value) \
pio_write_record(&(file_variable).desc,(void*)&(file_variable).record,\
sizeof((file_variable).record), (void*)&(value))
#define currec(file_variable) \
(pio_access_record(&(file_variable).desc, \
(void*)&(file_variable).record, \
sizeof((file_variable).record)), \
&(file_variable).record)
#define store(file_variable, value) \
pio_store_record(&(file_variable).desc, \
(void*)&(file_variable).record, \
sizeof((file_variable).record), (void*)&(value))
#define eof(file_variable) \
pio_check_end_of_file(&(file_variable).desc)
#define iostatus(file_variable) \
pio_iostatus(&(file_variable).desc)
#define ioerror(file_variable) \
pio_ioerror(&(file_variable).desc)
#define close(file_variable) \
pio_close_file(&(file_variable).desc)
#define seek(file_variable, position) \
pio_seek_file(&(file_variable).desc, (void*)&(file_variable).record, \
sizeof((file_variable).record), position)
#define rename(file_variable, new_name) \
pio_rename_file(&(file_variable).desc, new_name)
#define flush(file_variable) \
pio_flush_file(&(file_variable).desc)
#define Delete(file_variable) \
pio_delete_file(&(file_variable).desc)
#define noioerror(file_variable) \
pio_file_ignore_error(&(file_variable).desc)
#ifdef __VMS
#define get_file_version(file_variable) \
pio_get_file_version(&(file_variable).desc)
#endif
#define file_is_opened(file_variable) ((file_variable).desc.f != NULL)
#define page(text_variable) \
pio_output_end_of_page(&text_variable)
#define eoln(text_variable) \
pio_check_end_of_line(&text_variable)
void tread(text* text_file, char* format_string, ...);
void twrite(text* text_file, char* format_string, ...);
void cread(char* format_string, ...);
void cwrite(char* format_string, ...);
#endif
#ifdef TURBO_PASCAL
extern "C" integer ioresult;
#define ioResult ioresult
#endif
/*
* Codes returned by Sun Pascal open()
*/
enum open_file_errors {
file_successfully_opened = 0,
file_not_specified = 1,
unable_to_open_file = 2,
invalid_history = 3,
file_already_exists = 4
};
#endif