00001
00002
00003 #include "ACEXML/common/StrCharStream.h"
00004 #include "ACEXML/common/Encoding.h"
00005 #include "ace/ACE.h"
00006 #include "ace/Log_Msg.h"
00007 #include "ace/OS_NS_string.h"
00008
00009 ACEXML_StrCharStream::ACEXML_StrCharStream (void)
00010 : start_ (0), ptr_ (0), end_ (0), encoding_ (0), name_ (0)
00011 {
00012 }
00013
00014
00015 ACEXML_StrCharStream::~ACEXML_StrCharStream (void)
00016 {
00017 this->close();
00018 }
00019
00020 int
00021 ACEXML_StrCharStream::open (const ACEXML_Char *str, const ACEXML_Char* name)
00022 {
00023
00024 if (str != 0 && name != 0)
00025 {
00026 delete [] this->start_;
00027 if ((this->start_ = ACE::strnew (str)) == 0)
00028 return -1;
00029 delete [] this->name_;
00030 if ((this->name_ = ACE::strnew (name)) == 0)
00031 return -1;
00032 this->ptr_ = this->start_;
00033 this->end_ = this->start_ + ACE_OS::strlen (this->start_);
00034 return this->determine_encoding();
00035 }
00036 return -1;
00037 }
00038
00039 int
00040 ACEXML_StrCharStream::available (void)
00041 {
00042 if (this->start_ != 0)
00043 return static_cast<int> (this->end_ - this->start_);
00044 return -1;
00045 }
00046
00047 int
00048 ACEXML_StrCharStream::close (void)
00049 {
00050 delete[] this->start_;
00051 delete[] this->encoding_;
00052 this->encoding_ = 0;
00053 delete[] this->name_;
00054 this->name_ = 0;
00055 this->start_ = this->ptr_ = this->end_ = 0;
00056 return 0;
00057 }
00058
00059 int
00060 ACEXML_StrCharStream::determine_encoding (void)
00061 {
00062 if (this->start_ == 0)
00063 return -1;
00064 char input[4] = {0,0,0,0};
00065 char* sptr = (char*)this->start_;
00066 int i = 0;
00067 for ( ; i < 4 && sptr != (char*)this->end_; ++sptr, ++i)
00068 input[i] = *sptr;
00069 const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
00070 if (!temp)
00071 return -1;
00072 else
00073 {
00074 delete [] this->encoding_;
00075 this->encoding_ = ACE::strnew (temp);
00076
00077 }
00078 return 0;
00079 }
00080
00081 void
00082 ACEXML_StrCharStream::rewind (void)
00083 {
00084 this->ptr_ = this->start_;
00085 this->determine_encoding();
00086 }
00087
00088 int
00089 ACEXML_StrCharStream::get (ACEXML_Char& ch)
00090 {
00091 if (this->start_ != 0 && this->ptr_ != this->end_)
00092 {
00093 ch = *this->ptr_++;
00094 return 0;
00095 }
00096 return -1;
00097 }
00098
00099 int
00100 ACEXML_StrCharStream::read (ACEXML_Char *str, size_t len)
00101 {
00102 if (this->start_ != 0 &&
00103 this->ptr_ != this->end_)
00104 {
00105 if (len * sizeof (ACEXML_Char) > (size_t) (this->end_ - this->ptr_))
00106 len = this->end_ - this->ptr_;
00107 ACE_OS::strncpy (str, this->ptr_, len);
00108 this->ptr_ += len;
00109 return static_cast<int> (len);
00110 }
00111 return 0;
00112 }
00113
00114 int
00115 ACEXML_StrCharStream::peek (void)
00116 {
00117 if (this->start_ != 0 && this->ptr_ != this->end_)
00118 return *this->ptr_;
00119 return -1;
00120 }
00121
00122 const ACEXML_Char*
00123 ACEXML_StrCharStream::getEncoding (void)
00124 {
00125 return this->encoding_;
00126 }
00127
00128 const ACEXML_Char*
00129 ACEXML_StrCharStream::getSystemId(void)
00130 {
00131 return this->name_;
00132 }