Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StringConv.hh
1 #ifndef __StringConv_hh__
2 #define __StringConv_hh__
3 
4 #ifdef WIN32
5 # pragma warning(push)
6 # pragma warning(disable: 4005 4996)
7 #endif // WIN32
8 
9 #include <wx/string.h>
10 #include <string>
11 #include <iostream>
12 #include <wx/regex.h>
13 
14 namespace BIAS {
15 
16 /*
17 inline const char* WxToAscii(const wxString& thestring)
18 {
19  return (const char*)thestring.mb_str(wxConvUTF8);
20 }
21 */
22 
23 /// Converts a wxString to a std::string.
24 ///
25 /// Please note the existing wx macros _ and _T/wxT, too
26 #define WxToAscii(s) (const char*)(wxString(s).mb_str(wxConvUTF8))
27 #define WxToFilenames(s) (const char*)(wxString(s).mb_str(wxConvFile))
28 #define WxToFunctionString(s) (const char*)(wxString(s).fn_str())
29 
30 /// Converts a C string to a wxString.
31 /// Please note the existing wx macros _ and _T/wxT, too
32 inline wxString AsciiToWx(const char* thestring)
33 {
34  return wxString(thestring, wxConvUTF8);
35 }
36 
37 inline wxString FilenamesToWx(const char* thestring)
38 {
39  return wxString(thestring, wxConvFile);
40 }
41 
42 /// Converts a std::string to a wxString.
43 /// Please note the existing wx macros _ and _T/wxT, too
44 inline wxString AsciiToWx(const std::string& thestring)
45 {
46  return wxString(thestring.c_str(), wxConvUTF8);
47 }
48 
49 inline wxString FilenamesToWx(const std::string& thestring)
50 {
51  return wxString(thestring.c_str(), wxConvFile);
52 }
53 
54 
55 /**
56  Class for converting an array of wxStrings to an array
57  of non-const ASCII strings. Required for using getopt
58  in UNICODE wx tools.
59 */
61 {
62 public:
63  WxToAsciiArray(wxChar** strings, int size)
64  {
65  _AsciiStrings = new char*[size];
66  for (int i = 0; i < size; i++)
67  {
68  _AsciiStrings[i] = strdup(WxToAscii(strings[i]));
69  }
70  _Size = size;
71  }
72 
74  {
75  for (int i = 0; i < _Size; i++)
76  {
77  free(_AsciiStrings[i]);
78  }
79  delete[] _AsciiStrings;
80  }
81 
82  operator char**()
83  {
84  return _AsciiStrings;
85  }
86 
87 private:
88  char** _AsciiStrings;
89  int _Size;
90 };
91 
92 
93 inline std::string IsoLatin1ToUtf8(const std::string &s)
94 {
95  return std::string(wxString(s.c_str(),wxCSConv(_T("ISO8859-1"))).mb_str(wxConvUTF8));
96 }
97 
98 
99  inline void CleanStringIP(wxString &input, const wxString replaceWith=wxEmptyString)
100 {
101  wxRegEx rex( wxT("[^[:alnum:]_()-,]") );
102  if ( rex.IsValid() ) {
103  rex.ReplaceAll(&input, replaceWith);
104  }
105 }
106 
107 inline wxString CleanString(const wxString &input, const wxString replaceWith=wxEmptyString)
108 {
109  wxString res = input;
110  CleanStringIP(res, replaceWith);
111  return res;
112 }
113 
114 
115 } // namespace BIAS
116 
117 #ifdef WIN32
118 # pragma warning(pop)
119 #endif // WIN32
120 
121 #endif // __StringConv_hh__
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
Definition: StringConv.hh:32
std::string IsoLatin1ToUtf8(const std::string &s)
Definition: StringConv.hh:93
wxString FilenamesToWx(const char *thestring)
Definition: StringConv.hh:37
WxToAsciiArray(wxChar **strings, int size)
Definition: StringConv.hh:63
Class for converting an array of wxStrings to an array of non-const ASCII strings.
Definition: StringConv.hh:60
wxString CleanString(const wxString &input, const wxString replaceWith=wxEmptyString)
Definition: StringConv.hh:107
void CleanStringIP(wxString &input, const wxString replaceWith=wxEmptyString)
Definition: StringConv.hh:99