Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / ACE / ACEXML / common / ZipCharStream.cpp
blob7dcf3a9b47c652edb908bcb642d4b2d21092ae60
1 #ifdef USE_ZZIP
3 #include "ACEXML/common/ZipCharStream.h"
4 #include "ace/ACE.h"
7 ACEXML_ZipCharStream::ACEXML_ZipCharStream ()
8 : filename_ (0), encoding_ (0), size_ (0), infile_ (0), pos_ (0),
9 limit_ (0)
13 ACEXML_ZipCharStream::~ACEXML_ZipCharStream ()
15 this->close();
18 int
19 ACEXML_ZipCharStream::open (const ACEXML_Char *name)
21 delete[] this->filename_;
22 this->filename_ = 0;
24 delete[] this->encoding_;
25 this->encoding_ = 0;
27 this->infile_ = zzip_fopen (name, ACE_TEXT ("r"));
28 if (this->infile_ == 0)
29 return -1;
31 this->filename_ = ACE::strnew (ACE::basename (name));
32 return this->determine_encoding();
35 int
36 ACEXML_ZipCharStream::determine_encoding ()
38 if (this->infile_ == 0)
39 return -1;
40 char input[4];
41 int i = 0;
42 for (; i < 4 && (input[i] = this->peekchar_i(i)) > 0; ++i)
44 if (i < 4)
45 return -1;
46 const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
47 if (!temp)
48 return -1;
49 else
51 if (this->encoding_)
52 delete [] this->encoding_;
53 this->encoding_ = ACE::strnew (temp);
54 // ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("File's encoding is %s\n"),
55 // this->encoding_));
58 // Move over the byte-order-mark if present.
60 for (int j = 0; j < 3; ++j)
62 ACEXML_Char ch;
63 if ((ch = this->peekchar_i()) < 0)
64 return -1;
65 if (ch == '\xFF' || ch == '\xFE' || ch == '\xEF' || ch == '\xBB' ||
66 ch == '\xBF')
67 this->get(ch);
68 else
69 break;
72 return 0;
75 void
76 ACEXML_ZipCharStream::rewind()
78 if (this->infile_ == 0)
79 return;
80 zzip_rewind (this->infile_);
81 this->determine_encoding();
84 int
85 ACEXML_ZipCharStream::available ()
87 if (this->infile_ == 0)
88 return -1;
89 long curr;
90 if ((curr = zzip_tell (this->infile_)) < 0)
91 return -1;
92 return (this->size_ - curr);
95 int
96 ACEXML_ZipCharStream::close ()
98 if (this->infile_ != 0)
100 zzip_close (this->infile_);
101 this->infile_ = 0;
103 delete[] this->filename_;
104 this->filename_ = 0;
105 delete[] this->encoding_;
106 this->encoding_ = 0;
107 this->size_ = 0;
108 this->pos_ = 0;
109 this->limit_ = 0;
110 return 0;
115 ACEXML_ZipCharStream::getchar_i (char& ch)
117 if (this->infile_ == 0)
118 return -1;
120 if (this->pos_ < this->limit_)
122 ch = this->buf_[this->pos_++];
123 return 0;
125 this->limit_ = zzip_read (this->infile_, this->buf_, sizeof (this->buf_));
126 if (this->limit_ == 0)
127 return -1;
128 this->pos_ = 0;
129 ch = this->buf_[this->pos_++];
130 return 0;
134 ACEXML_ZipCharStream::peekchar_i (ACE_OFF_T offset)
136 if (this->infile_ == 0)
137 return -1;
139 if (offset > (ACE_OFF_T) sizeof (this->buf_))
140 return -1;
141 if (this->pos_ + offset < this->limit_)
142 return this->buf_[this->pos_ + offset];
143 int i = 0;
144 for (; this->pos_ < this->limit_; ++this->pos_, ++i)
145 this->buf_[i] = this->buf_[this->pos_];
146 this->limit_ = zzip_read (this->infile_, this->buf_ + i,
147 sizeof (this->buf_) - i);
148 this->limit_ += i;
149 if (this->limit_ == 0)
150 return -1;
151 this->pos_ = 0;
152 return this->buf_[this->pos_ + offset];
156 ACEXML_ZipCharStream::read (ACEXML_Char *str, size_t len)
158 if (this->infile_ == 0)
159 return -1;
161 size_t i = 0;
162 for (; i < len && this->pos_ < this->limit_; ++i)
163 str[i] = this->buf_[this->pos_++];
164 if (i == len)
165 return len;
166 len = len - i;
167 this->pos_ = 0;
168 this->limit_ = 0;
169 int bytes = zzip_fread (str + i, sizeof (ACEXML_Char), len, this->infile_);
170 return (bytes + i);
174 ACEXML_ZipCharStream::get (ACEXML_Char& ch)
176 #if defined (ACE_USES_WCHAR)
177 return this->get_i (ch);
178 #else
179 return this->getchar_i (ch);
180 #endif /* ACE_USES_WCHAR */
185 ACEXML_ZipCharStream::peek ()
187 #if defined (ACE_USES_WCHAR)
188 return this->peek_i();
189 #else
190 return this->peekchar_i();
191 #endif /* ACE_USES_WCHAR */
194 const ACEXML_Char*
195 ACEXML_ZipCharStream::getEncoding ()
197 return this->encoding_;
200 const ACEXML_Char*
201 ACEXML_ZipCharStream::getSystemId ()
203 return this->filename_;
206 #if defined (ACE_USES_WCHAR)
208 ACEXML_ZipCharStream::get_i (ACEXML_Char& ch)
210 if (ACE_OS::strcmp (this->encoding_, ACE_TEXT ("UTF-8")) == 0)
211 return this->getchar_i (ch);
213 int BE = (ACE_OS::strcmp (this->encoding_,
214 ACE_TEXT ("UTF-16BE")) == 0) ? 1 : 0;
215 ACEXML_Char input[2];
216 int i = 0;
217 for (; i < 2 && (this->getchar_i (input[i]) == 0); ++i)
219 if (i < 2)
221 ch = 0;
222 return -1;
224 ch = BE ? input[0] << 8 | input[1] : input[1] << 8 | input[0];
225 return 0;
229 ACEXML_ZipCharStream::peek_i ()
231 // If we are reading a UTF-8 encoded file, just use the plain unget.
232 if (ACE_OS::strcmp (this->encoding_, ACE_TEXT ("UTF-8")) == 0)
233 return this->peekchar_i();
235 // Peek into the stream. This reads two characters off the stream, keeps
236 // it in peek_.
237 int BE = (ACE_OS::strcmp (this->encoding_,
238 ACE_TEXT ("UTF-16BE")) == 0) ? 1 : 0;
240 ACEXML_Char input[2];
241 int i = 0;
242 for (; i < 2 && (input[i] = this->peekchar_i (i)) > 0; ++i)
244 if (i < 2)
245 return -1;
246 return (BE ? input[0] << 8 | input[1] : input[1] << 8 | input[0]);
248 #endif /* ACE_USES_WCHAR */
250 #endif /* USE_ZZIP */