OMNeT++ Simulation Library  6.0.3
stringutil.h
1 //=========================================================================
2 // STRINGUTIL.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //=========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 2006-2017 OpenSim Ltd.
10 
11  This file is distributed WITHOUT ANY WARRANTY. See the file
12  `license' for details on this and other legal matters.
13 *--------------------------------------------------------------*/
14 
15 #ifndef __OMNETPP_STRINGUTIL_H
16 #define __OMNETPP_STRINGUTIL_H
17 
18 #include <cstdarg>
19 #include <cstring>
20 #include <string>
21 #include <map>
22 #include <vector>
23 #include "simkerneldefs.h"
24 
25 namespace omnetpp {
26 
35 inline bool opp_isempty(const char *s) {return !s || !s[0];}
36 
41 inline const char *opp_nulltoempty(const char *s) {return s ? s : "";}
42 
47 inline const char *opp_emptytodefault(const char *s, const char *defaultString) {return opp_isempty(s) ? defaultString : s;}
48 
52 SIM_API bool opp_isblank(const char *txt);
53 
58 inline int opp_strlen(const char *s)
59 {
60  return s ? strlen(s) : 0;
61 }
62 
67 inline char *opp_strdup(const char *s)
68 {
69  if (!s || !s[0]) return nullptr;
70  char *p = new char[strlen(s)+1];
71  strcpy(p,s);
72  return p;
73 }
74 
79 inline char *opp_strcpy(char *s1, const char *s2)
80 {
81  return strcpy(s1, s2 ? s2 : "");
82 }
83 
88 inline int opp_strcmp(const char *s1, const char *s2)
89 {
90  if (s1)
91  return s2 ? strcmp(s1,s2) : (*s1 ? 1 : 0);
92  else
93  return (s2 && *s2) ? -1 : 0;
94 }
95 
99 SIM_API std::string opp_trim(const std::string& text);
100 
106 SIM_API char *opp_strprettytrunc(char *dest, const char *src, unsigned maxlen);
107 
111 _OPP_GNU_ATTRIBUTE(format(printf, 1, 2))
112 SIM_API std::string opp_stringf(const char *fmt, ...);
113 
117 SIM_API std::string opp_vstringf(const char *fmt, va_list& args);
118 
122 SIM_API std::string opp_replacesubstring(const std::string& text, const std::string& substring, const std::string& replacement, bool replaceAll);
123 
129 SIM_API std::vector<std::string> opp_splitandtrim(const std::string& text);
130 
136 SIM_API std::vector<std::string> opp_split(const std::string& text, const std::string& separator);
137 
143 SIM_API std::vector<std::string> opp_splitandtrim(const std::string& text, const std::string& separator);
144 
148 SIM_API bool opp_stringbeginswith(const char *s, const char *prefix);
149 
153 SIM_API bool opp_stringendswith(const char *s, const char *ending);
154 
158 SIM_API std::string opp_substringbefore(const std::string& str, const std::string& substr);
159 
163 SIM_API std::string opp_substringafter(const std::string& str, const std::string& substr);
164 
168 SIM_API std::string opp_substringbeforelast(const std::string& str, const std::string& substr);
169 
173 SIM_API std::string opp_substringafterlast(const std::string& str, const std::string& substr);
174 
178 SIM_API std::string opp_removestart(const std::string& str, const std::string& prefix);
179 
183 SIM_API std::string opp_removeend(const std::string& str, const std::string& end);
184 
188 SIM_API std::string opp_strlower(const char *s);
189 
193 SIM_API std::string opp_strupper(const char *s);
194 
200 SIM_API const char *opp_strnistr(const char *haystack, const char *needle, int n, bool caseSensitive);
201 
206 SIM_API int opp_strdictcmp(const char *s1, const char *s2);
207 
212 SIM_API std::string opp_join(const char *separator, const char *s1, const char *s2);
213 
218 SIM_API std::string opp_join(const char *separator, const std::string& s1, const std::string& s2);
219 
225 SIM_API std::string opp_join(const char **strings, const char *separator, bool skipEmpty=false, char quoteChar=0);
226 
232 SIM_API std::string opp_join(const char **strings, int n, const char *separator, bool skipEmpty=false, char quoteChar=0);
233 
239 SIM_API std::string opp_join(const std::vector<std::string>& strings, const char *separator, bool skipEmpty=false, char quoteChar=0);
240 
244 SIM_API char *opp_itoa(char *buf, int d);
245 
249 SIM_API char *opp_ltoa(char *buf, long d);
250 
254 SIM_API char *opp_i64toa(char *buf, int64_t d);
255 
262 SIM_API char *opp_dtoa(char *buf, const char *format, double d);
263 
270 SIM_API long opp_strtol(const char *s, char **endptr);
271 
279 SIM_API long opp_atol(const char *s);
280 
287 SIM_API unsigned long opp_strtoul(const char *s, char **endptr);
288 
296 SIM_API unsigned long opp_atoul(const char *s);
297 
304 SIM_API long long opp_strtoll(const char *s, char **endptr);
305 
313 SIM_API long long opp_atoll(const char *s);
314 
321 SIM_API unsigned long long opp_strtoull(const char *s, char **endptr);
322 
330 SIM_API unsigned long long opp_atoull(const char *s);
331 
336 SIM_API double opp_strtod(const char *s, char **endptr);
337 
343 SIM_API double opp_atof(const char *s);
344 
347 } // namespace omnetpp
348 
349 #endif
350 
351 
omnetpp::opp_removestart
SIM_API std::string opp_removestart(const std::string &str, const std::string &prefix)
Remove the prefix if the s string starts with it, otherwise return the string unchanged.
omnetpp::opp_ltoa
SIM_API char * opp_ltoa(char *buf, long d)
Prints the d integer into the given buffer, then returns the buffer pointer.
omnetpp::opp_stringf
SIM_API std::string opp_stringf(const char *fmt,...)
Create a string using printf-like formatting. Allocates storage dynamically.
omnetpp::opp_strprettytrunc
SIM_API char * opp_strprettytrunc(char *dest, const char *src, unsigned maxlen)
Copies src string into dest, and if its length would exceed maxlen, it is truncated with an ellipsis....
omnetpp::opp_atol
SIM_API long opp_atol(const char *s)
Like the standard atol(), but throws opp_runtime_error if an overflow occurs during conversion,...
omnetpp::opp_isempty
bool opp_isempty(const char *s)
Returns true if the string is nullptr or has zero length.
Definition: stringutil.h:35
omnetpp::opp_split
SIM_API std::vector< std::string > opp_split(const std::string &text, const std::string &separator)
Split a string into parts separated by the given separator. If the input string is empty,...
omnetpp::opp_dtoa
SIM_API char * opp_dtoa(char *buf, const char *format, double d)
Prints the d double into the given buffer, then returns the buffer pointer. If d is finite,...
omnetpp::opp_atof
SIM_API double opp_atof(const char *s)
Like the standard atof(), but throws opp_runtime_error if an overflow occurs during conversion,...
omnetpp::opp_isblank
SIM_API bool opp_isblank(const char *txt)
Returns true if the string only contains whitespace.
omnetpp::opp_strtod
SIM_API double opp_strtod(const char *s, char **endptr)
Like the standard strtod(), but throws opp_runtime_error if an overflow occurs during conversion.
omnetpp::opp_nulltoempty
const char * opp_nulltoempty(const char *s)
Returns the pointer passed as argument unchanged, except that if it was nullptr, it returns a pointer...
Definition: stringutil.h:41
omnetpp::opp_i64toa
SIM_API char * opp_i64toa(char *buf, int64_t d)
Prints the d integer into the given buffer, then returns the buffer pointer.
omnetpp::opp_replacesubstring
SIM_API std::string opp_replacesubstring(const std::string &text, const std::string &substring, const std::string &replacement, bool replaceAll)
Performs find/replace within a string.
omnetpp::opp_strtoll
SIM_API long long opp_strtoll(const char *s, char **endptr)
Like the standard strtoll(), but throws opp_runtime_error if an overflow occurs during conversion....
omnetpp::opp_strdup
char * opp_strdup(const char *s)
Duplicates the string, using new char[]. For nullptr and empty strings it returns nullptr.
Definition: stringutil.h:67
omnetpp::opp_strtoull
SIM_API unsigned long long opp_strtoull(const char *s, char **endptr)
Like the standard strtoull(), but throws opp_runtime_error if an overflow occurs during conversion....
omnetpp::opp_stringbeginswith
SIM_API bool opp_stringbeginswith(const char *s, const char *prefix)
Returns true if the first string begins with the second string.
omnetpp::opp_substringafterlast
SIM_API std::string opp_substringafterlast(const std::string &str, const std::string &substr)
Returns the substring after the last occurrence of the given substring, or "".
omnetpp::opp_join
SIM_API std::string opp_join(const std::vector< std::string > &strings, const char *separator, bool skipEmpty=false, char quoteChar=0)
Concatenate the strings passed in the vector, using the given separator, and putting each item betwee...
omnetpp::opp_itoa
SIM_API char * opp_itoa(char *buf, int d)
Prints the d integer into the given buffer, then returns the buffer pointer.
omnetpp::opp_strnistr
const SIM_API char * opp_strnistr(const char *haystack, const char *needle, int n, bool caseSensitive)
Locates the first occurrence of the nul-terminated string needle in the string haystack,...
omnetpp::opp_substringbeforelast
SIM_API std::string opp_substringbeforelast(const std::string &str, const std::string &substr)
Returns the substring up to the last occurrence of the given substring, or "".
omnetpp::opp_substringafter
SIM_API std::string opp_substringafter(const std::string &str, const std::string &substr)
Returns the substring after the first occurrence of the given substring, or "".
omnetpp::opp_strcpy
char * opp_strcpy(char *s1, const char *s2)
Same as the standard strcpy() function, except that nullptr in the second argument is treated as a po...
Definition: stringutil.h:79
omnetpp::opp_splitandtrim
SIM_API std::vector< std::string > opp_splitandtrim(const std::string &text, const std::string &separator)
Split a string into parts separated by the given separator, trimming each item of whitespace....
omnetpp::opp_atoul
SIM_API unsigned long opp_atoul(const char *s)
Like the standard atol(), but for unsigned long, and throws opp_runtime_error if an overflow occurs d...
omnetpp::opp_substringbefore
SIM_API std::string opp_substringbefore(const std::string &str, const std::string &substr)
Returns the substring up to the first occurrence of the given substring, or "".
omnetpp::opp_strlen
int opp_strlen(const char *s)
Same as the standard strlen() function, except that does not crash on nullptr but returns 0.
Definition: stringutil.h:58
omnetpp::opp_strtol
SIM_API long opp_strtol(const char *s, char **endptr)
Like the standard strtol(), but throws opp_runtime_error if an overflow occurs during conversion....
omnetpp::opp_atoll
SIM_API long long opp_atoll(const char *s)
Like the standard atoll(), but throws opp_runtime_error if an overflow occurs during conversion,...
omnetpp::opp_strupper
SIM_API std::string opp_strupper(const char *s)
Converts the string to upper case, and returns the result.
omnetpp::opp_strdictcmp
SIM_API int opp_strdictcmp(const char *s1, const char *s2)
Dictionary-compare two strings, the main difference from strcasecmp() being that integers embedded in...
omnetpp::opp_atoull
SIM_API unsigned long long opp_atoull(const char *s)
Like the standard atoull(), but throws opp_runtime_error if an overflow occurs during conversion,...
omnetpp::opp_removeend
SIM_API std::string opp_removeend(const std::string &str, const std::string &end)
Remove the given end string if the s string ends with it, otherwise return the string unchanged.
omnetpp::opp_vstringf
SIM_API std::string opp_vstringf(const char *fmt, va_list &args)
Create a string using printf-like formatting. Allocates storage dynamically.
omnetpp::opp_stringendswith
SIM_API bool opp_stringendswith(const char *s, const char *ending)
Returns true if the first string ends in the second string.
omnetpp::opp_strtoul
SIM_API unsigned long opp_strtoul(const char *s, char **endptr)
Like the standard strtoul(), but throws opp_runtime_error if an overflow occurs during conversion....
omnetpp::opp_strlower
SIM_API std::string opp_strlower(const char *s)
Converts the string to lower case, and returns the result.
omnetpp::opp_strcmp
int opp_strcmp(const char *s1, const char *s2)
Same as the standard strcmp() function, except that nullptr is treated exactly as an empty string (""...
Definition: stringutil.h:88
omnetpp::opp_trim
SIM_API std::string opp_trim(const std::string &text)
Removes any leading and trailing whitespace.
omnetpp::opp_emptytodefault
const char * opp_emptytodefault(const char *s, const char *defaultString)
Returns the pointer passed as argument unchanged, except that if it was empty, it returns the second ...
Definition: stringutil.h:47