Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
platform.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
27 #ifndef _HUGIN_UTILS_PLATFORM_H
28 #define _HUGIN_UTILS_PLATFORM_H
29 
30 #include <hugin_shared.h>
31 #include <math.h>
32 #include <string>
33 
34 // Platform identifiers
35 #ifndef MAC_OS_X
36  #if ((defined __APPLE__) && (defined __MACH__))
37  // Mac OS X platform
38  #define MAC_OS_X 1
39  #endif
40 #endif
41 
42 #ifndef UNIX_LIKE
43  #if __unix__ || MAC_OS_X
44  // __unix__ is not defined on Mac, but Mac is a BSD
45  #define UNIX_LIKE 1
46  #endif
47 #endif
48 
49 
50 namespace hugin_utils {
51 
53  template <class str>
54  str quoteStringInternal(const str & arg, const str & quotechar,
55  const str & replacements)
56  {
57  // loop over all chars..
58  str ret(arg);
59  size_t len = replacements.size();
60  for (size_t i = 0; i < len; i++) {
61  str source(replacements.substr(i,1));
62  str dest(quotechar + source);
63  size_t destlen = dest.size();
64  size_t idx = 0;
65  do {
66  idx = ret.find(source,idx);
67  if (idx != str::npos) {
68  ret.replace(idx, 1, dest);
69  // skip to next unknown char.
70  idx += destlen;
71  }
72  } while (idx != str::npos);
73  }
74  return ret;
75  }
76 
77 #ifdef _WIN32
78  template <class str>
80  str replaceBackslash(const str & arg)
81  {
82  str ret(arg);
83  size_t idx = 0;
84  do
85  {
86  idx = ret.find(str("\\"),idx);
87  if (idx != str::npos)
88  {
89  ret.replace(idx, 1, str("/"));
90  idx++;
91  }
92  }
93  while (idx != str::npos);
94  return ret;
95  };
96 #endif
97 
105  template <class str>
106  str quoteString(const str & arg)
107  {
108  #ifdef _WIN32
109  // escape all strange chars with ^
110  // is this true for create process?
111  return quoteStringInternal(arg, str("^"), str("^ \"$|()"));
112  #else
113  return quoteStringInternal(arg, str("\\"), str("\\ ~$\"|'`{}[]()"));
114  #endif
115  }
116 
127  template <class str>
128  str quoteStringShell(const str & arg)
129  {
130 #ifdef _WIN32
131  // Do not quote backslash,: and ~ on win32.
132  // we only need to escape hash (#) and $, all other chars are handled by quoting with " "
133  return str("\"")+quoteStringInternal(quoteStringInternal(replaceBackslash(arg),str("\\"),str("#")), str("$"), str("$"))+str("\"");
134 #else
135  return quoteStringInternal(quoteStringInternal(arg, str("\\"), str("\\ ~$\"|'`{}[]()*#:=")), str("$"), str("$"));
136 #endif
137  }
138 
142  template <class str>
143  str escapeStringMake(const str & arg)
144  {
145 #ifdef _WIN32
146  // Do not escape colon in windows because it causes problems with absolute paths
147  return quoteStringInternal(quoteStringInternal(replaceBackslash(arg), str("\\"), str(" #=")), str("$"), str("$"));
148 #else
149  return quoteStringInternal(quoteStringInternal(arg, str("\\"), str(" #:=")), str("$"), str("$"));
150 #endif
151  }
152 
157  template <class str>
158  str quoteFilename(const str & arg)
159  {
160  #ifdef _WIN32
161  str ret;
162  // just a guess
163  ret = quoteStringInternal(arg, str("^"), str("\""));
164  return str("\"") + ret + str("\"");
165  #else
166  str ret;
167  ret = quoteStringInternal(arg, str("\\"), str("\"$'\\"));
168  return str("\"") + ret + str("\"");
169  #endif
170  }
171 
172 } // namespace
173 
174 
175 #endif // _HUGIN_UTILS_PLATFORM_H
str quoteStringInternal(const str &arg, const str &quotechar, const str &replacements)
utility function; escapes characters in replacements with quotechar.
Definition: platform.h:54
str quoteFilename(const str &arg)
Quote a filename, so that it is surrounded by &quot;&quot;.
Definition: platform.h:158
str quoteString(const str &arg)
Try to escape special chars on windows and linux.
Definition: platform.h:106
str escapeStringMake(const str &arg)
Escape dangerous chars in makefile strings/filenames (space),#,=.
Definition: platform.h:143
str quoteStringShell(const str &arg)
Try to escape special chars in a string used by a unix type shell.
Definition: platform.h:128