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 && (this->start_ = ACE::strnew (str)) != 0
00026 && (this->name_ = ACE::strnew (name)) != 0)
00027 {
00028 this->ptr_ = this->start_;
00029 this->end_ = this->start_ + ACE_OS::strlen (this->start_);
00030 return this->determine_encoding();
00031 }
00032 return -1;
00033 }
00034
00035 int
00036 ACEXML_StrCharStream::available (void)
00037 {
00038 if (this->start_ != 0)
00039 return (this->end_ - this->start_);
00040 return -1;
00041 }
00042
00043 int
00044 ACEXML_StrCharStream::close (void)
00045 {
00046 delete[] this->start_;
00047 delete[] this->encoding_;
00048 this->encoding_ = 0;
00049 delete[] this->name_;
00050 this->name_ = 0;
00051 this->start_ = this->ptr_ = this->end_ = 0;
00052 return 0;
00053 }
00054
00055 int
00056 ACEXML_StrCharStream::determine_encoding (void)
00057 {
00058 if (this->start_ == 0)
00059 return -1;
00060 char input[4] = {0,0,0,0};
00061 char* sptr = (char*)this->start_;
00062 int i = 0;
00063 for ( ; i < 4 && sptr != (char*)this->end_; ++sptr, ++i)
00064 input[i] = *sptr;
00065 const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
00066 if (!temp)
00067 return -1;
00068 else
00069 {
00070 if (this->encoding_)
00071 delete [] this->encoding_;
00072 this->encoding_ = ACE::strnew (temp);
00073
00074 }
00075 return 0;
00076 }
00077
00078 void
00079 ACEXML_StrCharStream::rewind (void)
00080 {
00081 this->ptr_ = this->start_;
00082 this->determine_encoding();
00083 }
00084
00085 int
00086 ACEXML_StrCharStream::get (ACEXML_Char& ch)
00087 {
00088 if (this->start_ != 0 && this->ptr_ != this->end_)
00089 {
00090 ch = *this->ptr_++;
00091 return 0;
00092 }
00093 return -1;
00094 }
00095
00096 int
00097 ACEXML_StrCharStream::read (ACEXML_Char *str, size_t len)
00098 {
00099 if (this->start_ != 0 &&
00100 this->ptr_ != this->end_)
00101 {
00102 if (len * sizeof (ACEXML_Char) > (size_t) (this->end_ - this->ptr_))
00103 len = this->end_ - this->ptr_;
00104 ACE_OS::strncpy (str, this->ptr_, len);
00105 this->ptr_ += len;
00106 return static_cast<int> (len);
00107 }
00108 return 0;
00109 }
00110
00111 int
00112 ACEXML_StrCharStream::peek (void)
00113 {
00114 if (this->start_ != 0 && this->ptr_ != this->end_)
00115 return *this->ptr_;
00116 return -1;
00117 }
00118
00119 const ACEXML_Char*
00120 ACEXML_StrCharStream::getEncoding (void)
00121 {
00122 return this->encoding_;
00123 }
00124
00125 const ACEXML_Char*
00126 ACEXML_StrCharStream::getSystemId(void)
00127 {
00128 return this->name_;
00129 }