| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: IOStreamGetLine.cpp |
|---|
| 5 | // Purpose: Line based file descriptor reading |
|---|
| 6 | // Created: 2003/07/24 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | #include "IOStreamGetLine.h" |
|---|
| 12 | #include "CommonException.h" |
|---|
| 13 | |
|---|
| 14 | #include "MemLeakFindOn.h" |
|---|
| 15 | |
|---|
| 16 | // -------------------------------------------------------------------------- |
|---|
| 17 | // |
|---|
| 18 | // Function |
|---|
| 19 | // Name: IOStreamGetLine::IOStreamGetLine(int) |
|---|
| 20 | // Purpose: Constructor, taking file descriptor |
|---|
| 21 | // Created: 2003/07/24 |
|---|
| 22 | // |
|---|
| 23 | // -------------------------------------------------------------------------- |
|---|
| 24 | IOStreamGetLine::IOStreamGetLine(IOStream &Stream) |
|---|
| 25 | : mrStream(Stream) |
|---|
| 26 | { |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | // -------------------------------------------------------------------------- |
|---|
| 31 | // |
|---|
| 32 | // Function |
|---|
| 33 | // Name: IOStreamGetLine::~IOStreamGetLine() |
|---|
| 34 | // Purpose: Destructor |
|---|
| 35 | // Created: 2003/07/24 |
|---|
| 36 | // |
|---|
| 37 | // -------------------------------------------------------------------------- |
|---|
| 38 | IOStreamGetLine::~IOStreamGetLine() |
|---|
| 39 | { |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | // -------------------------------------------------------------------------- |
|---|
| 44 | // |
|---|
| 45 | // Function |
|---|
| 46 | // Name: IOStreamGetLine::GetLine(std::string &, bool, int) |
|---|
| 47 | // Purpose: Gets a line from the file, returning it in rOutput. If Preprocess is true, leading |
|---|
| 48 | // and trailing whitespace is removed, and comments (after #) |
|---|
| 49 | // are deleted. |
|---|
| 50 | // Returns true if a line is available now, false if retrying may get a line (eg timeout, signal), |
|---|
| 51 | // and exceptions if it's EOF. |
|---|
| 52 | // Created: 2003/07/24 |
|---|
| 53 | // |
|---|
| 54 | // -------------------------------------------------------------------------- |
|---|
| 55 | bool IOStreamGetLine::GetLine(std::string &rOutput, bool Preprocess, int Timeout) |
|---|
| 56 | { |
|---|
| 57 | return GetLineInternal(rOutput, Preprocess, Timeout); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | // -------------------------------------------------------------------------- |
|---|
| 62 | // |
|---|
| 63 | // Function |
|---|
| 64 | // Name: IOStreamGetLine::ReadMore() |
|---|
| 65 | // Purpose: Read more bytes from the handle, possible the |
|---|
| 66 | // console, into mBuffer and return the number of |
|---|
| 67 | // bytes read, 0 on EOF or -1 on error. |
|---|
| 68 | // Created: 2011/04/22 |
|---|
| 69 | // |
|---|
| 70 | // -------------------------------------------------------------------------- |
|---|
| 71 | int IOStreamGetLine::ReadMore(int Timeout) |
|---|
| 72 | { |
|---|
| 73 | int bytes = mrStream.Read(mBuffer, sizeof(mBuffer), Timeout); |
|---|
| 74 | |
|---|
| 75 | if(!mrStream.StreamDataLeft()) |
|---|
| 76 | { |
|---|
| 77 | mPendingEOF = true; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | return bytes; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | // -------------------------------------------------------------------------- |
|---|
| 85 | // |
|---|
| 86 | // Function |
|---|
| 87 | // Name: IOStreamGetLine::DetachFile() |
|---|
| 88 | // Purpose: Detaches the file handle, setting the file pointer correctly. |
|---|
| 89 | // Probably not good for sockets... |
|---|
| 90 | // Created: 2003/07/24 |
|---|
| 91 | // |
|---|
| 92 | // -------------------------------------------------------------------------- |
|---|
| 93 | void IOStreamGetLine::DetachFile() |
|---|
| 94 | { |
|---|
| 95 | // Adjust file pointer |
|---|
| 96 | int bytesOver = mBytesInBuffer - mBufferBegin; |
|---|
| 97 | ASSERT(bytesOver >= 0); |
|---|
| 98 | if(bytesOver > 0) |
|---|
| 99 | { |
|---|
| 100 | mrStream.Seek(0 - bytesOver, IOStream::SeekType_Relative); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | // -------------------------------------------------------------------------- |
|---|
| 106 | // |
|---|
| 107 | // Function |
|---|
| 108 | // Name: IOStreamGetLine::IgnoreBufferedData(int) |
|---|
| 109 | // Purpose: Ignore buffered bytes (effectively removing them from the |
|---|
| 110 | // beginning of the buffered data.) |
|---|
| 111 | // Cannot remove more bytes than are currently in the buffer. |
|---|
| 112 | // Be careful when this is used! |
|---|
| 113 | // Created: 22/12/04 |
|---|
| 114 | // |
|---|
| 115 | // -------------------------------------------------------------------------- |
|---|
| 116 | void IOStreamGetLine::IgnoreBufferedData(int BytesToIgnore) |
|---|
| 117 | { |
|---|
| 118 | int bytesInBuffer = mBytesInBuffer - mBufferBegin; |
|---|
| 119 | if(BytesToIgnore < 0 || BytesToIgnore > bytesInBuffer) |
|---|
| 120 | { |
|---|
| 121 | THROW_EXCEPTION(CommonException, IOStreamGetLineNotEnoughDataToIgnore) |
|---|
| 122 | } |
|---|
| 123 | mBufferBegin += BytesToIgnore; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|