| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: FdGetLine.cpp |
|---|
| 5 | // Purpose: Line based file descriptor reading |
|---|
| 6 | // Created: 2003/07/24 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | |
|---|
| 12 | #include <sys/types.h> |
|---|
| 13 | |
|---|
| 14 | #ifdef HAVE_UNISTD_H |
|---|
| 15 | #include <unistd.h> |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | #include "FdGetLine.h" |
|---|
| 19 | #include "CommonException.h" |
|---|
| 20 | |
|---|
| 21 | #include "MemLeakFindOn.h" |
|---|
| 22 | |
|---|
| 23 | // -------------------------------------------------------------------------- |
|---|
| 24 | // |
|---|
| 25 | // Function |
|---|
| 26 | // Name: FdGetLine::FdGetLine(int) |
|---|
| 27 | // Purpose: Constructor, taking file descriptor |
|---|
| 28 | // Created: 2003/07/24 |
|---|
| 29 | // |
|---|
| 30 | // -------------------------------------------------------------------------- |
|---|
| 31 | FdGetLine::FdGetLine(int fd) |
|---|
| 32 | : mFileHandle(fd) |
|---|
| 33 | { |
|---|
| 34 | if(mFileHandle < 0) {THROW_EXCEPTION(CommonException, BadArguments)} |
|---|
| 35 | //printf("FdGetLine buffer size = %d\n", sizeof(mBuffer)); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | // -------------------------------------------------------------------------- |
|---|
| 40 | // |
|---|
| 41 | // Function |
|---|
| 42 | // Name: FdGetLine::~FdGetLine() |
|---|
| 43 | // Purpose: Destructor |
|---|
| 44 | // Created: 2003/07/24 |
|---|
| 45 | // |
|---|
| 46 | // -------------------------------------------------------------------------- |
|---|
| 47 | FdGetLine::~FdGetLine() |
|---|
| 48 | { |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | // -------------------------------------------------------------------------- |
|---|
| 53 | // |
|---|
| 54 | // Function |
|---|
| 55 | // Name: FdGetLine::GetLine(bool) |
|---|
| 56 | // Purpose: Returns a file from the file. If Preprocess is true, leading |
|---|
| 57 | // and trailing whitespace is removed, and comments (after #) |
|---|
| 58 | // are deleted. |
|---|
| 59 | // Created: 2003/07/24 |
|---|
| 60 | // |
|---|
| 61 | // -------------------------------------------------------------------------- |
|---|
| 62 | std::string FdGetLine::GetLine(bool Preprocess) |
|---|
| 63 | { |
|---|
| 64 | if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)} |
|---|
| 65 | |
|---|
| 66 | std::string r; |
|---|
| 67 | bool result = GetLineInternal(r, Preprocess); |
|---|
| 68 | |
|---|
| 69 | if(!result) |
|---|
| 70 | { |
|---|
| 71 | // should never fail for FdGetLine |
|---|
| 72 | THROW_EXCEPTION(CommonException, Internal); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | return r; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | // -------------------------------------------------------------------------- |
|---|
| 80 | // |
|---|
| 81 | // Function |
|---|
| 82 | // Name: FdGetLine::ReadMore() |
|---|
| 83 | // Purpose: Read more bytes from the handle, possible the |
|---|
| 84 | // console, into mBuffer and return the number of |
|---|
| 85 | // bytes read, 0 on EOF or -1 on error. |
|---|
| 86 | // Created: 2011/04/22 |
|---|
| 87 | // |
|---|
| 88 | // -------------------------------------------------------------------------- |
|---|
| 89 | int FdGetLine::ReadMore(int Timeout) |
|---|
| 90 | { |
|---|
| 91 | int bytes; |
|---|
| 92 | |
|---|
| 93 | #ifdef WIN32 |
|---|
| 94 | if (mFileHandle == _fileno(stdin)) |
|---|
| 95 | { |
|---|
| 96 | bytes = console_read(mBuffer, sizeof(mBuffer)); |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer)); |
|---|
| 101 | } |
|---|
| 102 | #else // !WIN32 |
|---|
| 103 | bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer)); |
|---|
| 104 | #endif // WIN32 |
|---|
| 105 | |
|---|
| 106 | if(bytes == 0) |
|---|
| 107 | { |
|---|
| 108 | mPendingEOF = true; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | return bytes; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | // -------------------------------------------------------------------------- |
|---|
| 116 | // |
|---|
| 117 | // Function |
|---|
| 118 | // Name: FdGetLine::DetachFile() |
|---|
| 119 | // Purpose: Detaches the file handle, setting the file pointer correctly. |
|---|
| 120 | // Probably not good for sockets... |
|---|
| 121 | // Created: 2003/07/24 |
|---|
| 122 | // |
|---|
| 123 | // -------------------------------------------------------------------------- |
|---|
| 124 | void FdGetLine::DetachFile() |
|---|
| 125 | { |
|---|
| 126 | if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)} |
|---|
| 127 | |
|---|
| 128 | // Adjust file pointer |
|---|
| 129 | int bytesOver = mBufferBegin - mBufferBegin; |
|---|
| 130 | ASSERT(bytesOver >= 0); |
|---|
| 131 | if(bytesOver > 0) |
|---|
| 132 | { |
|---|
| 133 | if(::lseek(mFileHandle, 0 - bytesOver, SEEK_CUR) == -1) |
|---|
| 134 | { |
|---|
| 135 | THROW_EXCEPTION(CommonException, OSFileError) |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | // Unset file pointer |
|---|
| 140 | mFileHandle = -1; |
|---|
| 141 | } |
|---|
| 142 | |
|---|