00001
00027 #ifndef _HUGIN_UTILS_PLATFORM_H
00028 #define _HUGIN_UTILS_PLATFORM_H
00029
00030 #include <hugin_shared.h>
00031 #include <math.h>
00032 #include <string>
00033
00034 #if 0
00035
00036 #ifdef MSVC
00037 #define snprintf _snprintf
00038 #endif
00039
00040 #endif
00041
00042
00043 #ifndef MAC_OS_X
00044 #if ((defined __APPLE__) && (defined __MACH__))
00045
00046 #define MAC_OS_X 1
00047 #endif
00048 #endif
00049
00050 #ifndef UNIX_LIKE
00051 #if __unix__ || MAC_OS_X
00052
00053 #define UNIX_LIKE 1
00054 #endif
00055 #endif
00056
00057
00058 namespace hugin_utils {
00059
00061 IMPEX int getCPUCount();
00062
00064 template <class str>
00065 str quoteStringInternal(const str & arg, const str & quotechar,
00066 const str & replacements)
00067 {
00068
00069 str ret(arg);
00070 size_t len = replacements.size();
00071 for (size_t i = 0; i < len; i++) {
00072 str source(replacements.substr(i,1));
00073 str dest(quotechar + source);
00074 size_t destlen = dest.size();
00075 size_t idx = 0;
00076 do {
00077 idx = ret.find(source,idx);
00078 if (idx != str::npos) {
00079 ret.replace(idx, 1, dest);
00080
00081 idx += destlen;
00082 }
00083 } while (idx != str::npos);
00084 }
00085 return ret;
00086 }
00087
00088 #ifdef _WINDOWS
00089
00090 template <class str>
00091 str replaceBackslash(const str & arg)
00092 {
00093 str ret(arg);
00094 size_t idx = 0;
00095 do
00096 {
00097 idx = ret.find(str("\\"),idx);
00098 if (idx != str::npos)
00099 {
00100 ret.replace(idx, 1, str("/"));
00101 idx++;
00102 }
00103 }
00104 while (idx != str::npos);
00105 return ret;
00106 };
00107 #endif
00108
00116 template <class str>
00117 str quoteString(const str & arg)
00118 {
00119 #ifdef WIN32
00120
00121
00122 return quoteStringInternal(arg, str("^"), str("^ \"$|()"));
00123 #else
00124 return quoteStringInternal(arg, str("\\"), str("\\ ~$\"|'`{}[]()"));
00125 #endif
00126 }
00127
00138 template <class str>
00139 str quoteStringShell(const str & arg)
00140 {
00141 #ifdef WIN32
00142
00143
00144 return str("\"")+quoteStringInternal(quoteStringInternal(replaceBackslash(arg),str("\\"),str("#")), str("$"), str("$"))+str("\"");
00145 #else
00146 return quoteStringInternal(quoteStringInternal(arg, str("\\"), str("\\ ~$\"|'`{}[]()*#:=")), str("$"), str("$"));
00147 #endif
00148 }
00149
00153 template <class str>
00154 str escapeStringMake(const str & arg)
00155 {
00156 #ifdef WIN32
00157
00158 return quoteStringInternal(quoteStringInternal(replaceBackslash(arg), str("\\"), str(" #=")), str("$"), str("$"));
00159 #else
00160 return quoteStringInternal(quoteStringInternal(arg, str("\\"), str(" #:=")), str("$"), str("$"));
00161 #endif
00162 }
00163
00168 template <class str>
00169 str quoteFilename(const str & arg)
00170 {
00171 #ifdef WIN32
00172 str ret;
00173
00174 ret = quoteStringInternal(arg, str("^"), str("\""));
00175 return str("\"") + ret + str("\"");
00176 #else
00177 str ret;
00178 ret = quoteStringInternal(arg, str("\\"), str("\"$'\\"));
00179 return str("\"") + ret + str("\"");
00180 #endif
00181 }
00182
00183 }
00184
00185
00186 #endif // _HUGIN_UTILS_PLATFORM_H