00001 //*************************************************************************** 00002 // This source code is copyrighted 2002 by Google Inc. All rights 00003 // reserved. You are given a limited license to use this source code for 00004 // purposes of participating in the Google programming contest. If you 00005 // choose to use or distribute the source code for any other purpose, you 00006 // must either (1) first obtain written approval from Google, or (2) 00007 // prominently display the foregoing copyright notice and the following 00008 // warranty and liability disclaimer on each copy used or distributed. 00009 // 00010 // The source code and repository (the "Software") is provided "AS IS", 00011 // with no warranty, express or implied, including but not limited to the 00012 // implied warranties of merchantability and fitness for a particular 00013 // use. In no event shall Google Inc. be liable for any damages, direct 00014 // or indirect, even if advised of the possibility of such damages. 00015 //*************************************************************************** 00016 00017 00018 // The ReposReader class reads basic data types out of a pre-parsed 00019 // format repository file (provided as a std::ifstream). It knows how 00020 // to decode char*, signed and unsigned integers, and bytes, and 00021 // handles any required buffering while reading from the file. 00022 00023 #ifndef REPOS_READER_H 00024 #define REPOS_READER_H 00025 00026 #include <iostream> 00027 #include <string> 00028 #include "document.h" 00029 #include "basictypes.h" 00030 #include "netutil.h" 00031 00032 00033 class ReposReader { 00034 public: 00035 // Note: ReposReader does not take ownership of "repos". 00036 ReposReader(std::istream* repos, const string& repos_name); 00037 virtual ~ReposReader(void) { }; 00038 00039 // The following two routines read characters into an internal buffer 00040 // and return a pointer to the buffer. The buffer contents may be 00041 // modified on subsequent calls so copy it if you need it across 00042 // multiple calls. ReadCharStar returns the number of characters 00043 // read, while ReadCharsOnly takes the number to read as an argument. 00044 // We guarantee that the buffer is at least 1 byte bigger than the number 00045 // of chars returned (so the caller can add a '\0' if desired). 00046 const char* ReadCharStar(int* len); 00047 const char* ReadCharsOnly(int numchars); 00048 00049 uint32 ReadFixedUint32(); 00050 uint32 ReadVarUint32(); 00051 unsigned char ReadByte(); 00052 int ReadInt(); 00053 bool AtEnd() const; 00054 int cur_pos() const { return cur_pos_; } 00055 const string& repos_name() const { return repos_name_; } 00056 // Report a parsing error and exit. Gives some info about the 00057 // current position in the repository. 00058 void ParseError(const string& errmsg); 00059 00060 private: 00061 void RefillBuf(); 00062 void ReallocCharsBuf(int size); 00063 00064 static const int kDecodeBufSize = 1024; 00065 static const int kReadBufSize = 10000; 00066 00067 std::istream* repos_; 00068 const string repos_name_; 00069 char decode_buf_[kDecodeBufSize]; 00070 int decode_buf_len_; // number of chars currently in decode_buf_ 00071 int cur_pos_; 00072 00073 char* chars_buf_; // buffer for ReadCharStar and ReadCharsOnly 00074 int chars_buf_len_; // current size of chars_buf_ 00075 00076 Decoder decoder_; 00077 }; 00078 00079 #endif