aboutsummaryrefslogblamecommitdiffstats
path: root/file.cpp
blob: bf68bd61800eee579e6b3e37dab2352b0e014c01 (plain) (tree)
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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903






































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                             
// file.cpp
// Revision 9-jan-2005

#include "blassic.h"
#include "file.h"
#include "trace.h"
#include "error.h"
#include "var.h"

//#include "cursor.h"
//#include "edit.h"
//#include "graphics.h"

#include "sysvar.h"
//#include "socket.h"
#include "util.h"
using util::to_string;

#include <fstream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <algorithm>

// para strerror (errno)
#include <string.h>
#include <errno.h>

#include <iostream>
using std::cerr;
using std::endl;

#include <cassert>
#define ASSERT assert

//***********************************************
//		Auxiliary functions
//***********************************************

namespace {

BlInteger lengthoffileread (std::fstream & fs)
{
	// WARNING: The C++ standard does not guarantee that
	// this method obtains the length of the file.
	std::streamsize old= fs.tellg ();
	fs.seekg (0, std::ios::end);
	std::streamsize l= fs.tellg ();
	fs.seekg (old, std::ios::beg);
	// Not leave the file unusable in case something were wrong.
	fs.clear ();
	return static_cast <BlInteger> (l);
}

BlInteger lengthoffilewrite (std::fstream & fs)
{
	// WARNING: The C++ standard does not guarantee that
	// this method obtains the length of the file.
	std::streamsize old= fs.tellp ();
	fs.seekp (0, std::ios::end);
	std::streamsize l= fs.tellp ();
	fs.seekp (old, std::ios::beg);
	// Not leave the file unusable in case something were wrong.
	fs.clear ();
	return static_cast <BlInteger> (l);
}

} // namespace

namespace blassic {

namespace file {

//***********************************************
//              BlFile
//***********************************************

BlFile::BlFile (OpenMode nmode) :
	mode (nmode),
	cDelimiter (','),
	cQuote ('"'),
	cEscape ('\0')
{
}

BlFile::~BlFile ()
{
}

void BlFile::closein ()
	{ throw ErrFileMode; }
void BlFile::closeout ()
	{ throw ErrFileMode; }

void BlFile::reset (int, int, int, int)
	{ throw ErrFileMode; }

bool BlFile::istextwindow () const
{
	return false;
}

bool BlFile::eof ()
{
	if (showdebuginfo () )
		cerr << "This file type does not implement EOF" << endl;
	throw ErrFileMode;
}

size_t BlFile::loc ()
	{ throw ErrFileMode; }
void BlFile::flush ()
	{ throw ErrFileMode; }
void BlFile::getline (std::string &, bool)
	{ throw ErrFileMode; }
void BlFile::outstring (const std::string &)
	{ throw ErrFileMode; }
void BlFile::outchar (char)
	{ throw ErrFileMode; }

#if 0

void BlFile::outnumber (BlNumber n)
{
	//outstring (to_string (n) );
	std::ostringstream oss;
	oss << std::setprecision (16) << n;
	outstring (oss.str () );
}

void BlFile::outinteger (BlInteger n)
{
	outstring (to_string (n) );
}

void BlFile::outlinenumber (BlLineNumber l)
{
	std::ostringstream oss;
	oss << std::setw (7) << l;
	outstring (oss.str () );
}

#endif

BlFile & operator << (BlFile & bf, const std::string & str)
{
	TRACEFUNC (tr, "operator << (BlFile &, const string &)");

	bf.outstring (str);
	return bf;
}

BlFile & operator << (BlFile & bf, char c)
{
	bf.outchar (c);
	return bf;
}

BlFile & operator << (BlFile & bf, BlNumber n)
{
	//bf.outnumber (n);
	std::ostringstream oss;
	oss << std::setprecision (16) << n;
	bf.outstring (oss.str () );
	return bf;
}

BlFile & operator << (BlFile & bf, BlInteger n)
{
	//bf.outinteger (n);
	bf.outstring (to_string (n) );
	return bf;
}

BlFile & operator << (BlFile & bf, BlLineNumber l)
{
	//bf.outlinenumber (l);
	std::ostringstream oss;
	oss << std::setw (7) << l;
	bf.outstring (oss.str () );
	return bf;
}

BlFile & operator << (BlFile & bf, unsigned short n)
{
	bf.outstring (to_string (n) );
	return bf;
}

void BlFile::putspaces (size_t n)
{
	outstring (std::string (n, ' ') );
}

void BlFile::tab ()
{
	outchar ('\t');
}

void BlFile::tab (size_t n)
{
	// Provisional
	outstring (std::string (n, ' ') );
}

void BlFile::endline ()
{
	outchar ('\n');
}

void BlFile::put (size_t)
{ throw ErrFileMode; }

void BlFile::get (size_t)
{ throw ErrFileMode; }

void BlFile::field_clear ()
{ throw ErrFileMode; }

void BlFile::field (const std::vector <field_element> &)
{ throw ErrFileMode; }

void BlFile::field_append (const std::vector <field_element> &)
{ throw ErrFileMode; }

// assign doesn't throw because we call it for all open files,
// those that are no random files or does nor have the var in
// their fields just ignore it.
bool BlFile::assign (const std::string &, const Dimension &,
	const std::string &, Align)
{ return false; }
bool BlFile::assign_mid (const std::string &, const Dimension &,
	const std::string &, size_t, std::string::size_type)
{ return false; }

size_t BlFile::getwidth () const
{ throw ErrFileMode; }

void BlFile::movecharforward ()
{ throw ErrFileMode; }

void BlFile::movecharforward (size_t)
{ throw ErrFileMode; }

void BlFile::movecharback ()
{ throw ErrFileMode; }

void BlFile::movecharback (size_t)
{ throw ErrFileMode; }

void BlFile::movecharup ()
{ throw ErrFileMode; }

void BlFile::movecharup (size_t)
{ throw ErrFileMode; }

void BlFile::movechardown ()
{ throw ErrFileMode; }

void BlFile::movechardown (size_t)
{ throw ErrFileMode; }

void BlFile::showcursor ()
{ throw ErrFileMode; }

void BlFile::hidecursor ()
{ throw ErrFileMode; }

std::string BlFile::getkey ()
{ throw ErrFileMode; }

std::string BlFile::inkey ()
{ throw ErrFileMode; }

std::string BlFile::read (size_t)
{ throw ErrFileMode; }

void BlFile::gotoxy (int, int)
{ throw ErrFileMode; }

void BlFile::setcolor (int)
{ throw ErrFileMode; }

int BlFile::getcolor ()
{ throw ErrFileMode; }

void BlFile::setbackground (int)
{ throw ErrFileMode; }

int BlFile::getbackground ()
{ throw ErrFileMode; }

void BlFile::cls ()
{ throw ErrFileMode; }

std::string BlFile::copychr (BlChar, BlChar)
{ throw ErrFileMode; }

int BlFile::pos ()
{ throw ErrFileMode; }

int BlFile::vpos ()
{ throw ErrFileMode; }

void BlFile::tag ()
{ throw ErrFileMode; }

void BlFile::tagoff ()
{ /* Ignored */ }

bool BlFile::istagactive ()
{ return false; }

void BlFile::inverse (bool)
{ throw ErrFileMode; }

bool BlFile::getinverse ()
{ throw ErrFileMode; }

void BlFile::bright (bool)
{ throw ErrFileMode; }

bool BlFile::getbright ()
{ throw ErrFileMode; }

void BlFile::setwidth (size_t )
{ }

void BlFile::setmargin (size_t )
{ }

BlInteger BlFile::lof ()
{ throw ErrFileMode; }

bool BlFile::poll ()
{
	return false;
}

void BlFile::scroll (int)
{ throw ErrFileMode; }


//***********************************************
//		BlFileOut
//***********************************************

BlFileOut::BlFileOut () : BlFile (Output)
{ }

BlFileOut::BlFileOut (OpenMode mode) : BlFile (mode)
{ }

void BlFileOut::flush ()
{
	ofs () << std::flush;
}

void BlFileOut::outstring (const std::string & str)
{
	ofs () << str;
}

void BlFileOut::outchar (char c)
{
	ofs () << c;
}

#if 0

void BlFileOut::outnumber (BlNumber n)
{
	ofs () << n;
}

void BlFileOut::outinteger (BlInteger n)
{
	ofs () << n;
}

void BlFileOut::outlinenumber (BlLineNumber l)
{
	ofs () << std::setw (7) << l;
}

#endif

//***********************************************
//		BlFileOutString
//***********************************************

BlFile * newBlFileOutString ()
{
	return new BlFileOutString;
}

BlFileOutString::BlFileOutString ()
{ }

std::string BlFileOutString::str ()
{
	return oss.str ();
}

std::ostream & BlFileOutString::ofs ()
{
	return oss;
}

//***********************************************
//		BlFileOutput
//***********************************************

class BlFileOutput : public BlFileOut {
public:
	BlFileOutput (std::ostream & os);
	bool isfile () const { return true; }
private:
	std::ostream & ofs ();
	std::ostream & os;
};

class BlFileRegular : public BlFileOut {
public:
	BlFileRegular (const std::string & name, OpenMode mode);
	bool isfile () const { return true; }
	void getline (std::string & str, bool endline= true);
	bool eof ();
	void flush ();
	virtual std::string getkey ();
	virtual std::string inkey ();
	std::string read (size_t n);
	BlInteger lof ();
private:
	std::ostream & ofs ();
	std::fstream fs;
};

BlFile * newBlFileOutput (std::ostream & os)
{
	return new BlFileOutput (os);
}

BlFileOutput::BlFileOutput (std::ostream & os) :
	os (os)
{
}

std::ostream & BlFileOutput::ofs ()
{
	return os;
}

//***********************************************
//              BlFileRegular
//***********************************************

BlFile * newBlFileRegular (const std::string & name, OpenMode mode)
{
	return new BlFileRegular (name, mode);
}

BlFileRegular::BlFileRegular (const std::string & name, OpenMode nmode) :
	BlFileOut (nmode)
{
	TRACEFUNC (tr, "BlFileRegular::BlFileRegular");

	using std::ios;

	ios::openmode mode= ios::in;
	switch (nmode & ~ Binary)
	{
	case Input:
		mode= ios::in; break;
	case Output:
		mode= ios::out; break;
	case Append:
		//mode= ios::out | ios::ate; break;
		mode= ios::out | ios::app; break;
	default:
		if (showdebuginfo () )
			cerr << "Unexpected mode value" << endl;
		TRMESSAGE (tr, std::string ("Invalid mode ") +
			util::to_string (nmode) );
		throw ErrBlassicInternal;
	}
	if (nmode & Binary)
		mode|= ios::binary;
	fs.open (name.c_str (), mode);
	if (! fs.is_open () )
	{
		if (showdebuginfo () )
			cerr << "open (" << name << ", " << mode <<
				") failed: " << strerror (errno) <<
				endl;
		throw ErrFileNotFound;
	}
}

bool BlFileRegular::eof ()
{
	int c= fs.get ();
	if (! fs || c == EOF)
		return true;
	fs.unget ();
	return false;
}

void BlFileRegular::flush ()
{
	fs << std::flush;
}

void BlFileRegular::getline (std::string & str, bool)
{
	std::getline (fs, str);
	if (! fs)
	{
		if (fs.eof () )
			throw ErrPastEof;
		else
			throw ErrFileRead;
	}
}

std::string BlFileRegular::getkey ()
{
	return read (1);
}

std::string BlFileRegular::inkey ()
{
	int c= fs.get ();
	if (! fs || c == EOF)
	{
		fs.clear ();	// Allow further reads.
		return std::string ();
	}
	return std::string (1, static_cast <char> (c) );
}

std::string BlFileRegular::read (size_t n)
{
	util::auto_buffer <char> buf (n);
	fs.read (buf, n);
	if (! fs)
	{
		if (fs.eof () )
			throw ErrPastEof;
		else
			throw ErrFileRead;
	}
	return std::string (buf, n);
}

std::ostream & BlFileRegular::ofs ()
{
	return fs;
}

BlInteger BlFileRegular::lof ()
{
	if (getmode () & Input)
		return lengthoffileread (fs);
	else
		return lengthoffilewrite (fs);
}

//***********************************************
//              BlFileRandom
//***********************************************

class BlFileRandom : public BlFile {
public:
	BlFileRandom (const std::string & name, size_t record_len);
	bool isfile () const { return true; }
	bool eof ();
	virtual size_t loc ();
	void put (size_t pos);
	void get (size_t pos);
	void field_clear ();
	void field (const std::vector <field_element> & elem);
	void field_append (const std::vector <field_element> & elem);
	virtual bool assign (const std::string & name, const Dimension & dim,
		const std::string & value, Align align);
	virtual bool assign_mid (const std::string & name,
		const Dimension & dim,
		const std::string & value,
		size_t inipos, std::string::size_type len);
	struct field_chunk {
		std::string name;
		Dimension dim;
		size_t pos;
		size_t size;
		inline void getvar (char * buf) const;
	};
	typedef std::vector <field_chunk> vchunk;
	BlInteger lof ();
private:
	std::fstream fs;
	size_t len;
	size_t len_assigned;
	bool used;
	size_t actual;
	//auto_array buf;
	util::auto_buffer <char> buf;
	vchunk chunk;
};

BlFile * newBlFileRandom (const std::string & name, size_t record_len)
{
	return new BlFileRandom (name, record_len);
}

inline void BlFileRandom::field_chunk::getvar (char * buf) const
{
	if (dim.empty () )
		assignvarstring (name, std::string (buf + pos, size) );
	else
		assigndimstring (name, dim, std::string (buf + pos, size) );
}

BlFileRandom::BlFileRandom (const std::string & name, size_t record_len) :
	BlFile (Random),
	len (record_len),
	len_assigned (0),
	used (false),
	actual (0),
	buf (record_len)
{
	using std::ios;

	const ios::openmode iobin (ios::in | ios::out | ios::binary);
	fs.open (name.c_str (), iobin);
	if (! fs.is_open () )
	{
		// This can be necessary or not depending of the compiler
		// to create the file if not exist. We test it, anyway.
		if (errno == ENOENT)
		{
			fs.clear ();
			fs.open (name.c_str (), ios::out | ios::binary);
			if (fs.is_open () )
			{
				fs.close ();
				fs.open (name.c_str (), iobin);
			}

		}
		if (! fs.is_open () )
		{
			if (showdebuginfo () )
				cerr << "open " << name << " failed: " <<
					strerror (errno) << endl;
			throw ErrFileNotFound;
		}
	}
	std::fill_n (buf.begin (), len, '\0');
}

bool BlFileRandom::eof ()
{
	fs.clear ();
	fs.seekg (actual * len);
	if (! fs)
	{
		if (fs.eof () )
			return true;
		throw ErrFileRead;
	}
	char testchar;
	fs.read (& testchar, 1);
	if (! fs)
	{
		if (fs.eof () )
			return true;
		throw ErrFileRead;
	}
	return fs.gcount () != 1;
}

size_t BlFileRandom::loc ()
{
	return used ? actual + 1 : size_t (0);
}


void BlFileRandom::put (size_t pos)
{
	used= true;
	if (pos != 0)
	{
		actual= pos - 1;
		fs.seekp (actual * len);
	}
	fs.write (buf, len);
	++actual;
}

namespace {

class GetVar {
public:
	GetVar (char * buf) : buf (buf) { }
	void operator () (const BlFileRandom::field_chunk & chunk)
	{
		chunk.getvar (buf);
	}
private:
	char * buf;
};

} // namespace

void BlFileRandom::get (size_t pos)
{
	using std::string;

	used= true;
	if (pos != 0)
		actual= pos - 1;
	fs.clear ();
	fs.seekg (actual * len);
	fs.read (buf, len);
	std::streamsize r= fs.gcount ();
	if (r < std::streamsize (len) )
		std::fill_n (buf.begin () + r, len - r, ' ');
	std::for_each (chunk.begin (), chunk.end (), GetVar (buf) );
	++actual;
}

namespace {

class MakeChunk {
public:
	MakeChunk (size_t len, size_t & pos) :
		len (len),
		pos (pos)
	{ }
	BlFileRandom::field_chunk operator ()
		(const BlFile::field_element & elem)
	{
		size_t size= elem.size;
		BlFileRandom::field_chunk fc;
		fc.name= elem.name;
		fc.dim= elem.dim;
		fc.pos= pos;
		fc.size= size;
		pos+= size;
		if (pos > len)
			throw ErrFieldOverflow;
		return fc;
	}
	size_t getpos () const { return pos; }
private:
	const size_t len;
	size_t & pos;
};

} // namespace

void BlFileRandom::field_clear ()
{
	chunk.clear ();
	len_assigned= 0;
}

void BlFileRandom::field_append (const std::vector <field_element> & elem)
{
	TRACEFUNC (tr, "BlFileRandom::field_append");

	// Create the field chunks.
	//MakeChunk maker (len, len_assigned);
	std::transform (elem.begin (), elem.end (),
		std::back_inserter (chunk),
		MakeChunk (len, len_assigned) );
	// Check the assigned mark.
	TRMESSAGE (tr, std::string ("len_assigned= ") +
		util::to_string (len_assigned) );

	// Assign initial values to the field variables (filled with
	// zeroes if the file is opened and not touched).
	// Do it for all fields, not only the added now.
	std::for_each (chunk.begin (), chunk.end (), GetVar (buf) );
}

void BlFileRandom::field (const std::vector <field_element> & elem)
{
	field_clear ();
	field_append (elem);
}

namespace {

class field_var_is {
public:
	field_var_is (const std::string & name, const Dimension & dim) :
		name (name), dim (dim)
	{ }
	bool operator () (const BlFileRandom::field_chunk & chunk)
	{
		return chunk.name == name && chunk.dim == dim;
	}
private:
	const std::string & name;
	const Dimension & dim;
};

} // namespace

bool BlFileRandom::assign (const std::string & name, const Dimension & dim,
		const std::string & value, Align align)
{
	vchunk::iterator pe= std::find_if (chunk.begin (), chunk.end (),
		field_var_is (name, dim) );
	if (pe == chunk.end () )
		return false;
	if (pe->size == 0)
		return true;
	char * init= buf + pe->pos;
	std::string str;

	// Changed the behaviour: now does the same as gwbasic.

	#if 0
	std::string::size_type l= value.size ();
	if (align == AlignLeft)
	{
		if (l < pe->size)
			str= value + std::string (pe->size - l, ' ');
		else
			str= value.substr (0, pe->size);
	}
	else
	{
		if (l < pe->size)
			str= std::string (pe->size - l, ' ') + value;
		else
			str= value.substr (l - pe->size);
	}
	#else
	if (align == AlignLeft)
		str= util::stringlset (value, pe->size);
	else
		str= util::stringrset (value, pe->size);
	#endif
	ASSERT (str.size () == pe->size);
	#if 0
	std::copy (str.begin (), str.end (), init);
	#else
	str.copy (init, pe->size);
	#endif
	// Now also assign to the standalone var, so that the
	// value assiged can be checked.
	if (dim.empty () )
		assignvarstring (name, str);
	else
		assigndimstring (name, dim, str);
	return true;
}

bool BlFileRandom::assign_mid (const std::string & name,
	const Dimension & dim,
	const std::string & value,
	size_t inipos, std::string::size_type len)
{
	vchunk::iterator pe= std::find_if (chunk.begin (), chunk.end (),
		field_var_is (name, dim) );
	if (pe == chunk.end () )
		return false;
	const size_t size= pe->size;
	if (size == 0)
		return true;
	if (inipos >= size)
		return true;
	char * const init= buf + pe->pos;
	char * const initmid= init + inipos;
	size_t l= size - inipos;
	if (len > l)
		len= l;
	std::string str= value.substr (0, len);
	if (str.size () < len)
		str+= std::string (len - str.size (), ' ');
	str.copy (initmid, str.size () );
	// Now also assign to the standalone var, so that the
	// value assiged can be checked.
	if (dim.empty () )
		assignvarstring (name, std::string (init, size) );
	else
		assigndimstring (name, dim, std::string (init, size) );
	return true;
}

BlInteger BlFileRandom::lof ()
{
	return lengthoffileread (fs);
}

} // namespace file

} // namespace blassic

// Fin de file.cpp
Un proyecto texto-plano.xyz