ACEXML_FileCharStream Class Reference

#include <ACEXML/common/FileCharStream.h>

Inheritance diagram for ACEXML_FileCharStream:

Inheritance graph
[legend]
Collaboration diagram for ACEXML_FileCharStream:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ACEXML_FileCharStream (void)
 Default constructor.

virtual ~ACEXML_FileCharStream (void)
 Destructor.

int open (const ACEXML_Char *name)
 Open a file.

virtual int available (void)
virtual int close (void)
virtual int get (ACEXML_Char &ch)
virtual int read (ACEXML_Char *str, size_t len)
virtual int determine_encoding (void)
virtual int peek (void)
virtual void rewind (void)
virtual const ACEXML_ChargetEncoding (void)
virtual const ACEXML_ChargetSystemId (void)

Private Member Functions

int getchar_i (char &ch)

Private Attributes

ACEXML_Charfilename_
ACEXML_Charencoding_
off_t size_
FILE * infile_
ACEXML_Char peek_

Detailed Description

An implementation of ACEXML_CharStream for reading input from a file.

Definition at line 31 of file FileCharStream.h.


Constructor & Destructor Documentation

ACEXML_FileCharStream::ACEXML_FileCharStream void   ) 
 

Default constructor.

Definition at line 13 of file FileCharStream.cpp.

00014   : filename_ (0), encoding_ (0), size_ (0), infile_ (0), peek_ (0)
00015 {
00016 }

ACEXML_FileCharStream::~ACEXML_FileCharStream void   )  [virtual]
 

Destructor.

Definition at line 18 of file FileCharStream.cpp.

References close().

00019 {
00020   this->close();
00021 }


Member Function Documentation

int ACEXML_FileCharStream::available void   )  [virtual]
 

Returns the available ACEXML_Char in the buffer. -1 if the object is not initialized properly.

Implements ACEXML_CharStream.

Definition at line 101 of file FileCharStream.cpp.

References ACE_OS::ftell(), and infile_.

00102 {
00103   if (this->infile_ == 0)
00104     return -1;
00105 
00106   long curr;
00107   if ((curr = ACE_OS::ftell (this->infile_)) < 0)
00108     return -1;
00109   return (this->size_ - curr);
00110 }

int ACEXML_FileCharStream::close void   )  [virtual]
 

Close this stream and release all resources used by it.

Implements ACEXML_CharStream.

Definition at line 113 of file FileCharStream.cpp.

References encoding_, ACE_OS::fclose(), infile_, and peek_.

Referenced by ~ACEXML_FileCharStream().

00114 {
00115   if (this->infile_ != 0)
00116     {
00117       ACE_OS::fclose (this->infile_);
00118       this->infile_ = 0;
00119     }
00120   delete[] this->filename_;
00121   this->filename_ = 0;
00122   delete[] this->encoding_;
00123   this->encoding_ = 0;
00124   this->size_ = 0;
00125   this->peek_ = 0;
00126   return 0;
00127 }

int ACEXML_FileCharStream::determine_encoding void   )  [virtual]
 

Determine the encoding of the file.

Definition at line 46 of file FileCharStream.cpp.

References ACEXML_Char, encoding_, ACEXML_Encoding::get_encoding(), getchar_i(), infile_, ACE_OS::rewind(), and ACE::strnew().

Referenced by open(), and rewind().

00047 {
00048   if (this->infile_ == 0)
00049     return -1;
00050 
00051   char input[4];
00052   int retval = 0;
00053   int i = 0;
00054   for (; i < 4 && retval != -1; ++i)
00055     retval = this->getchar_i(input[i]);
00056   if (i < 4)
00057     return -1;
00058 
00059   // Rewind the stream
00060   ACE_OS::rewind (this->infile_);
00061 
00062   const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
00063   if (!temp)
00064     return -1;
00065   else
00066     {
00067       if (this->encoding_)
00068         delete [] this->encoding_;
00069       this->encoding_ = ACE::strnew (temp);
00070 //       ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("File's encoding is %s\n"),
00071 //                   this->encoding_));
00072     }
00073   // Move over the byte-order-mark if present.
00074   char ch;
00075   for (int j = 0; j < 3; ++j)
00076     {
00077       if (this->getchar_i (ch) < 0)
00078         return -1;
00079       if (ch == '\xFF' || ch == '\xFE' || ch == '\xEF' || ch == '\xBB' ||
00080           ch == '\xBF')
00081         continue;
00082       else
00083         {
00084           ungetc (ch, this->infile_);
00085           break;
00086         }
00087     }
00088   return 0;
00089 }

int ACEXML_FileCharStream::get ACEXML_Char ch  )  [virtual]
 

Read the next ACEXML_Char. Return -1 if we are not able to return an ACEXML_Char, 0 if succees.

Implements ACEXML_CharStream.

Definition at line 148 of file FileCharStream.cpp.

References ACEXML_Char, ACE_OS::fgetc(), and infile_.

00149 {
00150   if (this->infile_ == 0)
00151     return -1;
00152 #if defined (ACE_USES_WCHAR)
00153   return this->get_i (ch);
00154 #else
00155   ch = (ACEXML_Char) ACE_OS::fgetc (this->infile_);
00156   return (feof(this->infile_) ? -1 : 0);
00157 #endif /* ACE_USES_WCHAR */
00158 }

int ACEXML_FileCharStream::getchar_i char &  ch  )  [private]
 

Read the next character as a normal character. Return -1 if EOF is reached, else return 0.

Definition at line 131 of file FileCharStream.cpp.

References ACE_OS::fgetc().

Referenced by determine_encoding().

00132 {
00133   ch = static_cast<char> (ACE_OS::fgetc (this->infile_));
00134   return (feof(this->infile_) ? -1 : 0);
00135 }

const ACEXML_Char * ACEXML_FileCharStream::getEncoding void   )  [virtual]
 

Implements ACEXML_CharStream.

Definition at line 246 of file FileCharStream.cpp.

References encoding_.

00247 {
00248   return this->encoding_;
00249 }

const ACEXML_Char * ACEXML_FileCharStream::getSystemId void   )  [virtual]
 

Implements ACEXML_CharStream.

Definition at line 252 of file FileCharStream.cpp.

00253 {
00254   return this->filename_;
00255 }

int ACEXML_FileCharStream::open const ACEXML_Char name  ) 
 

Open a file.

Definition at line 24 of file FileCharStream.cpp.

References ACE_stat, ACE_TEXT, ACEXML_Char, determine_encoding(), encoding_, ACE_OS::fopen(), infile_, ACE_OS::stat(), and ACE::strnew().

Referenced by ACEXML_StreamFactory::create_stream().

00025 {
00026   delete[] this->filename_;
00027   this->filename_ = 0;
00028 
00029   delete[] this->encoding_;
00030   this->encoding_ = 0;
00031 
00032   this->infile_ = ACE_OS::fopen (name, ACE_TEXT ("r"));
00033   if (this->infile_ == 0)
00034     return -1;
00035 
00036   ACE_stat statbuf;
00037   if (ACE_OS::stat (name, &statbuf) < 0)
00038     return -1;
00039 
00040   this->size_ = statbuf.st_size;
00041   this->filename_ = ACE::strnew (name);
00042   return this->determine_encoding();
00043 }

int ACEXML_FileCharStream::peek void   )  [virtual]
 

Peek the next ACEXML_Char in the CharStream. Return the character if success, -1 if EOF is reached.

Implements ACEXML_CharStream.

Definition at line 161 of file FileCharStream.cpp.

References ACEXML_Char, ACE_OS::fgetc(), and infile_.

00162 {
00163   if (this->infile_ == 0)
00164     return -1;
00165 #if defined (ACE_USES_WCHAR)
00166   return this->peek_i();
00167 #else
00168 
00169   ACEXML_Char ch = static_cast<ACEXML_Char> (ACE_OS::fgetc (this->infile_));
00170   ::ungetc (ch, this->infile_);
00171   return ch;
00172 #endif /* ACE_USES_WCHAR */
00173 }

int ACEXML_FileCharStream::read ACEXML_Char str,
size_t  len
[virtual]
 

Read the next batch of ACEXML_Char strings

Implements ACEXML_CharStream.

Definition at line 138 of file FileCharStream.cpp.

References ACEXML_Char, ACE_OS::fread(), and infile_.

00140 {
00141   if (this->infile_ == 0)
00142     return -1;
00143 
00144   return static_cast<int> (ACE_OS::fread (str, sizeof (ACEXML_Char), len, this->infile_));
00145 }

void ACEXML_FileCharStream::rewind void   )  [virtual]
 

Resets the file pointer to the beginning of the stream.

Implements ACEXML_CharStream.

Definition at line 92 of file FileCharStream.cpp.

References determine_encoding(), infile_, and ACE_OS::rewind().

00093 {
00094   if (this->infile_ == 0)
00095     return;
00096   ACE_OS::rewind (this->infile_);
00097   this->determine_encoding();
00098 }


Member Data Documentation

ACEXML_Char* ACEXML_FileCharStream::encoding_ [private]
 

Definition at line 117 of file FileCharStream.h.

Referenced by close(), determine_encoding(), getEncoding(), and open().

ACEXML_Char* ACEXML_FileCharStream::filename_ [private]
 

Definition at line 116 of file FileCharStream.h.

FILE* ACEXML_FileCharStream::infile_ [private]
 

Definition at line 119 of file FileCharStream.h.

Referenced by available(), close(), determine_encoding(), get(), open(), peek(), read(), and rewind().

ACEXML_Char ACEXML_FileCharStream::peek_ [private]
 

Definition at line 124 of file FileCharStream.h.

Referenced by close().

off_t ACEXML_FileCharStream::size_ [private]
 

Definition at line 118 of file FileCharStream.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:47:56 2006 for ACEXML by doxygen 1.3.6