Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

StrCharStream.cpp

Go to the documentation of this file.
00001 // $Id: StrCharStream.cpp,v 1.1.1.1.2.2 2003/04/21 19:14:54 chad Exp $
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 
00008 ACEXML_StrCharStream::ACEXML_StrCharStream (void)
00009   : start_ (0), ptr_ (0), end_ (0), encoding_ (0)
00010 {
00011 }
00012 
00013 ACEXML_StrCharStream::ACEXML_StrCharStream (const ACEXML_Char *str)
00014   : start_ (0), ptr_ (0), end_ (0), encoding_ (0)
00015 {
00016   this->open (str);
00017 }
00018 
00019 
00020 ACEXML_StrCharStream::~ACEXML_StrCharStream (void)
00021 {
00022   this->close();
00023 }
00024 
00025 int
00026 ACEXML_StrCharStream::open (const ACEXML_Char *str)
00027 {
00028   delete[] this->start_;
00029   delete[] this->encoding_;
00030 
00031   if (str != 0 && (this->start_ = ACE::strnew (str)) != 0)
00032     {
00033       this->ptr_ = this->start_;
00034       this->end_ = this->start_ + ACE_OS_String::strlen (this->start_);
00035       this->determine_encoding();
00036       return 0;
00037     }
00038 
00039   this->start_ = this->ptr_ = this->end_ = 0;
00040   return -1;                // Invalid string passed.
00041 }
00042 
00043 int
00044 ACEXML_StrCharStream::available (void)
00045 {
00046   if (this->start_ != 0)
00047     return (this->end_ - this->start_); // @@ Will this work on all platforms?
00048   return -1;
00049 }
00050 
00051 int
00052 ACEXML_StrCharStream::close (void)
00053 {
00054   delete[] this->start_;
00055   delete[] this->encoding_;
00056   this->start_ = this->ptr_ = this->end_ = 0;
00057   return 0;
00058 }
00059 
00060 int
00061 ACEXML_StrCharStream::determine_encoding (void)
00062 {
00063   char input[4];
00064   char* sptr  = (char*)this->start_;
00065   int i = 0;
00066   for ( ; i < 4 && sptr != (char*)this->end_; ++sptr, ++i)
00067     {
00068       input[i] = *sptr;
00069     }
00070   if (i < 4)
00071     return -1;
00072   const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
00073   if (!temp)
00074     return -1;
00075   if (ACE_OS::strcmp (temp,
00076                       ACEXML_Encoding::encoding_names_[ACEXML_Encoding::OTHER]) == 0)
00077     return -1;
00078   else
00079     {
00080       this->encoding_ = ACE::strnew (temp);
00081       ACE_DEBUG ((LM_DEBUG, "String's encoding is %s\n", this->encoding_));
00082     }
00083   return 0;
00084 }
00085 
00086 void
00087 ACEXML_StrCharStream::rewind (void)
00088 {
00089   this->ptr_ = this->start_;
00090 }
00091 
00092 int
00093 ACEXML_StrCharStream::get (ACEXML_Char& ch)
00094 {
00095   if (this->start_ != 0 && this->ptr_ != this->end_)
00096     {
00097       ch = *this->ptr_++;
00098 
00099       return 0;
00100     }
00101 
00102   return -1;
00103 }
00104 
00105 int
00106 ACEXML_StrCharStream::read (ACEXML_Char *str,
00107                             size_t len)
00108 {
00109   if (this->start_ != 0 &&
00110       this->ptr_ != this->end_)
00111     {
00112       if ((int) len > this->end_ - this->ptr_)
00113         len = this->end_ - this->ptr_;
00114 
00115       ACE_OS_String::strncpy (str, this->ptr_, len);
00116       this->ptr_ += len;
00117       return ACE_static_cast(int, len);
00118     }
00119   return 0;
00120 }
00121 
00122 int
00123 ACEXML_StrCharStream::peek (void)
00124 {
00125   if (this->start_ != 0 && this->ptr_ != this->end_)
00126     return *this->ptr_;
00127   return -1;
00128 }
00129 
00130 const ACEXML_Char*
00131 ACEXML_StrCharStream::getEncoding (void)
00132 {
00133   return this->encoding_;
00134 }

Generated on Mon Jun 16 13:23:26 2003 for ACEXML by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002