xref: /aoo41x/main/soltools/inc/simstr.hxx (revision 38268e38)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SOLTOOLS_SIMSTR_HXX
25 #define SOLTOOLS_SIMSTR_HXX
26 
27 
28 class  Simstr   /// Simple string class.
29 {
30 // INTERFACE
31   public:
32     // LIFECYCLE
33                         Simstr(
34                            const char *         str = 0);
35                         Simstr(                 /** Creates Simstr out of a copy of the first
36                                                     'nrOfBytes' bytes of 'anyBytes'.
37                                                     Adds a '\0' at the end.  */
38                            const char *         anybytes,
39                            int                  nrOfBytes);
40 					    Simstr(                 /** Creates Simstr out of a copy of the described bytes within 'anyBytes'.
41                                                     Adds a '\0' at the end.  */
42                            const char *         anybytes,
43                            int			        firstBytesPos,
44                            int                  nrOfBytes );
45                         Simstr(                 /// Creates Simstr of 'anzahl' times 'c'.
46                            char                 c,
47                            int                  anzahl);
48                         Simstr(
49                            const Simstr &       S);
50                         ~Simstr();
51 
52 
53     // OPERATORS
54                         operator const char*() const;
55 
56     Simstr &            operator=(
57                            const Simstr &       S );
58 
59     Simstr              operator+(
60                            const Simstr &       S ) const;
61     Simstr &            operator+=(
62                            const Simstr &       S );
63     Simstr &            operator+=(
64                            const char *         s );
65 
66     bool                operator==(
67                            const Simstr &       S ) const;
68     bool                operator!=(
69                            const Simstr &       S ) const;
70     bool                operator<(
71                            const Simstr &       S ) const;
72     bool                operator>(
73                            const Simstr &       S ) const;
74     bool                operator<=(
75                            const Simstr &       S ) const;
76     bool                operator>=(
77 						   const Simstr &       S ) const;
78     // INFO
79     static const Simstr &
80                         null_();
81 
82     const char *        str() const;
83     int                 l() const;                    // Length of string without '\0' at end.
84     char *              s();   // ATTENTION !!!       // Only to be used, when a function needs a 'char*' but
85 												      //   nevertheless THAT WILL BE NOT CHANGED!
86                                                       //   Typecasts to 'const char*' are performed automatically.
87     char                get(
88                             int                 n) const;
89 	char                get_front() const;
90     char                get_back() const;
91     Simstr              get(
92                             int                 startPos,
93 						    int                 anzahl ) const;
94     Simstr              get_front(
95                             int                 anzahl ) const;
96 	Simstr              get_back(
97                             int                 anzahl ) const;
98 
99     int                 pos_first(
100                             char                c ) const;
101 	int                 pos_first_after(
102 						    char                c,
103                             int                 startSearchPos ) const;
104     int                 pos_last(
105                             char                c ) const;
106 	int                 pos_first(
107                             const Simstr &      S ) const;
108 	int                 pos_last(
109                             const Simstr &      S ) const;
110 	int                 count(
111 	  					   char                 c ) const;
112 	bool                is_empty() const;                         // Only true if object == "".
113 	bool                is_no_text() const;                       // String may contain spaces or tabs.
114 
115     Simstr              get_first_token(
116                             char                c ) const;
117     Simstr              get_last_token(
118 						   char                 c ) const;
119 
120     // ACCESS
121     char &              ch(                     /** Reference to sz[n]. Allows change of this char.
122                                                     !!! No safety, if n is out of the allowed range! */
123                             int                 n );
124 
125     // OPERATIONS
126     void                insert(
127                             int                 pos,
128                             char                c );
129     void                push_front(
130                             char                c );
131     void                push_back(
132                             char                c );
133 	void                insert(
134                             int                 pos,
135                             const Simstr &      S );
136 	void                push_front(
137                             const Simstr &      S );
138 	void                push_back(
139                             const Simstr &      S );
140 
141 	void                remove(
142 	  					    int                 pos,
143 						    int                 anzahl = 1 );
144 	void			    remove_trailing_blanks();
145 	void                pop_front(
146 						    int                 anzahl = 1 );
147     void                pop_back(
148 						    int                 anzahl = 1 );
149     void                rem_back_from(
150 						    int                 removeStartPos );
151     void                remove_all(
152                             char                c );
153 	void                remove_all(                             // Starts search left.
154                             const Simstr &      S );
155     void                strip(
156                             char                c );                // Removes all characters == c from front and back.
157                                                              //   c == ' ' removes also TABs !!!
158     void                empty();                                // Changes object to the value "".
159 
160     void                replace(
161                             int                 pos,
162                             char                c );
163     void                replace(
164                             int                 startPos,
165                             int                 anzahl,
166 						    const Simstr &      S );
167     void                replace_all(
168                             char                oldCh,
169 						    char                newCh );
170 	void                replace_all(
171                             const Simstr &      oldS,
172                             const Simstr &      newS );
173     void                to_lower();
174 
175 	Simstr              take_first_token(       /// Token is removed from the Simstr.
176                            char                 c );
177 	Simstr              take_last_token(        /// Token is removed from the Simstr.
178                            char                 c );
179   private:
180     // DATA
181     char *              sz;
182     int                 len;
183 };
184 
185 // Simstr - char* / char - concatenations
186 Simstr operator+(const char * str, const Simstr & S);
187 Simstr operator+(const Simstr & S, const char * str);
188 Simstr operator+(char c, const Simstr & S);
189 Simstr operator+(const Simstr & S, char c);
190 
191 // Simstr - char* - comparison operators
192 bool  operator==(const Simstr & S, const char * str);
193 bool  operator!=(const Simstr & S, const char * str);
194 bool  operator<(const Simstr & S, const char * str);
195 bool  operator>(const Simstr & S, const char * str);
196 bool  operator<=(const Simstr & S, const char * str);
197 bool  operator>=(const Simstr & S, const char * str);
198 bool  operator==(const char * str, const Simstr & S);
199 bool  operator!=(const char * str, const Simstr & S);
200 bool  operator<(const char * str, const Simstr & S);
201 bool  operator>(const char * str, const Simstr & S);
202 bool  operator<=(const char * str, const Simstr & S);
203 bool  operator>=(const char * str, const Simstr & S);
204 
205 
206 inline const char *
str() const207 Simstr::str() const                    { return sz; }
208 inline char *
s()209 Simstr::s()                            { return sz; }
210 inline int
l() const211 Simstr::l() const                      { return len; }
212 inline
213 Simstr::operator const char*() const   { return sz; }
214 inline bool
is_empty() const215 Simstr::is_empty() const               { return len == 0; }
216 
217 
218 #endif
219 
220