Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / ACEXML / common / StrCharStream.cpp
blob339bf4bd8feb91be837f1c1691d4185b8114bbca
1 #include "ACEXML/common/StrCharStream.h"
2 #include "ACEXML/common/Encoding.h"
3 #include "ace/ACE.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_string.h"
7 ACEXML_StrCharStream::ACEXML_StrCharStream ()
8 : start_ (0), ptr_ (0), end_ (0), encoding_ (0), name_ (0)
13 ACEXML_StrCharStream::~ACEXML_StrCharStream ()
15 this->close();
18 int
19 ACEXML_StrCharStream::open (const ACEXML_Char *str, const ACEXML_Char* name)
21 if (str != 0 && name != 0)
23 delete [] this->start_;
24 if ((this->start_ = ACE::strnew (str)) == 0)
25 return -1;
26 delete [] this->name_;
27 if ((this->name_ = ACE::strnew (name)) == 0)
28 return -1;
29 this->ptr_ = this->start_;
30 this->end_ = this->start_ + ACE_OS::strlen (this->start_);
31 return this->determine_encoding();
33 return -1; // Invalid string passed.
36 int
37 ACEXML_StrCharStream::available ()
39 if (this->start_ != 0)
40 return static_cast<int> (this->end_ - this->start_); // @@ Will this work on all platforms?
41 return -1;
44 int
45 ACEXML_StrCharStream::close ()
47 delete[] this->start_;
48 delete[] this->encoding_;
49 this->encoding_ = 0;
50 delete[] this->name_;
51 this->name_ = 0;
52 this->start_ = this->ptr_ = this->end_ = 0;
53 return 0;
56 int
57 ACEXML_StrCharStream::determine_encoding ()
59 if (this->start_ == 0)
60 return -1;
61 char input[4] = {0,0,0,0};
62 char* sptr = (char*)this->start_;
63 int i = 0;
64 for ( ; i < 4 && sptr != (char*)this->end_; ++sptr, ++i)
65 input[i] = *sptr;
66 const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
67 if (!temp)
68 return -1;
69 else
71 delete [] this->encoding_;
72 this->encoding_ = ACE::strnew (temp);
73 // ACE_DEBUG ((LM_DEBUG, "String's encoding is %s\n", this->encoding_));
75 return 0;
78 void
79 ACEXML_StrCharStream::rewind ()
81 this->ptr_ = this->start_;
82 this->determine_encoding();
85 int
86 ACEXML_StrCharStream::get (ACEXML_Char& ch)
88 if (this->start_ != 0 && this->ptr_ != this->end_)
90 ch = *this->ptr_++;
91 return 0;
93 return -1;
96 int
97 ACEXML_StrCharStream::read (ACEXML_Char *str, size_t len)
99 if (this->start_ != 0 &&
100 this->ptr_ != this->end_)
102 if (len * sizeof (ACEXML_Char) > (size_t) (this->end_ - this->ptr_))
103 len = this->end_ - this->ptr_;
104 ACE_OS::strncpy (str, this->ptr_, len);
105 this->ptr_ += len;
106 return static_cast<int> (len);
108 return 0;
112 ACEXML_StrCharStream::peek ()
114 if (this->start_ != 0 && this->ptr_ != this->end_)
115 return *this->ptr_;
116 return -1;
119 const ACEXML_Char*
120 ACEXML_StrCharStream::getEncoding ()
122 return this->encoding_;
125 const ACEXML_Char*
126 ACEXML_StrCharStream::getSystemId()
128 return this->name_;