Project

General

Profile

Statistics
| Branch: | Revision:

root / src / common / stringutil.h @ 81ad8b66

History | View | Annotate | Download (7.81 KB)

1
//=========================================================================
2
//  STRINGUTIL.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//=========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17
#ifndef _STRINGUTIL_H_
18
#define _STRINGUTIL_H_
19

    
20
#include <stdarg.h>
21
#include <string.h>
22
#include <string>
23
#include "commondefs.h"
24
#include "commonutil.h"
25

    
26
//NAMESPACE_BEGIN
27

    
28
/**
29
 * Returns true if the string is NULL or has zero length.
30
 */
31
inline bool opp_isempty(const char *s)  {return !s || !s[0];}
32

    
33
/**
34
 * Returns the pointer passed as argument unchanged, except that if it was NULL,
35
 * it returns a pointer to a null string ("").
36
 */
37
inline const char *opp_nulltoempty(const char *s)  {return s ? s : "";}
38

    
39
/**
40
 * Returns true if the string only contains whitespace.
41
 */
42
COMMON_API bool opp_isblank(const char *txt);
43

    
44
//
45
// The following few inline functions are present in the simkernel's utils.h too;
46
// define them conditionally to prevent clashes.
47
//
48
#ifndef __SIMUTIL_H
49

    
50
/**
51
 * Same as the standard strlen() function, except that does not crash
52
 * on NULL pointers but returns 0.
53
 */
54
inline int opp_strlen(const char *s)
55
{
56
    return s ? strlen(s) : 0;
57
}
58

    
59
/**
60
 * Duplicates the string, using <tt>new char[]</tt>. For NULLs and empty
61
 * strings it returns NULL.
62
 */
63
inline char *opp_strdup(const char *s)
64
{
65
    if (!s || !s[0]) return NULL;
66
    char *p = new char[strlen(s)+1];
67
    strcpy(p,s);
68
    return p;
69
}
70

    
71
/**
72
 * Same as the standard strcpy() function, except that NULL pointers
73
 * in the second argument are treated like pointers to a null string ("").
74
 */
75
inline char *opp_strcpy(char *s1, const char *s2)
76
{
77
    return strcpy(s1, s2 ? s2 : "");
78
}
79

    
80
/**
81
 * Same as the standard strcmp() function, except that NULL pointers
82
 * are treated exactly as empty strings ("").
83
 */
84
inline int opp_strcmp(const char *s1, const char *s2)
85
{
86
    if (s1)
87
        return s2 ? strcmp(s1,s2) : (*s1 ? 1 : 0);
88
    else
89
        return (s2 && *s2) ? -1 : 0;
90
}
91
#endif //__SIMUTIL_H
92

    
93
/**
94
 * Removes any leading and trailing whitespace.
95
 */
96
COMMON_API std::string opp_trim(const char *txt);
97

    
98
/**
99
 * Reverse of opp_quotestr(): remove quotes and resolve backslashed escapes.
100
 *
101
 * Throws an exception if there is a parse error. If there is anything
102
 * (non-whitespace) in the input after the string literal, endp is set to
103
 * that character; otherwise endp is set to point to the terminating zero
104
 * of the string.
105
 */
106
COMMON_API std::string opp_parsequotedstr(const char *txt, const char *&endp);
107

    
108
/**
109
 * Reverse of opp_quotestr(): remove quotes and resolve backslashed escapes.
110
 *
111
 * Throws an exception if there is a parse error.
112
 */
113
COMMON_API std::string opp_parsequotedstr(const char *txt);
114

    
115
/**
116
 * Surround the given string with "quotes", also escape with backslash
117
 * where needed.
118
 */
119
COMMON_API std::string opp_quotestr(const char *txt);
120

    
121
/**
122
 * Returns true if the string contains space, backslash, quote, or anything
123
 * else that would make quoting (opp_quotestr()) necessary before writing
124
 * it into a data file.
125
 */
126
COMMON_API bool opp_needsquotes(const char *txt);
127

    
128
/**
129
 * Combines opp_needsquotes() and opp_quotestr().
130
 */
131
inline std::string opp_quotestr_ifneeded(const char *txt)
132
{
133
    return opp_needsquotes(txt) ? opp_quotestr(txt) : std::string(txt);
134
}
135

    
136
/**
137
 * A macro version of opp_quotestr_ifneeded(). This is more efficient,
138
 * because it avoids conversion to std::string when no quoting is needed.
139
 */
140
#define QUOTE(txt)   (opp_needsquotes(txt) ? opp_quotestr(txt).c_str() : (txt))
141

    
142
/**
143
 * Create a string using printf-like formatting. Limit: 1023 chars.
144
 */
145
COMMON_API std::string opp_stringf(const char *fmt, ...);
146

    
147
/**
148
 * Create a string using printf-like formatting. Limit: 1023 chars.
149
 */
150
COMMON_API std::string opp_vstringf(const char *fmt, va_list& args);
151

    
152
/**
153
 * A limited vsscanf implementation, used by cStatistic::freadvarsf()
154
 */
155
COMMON_API int opp_vsscanf(const char *s, const char *fmt, va_list va);
156

    
157
/**
158
 * Performs find/replace within a string.
159
 */
160
COMMON_API std::string opp_replacesubstring(const char *s, const char *substring, const char *replacement, bool replaceAll);
161

    
162
/**
163
 * Inserts newlines into the string, performing rudimentary line breaking.
164
 */
165
COMMON_API std::string opp_breaklines(const char *text, int maxLineLength);
166

    
167
/**
168
 * Indent each line of the input text.
169
 */
170
COMMON_API std::string opp_indentlines(const char *text, const char *indent);
171

    
172
/**
173
 * Returns true if the first string begins with the second string.
174
 */
175
COMMON_API bool opp_stringbeginswith(const char *s, const char *prefix);
176

    
177
/**
178
 * Returns true if the first string ends in the second string.
179
 */
180
COMMON_API bool opp_stringendswith(const char *s, const char *ending);
181

    
182
/**
183
 * Concatenates up to four strings. Returns a pointer to a static buffer
184
 * of length 256. If the result length would exceed 256, it is truncated.
185
 */
186
COMMON_API char *opp_concat(const char *s1, const char *s2, const char *s3=NULL, const char *s4=NULL);
187

    
188
/**
189
 * Converts the string to uppercase. Returns a pointer to  the argument.
190
 */
191
COMMON_API char *opp_strupr(char *s);
192

    
193
/**
194
 * Converts the string to lowercase. Returns a pointer to  the argument.
195
 */
196
COMMON_API char *opp_strlwr(char *s);
197

    
198
/**
199
 * If either s1 or s2 is empty, returns the other one, otherwise returns
200
 * s1 + separator + s2.
201
 */
202
COMMON_API std::string opp_join(const char *separator, const char *s1, const char *s2);
203

    
204
/**
205
 * Dictionary-compare two strings, the main difference from strcasecmp()
206
 * being that integers embedded in the strings are compared in
207
 * numerical order.
208
 */
209
COMMON_API int strdictcmp(const char *s1, const char *s2);
210

    
211
/**
212
 * Like the standard strtol() with base==0, but throws opp_runtime_error if an
213
 * overflow occurs during conversion.
214
 */
215
COMMON_API long opp_strtol(const char *s, char **endptr);
216

    
217
/**
218
 * Like the standard atol(), but throws opp_runtime_error if an overflow
219
 * occurs during conversion, or if there is (non-whitespace) trailing garbage
220
 * after the number.
221
 */
222
COMMON_API long opp_atol(const char *s);
223

    
224
/**
225
 * Like the standard strtoul() with base==0, but throws opp_runtime_error if an
226
 * overflow occurs during conversion.
227
 */
228
COMMON_API unsigned long opp_strtoul(const char *s, char **endptr);
229

    
230
/**
231
 * Like the standard atol(), but for unsigned long, and throws opp_runtime_error
232
 * if an overflow occurs during conversion, or if there is (non-whitespace)
233
 * trailing garbage after the number.
234
 */
235
COMMON_API unsigned long opp_atoul(const char *s);
236

    
237
/**
238
 * Like the standard strtod(), but throws opp_runtime_error if an overflow
239
 * occurs during conversion.
240
 */
241
COMMON_API double opp_strtod(const char *s, char **endptr);
242

    
243
/**
244
 * Like the standard atof(), but throws opp_runtime_error if an overflow
245
 * occurs during conversion, or if there is (non-whitespace) trailing garbage
246
 * after the number.
247
 */
248
COMMON_API double opp_atof(const char *s);
249

    
250
/**
251
 * s should point to a double quote '"'. The function returns a pointer to
252
 * the matching quote (i.e. the end of the string literal), or NULL if
253
 * not found. It recognizes escaping embedded quotes with backslashes
254
 * (C-style string literals).
255
 */
256
COMMON_API const char *opp_findmatchingquote(const char *s);
257

    
258
/**
259
 * s should point to an open parenthesis. The function returns the matching
260
 * paren, or NULL if not found. It does not search inside string constants
261
 * delimited by double quotes ('"'); it uses opp_findmatchingquote() to
262
 * parse them. Note: a NULL return value (unmatched left paren) may also
263
 * be caused by an unterminated string constant.
264
 */
265
COMMON_API const char *opp_findmatchingparen(const char *s);
266

    
267
//NAMESPACE_END
268

    
269

    
270
#endif
271

    
272