• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 // -*- c++ -*-
19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
20 
21 //               P V S T R I N G   C L A S S
22 
23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
24 
25 // - - Inclusion - - - - - - - - - - - - - - - - - - - - - - - - - - - -
26 
27 #include "pv_string.h"
28 #include "oscl_mem.h"
29 
30 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31 
String()32 String::String()
33 {
34     rep = OSCL_NEW(Srep, (0, ""));
35 }
36 
String(const String & src)37 String::String(const String& src)
38 {
39     src.rep->mutex.Lock();
40     src.rep->refcnt++;
41     rep = src.rep;
42     src.rep->mutex.Unlock();
43 }
44 
~String()45 String::~String()
46 {
47     rep->mutex.Lock();
48     if (--rep->refcnt == 0)
49     {
50         OSCL_DELETE(rep);
51     }
52     else rep->mutex.Unlock();
53 }
54 
operator =(const String & src)55 String& String::operator=(const String & src)
56 {
57 
58     if (rep == src.rep)
59     {
60         return *this;  // protect against "str = str"
61     }
62     src.rep->mutex.Lock();
63     src.rep->refcnt++;
64     rep->mutex.Lock();
65     if (--rep->refcnt == 0)
66     {
67         OSCL_DELETE(rep);
68     }
69     else
70     {
71         rep->mutex.Unlock();
72     }
73 
74     rep = src.rep;
75     src.rep->mutex.Unlock();
76     return *this;
77 
78 }
79 
operator +=(const char * src)80 String& String::operator+=(const char * src)
81 {
82     Srep *new_rep;
83     int new_size = rep->size + oscl_strlen(src);
84     new_rep = OSCL_NEW(Srep, (new_size, rep->buffer));
85     oscl_strcat(new_rep->buffer, src);
86     rep->mutex.Lock();
87     if (--rep->refcnt == 0)
88     {
89         OSCL_DELETE(rep);
90     }
91     else
92     {
93         rep->mutex.Unlock();
94     }
95     rep = new_rep;
96     return *this;
97 }
98 
operator +=(const String & src)99 String& String::operator+=(const String & src)
100 {
101     Srep *new_rep;
102     int new_size = rep->size + src.rep->size;
103     new_rep = OSCL_NEW(Srep, (new_size, rep->buffer));
104 
105     oscl_strcat(new_rep->buffer, src.rep->buffer);
106     rep->mutex.Lock();
107     if (--rep->refcnt == 0)
108     {
109         OSCL_DELETE(rep);
110     }
111     else
112     {
113         rep->mutex.Unlock();
114     }
115     rep = new_rep;
116     return *this;
117 }
118 
operator +=(const char c)119 String & String::operator+=(const char c)
120 {
121     char tmp_str[2];
122     tmp_str[0] = c;
123     tmp_str[1] = '\0';
124 
125     return ((*this) += tmp_str);
126 }
127 
128 
129 
String(const char * src)130 String::String(const char *src)
131 {
132     rep = OSCL_NEW(Srep, (oscl_strlen(src), src));
133 }
134 
String(const char * src,int length)135 String::String(const char *src, int length)
136 {
137     rep = OSCL_NEW(Srep, (length, src));
138 }
139 
140 
operator =(const char * src)141 String& String::operator=(const char * src)
142 {
143     rep->mutex.Lock();
144     if (rep->refcnt == 1)
145     {
146         rep->assign(oscl_strlen(src), src);
147         rep->mutex.Unlock();
148     }
149     else
150     {
151         rep->refcnt--;
152         rep->mutex.Unlock();
153         rep = OSCL_NEW(Srep, (oscl_strlen(src), src));
154     }
155 
156     return *this;
157 }
158 
159 
160 
operator [](int index) const161 char String::operator[](int index) const
162 {
163     if (index < 0 || index >= rep->size)
164         return '\0';
165     return rep->buffer[index];
166 }
167 
168 
hash() const169 char String::hash() const
170 {
171     unsigned long h = 0;
172     char uc = 0;
173     int ii;
174     char *ptr;
175 
176     for (ii = 0, ptr = rep->buffer ; ii < rep->size; ++ii, ++ptr)
177     {
178         h = 5 * h + *ptr;
179     }
180 
181     for (ii = 0; ii < 4; ++ii)
182     {
183         uc ^= h & 0xFF;
184         h >>= 8;
185     }
186     return uc;
187 }
188