Go to the documentation of this file.00001
00002
00003 #ifdef USE_ZZIP
00004
00005 #include "ACEXML/common/ZipCharStream.h"
00006 #include "ace/ACE.h"
00007
00008
00009 ACEXML_ZipCharStream::ACEXML_ZipCharStream (void)
00010 : filename_ (0), encoding_ (0), size_ (0), infile_ (0), pos_ (0),
00011 limit_ (0)
00012 {
00013 }
00014
00015 ACEXML_ZipCharStream::~ACEXML_ZipCharStream (void)
00016 {
00017 this->close();
00018 }
00019
00020 int
00021 ACEXML_ZipCharStream::open (const ACEXML_Char *name)
00022 {
00023 delete[] this->filename_;
00024 this->filename_ = 0;
00025
00026 delete[] this->encoding_;
00027 this->encoding_ = 0;
00028
00029 this->infile_ = zzip_fopen (name, ACE_TEXT ("r"));
00030 if (this->infile_ == 0)
00031 return -1;
00032
00033 this->filename_ = ACE::strnew (ACE::basename (name));
00034 return this->determine_encoding();
00035 }
00036
00037 int
00038 ACEXML_ZipCharStream::determine_encoding (void)
00039 {
00040 if (this->infile_ == 0)
00041 return -1;
00042 char input[4];
00043 int i = 0;
00044 for (; i < 4 && (input[i] = this->peekchar_i(i)) > 0; ++i)
00045 ;
00046 if (i < 4)
00047 return -1;
00048 const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
00049 if (!temp)
00050 return -1;
00051 else
00052 {
00053 if (this->encoding_)
00054 delete [] this->encoding_;
00055 this->encoding_ = ACE::strnew (temp);
00056
00057
00058 }
00059
00060
00061
00062 for (int j = 0; j < 3; ++j)
00063 {
00064 ACEXML_Char ch;
00065 if ((ch = this->peekchar_i()) < 0)
00066 return -1;
00067 if (ch == '\xFF' || ch == '\xFE' || ch == '\xEF' || ch == '\xBB' ||
00068 ch == '\xBF')
00069 this->get(ch);
00070 else
00071 break;
00072 }
00073
00074 return 0;
00075 }
00076
00077 void
00078 ACEXML_ZipCharStream::rewind()
00079 {
00080 if (this->infile_ == 0)
00081 return;
00082 zzip_rewind (this->infile_);
00083 this->determine_encoding();
00084 }
00085
00086 int
00087 ACEXML_ZipCharStream::available (void)
00088 {
00089 if (this->infile_ == 0)
00090 return -1;
00091 long curr;
00092 if ((curr = zzip_tell (this->infile_)) < 0)
00093 return -1;
00094 return (this->size_ - curr);
00095 }
00096
00097 int
00098 ACEXML_ZipCharStream::close (void)
00099 {
00100 if (this->infile_ != 0)
00101 {
00102 zzip_close (this->infile_);
00103 this->infile_ = 0;
00104 }
00105 delete[] this->filename_;
00106 this->filename_ = 0;
00107 delete[] this->encoding_;
00108 this->encoding_ = 0;
00109 this->size_ = 0;
00110 this->pos_ = 0;
00111 this->limit_ = 0;
00112 return 0;
00113 }
00114
00115
00116 int
00117 ACEXML_ZipCharStream::getchar_i (char& ch)
00118 {
00119 if (this->infile_ == 0)
00120 return -1;
00121
00122 if (this->pos_ < this->limit_)
00123 {
00124 ch = this->buf_[this->pos_++];
00125 return 0;
00126 }
00127 this->limit_ = zzip_read (this->infile_, this->buf_, sizeof (this->buf_));
00128 if (this->limit_ == 0)
00129 return -1;
00130 this->pos_ = 0;
00131 ch = this->buf_[this->pos_++];
00132 return 0;
00133 }
00134
00135 int
00136 ACEXML_ZipCharStream::peekchar_i (ACE_OFF_T offset)
00137 {
00138 if (this->infile_ == 0)
00139 return -1;
00140
00141 if (offset > (ACE_OFF_T) sizeof (this->buf_))
00142 return -1;
00143 if (this->pos_ + offset < this->limit_)
00144 return this->buf_[this->pos_ + offset];
00145 int i = 0;
00146 for (; this->pos_ < this->limit_; ++this->pos_, ++i)
00147 this->buf_[i] = this->buf_[this->pos_];
00148 this->limit_ = zzip_read (this->infile_, this->buf_ + i,
00149 sizeof (this->buf_) - i);
00150 this->limit_ += i;
00151 if (this->limit_ == 0)
00152 return -1;
00153 this->pos_ = 0;
00154 return this->buf_[this->pos_ + offset];
00155 }
00156
00157 int
00158 ACEXML_ZipCharStream::read (ACEXML_Char *str, size_t len)
00159 {
00160 if (this->infile_ == 0)
00161 return -1;
00162
00163 size_t i = 0;
00164 for (; i < len && this->pos_ < this->limit_; ++i)
00165 str[i] = this->buf_[this->pos_++];
00166 if (i == len)
00167 return len;
00168 len = len - i;
00169 this->pos_ = 0;
00170 this->limit_ = 0;
00171 int bytes = zzip_fread (str + i, sizeof (ACEXML_Char), len, this->infile_);
00172 return (bytes + i);
00173 }
00174
00175 int
00176 ACEXML_ZipCharStream::get (ACEXML_Char& ch)
00177 {
00178 #if defined (ACE_USES_WCHAR)
00179 return this->get_i (ch);
00180 #else
00181 return this->getchar_i (ch);
00182 #endif
00183 }
00184
00185
00186 int
00187 ACEXML_ZipCharStream::peek (void)
00188 {
00189 #if defined (ACE_USES_WCHAR)
00190 return this->peek_i();
00191 #else
00192 return this->peekchar_i();
00193 #endif
00194 }
00195
00196 const ACEXML_Char*
00197 ACEXML_ZipCharStream::getEncoding (void)
00198 {
00199 return this->encoding_;
00200 }
00201
00202 const ACEXML_Char*
00203 ACEXML_ZipCharStream::getSystemId (void)
00204 {
00205 return this->filename_;
00206 }
00207
00208 #if defined (ACE_USES_WCHAR)
00209 int
00210 ACEXML_ZipCharStream::get_i (ACEXML_Char& ch)
00211 {
00212 if (ACE_OS::strcmp (this->encoding_, ACE_TEXT ("UTF-8")) == 0)
00213 return this->getchar_i (ch);
00214
00215 int BE = (ACE_OS::strcmp (this->encoding_,
00216 ACE_TEXT ("UTF-16BE")) == 0) ? 1 : 0;
00217 ACEXML_Char input[2];
00218 int i = 0;
00219 for (; i < 2 && (this->getchar_i (input[i]) == 0); ++i)
00220 ;
00221 if (i < 2)
00222 {
00223 ch = 0;
00224 return -1;
00225 }
00226 ch = BE ? input[0] << 8 | input[1] : input[1] << 8 | input[0];
00227 return 0;
00228 }
00229
00230 int
00231 ACEXML_ZipCharStream::peek_i (void)
00232 {
00233
00234
00235 if (ACE_OS::strcmp (this->encoding_, ACE_TEXT ("UTF-8")) == 0)
00236 return this->peekchar_i();
00237
00238
00239
00240 int BE = (ACE_OS::strcmp (this->encoding_,
00241 ACE_TEXT ("UTF-16BE")) == 0) ? 1 : 0;
00242
00243 ACEXML_Char input[2];
00244 int i = 0;
00245 for (; i < 2 && (input[i] = this->peekchar_i (i)) > 0; ++i)
00246 ;
00247 if (i < 2)
00248 return -1;
00249 return (BE ? input[0] << 8 | input[1] : input[1] << 8 | input[0]);
00250 }
00251 #endif
00252
00253 #else
00254 #if defined (__HP_aCC)
00255 static int shut_up_aCC = 0;
00256 #endif
00257
00258 #endif