3 #include "ACEXML/common/ZipCharStream.h"
7 ACEXML_ZipCharStream::ACEXML_ZipCharStream ()
8 : filename_ (0), encoding_ (0), size_ (0), infile_ (0), pos_ (0),
13 ACEXML_ZipCharStream::~ACEXML_ZipCharStream ()
19 ACEXML_ZipCharStream::open (const ACEXML_Char
*name
)
21 delete[] this->filename_
;
24 delete[] this->encoding_
;
27 this->infile_
= zzip_fopen (name
, ACE_TEXT ("r"));
28 if (this->infile_
== 0)
31 this->filename_
= ACE::strnew (ACE::basename (name
));
32 return this->determine_encoding();
36 ACEXML_ZipCharStream::determine_encoding ()
38 if (this->infile_
== 0)
42 for (; i
< 4 && (input
[i
] = this->peekchar_i(i
)) > 0; ++i
)
46 const ACEXML_Char
* temp
= ACEXML_Encoding::get_encoding (input
);
52 delete [] this->encoding_
;
53 this->encoding_
= ACE::strnew (temp
);
54 // ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("File's encoding is %s\n"),
58 // Move over the byte-order-mark if present.
60 for (int j
= 0; j
< 3; ++j
)
63 if ((ch
= this->peekchar_i()) < 0)
65 if (ch
== '\xFF' || ch
== '\xFE' || ch
== '\xEF' || ch
== '\xBB' ||
76 ACEXML_ZipCharStream::rewind()
78 if (this->infile_
== 0)
80 zzip_rewind (this->infile_
);
81 this->determine_encoding();
85 ACEXML_ZipCharStream::available ()
87 if (this->infile_
== 0)
90 if ((curr
= zzip_tell (this->infile_
)) < 0)
92 return (this->size_
- curr
);
96 ACEXML_ZipCharStream::close ()
98 if (this->infile_
!= 0)
100 zzip_close (this->infile_
);
103 delete[] this->filename_
;
105 delete[] this->encoding_
;
115 ACEXML_ZipCharStream::getchar_i (char& ch
)
117 if (this->infile_
== 0)
120 if (this->pos_
< this->limit_
)
122 ch
= this->buf_
[this->pos_
++];
125 this->limit_
= zzip_read (this->infile_
, this->buf_
, sizeof (this->buf_
));
126 if (this->limit_
== 0)
129 ch
= this->buf_
[this->pos_
++];
134 ACEXML_ZipCharStream::peekchar_i (ACE_OFF_T offset
)
136 if (this->infile_
== 0)
139 if (offset
> (ACE_OFF_T
) sizeof (this->buf_
))
141 if (this->pos_
+ offset
< this->limit_
)
142 return this->buf_
[this->pos_
+ offset
];
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
);
149 if (this->limit_
== 0)
152 return this->buf_
[this->pos_
+ offset
];
156 ACEXML_ZipCharStream::read (ACEXML_Char
*str
, size_t len
)
158 if (this->infile_
== 0)
162 for (; i
< len
&& this->pos_
< this->limit_
; ++i
)
163 str
[i
] = this->buf_
[this->pos_
++];
169 int bytes
= zzip_fread (str
+ i
, sizeof (ACEXML_Char
), len
, this->infile_
);
174 ACEXML_ZipCharStream::get (ACEXML_Char
& ch
)
176 #if defined (ACE_USES_WCHAR)
177 return this->get_i (ch
);
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();
190 return this->peekchar_i();
191 #endif /* ACE_USES_WCHAR */
195 ACEXML_ZipCharStream::getEncoding ()
197 return this->encoding_
;
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];
217 for (; i
< 2 && (this->getchar_i (input
[i
]) == 0); ++i
)
224 ch
= BE
? input
[0] << 8 | input
[1] : input
[1] << 8 | input
[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
237 int BE
= (ACE_OS::strcmp (this->encoding_
,
238 ACE_TEXT ("UTF-16BE")) == 0) ? 1 : 0;
240 ACEXML_Char input
[2];
242 for (; i
< 2 && (input
[i
] = this->peekchar_i (i
)) > 0; ++i
)
246 return (BE
? input
[0] << 8 | input
[1] : input
[1] << 8 | input
[0]);
248 #endif /* ACE_USES_WCHAR */
250 #endif /* USE_ZZIP */