File: XString.h

package info (click to toggle)
postman 2.0-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,036 kB
  • ctags: 3,078
  • sloc: cpp: 31,131; ansic: 1,948; sh: 787; makefile: 285
file content (136 lines) | stat: -rw-r--r-- 4,877 bytes parent folder | download | duplicates (3)
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
/*
XString.h
*/


#ifndef __XSTRING_H
#define __XSTRING_H

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

typedef int UINT;

class XString {
public:
        // Constructor(s) / Destructors
        //-----------------------------
        XString( const char *Value = "" );
        XString( const XString &Value );
        virtual ~XString() {if ((BufferLen != -1) && (Buffer != NULL)) {delete [] Buffer;}}

        //Operators
        //----------
        const XString & operator = ( const XString &Rhs );
        const XString & operator +=( const XString &Rhs );
        const XString & operator +=( const char );
        int                             operator ==( const XString &Rhs ) const;
        int                             operator !=( const XString &Rhs ) const;
        int                             operator < ( const XString &Rhs ) const;
        int                             operator > ( const XString &Rhs ) const;
        int                             operator <=( const XString &Rhs ) const;
        int                             operator >=( const XString &Rhs ) const;
        char                    operator []( UINT Index ) const;
        char&                   operator []( UINT Index );

        //Methods
        //-------
        //INSPECTORS
        //==========
        char                    charAt( UINT index ) const;
        int                             compareTo( const XString &anotherString ) const;
        const char*             cstr( ) const { return Buffer; }
        int                             endsWith( const XString &suffix ) const;
        int                             equals( const XString &anObject ) const;
        int                             equalsIgnoreCase( const XString &anotherString ) const;
        int                             indexOf( char ch ) const;
        int                             indexOf( char ch, UINT fromIndex ) const;
        int                             indexOf( const XString &str ) const;
        int                             indexOf( const XString &str, UINT fromIndex ) const;
        int                             lastIndexOf( char ch ) const;
        int                             lastIndexOf( char ch, UINT fromIndex ) const;
        int                             lastIndexOf( const XString &str ) const;
        int                             lastIndexOf( const XString &str, UINT fromIndex ) const;
        const UINT              length( ) const { return Length; }
        int                            startsWith( const XString &prefix ) const;
        int                             startsWith( const XString &prefix, UINT toffset ) const;
        XString                 substring( UINT beginIndex ) const;
        XString                 substring( UINT beginIndex, UINT endIndex ) const;
        XString                 toLowerCase( ) const;
        XString                 toUpperCase( ) const;
        XString                 trim( ) const; 
        int                     empty () const;
         
        //Methods
        //-------
        //MODIFIERS
        //=========
        const XString&  concat( const XString &str );
        void                    replace( char oldChar, char newChar );
        void                    replace( const XString& match, const XString& replace );
        void                    Format( const char* fmt, ...);
        void            Cut   ( UINT donde);

        // Friends
        //--------
        friend const XString&   operator + ( XString Lhs, const XString &Rhs );

protected:
        //Members
        //-------
        char *Buffer;                      // Stores the chars
        UINT BufferLen;                   // Max strlen for Buffer
        UINT Length;                             // Length of string

        void GetBuffer(UINT MaxStrLen);
        void Double( );
        void verifyIndex( UINT index, char *debug ) const;
};

//Class Functions
//===============

inline int
XString::empty(void) const
{
    if (strcmp(Buffer, "") == 0) {return 1;} else {return 0;}
}


inline void
XString::GetBuffer(UINT MaxStrLen)
{
        BufferLen = MaxStrLen;
        Buffer = new char[BufferLen + 1];
}

inline void
XString::Double( )
{
        char *temp = Buffer;
        GetBuffer( ++BufferLen * 2 );
        strncpy( Buffer, temp, strlen(temp)+1 );
        delete [] temp;
}

inline void
XString::verifyIndex( UINT index, char *debug ) const
{
        if ( index < 0 || index >= Length )
        {
                fprintf (stderr, "XString.h: Index Out Of Bounds. Function='%s'. Buffer='%s'. Total=%d, index=%d\n", 
                                 debug, Buffer, Length, index);
                exit(1);
        }
}

//Friend Functions
//================
inline const XString &
operator+( XString Lhs, const XString &Rhs ) 
{
        return Lhs += Rhs;
}

#endif