-
Notifications
You must be signed in to change notification settings - Fork 22
/
cString.h
114 lines (101 loc) · 4.16 KB
/
cString.h
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
/*
*
* Copyright (C) 2011-2012 Amr Thabet <amr.thabet[at]student.alx.edu.eg>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to Amr Thabet
* amr.thabet[at]student.alx.edu.eg
*
*/
#if !defined(__STR_H__)
#define __STR_H__
#include "Packetyzer.h"
#ifdef _WIN32
#define SOMEDEF _cdecl
#else
#define SOMEDEF
#endif
class DLLEXPORT Packetyzer::Elements::cString
{
public:
// constructors and destructor
cString(const cString& str);
cString(const char* str);
cString(const double var) { VarToString(var); }
cString() { m_nLength = 0; m_pString = 0; }
virtual ~cString() { if (m_pString)free(m_pString); }
// operator overloading helper
template <class T> friend cString SOMEDEF operator +(T var, const cString& str);
// operator overloading
cString& operator =(const char* str);
cString& operator =(const cString& str);
cString& operator =(const double var) { VarToString(var); return *this; }
template <class T>
cString operator +(T var) { cString tstr = *this; return tstr += var; }
cString& operator +=(double str) { return *this += (cString)str; }
cString& operator +=(const char* str) { return *this += (cString)str; }
cString& operator +=(const char str) { return *this += (cString)str; }
cString& operator +=(const cString& str);
cString& operator <<(const cString& str);
char operator [](int i);
// add more logic comparison operators as following, for example, although not efficient
virtual bool operator !=(char* str) { return strcmp(str, m_pString) != 0; }
virtual bool operator ==(char* str) { return strcmp(str, m_pString) == 0; }
// c type string conversion
operator char* () { return m_pString; }
operator const char* () const { return m_pString; }
char* GetChar() { return m_pString; }
// numeric conversion
template <class T> int GetValue(T& var) { return GetVar(var); }
// search the match string : WildCards can be '?' and '*' combination
// return value : true (pattern matchs string), false (no match)
bool ScanWildcard(const char* WildCards) { return Match((char*)WildCards, m_pString); }
int Search(const char* str);
int Search(const char c);
void Replace(char src, char dest);
// format string
int Format(const char* format, ...);
void Substr(cString Str, DWORD offset,DWORD length);
void Substr(cString Str, DWORD offset){Substr(Str,offset,strlen(m_pString)-offset); }
DWORD GetLength(){return m_nLength;};
protected:
// can use faster algorithm for search ?
virtual bool Match(char*, char*);
virtual bool Scan(char*&, char*&);
// have any good conversion method ?
virtual void VarToString(const double var);
// numeric conversion helpers
bool NumericParse(void* pvar, char flag);
bool GetVar(bool& var) { return NumericParse((void*)&var, 'b'); }
bool GetVar(char& var) { return NumericParse((void*)&var, 'c'); }
bool GetVar(short& var) { return NumericParse((void*)&var, 's'); }
bool GetVar(int& var) { return NumericParse((void*)&var, 'i'); }
bool GetVar(long& var) { return NumericParse((void*)&var, 'l'); }
bool GetVar(float& var) { return NumericParse((void*)&var, 'f'); }
bool GetVar(double& var) { return NumericParse((void*)&var, 'd'); }
bool GetVar(unsigned char& var) { return NumericParse((void*)&var, 'C'); }
bool GetVar(unsigned short& var) { return NumericParse((void*)&var, 'S'); }
bool GetVar(unsigned int& var) { return NumericParse((void*)&var, 'I'); }
bool GetVar(unsigned long& var) { return NumericParse((void*)&var, 'L'); }
// data block
int m_nLength;
public:
char* m_pString;
};
template <class T>
cString operator +(T var, const cString& str)
{
cString svar = var;
return svar += str;
};
#endif