summaryrefslogtreecommitdiff
path: root/lib/common/IOStreamGetLine.cpp
blob: ef8930b8e0cbf85ccdff30f30cd596b936c36b5a (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
// --------------------------------------------------------------------------
//
// File
//		Name:    IOStreamGetLine.cpp
//		Purpose: Line based file descriptor reading
//		Created: 2003/07/24
//
// --------------------------------------------------------------------------

#include "Box.h"
#include "IOStreamGetLine.h"
#include "CommonException.h"

#include "MemLeakFindOn.h"

// --------------------------------------------------------------------------
//
// Function
//		Name:    IOStreamGetLine::IOStreamGetLine(int)
//		Purpose: Constructor, taking file descriptor
//		Created: 2003/07/24
//
// --------------------------------------------------------------------------
IOStreamGetLine::IOStreamGetLine(IOStream &Stream)
: mrStream(Stream)
{
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    IOStreamGetLine::~IOStreamGetLine()
//		Purpose: Destructor
//		Created: 2003/07/24
//
// --------------------------------------------------------------------------
IOStreamGetLine::~IOStreamGetLine()
{
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    IOStreamGetLine::GetLine(std::string &, bool, int)
//		Purpose: Gets a line from the file, returning it in rOutput. If Preprocess is true, leading
//				 and trailing whitespace is removed, and comments (after #)
//				 are deleted.
//				 Returns true if a line is available now, false if retrying may get a line (eg timeout, signal),
//				 and exceptions if it's EOF.
//		Created: 2003/07/24
//
// --------------------------------------------------------------------------
bool IOStreamGetLine::GetLine(std::string &rOutput, bool Preprocess, int Timeout)
{
	return GetLineInternal(rOutput, Preprocess, Timeout);
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    IOStreamGetLine::ReadMore()
//		Purpose: Read more bytes from the handle, possible the
//			 console, into mBuffer and return the number of
//			 bytes read, 0 on EOF or -1 on error.
//		Created: 2011/04/22
//
// --------------------------------------------------------------------------
int IOStreamGetLine::ReadMore(int Timeout)
{
	int bytes = mrStream.Read(mBuffer, sizeof(mBuffer), Timeout);
	
	if(!mrStream.StreamDataLeft())
	{
		mPendingEOF = true;
	}

	return bytes;	
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    IOStreamGetLine::DetachFile()
//		Purpose: Detaches the file handle, setting the file pointer correctly.
//				 Probably not good for sockets...
//		Created: 2003/07/24
//
// --------------------------------------------------------------------------
void IOStreamGetLine::DetachFile()
{
	// Adjust file pointer
	int bytesOver = mBytesInBuffer - mBufferBegin;
	ASSERT(bytesOver >= 0);
	if(bytesOver > 0)
	{
		mrStream.Seek(0 - bytesOver, IOStream::SeekType_Relative);
	}
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    IOStreamGetLine::IgnoreBufferedData(int)
//		Purpose: Ignore buffered bytes (effectively removing them from the
//				 beginning of the buffered data.)
//				 Cannot remove more bytes than are currently in the buffer.
//				 Be careful when this is used!
//		Created: 22/12/04
//
// --------------------------------------------------------------------------
void IOStreamGetLine::IgnoreBufferedData(int BytesToIgnore)
{
	int bytesInBuffer = mBytesInBuffer - mBufferBegin;
	if(BytesToIgnore < 0 || BytesToIgnore > bytesInBuffer)
	{
		THROW_EXCEPTION(CommonException, IOStreamGetLineNotEnoughDataToIgnore)
	}
	mBufferBegin += BytesToIgnore;
}