File: asmfile.cpp

package info (click to toggle)
pasmo 0.5.3-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 892 kB
  • ctags: 1,805
  • sloc: cpp: 8,508; asm: 3,020; sh: 790; makefile: 627
file content (590 lines) | stat: -rw-r--r-- 11,119 bytes parent folder | download | duplicates (7)
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
// asmfile.cpp
// Revision 15-jan-2005

#include "asmfile.h"

#include <vector>
#include <stdexcept>

using std::runtime_error;

#include <assert.h>

#define ASSERT assert

namespace {


class FileNotFound : public runtime_error {
public:
	FileNotFound (const std::string & filename) :
		runtime_error ("File '" + filename + "' not found")
	{ }
};

class InvalidInclude : public runtime_error {
public:
	InvalidInclude (const Token & tok) :
		runtime_error ("Unexpected " + tok.str () +
			" after INCLUDE file name")
	{ }
};


class FileLine {
public:
	FileLine (const std::string & text_n, const Tokenizer & tkz_n,
		size_t linenum_n);
	bool empty () const;
	size_t numline () const;
	Tokenizer & gettkz ();
	const std::string & getstrline () const;
private:
	std::string text;
	Tokenizer tkz;
	size_t linenum;
};

FileLine::FileLine (const std::string & text_n, const Tokenizer & tkz_n,
		size_t linenum_n) :
	text (text_n),
	tkz (tkz_n),
	linenum (linenum_n)
{
}

bool FileLine::empty () const
{
	return tkz.empty ();
}

size_t FileLine::numline () const
{
	return linenum;
}

Tokenizer & FileLine::gettkz ()
{
	return tkz;
}

const std::string & FileLine::getstrline () const
{
	return text;
}


typedef std::vector <FileLine> filelines_t;


class FileRef {
public:
	FileRef (const std::string & name, size_t linebeg);
	void setend (size_t n);

	size_t linebegin () const;
	size_t lineend () const;
	std::string name () const;

	bool lineempty (size_t n) const;
	size_t numline (size_t n) const;
	Tokenizer & gettkz (size_t n);
	const std::string & getstrline (size_t n) const;

	void pushline (const std::string & text, const Tokenizer & tkz,
		size_t realnumline);
private:
	std::string filename;
	filelines_t line;
	size_t l_begin;
	size_t l_end;
};

FileRef::FileRef (const std::string & name, size_t linebeg) :
	filename (name),
	l_begin (linebeg)
{ }

void FileRef::setend (size_t n)
{
	l_end= n;
}

size_t FileRef::linebegin () const
{
	return l_begin;
}

size_t FileRef::lineend () const
{
	return l_end;
}

std::string FileRef::name () const
{
	return filename;
}

bool FileRef::lineempty (size_t n) const
{
	ASSERT (n < line.size () );

	return line [n].empty ();
}

size_t FileRef::numline (size_t n) const
{
	ASSERT (n < line.size () );

	return line [n].numline ();
}

Tokenizer & FileRef::gettkz (size_t n)
{
	ASSERT (n < line.size () );

	return line [n].gettkz ();
}

const std::string & FileRef::getstrline (size_t n) const
{
	ASSERT (n < line.size () );

	return line [n].getstrline ();
}

void FileRef::pushline (const std::string & text, const Tokenizer & tkz,
	size_t realnumline)
{
	//line.push_back (FileLine (text, tkz) );
	FileLine fl (text, tkz, realnumline);
	line.push_back (fl);
}


struct LineContent {
public:
	LineContent (size_t linenumn, size_t filenumn);
	size_t getfileline () const;
	size_t getfilenum () const;
private:
	size_t filenum;
	size_t linenum;
};

LineContent::LineContent (size_t filenumn, size_t linenumn) :
	filenum (filenumn),
	linenum (linenumn)
{ }

size_t LineContent::getfileline () const
{
	return linenum;
}

size_t LineContent::getfilenum () const
{
	return filenum;
}


} // namespace


class AsmFile::In {
public:
	In ();
	void addref ();
	void delref ();

	size_t numlines () const;
	size_t numfiles () const;

	bool lineempty (size_t n) const;
	Tokenizer & gettkz (size_t n);
	const std::string & getstrline (size_t n) const;

	void addincludedir (const std::string & dirname);
	void openis (std::ifstream & is, const std::string & filename,
		std::ios::openmode mode) const;
	void copyfile (FileRef & fr, std::ostream & outverb);
	void loadfile (const std::string & filename, bool nocase,
		std::ostream & outverb, std::ostream& outerr);
	void showlineinfo (std::ostream & os, size_t nline) const;
private:
	In (const In &); // Forbidden.
	In & operator = (const In &); // Forbidden.

	const FileRef & getfile (size_t n) const;
	FileRef & getfile (size_t n);

	const LineContent & getline (size_t n) const;
	LineContent & getline (size_t n);

	size_t numrefs;

	typedef std::vector <LineContent> vlinecont_t;
	vlinecont_t vlinecont;

	std::vector <FileRef> vfileref;

	void pushline (size_t linenum, size_t file);

	// ******** Paths for include ************

	std::vector <std::string> includepath;
};

AsmFile::In::In ()
{
	numrefs= 1;
}

void AsmFile::In::addref ()
{
	++numrefs;
}

void AsmFile::In::delref ()
{
	--numrefs;
	if (numrefs == 0)
		delete this;
}

size_t AsmFile::In::numlines () const
{
	return vlinecont.size ();
}

size_t AsmFile::In::numfiles () const
{
	return vfileref.size ();
}

const FileRef & AsmFile::In::getfile (size_t n) const
{
	ASSERT (n < numfiles () );
	return vfileref [n];
}

FileRef & AsmFile::In::getfile (size_t n)
{
	ASSERT (n < numfiles () );
	return vfileref [n];
}

const LineContent & AsmFile::In::getline (size_t n) const
{
	ASSERT (n < numlines () );
	return vlinecont [n];
}

LineContent & AsmFile::In::getline (size_t n)
{
	ASSERT (n < numlines () );
	return vlinecont [n];
}

bool AsmFile::In::lineempty (size_t n) const
{
	ASSERT (n < numlines () );

	//return vlinecont [n].empty ();
	const LineContent & lc= getline (n);
	return getfile (lc.getfilenum () ).lineempty (lc.getfileline () );
}

Tokenizer & AsmFile::In::gettkz (size_t n)
{
	ASSERT (n < numlines () );

	//return vlinecont [n].gettkz ();
	LineContent & lc= getline (n);
	return getfile (lc.getfilenum () ).gettkz (lc.getfileline () );
}

const std::string & AsmFile::In::getstrline (size_t n) const
{
	ASSERT (n < numlines () );

	//return vlinecont [n].getstrline ();
	const LineContent & lc= getline (n);
	return getfile (lc.getfilenum () ).getstrline (lc.getfileline () );
}

void AsmFile::In::addincludedir (const std::string & dirname)
{
	std::string aux (dirname);
	std::string::size_type l= aux.size ();
	if (l == 0)
		return;
	char c= aux [l - 1];
	if (c != '\\' && c != '/')
		aux+= '/';
	includepath.push_back (aux);
}

void AsmFile::In::openis (std::ifstream & is, const std::string & filename,
	std::ios::openmode mode) const
{
	ASSERT (! is.is_open () );
	is.open (filename.c_str (), mode);
	if (is.is_open () )
		return;
	for (size_t i= 0; i < includepath.size (); ++i)
	{
		std::string path (includepath [i] );
		path+= filename;
		is.clear ();
		is.open (path.c_str (), mode);
		if (is.is_open () )
			return;
	}
	throw FileNotFound (filename);
}

void AsmFile::In::pushline (size_t filenum, size_t linenum)
{
	ASSERT (filenum < vfileref.size () );

	vlinecont.push_back (LineContent (filenum, linenum) );
}

void AsmFile::In::copyfile (FileRef & fr, std::ostream & outverb)
{
	using std::endl;

	outverb << "Reloading file: " << fr.name () <<
		" in " << numlines () << endl;

	const size_t linebegin= fr.linebegin ();
	const size_t lineend= fr.lineend ();

	for (size_t i= linebegin; i < lineend; ++i)
	{
		LineContent l= getline (i);
		vlinecont.push_back (l);
	}

	outverb << "Finished reloading file: " << fr.name () <<
		" in " << numlines () << endl;
}

void AsmFile::In::loadfile (const std::string & filename, bool nocase,
	std::ostream & outverb, std::ostream& outerr)
{
	using std::endl;

	for (size_t i= 0; i < vfileref.size (); ++i)
	{
		if (vfileref [i].name () == filename)
		{
			copyfile (vfileref [i], outverb);
			return;
		}
	}

	// Load the file in memory.

	outverb << "Loading file: " << filename <<
		" in " << numlines () << endl;

	std::ifstream file;
	openis (file, filename, std::ios::in);

	vfileref.push_back (FileRef (filename, numlines () ) );
	const size_t filenum= vfileref.size () - 1;

	std::string line;
	size_t linenum;
	size_t realnum;

	try
	{	
		for (linenum= 0, realnum= 0;
			std::getline (file, line);
			++linenum, ++realnum)
		{
			Tokenizer tz (line, nocase);
			Token tok= tz.gettoken ();
			getfile (filenum).pushline (line, tz, realnum);
			pushline (filenum, linenum);
			if (tok.type () == TypeINCLUDE)
			{
				std::string includefile= tz.getincludefile ();
				tok= tz.gettoken ();
				if (tok.type () != TypeEndLine)
					throw InvalidInclude (tok);

				loadfile (includefile, nocase,
					outverb, outerr);

				Tokenizer tzaux (TypeEndOfInclude);
				getfile (filenum).pushline ("", tzaux, 0);
				++linenum;
				pushline (filenum, linenum);
			}
		}
		getfile (filenum).setend (numlines () );
	}
	catch (...)
	{
		outerr << "ERROR on line " << linenum + 1 <<
			" of file " << filename << endl;
		throw;
	}

	outverb << "Finished loading file: " << filename <<
		" in " << numlines () << endl;
}

void AsmFile::In::showlineinfo (std::ostream & os, size_t nline) const
{
	ASSERT (nline < numlines () );

	const LineContent & linf= getline (nline);
	const FileRef & fileref= getfile (linf.getfilenum () );

	os << " on line " << fileref.numline (linf.getfileline () ) + 1 <<
		" of file " << fileref.name ();
}


//*******************************************************************


AsmFile::AsmFile () :
	pin (new In)
{
}

AsmFile::AsmFile (const AsmFile & af) :
	pin (af.pin)
{
	pin->addref ();
}

AsmFile::~AsmFile ()
{
	pin->delref ();
}

// These functions are for propagate constness to the internal class.

inline AsmFile::In & AsmFile::in ()
{
	return * pin;
}

inline const AsmFile::In & AsmFile::in () const
{
	return * pin;
}

void AsmFile::addincludedir (const std::string & dirname)
{
	in ().addincludedir (dirname);
}

void AsmFile::openis (std::ifstream & is, const std::string & filename,
	std::ios::openmode mode) const
{
	in ().openis (is, filename, mode);
}

void AsmFile::loadfile (const std::string & filename, bool nocase,
	std::ostream & outverb, std::ostream& outerr)
{
	in ().loadfile (filename, nocase, outverb, outerr);
}

bool AsmFile::getvalidline ()
{
	for (;;)
	{
		if (currentline >= in ().numlines () )
			return false;
		//if (! in ().getlinecont (currentline).empty () )
		if (! in ().lineempty (currentline) )
			return true;
		++currentline;
	}
}

bool AsmFile::passeof () const
{
	return currentline >= in ().numlines ();
}

size_t AsmFile::getline () const
{
	return currentline;
}

Tokenizer & AsmFile::getcurrentline ()
{
	ASSERT (! passeof () );
	//Tokenizer & tz= in ().getlinecont (currentline).gettkz ();
	Tokenizer & tz= in ().gettkz (currentline);
	tz.reset ();
	return tz;
}

const std::string & AsmFile::getcurrenttext () const
{
	ASSERT (! passeof () );
	//return in ().getlinecont (currentline).getstrline ();
	return in ().getstrline (currentline);
}

void AsmFile::setline (size_t line)
{
	currentline= line;
}

void AsmFile::setendline ()
{
	currentline= in ().numlines ();
}

void AsmFile::beginline ()
{
	currentline= LINE_BEGIN;
}

bool AsmFile::nextline ()
{
	if (currentline == LINE_BEGIN)
		currentline= 0;
	else
	{
		if (passeof () )
			return false;
		++currentline;
	}
	if (! getvalidline () )
		return false;
	return true;
}

void AsmFile::prevline ()
{
	ASSERT (currentline > 0);
	--currentline;
}

void AsmFile::showlineinfo (std::ostream & os, size_t nline) const
{
	in ().showlineinfo (os, nline);
}

void AsmFile::showcurrentlineinfo (std::ostream & os) const
{
	if (passeof () )
		os << " detected after end of file";
	else
		in ().showlineinfo (os, getline () );
}

// End of asmfile.cpp