| 4394 |
ripley |
1 |
/* Prevent multiple inclusions. */
|
|
|
2 |
#ifndef _STRING_CLASS_
|
|
|
3 |
#define _STRING_CLASS_
|
|
|
4 |
|
|
|
5 |
/* Eliminate any prior defintion of string. */
|
|
|
6 |
#ifdef string
|
|
|
7 |
#undef string
|
|
|
8 |
#endif
|
|
|
9 |
|
|
|
10 |
/* Define the new string type. */
|
|
|
11 |
#define string strclass
|
|
|
12 |
|
|
|
13 |
/* Include a few standard header files. */
|
|
|
14 |
#include <stdio.h>
|
|
|
15 |
#include <stdlib.h>
|
|
|
16 |
#include <string.h>
|
|
|
17 |
|
|
|
18 |
/*
|
|
|
19 |
* I/O functions.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
#define print(s) printf("%s",(char*)((string)s))
|
|
|
23 |
|
|
|
24 |
/*
|
|
|
25 |
* C function defintions.
|
|
|
26 |
*/
|
|
|
27 |
#ifdef __cplusplus
|
|
|
28 |
extern "C" {
|
|
|
29 |
#endif
|
|
|
30 |
|
|
|
31 |
/*
|
|
|
32 |
* Safe string utility functions.
|
|
|
33 |
*/
|
|
|
34 |
char *new_string(char *src);
|
|
|
35 |
void del_string(char *str);
|
|
|
36 |
long string_length(char *s);
|
|
|
37 |
void copy_string(char *dest, char *src);
|
|
|
38 |
int compare_strings(char *s1, char *s2);
|
|
|
39 |
char *add_strings(char *s1, char *s2);
|
|
|
40 |
char *char_to_string(char ch);
|
|
|
41 |
char *int_to_string(long i);
|
|
|
42 |
char *float_to_string(float f);
|
|
|
43 |
|
|
|
44 |
#ifdef __cplusplus
|
|
|
45 |
}
|
|
|
46 |
#endif
|
|
|
47 |
|
|
|
48 |
/*
|
|
|
49 |
* C defintions.
|
|
|
50 |
*/
|
|
|
51 |
#ifndef __cplusplus
|
|
|
52 |
|
|
|
53 |
typedef char *string;
|
|
|
54 |
|
|
|
55 |
/*
|
|
|
56 |
* C++ defintions.
|
|
|
57 |
*/
|
|
|
58 |
#else
|
|
|
59 |
|
|
|
60 |
/*
|
|
|
61 |
* String class.
|
|
|
62 |
*/
|
|
|
63 |
class string
|
|
|
64 |
{
|
|
|
65 |
protected:
|
|
|
66 |
char *str; /* the string itself */
|
|
|
67 |
|
|
|
68 |
public:
|
|
|
69 |
string(void) {str=new_string(NULL);}
|
|
|
70 |
string(char *s) {str=new_string(s);}
|
|
|
71 |
string(string& s) {str=new_string(s.str);}
|
|
|
72 |
string(char ch) {str=new_string(char_to_string(ch));}
|
|
|
73 |
string(int i) {str=new_string(int_to_string(i));}
|
|
|
74 |
string(long i) {str=new_string(int_to_string(i));}
|
|
|
75 |
string(float f) {str=new_string(float_to_string(f));}
|
|
|
76 |
~string(void) {del_string(str);}
|
|
|
77 |
|
|
|
78 |
char& operator [] (unsigned i) {return str[i];}
|
|
|
79 |
operator char* (void) {return str;}
|
|
|
80 |
|
|
|
81 |
string& operator = (string s2)
|
|
|
82 |
{if (str != s2.str) {del_string(str); str=new_string(s2.str);} return *this;}
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
inline string operator + (string s1, string s2)
|
|
|
86 |
{return add_strings(s1,s2);}
|
|
|
87 |
inline string operator + (string s1, char *s2)
|
|
|
88 |
{return add_strings(s1,s2);}
|
|
|
89 |
inline string operator + (char *s1, string s2)
|
|
|
90 |
{return add_strings(s1,s2);}
|
|
|
91 |
|
|
|
92 |
inline string operator + (string s1, char ch)
|
|
|
93 |
{return add_strings(s1,char_to_string(ch));}
|
|
|
94 |
inline string operator + (char ch, string s2)
|
|
|
95 |
{return add_strings(char_to_string(ch),s2);}
|
|
|
96 |
inline string operator + (string s1, int i)
|
|
|
97 |
{return add_strings(s1,int_to_string(i));}
|
|
|
98 |
inline string operator + (int i, string s2)
|
|
|
99 |
{return add_strings(int_to_string(i),s2);}
|
|
|
100 |
inline string operator + (string s1, long i)
|
|
|
101 |
{return add_strings(s1,int_to_string(i));}
|
|
|
102 |
inline string operator + (long i, string s2)
|
|
|
103 |
{return add_strings(int_to_string(i),s2);}
|
|
|
104 |
inline string operator + (string s1, float f)
|
|
|
105 |
{return add_strings(s1,float_to_string(f));}
|
|
|
106 |
inline string operator + (float f, string s2)
|
|
|
107 |
{return add_strings(float_to_string(f),s2);}
|
|
|
108 |
|
|
|
109 |
inline int operator == (string s1, string s2)
|
|
|
110 |
{return (compare_strings(s1,s2) == 0);}
|
|
|
111 |
inline int operator == (string s1, char *s2)
|
|
|
112 |
{return (compare_strings(s1,s2) == 0);}
|
|
|
113 |
inline int operator == (char *s1, string s2)
|
|
|
114 |
{return (compare_strings(s1,s2) == 0);}
|
|
|
115 |
inline int operator == (string s1, char ch)
|
|
|
116 |
{return (compare_strings(s1,char_to_string(ch)) == 0);}
|
|
|
117 |
inline int operator == (char ch, string s2)
|
|
|
118 |
{return (compare_strings(char_to_string(ch),s2) == 0);}
|
|
|
119 |
inline int operator == (string s1, int ch)
|
|
|
120 |
{return (compare_strings(s1,char_to_string(ch)) == 0);}
|
|
|
121 |
inline int operator == (int ch, string s2)
|
|
|
122 |
{return (compare_strings(char_to_string(ch),s2) == 0);}
|
|
|
123 |
|
|
|
124 |
inline int operator != (string s1, string s2)
|
|
|
125 |
{return compare_strings(s1,s2);}
|
|
|
126 |
inline int operator != (string s1, char *s2)
|
|
|
127 |
{return compare_strings(s1,s2);}
|
|
|
128 |
inline int operator != (char *s1, string s2)
|
|
|
129 |
{return compare_strings(s1,s2);}
|
|
|
130 |
inline int operator != (string s1, char ch)
|
|
|
131 |
{return compare_strings(s1,char_to_string(ch));}
|
|
|
132 |
inline int operator != (char ch, string s2)
|
|
|
133 |
{return compare_strings(char_to_string(ch),s2);}
|
|
|
134 |
inline int operator != (string s1, int ch)
|
|
|
135 |
{return compare_strings(s1,char_to_string(ch));}
|
|
|
136 |
inline int operator != (int ch, string s2)
|
|
|
137 |
{return compare_strings(char_to_string(ch),s2);}
|
|
|
138 |
|
|
|
139 |
/*
|
|
|
140 |
* End of C++ defintions.
|
|
|
141 |
*/
|
|
|
142 |
#endif
|
|
|
143 |
|
|
|
144 |
/*
|
|
|
145 |
* End of string class definition.
|
|
|
146 |
*/
|
|
|
147 |
#endif
|