Changeset 2935


Ignore:
Timestamp:
22/04/2011 14:41:07 (13 months ago)
Author:
chris
Message:

Refactor GetLines? to share common code, fix whitespace removal at end of line.

Location:
box/trunk/lib/common
Files:
4 edited
2 copied

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/common/FdGetLine.cpp

    r2883 r2935  
    2121#include "MemLeakFindOn.h" 
    2222 
    23 // utility whitespace function 
    24 inline bool iw(int c) 
    25 { 
    26         return (c == ' ' || c == '\t' || c == '\v' || c == '\f'); // \r, \n are already excluded 
    27 } 
    28  
    29  
    3023// -------------------------------------------------------------------------- 
    3124// 
     
    3730// -------------------------------------------------------------------------- 
    3831FdGetLine::FdGetLine(int fd) 
    39         : mFileHandle(fd), 
    40           mLineNumber(0), 
    41           mBufferBegin(0), 
    42           mBytesInBuffer(0), 
    43           mPendingEOF(false), 
    44           mEOF(false) 
     32: mFileHandle(fd) 
    4533{ 
    4634        if(mFileHandle < 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
     
    7563{ 
    7664        if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)} 
    77  
    78         // EOF? 
    79         if(mEOF) {THROW_EXCEPTION(CommonException, GetLineEOF)} 
    8065         
    8166        std::string r; 
     67        bool result = GetLineInternal(r, Preprocess); 
    8268 
    83         bool foundLineEnd = false; 
     69        if(!result) 
     70        { 
     71                // should never fail for FdGetLine 
     72                THROW_EXCEPTION(CommonException, Internal); 
     73        } 
     74         
     75        return r; 
     76} 
    8477 
    85         while(!foundLineEnd && !mEOF) 
     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// -------------------------------------------------------------------------- 
     89int FdGetLine::ReadMore(int Timeout) 
     90{ 
     91        int bytes; 
     92         
     93#ifdef WIN32 
     94        if (mFileHandle == _fileno(stdin)) 
    8695        { 
    87                 // Use any bytes left in the buffer 
    88                 while(mBufferBegin < mBytesInBuffer) 
    89                 { 
    90                         int c = mBuffer[mBufferBegin++]; 
    91                         if(c == '\r') 
    92                         { 
    93                                 // Ignore nasty Windows line ending extra chars 
    94                         } 
    95                         else if(c == '\n') 
    96                         { 
    97                                 // Line end! 
    98                                 foundLineEnd = true; 
    99                                 break; 
    100                         } 
    101                         else 
    102                         { 
    103                                 // Add to string 
    104                                 r += c; 
    105                         } 
    106                          
    107                         // Implicit line ending at EOF 
    108                         if(mBufferBegin >= mBytesInBuffer && mPendingEOF) 
    109                         { 
    110                                 foundLineEnd = true; 
    111                         } 
    112                 } 
    113                  
    114                 // Check size 
    115                 if(r.size() > FDGETLINE_MAX_LINE_SIZE) 
    116                 { 
    117                         THROW_EXCEPTION(CommonException, GetLineTooLarge) 
    118                 } 
    119                  
    120                 // Read more in? 
    121                 if(!foundLineEnd && mBufferBegin >= mBytesInBuffer && !mPendingEOF) 
    122                 { 
    123 #ifdef WIN32 
    124                         int bytes; 
    125  
    126                         if (mFileHandle == _fileno(stdin)) 
    127                         { 
    128                                 bytes = console_read(mBuffer, sizeof(mBuffer)); 
    129                         } 
    130                         else 
    131                         { 
    132                                 bytes = ::read(mFileHandle, mBuffer,  
    133                                         sizeof(mBuffer)); 
    134                         } 
    135 #else // !WIN32 
    136                         int bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer)); 
    137 #endif // WIN32 
    138                          
    139                         // Error? 
    140                         if(bytes == -1) 
    141                         { 
    142                                 THROW_EXCEPTION(CommonException, OSFileError) 
    143                         } 
    144                          
    145                         // Adjust buffer info 
    146                         mBytesInBuffer = bytes; 
    147                         mBufferBegin = 0; 
    148                          
    149                         // EOF / closed? 
    150                         if(bytes == 0) 
    151                         { 
    152                                 mPendingEOF = true; 
    153                         } 
    154                 } 
    155                  
    156                 // EOF? 
    157                 if(mPendingEOF && mBufferBegin >= mBytesInBuffer) 
    158                 { 
    159                         // File is EOF, and now we've depleted the buffer completely, so tell caller as well. 
    160                         mEOF = true; 
    161                 } 
    162         } 
    163  
    164         if(!Preprocess) 
    165         { 
    166                 return r; 
     96                bytes = console_read(mBuffer, sizeof(mBuffer)); 
    16797        } 
    16898        else 
    16999        { 
    170                 // Check for comment char, but char before must be whitespace 
    171                 int end = 0; 
    172                 int size = r.size(); 
    173                 while(end < size) 
    174                 { 
    175                         if(r[end] == '#' && (end == 0 || (iw(r[end-1])))) 
    176                         { 
    177                                 break; 
    178                         } 
    179                         end++; 
    180                 } 
    181                  
    182                 // Remove whitespace 
    183                 int begin = 0; 
    184                 while(begin < size && iw(r[begin])) 
    185                 { 
    186                         begin++; 
    187                 } 
     100                bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer)); 
     101        } 
     102#else // !WIN32 
     103        bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer)); 
     104#endif // WIN32 
    188105 
    189                 if(end < size && !iw(r[end])) 
    190                 { 
    191                         end--; 
    192                 } 
    193  
    194                 while(end > begin && end < size && iw(r[end])) 
    195                 { 
    196                         end--; 
    197                 } 
    198                  
    199                 // Return a sub string 
    200                 return r.substr(begin, end - begin + 1); 
     106        if(bytes == 0) 
     107        { 
     108                mPendingEOF = true; 
    201109        } 
    202110} 
     
    208116//              Name:    FdGetLine::DetachFile() 
    209117//              Purpose: Detaches the file handle, setting the file pointer correctly. 
    210 //                              Probably not good for sockets... 
     118//                      Probably not good for sockets... 
    211119//              Created: 2003/07/24 
    212120// 
     
    231139} 
    232140 
    233  
  • box/trunk/lib/common/FdGetLine.h

    r2415 r2935  
    1313#include <string> 
    1414 
    15 #ifdef BOX_RELEASE_BUILD 
    16         #define FDGETLINE_BUFFER_SIZE           1024 
    17 #elif defined WIN32 
    18         // need enough space for at least one unicode character  
    19         // in UTF-8 when calling console_read() from bbackupquery 
    20         #define FDGETLINE_BUFFER_SIZE           5 
    21 #else 
    22         #define FDGETLINE_BUFFER_SIZE           4 
    23 #endif 
    24  
    25 // Just a very large upper bound for line size to avoid 
    26 // people sending lots of data over sockets and causing memory problems. 
    27 #define FDGETLINE_MAX_LINE_SIZE                 (1024*256) 
     15#include "GetLine.h" 
    2816 
    2917// -------------------------------------------------------------------------- 
     
    3523// 
    3624// -------------------------------------------------------------------------- 
    37 class FdGetLine 
     25class FdGetLine : public GetLine 
    3826{ 
    3927public: 
     
    4432 
    4533public: 
    46         std::string GetLine(bool Preprocess = false); 
    47         bool IsEOF() {return mEOF;} 
    48         int GetLineNumber() {return mLineNumber;} 
    49          
     34        virtual std::string GetLine(bool Preprocess = false); 
    5035        // Call to detach, setting file pointer correctly to last bit read. 
    5136        // Only works for lseek-able file descriptors. 
    5237        void DetachFile(); 
    53          
     38        // if we read 0 bytes from an fd, it must be end of stream, 
     39        // because we don't support timeouts 
     40        virtual bool IsStreamDataLeft() { return false; } 
     41 
     42protected: 
     43        int ReadMore(int Timeout = IOStream::TimeOutInfinite); 
     44 
    5445private: 
    55         char mBuffer[FDGETLINE_BUFFER_SIZE]; 
    5646        int mFileHandle; 
    57         int mLineNumber; 
    58         int mBufferBegin; 
    59         int mBytesInBuffer; 
    60         bool mPendingEOF; 
    61         bool mEOF; 
    6247}; 
    6348 
  • box/trunk/lib/common/GetLine.cpp

    r2883 r2935  
    22// 
    33// File 
    4 //              Name:    FdGetLine.cpp 
    5 //              Purpose: Line based file descriptor reading 
    6 //              Created: 2003/07/24 
     4//              Name:    GetLine.cpp 
     5//              Purpose: Common base class for line based file descriptor reading 
     6//              Created: 2011/04/22 
    77// 
    88// -------------------------------------------------------------------------- 
     
    1616#endif 
    1717 
    18 #include "FdGetLine.h" 
     18#include "GetLine.h" 
    1919#include "CommonException.h" 
    2020 
     
    3131// 
    3232// Function 
    33 //              Name:    FdGetLine::FdGetLine(int) 
     33//              Name:    GetLine::GetLine(int) 
    3434//              Purpose: Constructor, taking file descriptor 
    35 //              Created: 2003/07/24 
     35//              Created: 2011/04/22 
    3636// 
    3737// -------------------------------------------------------------------------- 
    38 FdGetLine::FdGetLine(int fd) 
    39         : mFileHandle(fd), 
    40           mLineNumber(0), 
    41           mBufferBegin(0), 
    42           mBytesInBuffer(0), 
    43           mPendingEOF(false), 
    44           mEOF(false) 
    45 { 
    46         if(mFileHandle < 0) {THROW_EXCEPTION(CommonException, BadArguments)} 
    47         //printf("FdGetLine buffer size = %d\n", sizeof(mBuffer)); 
    48 } 
    49  
     38GetLine::GetLine() 
     39: mLineNumber(0), 
     40  mBufferBegin(0), 
     41  mBytesInBuffer(0), 
     42  mPendingEOF(false), 
     43  mEOF(false) 
     44{ } 
    5045 
    5146// -------------------------------------------------------------------------- 
    5247// 
    5348// Function 
    54 //              Name:    FdGetLine::~FdGetLine() 
    55 //              Purpose: Destructor 
    56 //              Created: 2003/07/24 
     49//              Name:    GetLine::GetLineInternal(std::string &, bool, int) 
     50//              Purpose: Gets a line from the file, returning it in rOutput. 
     51//                       If Preprocess is true, leading and trailing 
     52//                       whitespace is removed, and comments (after #)  are 
     53//                       deleted. Returns true if a line is available now, 
     54//                       false if retrying may get a line (eg timeout, 
     55//                       signal), and exceptions if it's EOF. 
     56//              Created: 2011/04/22 
    5757// 
    5858// -------------------------------------------------------------------------- 
    59 FdGetLine::~FdGetLine() 
     59bool GetLine::GetLineInternal(std::string &rOutput, bool Preprocess, 
     60        int Timeout) 
    6061{ 
    61 } 
    62  
    63  
    64 // -------------------------------------------------------------------------- 
    65 // 
    66 // Function 
    67 //              Name:    FdGetLine::GetLine(bool) 
    68 //              Purpose: Returns a file from the file. If Preprocess is true, leading 
    69 //                               and trailing whitespace is removed, and comments (after #) 
    70 //                               are deleted. 
    71 //              Created: 2003/07/24 
    72 // 
    73 // -------------------------------------------------------------------------- 
    74 std::string FdGetLine::GetLine(bool Preprocess) 
    75 { 
    76         if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)} 
    77  
    7862        // EOF? 
    7963        if(mEOF) {THROW_EXCEPTION(CommonException, GetLineEOF)} 
    8064         
    81         std::string r; 
     65        // Initialise string to stored into 
     66        rOutput = mPendingString; 
     67        mPendingString.erase(); 
    8268 
    8369        bool foundLineEnd = false; 
     
    10288                        { 
    10389                                // Add to string 
    104                                 r += c; 
     90                                rOutput += c; 
    10591                        } 
    10692                         
     
    11399                 
    114100                // Check size 
    115                 if(r.size() > FDGETLINE_MAX_LINE_SIZE) 
     101                if(rOutput.size() > GETLINE_MAX_LINE_SIZE) 
    116102                { 
    117103                        THROW_EXCEPTION(CommonException, GetLineTooLarge) 
     
    121107                if(!foundLineEnd && mBufferBegin >= mBytesInBuffer && !mPendingEOF) 
    122108                { 
    123 #ifdef WIN32 
    124                         int bytes; 
    125  
    126                         if (mFileHandle == _fileno(stdin)) 
    127                         { 
    128                                 bytes = console_read(mBuffer, sizeof(mBuffer)); 
    129                         } 
    130                         else 
    131                         { 
    132                                 bytes = ::read(mFileHandle, mBuffer,  
    133                                         sizeof(mBuffer)); 
    134                         } 
    135 #else // !WIN32 
    136                         int bytes = ::read(mFileHandle, mBuffer, sizeof(mBuffer)); 
    137 #endif // WIN32 
     109                        int bytes = ReadMore(Timeout); 
    138110                         
    139111                        // Error? 
     
    147119                        mBufferBegin = 0; 
    148120                         
    149                         // EOF / closed? 
    150                         if(bytes == 0) 
     121                        // No data returned? 
     122                        if(bytes == 0 && IsStreamDataLeft()) 
    151123                        { 
    152                                 mPendingEOF = true; 
     124                               // store string away 
     125                               mPendingString = rOutput; 
     126                               // Return false; 
     127                               return false; 
    153128                        } 
    154129                } 
     
    162137        } 
    163138 
    164         if(!Preprocess) 
    165         { 
    166                 return r; 
    167         } 
    168         else 
     139        if(Preprocess) 
    169140        { 
    170141                // Check for comment char, but char before must be whitespace 
     142                // end points to a gap between characters, may equal start if 
     143                // the string to be extracted has zero length, and indexes the 
     144                // first character not in the string (== length, or a # mark 
     145                // or whitespace) 
    171146                int end = 0; 
    172                 int size = r.size(); 
     147                int size = rOutput.size(); 
    173148                while(end < size) 
    174149                { 
    175                         if(r[end] == '#' && (end == 0 || (iw(r[end-1])))) 
     150                        if(rOutput[end] == '#' && (end == 0 || (iw(rOutput[end-1])))) 
    176151                        { 
    177152                                break; 
     
    182157                // Remove whitespace 
    183158                int begin = 0; 
    184                 while(begin < size && iw(r[begin])) 
     159                while(begin < size && iw(rOutput[begin])) 
    185160                { 
    186161                        begin++; 
    187162                } 
    188163 
    189                 if(end < size && !iw(r[end])) 
    190                 { 
    191                         end--; 
    192                 } 
    193  
    194                 while(end > begin && end < size && iw(r[end])) 
     164                while(end > begin && end <= size && iw(rOutput[end-1])) 
    195165                { 
    196166                        end--; 
     
    198168                 
    199169                // Return a sub string 
    200                 return r.substr(begin, end - begin + 1); 
     170                rOutput = rOutput.substr(begin, end - begin); 
    201171        } 
     172 
     173        return true; 
    202174} 
    203175 
    204176 
    205 // -------------------------------------------------------------------------- 
    206 // 
    207 // Function 
    208 //              Name:    FdGetLine::DetachFile() 
    209 //              Purpose: Detaches the file handle, setting the file pointer correctly. 
    210 //                               Probably not good for sockets... 
    211 //              Created: 2003/07/24 
    212 // 
    213 // -------------------------------------------------------------------------- 
    214 void FdGetLine::DetachFile() 
    215 { 
    216         if(mFileHandle == -1) {THROW_EXCEPTION(CommonException, GetLineNoHandle)} 
    217  
    218         // Adjust file pointer 
    219         int bytesOver = mBufferBegin - mBufferBegin; 
    220         ASSERT(bytesOver >= 0); 
    221         if(bytesOver > 0) 
    222         { 
    223                 if(::lseek(mFileHandle, 0 - bytesOver, SEEK_CUR) == -1) 
    224                 { 
    225                         THROW_EXCEPTION(CommonException, OSFileError) 
    226                 } 
    227         } 
    228  
    229         // Unset file pointer 
    230         mFileHandle = -1; 
    231 } 
    232  
    233  
  • box/trunk/lib/common/GetLine.h

    r2415 r2935  
    22// 
    33// File 
    4 //              Name:    FdGetLine.h 
    5 //              Purpose: Line based file descriptor reading 
    6 //              Created: 2003/07/24 
     4//              Name:    GetLine.h 
     5//              Purpose: Common base class for line based file descriptor reading 
     6//              Created: 2011/04/22 
    77// 
    88// -------------------------------------------------------------------------- 
    99 
    10 #ifndef FDGETLINE__H 
    11 #define FDGETLINE__H 
     10#ifndef GETLINE__H 
     11#define GETLINE__H 
    1212 
    1313#include <string> 
    1414 
    1515#ifdef BOX_RELEASE_BUILD 
    16         #define FDGETLINE_BUFFER_SIZE           1024 
     16        #define GETLINE_BUFFER_SIZE             1024 
    1717#elif defined WIN32 
    1818        // need enough space for at least one unicode character  
    1919        // in UTF-8 when calling console_read() from bbackupquery 
    20         #define FDGETLINE_BUFFER_SIZE           5 
     20        #define GETLINE_BUFFER_SIZE             5 
    2121#else 
    22         #define FDGETLINE_BUFFER_SIZE           4 
     22        #define GETLINE_BUFFER_SIZE             4 
    2323#endif 
    2424 
    2525// Just a very large upper bound for line size to avoid 
    2626// people sending lots of data over sockets and causing memory problems. 
    27 #define FDGETLINE_MAX_LINE_SIZE                 (1024*256) 
     27#define GETLINE_MAX_LINE_SIZE                   (1024*256) 
    2828 
    2929// -------------------------------------------------------------------------- 
    3030// 
    3131// Class 
    32 //              Name:    FdGetLine 
    33 //              Purpose: Line based file descriptor reading 
    34 //              Created: 2003/07/24 
     32//              Name:    GetLine 
     33//              Purpose: Common base class for line based file descriptor reading 
     34//              Created: 2011/04/22 
    3535// 
    3636// -------------------------------------------------------------------------- 
    37 class FdGetLine 
     37class GetLine 
    3838{ 
    39 public: 
    40         FdGetLine(int fd); 
    41         ~FdGetLine(); 
     39protected: 
     40        GetLine(); 
     41 
    4242private: 
    43         FdGetLine(const FdGetLine &rToCopy); 
     43        GetLine(const GetLine &rToCopy); 
    4444 
    4545public: 
    46         std::string GetLine(bool Preprocess = false); 
    47         bool IsEOF() {return mEOF;} 
     46        virtual bool IsEOF() {return mEOF;} 
    4847        int GetLineNumber() {return mLineNumber;} 
    4948         
    50         // Call to detach, setting file pointer correctly to last bit read. 
    51         // Only works for lseek-able file descriptors. 
    52         void DetachFile(); 
    53          
    54 private: 
    55         char mBuffer[FDGETLINE_BUFFER_SIZE]; 
    56         int mFileHandle; 
     49protected: 
     50        bool GetLineInternal(std::string &rOutput, 
     51                bool Preprocess = false, 
     52                int Timeout = IOStream::TimeOutInfinite); 
     53        virtual int ReadMore(int Timeout = IOStream::TimeOutInfinite) = 0; 
     54        virtual bool IsStreamDataLeft() = 0; 
     55 
     56        char mBuffer[GETLINE_BUFFER_SIZE]; 
    5757        int mLineNumber; 
    5858        int mBufferBegin; 
    5959        int mBytesInBuffer; 
    6060        bool mPendingEOF; 
     61        std::string mPendingString; 
    6162        bool mEOF; 
    6263}; 
    6364 
    64 #endif // FDGETLINE__H 
     65#endif // GETLINE__H 
    6566 
  • box/trunk/lib/common/IOStreamGetLine.cpp

    r2883 r2935  
    1414#include "MemLeakFindOn.h" 
    1515 
    16 // utility whitespace function 
    17 inline bool iw(int c) 
    18 { 
    19         return (c == ' ' || c == '\t' || c == '\v' || c == '\f'); // \r, \n are already excluded 
    20 } 
    21  
    22  
    2316// -------------------------------------------------------------------------- 
    2417// 
     
    3023// -------------------------------------------------------------------------- 
    3124IOStreamGetLine::IOStreamGetLine(IOStream &Stream) 
    32         : mrStream(Stream), 
    33           mLineNumber(0), 
    34           mBufferBegin(0), 
    35           mBytesInBuffer(0), 
    36           mPendingEOF(false), 
    37           mEOF(false) 
     25: mrStream(Stream) 
    3826{ 
    3927} 
     
    6755bool IOStreamGetLine::GetLine(std::string &rOutput, bool Preprocess, int Timeout) 
    6856{ 
    69         // EOF? 
    70         if(mEOF) {THROW_EXCEPTION(CommonException, GetLineEOF)} 
     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// -------------------------------------------------------------------------- 
     71int IOStreamGetLine::ReadMore(int Timeout) 
     72{ 
     73        int bytes = mrStream.Read(mBuffer, sizeof(mBuffer), Timeout); 
    7174         
    72         // Initialise string to stored into 
    73         std::string r(mPendingString); 
    74         mPendingString.erase(); 
    75  
    76         bool foundLineEnd = false; 
    77  
    78         while(!foundLineEnd && !mEOF) 
     75        if(!mrStream.StreamDataLeft()) 
    7976        { 
    80                 // Use any bytes left in the buffer 
    81                 while(mBufferBegin < mBytesInBuffer) 
    82                 { 
    83                         int c = mBuffer[mBufferBegin++]; 
    84                         if(c == '\r') 
    85                         { 
    86                                 // Ignore nasty Windows line ending extra chars 
    87                         } 
    88                         else if(c == '\n') 
    89                         { 
    90                                 // Line end! 
    91                                 foundLineEnd = true; 
    92                                 break; 
    93                         } 
    94                         else 
    95                         { 
    96                                 // Add to string 
    97                                 r += c; 
    98                         } 
    99                          
    100                         // Implicit line ending at EOF 
    101                         if(mBufferBegin >= mBytesInBuffer && mPendingEOF) 
    102                         { 
    103                                 foundLineEnd = true; 
    104                         } 
    105                 } 
    106                  
    107                 // Check size 
    108                 if(r.size() > IOSTREAMGETLINE_MAX_LINE_SIZE) 
    109                 { 
    110                         THROW_EXCEPTION(CommonException, GetLineTooLarge) 
    111                 } 
    112                  
    113                 // Read more in? 
    114                 if(!foundLineEnd && mBufferBegin >= mBytesInBuffer && !mPendingEOF) 
    115                 { 
    116                         int bytes = mrStream.Read(mBuffer, sizeof(mBuffer), Timeout); 
    117                          
    118                         // Adjust buffer info 
    119                         mBytesInBuffer = bytes; 
    120                         mBufferBegin = 0; 
    121                          
    122                         // EOF / closed? 
    123                         if(!mrStream.StreamDataLeft()) 
    124                         { 
    125                                 mPendingEOF = true; 
    126                         } 
    127                          
    128                         // No data returned? 
    129                         if(bytes == 0 && mrStream.StreamDataLeft()) 
    130                         { 
    131                                 // store string away 
    132                                 mPendingString = r; 
    133                                 // Return false; 
    134                                 return false; 
    135                         } 
    136                 } 
    137                  
    138                 // EOF? 
    139                 if(mPendingEOF && mBufferBegin >= mBytesInBuffer) 
    140                 { 
    141                         // File is EOF, and now we've depleted the buffer completely, so tell caller as well. 
    142                         mEOF = true; 
    143                 } 
     77                mPendingEOF = true; 
    14478        } 
    14579 
    146         if(!Preprocess) 
    147         { 
    148                 rOutput = r; 
    149                 return true; 
    150         } 
    151         else 
    152         { 
    153                 // Check for comment char, but char before must be whitespace 
    154                 int end = 0; 
    155                 int size = r.size(); 
    156                 while(end < size) 
    157                 { 
    158                         if(r[end] == '#' && (end == 0 || (iw(r[end-1])))) 
    159                         { 
    160                                 break; 
    161                         } 
    162                         end++; 
    163                 } 
    164                  
    165                 // Remove whitespace 
    166                 int begin = 0; 
    167                 while(begin < size && iw(r[begin])) 
    168                 { 
    169                         begin++; 
    170                 } 
    171  
    172                 if(end < size && !iw(r[end])) 
    173                 { 
    174                         end--; 
    175                 } 
    176  
    177                 while(end > begin && end < size && iw(r[end])) 
    178                 { 
    179                         end--; 
    180                 } 
    181                  
    182                 // Return a sub string 
    183                 rOutput = r.substr(begin, end - begin + 1); 
    184                 return true; 
    185         } 
     80        return bytes;    
    18681} 
    18782 
  • box/trunk/lib/common/IOStreamGetLine.h

    r2415 r2935  
    1313#include <string> 
    1414 
     15#include "GetLine.h" 
    1516#include "IOStream.h" 
    16  
    17 #ifdef BOX_RELEASE_BUILD 
    18         #define IOSTREAMGETLINE_BUFFER_SIZE             1024 
    19 #else 
    20         #define IOSTREAMGETLINE_BUFFER_SIZE             4 
    21 #endif 
    22  
    23 // Just a very large upper bound for line size to avoid 
    24 // people sending lots of data over sockets and causing memory problems. 
    25 #define IOSTREAMGETLINE_MAX_LINE_SIZE                   (1024*256) 
    2617 
    2718// -------------------------------------------------------------------------- 
     
    3324// 
    3425// -------------------------------------------------------------------------- 
    35 class IOStreamGetLine 
     26class IOStreamGetLine : public GetLine 
    3627{ 
    3728public: 
     
    4334public: 
    4435        bool GetLine(std::string &rOutput, bool Preprocess = false, int Timeout = IOStream::TimeOutInfinite); 
    45         bool IsEOF() {return mEOF;} 
    46         int GetLineNumber() {return mLineNumber;} 
    4736         
    4837        // Call to detach, setting file pointer correctly to last bit read. 
    4938        // Only works for lseek-able file descriptors. 
    5039        void DetachFile(); 
     40         
     41        virtual bool IsStreamDataLeft() 
     42        { 
     43                return mrStream.StreamDataLeft(); 
     44        } 
    5145         
    5246        // For doing interesting stuff with the remaining data... 
     
    5650        void IgnoreBufferedData(int BytesToIgnore); 
    5751        IOStream &GetUnderlyingStream() {return mrStream;} 
     52 
     53protected: 
     54        int ReadMore(int Timeout = IOStream::TimeOutInfinite); 
    5855         
    5956private: 
    60         char mBuffer[IOSTREAMGETLINE_BUFFER_SIZE]; 
    6157        IOStream &mrStream; 
    62         int mLineNumber; 
    63         int mBufferBegin; 
    64         int mBytesInBuffer; 
    65         bool mPendingEOF; 
    66         bool mEOF; 
    67         std::string mPendingString; 
    6858}; 
    6959 
Note: See TracChangeset for help on using the changeset viewer.