summaryrefslogtreecommitdiff
path: root/ACEXML/common/StrCharStream.cpp
blob: 9bfcc95072c64ad9fb210bb1e0ad5ca920e171f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// $Id$

#include "ACEXML/common/StrCharStream.h"
#include "ACEXML/common/Encoding.h"
#include "ace/ACE.h"

ACEXML_StrCharStream::ACEXML_StrCharStream (void)
  : start_ (0), ptr_ (0), end_ (0), encoding_ (0)
{
}

ACEXML_StrCharStream::ACEXML_StrCharStream (const ACEXML_Char *str)
  : start_ (0), ptr_ (0), end_ (0), encoding_ (0)
{
  this->open (str);
}


ACEXML_StrCharStream::~ACEXML_StrCharStream (void)
{
  this->close();
}

int
ACEXML_StrCharStream::open (const ACEXML_Char *str)
{
  delete[] this->start_;
  delete[] this->encoding_;

  if (str != 0 && (this->start_ = ACE::strnew (str)) != 0)
    {
      this->ptr_ = this->start_;
      this->end_ = this->start_ + ACE_OS_String::strlen (this->start_);
      if (this->determine_encoding() == -1)
        return -1;
      return 0;
    }

  this->start_ = this->ptr_ = this->end_ = 0;
  return -1;                // Invalid string passed.
}

int
ACEXML_StrCharStream::available (void)
{
  if (this->start_ != 0)
    return (this->end_ - this->start_); // @@ Will this work on all platforms?
  return -1;
}

int
ACEXML_StrCharStream::close (void)
{
  delete[] this->start_;
  delete[] this->encoding_;
  this->start_ = this->ptr_ = this->end_ = 0;
  return 0;
}

int
ACEXML_StrCharStream::determine_encoding (void)
{
  char input[4];
  int retval = 0;
  char* sptr  = (char*)this->start_;
  int i = 0;
  for ( ; i < 4 && sptr != (char*)this->end_; ++sptr, ++i)
    {
      retval = input[i] = *sptr;
    }
  if (i < 4)
    return -1;
  const ACEXML_Char* temp = ACEXML_Encoding::get_encoding (input);
  if (!temp)
    return -1;
  else
    {
      this->encoding_ = ACE::strnew (temp);
      // ACE_DEBUG ((LM_DEBUG, "String's encoding is %s\n", this->encoding_));
    }
  return 0;
}

void
ACEXML_StrCharStream::rewind (void)
{
  this->ptr_ = this->start_;
}

int
ACEXML_StrCharStream::get (ACEXML_Char& ch)
{
  if (this->start_ != 0 && this->ptr_ != this->end_)
    {
      ch = *this->ptr_++;
      return 0;
    }
  return -1;
}

int
ACEXML_StrCharStream::read (ACEXML_Char *str, size_t len)
{
  if (this->start_ != 0 &&
      this->ptr_ != this->end_)
    {
      if (len * sizeof (ACEXML_Char) > (size_t) (this->end_ - this->ptr_))
        len = this->end_ - this->ptr_;
      ACE_OS_String::strncpy (str, this->ptr_, len);
      this->ptr_ += len;
      return len;
    }
  return 0;
}

int
ACEXML_StrCharStream::peek (void)
{
  if (this->start_ != 0 && this->ptr_ != this->end_)
    return *this->ptr_;
  return -1;
}

const ACEXML_Char*
ACEXML_StrCharStream::getEncoding (void)
{
  return this->encoding_;
}