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
|
// --------------------------------------------------------------------------
//
// File
// Name: FdGetLine.cpp
// Purpose: Line based file descriptor reading
// Created: 2003/07/24
//
// --------------------------------------------------------------------------
#include "Box.h"
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "FdGetLine.h"
#include "CommonException.h"
#include "MemLeakFindOn.h"
// --------------------------------------------------------------------------
//
// Function
// Name: FdGetLine::FdGetLine(int)
// Purpose: Constructor, taking file descriptor
// Created: 2003/07/24
//
// --------------------------------------------------------------------------
FdGetLine::FdGetLine(int fd)
: mFileHandle(fd)
{
if(mFileHandle < 0) {THROW_EXCEPTION(CommonException, BadArguments)}
//printf("FdGetLine buffer size = %d\n", sizeof(mBuffer));
}
// --------------------------------------------------------------------------
//
// Function
// Name: FdGetLine::~FdGetLine()
// Purpose: Destructor
// Created: 2003/07/24
//
// --------------------------------------------------------------------------
FdGetLine::~FdGetLine()
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: FdGetLine::GetLine(bool)
// Purpose: Returns a file from the file. If Preprocess is true, leading
// and trailing whitespace is removed, and comments (after #)
// are deleted.
// Created: 2003/07/24
//
// --------------------------------------------------------------------------
std::string FdGetLine::GetLine(bool Preprocess)
{
if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)}
std::string r;
bool result = GetLineInternal(r, Preprocess);
if(!result)
{
// should never fail for FdGetLine
THROW_EXCEPTION(CommonException, Internal);
}
return r;
}
// --------------------------------------------------------------------------
//
// Function
// Name: FdGetLine::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 FdGetLine::ReadMore(int Timeout)
{
int bytes;
#ifdef WIN32
if (mFileHandle == _fileno(stdin))
{
bytes = console_read(mBuffer, sizeof(mBuffer));
}
else
{
bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer));
}
#else // !WIN32
bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer));
#endif // WIN32
if(bytes == 0)
{
mPendingEOF = true;
}
return bytes;
}
// --------------------------------------------------------------------------
//
// Function
// Name: FdGetLine::DetachFile()
// Purpose: Detaches the file handle, setting the file pointer correctly.
// Probably not good for sockets...
// Created: 2003/07/24
//
// --------------------------------------------------------------------------
void FdGetLine::DetachFile()
{
if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)}
// Adjust file pointer
int bytesOver = mBufferBegin - mBufferBegin;
ASSERT(bytesOver >= 0);
if(bytesOver > 0)
{
if(::lseek(mFileHandle, 0 - bytesOver, SEEK_CUR) == -1)
{
THROW_EXCEPTION(CommonException, OSFileError)
}
}
// Unset file pointer
mFileHandle = -1;
}
|