File: ascstring.cpp

package info (click to toggle)
asc 2.1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 59,052 kB
  • ctags: 25,676
  • sloc: cpp: 145,189; sh: 8,705; ansic: 5,564; makefile: 551; perl: 150
file content (230 lines) | stat: -rw-r--r-- 5,202 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

#include "ascstring.h"

/*! \class ASCString ASCString.h

    \brief The ASCString class provides an abstract way to manipulate strings.

    Depending on the prepocessor definition _UNICODE, ASCString will use Unicode text or C-null 
    terminated char array.

    \warning
    Be extremely carefull if you have to modify this class. No virtual destructor is provided. 
    This may result in memory leaks if you modify this class to free dynamically allocated memory
    in its destructor.
    The same warning applies to classes deriving from ASCString ( if any ).

    \code

    ASCString* pStr = new ASCString( "My string" );

    ASCInheritedString* pInherited = ( ASCInheritedString* ) pStr;

    // pStr's destructor will not be called when deleting pInherited. 
    // If ASCString has been modified to free memory in its destructor, 
    // this memory will never be freed up.
    delete pInherited;      

    \endcode
*/

/*!
    Convert this ASCString to lowercase.

    \return returns a reference on this ASCString.
*/
ASCString& ASCString::toLower ( )
{
//    auto_ptr< charT > l_autopBuf ( new charT [ length () + sizeof ( charT ) ] );
//    charT* l_pBuf = l_autopBuf.get();
    charT* l_pBuf = new charT [ length () + sizeof ( charT ) ];

    ASCStringHelpers::_Strcpy ( l_pBuf, c_str () );
    ASCStringHelpers::_Strlwr ( l_pBuf );
    assign  ( l_pBuf );

    delete[] l_pBuf;
    return *this;
}

/*!
    Convert this ASCString to uppercase.

    \return returns a reference on this ASCString.
*/
ASCString& ASCString::toUpper ( )
{
//    auto_ptr< charT > l_autopBuf ( new charT [ length () + sizeof ( charT ) ] );
    charT* l_pBuf = new charT [ length () + sizeof ( charT ) ];

    ASCStringHelpers::_Strcpy ( l_pBuf, c_str () );
    ASCStringHelpers::_Strupr ( l_pBuf );
    assign  ( l_pBuf );

    delete[] l_pBuf;
    return *this;
}

/*!
    Format this ASCString as sprintf does.

    \param pFormat a format-control string.
    \param ... Optional arguments

    \return returns a reference on this ASCString.

    See standard system documentation for more information on \e sprintf.
*/
ASCString& ASCString::format ( const charT* pFormat, ... )
{
    std::va_list arg_ptr;
    va_start ( arg_ptr, pFormat );

    vaformat( pFormat, arg_ptr );

    va_end ( arg_ptr );

    return *this;
}


ASCString&  ASCString::vaformat     ( const charT* pFormat, va_list ap )
{
    int  l_iNbChar = 10000;
    bool l_bIsDone = false;

    while ( l_bIsDone == false )
    {
        charT* l_pBuf = new charT [ l_iNbChar ];

        int l_iNbCharWritten = ASCStringHelpers::_Vsnprintf ( l_pBuf, l_iNbChar, pFormat, ap );

        if ( l_iNbCharWritten != -1 )
        {
            // ok, l_pBuf was large enough to hold the whole formated string
            assign ( l_pBuf );
            l_bIsDone = true;
        }
        else
        {
            // l_pBuf is not large enough to hold the whole formated string.
            // Double the number of characters l_pBuf can hold and retry 
            // to format the string.
            l_iNbChar *= 2;
        }

        delete [] l_pBuf;
    };
    return *this;
}


/*!
    Print this ASCString to the standard output stream.

    \note this function is provided for convenience. It is equivalent to :

    \code
    
    ASCString strFoo ( "foo" );

    printf ( "%s", strFoo.c_str () );

    \endcode

    See standard system documentation for more information on \e printf.
*/
void ASCString::printf ( )
{
    ASCStringHelpers::_Printf ( c_str () );
}


/**
   Checks if the last characters of string are equal to s
*/    
bool ASCString::endswith( const ASCString& s ) const
{
   size_type p =  rfind( s );
   if ( p != npos ) 
      return p == length() - s.length();
   else   
      return false;
}



/*!
    Duplicate and convert to lowercase.

    \param String a const reference to an ASCString object which will be duplicated and converted to lowercase.

    \return returns an ASCString object that contains a lowercased copy of \a String.

    \relates ASCString

*/
ASCString copytoLower ( const ASCString& String )
{
    ASCString l_TempString ( String );
    l_TempString.toLower ();

    return l_TempString;
}

/*!
    Duplicate and convert to uppercase.

    \param String a const reference to an ASCString object which will be duplicated and converted to uppercase.

    \return returns an ASCString object that contains an uppercased copy of \a String.

    \relates ASCString

*/
ASCString copytoUpper ( const ASCString& String )
{
    ASCString l_TempString ( String );
    l_TempString.toUpper ();

    return l_TempString;
}

ASCString ASCString::toString(int i )
{
   ASCString s;
   s.format("%d",i);
   return s;
}

#ifdef SIZE_T_not_identical_to_INT
ASCString ASCString::toString( size_t i )
{
   ASCString s;
   s.format("%d",i);
   return s;
}
#endif 


ASCString ASCString::toString(double d )
{
   ASCString s;
   s.format("%f",d);
   return s;
}

const ASCString operator+ ( const ASCString& s1, const ASCString& s2 )
{
   ASCString s = s1;
   s += s2;
   return s;
}

const ASCString operator+ ( const char* s1, const ASCString& s2 )
{
   ASCString s = s1;
   s += s2;
   return s;
}