00001
00024 #include <string>
00025 #include <sys/stat.h>
00026 #include <sys/types.h>
00027 #include <errno.h>
00028
00029 using namespace std;
00030
00031
00032 static const char g_shell_chars[] = {'\'','"', '>', '<',
00033 '[', ']', '(', ')',
00034 '\\', '/','~', '!',
00035 '#', '$', '&', '|',
00036 ';', ' ', '\0' };
00037
00038
00040
00045 void replace_str(string& to_change, const string& to_find, const string& replace_with)
00046 {
00047 size_t pos(0);
00048
00049 while((pos = to_change.find(to_find, pos)) != string::npos) {
00050 to_change.replace(pos, to_find.length(), replace_with);
00051 pos += replace_with.length();
00052 }
00053 }
00054
00055
00057
00060 void remove_shell_chars(string& to_change)
00061 {
00062 int i(0);
00063 size_t pos;
00064
00065 while(g_shell_chars[i] != 0) {
00066 pos = 0;
00067 while((pos = to_change.find(g_shell_chars[i], pos)) != string::npos) {
00068 to_change.erase(pos, 1);
00069 }
00070 ++i;
00071 }
00072 }
00073
00075
00078 void escape_shell_chars(string& to_change)
00079 {
00080 int i(0);
00081 size_t pos;
00082
00083 while(g_shell_chars[i] != 0) {
00084 pos = 0;
00085 while((pos = to_change.find(g_shell_chars[i], pos)) != string::npos) {
00086 to_change.insert(pos, 1, '\\');
00087 pos += 2;
00088 }
00089 ++i;
00090 }
00091 }
00092
00094
00098 void expand_tilde(string& tochange) {
00099
00100 if ((! tochange.empty()) && (tochange[0] == '~')) {
00101 if ((tochange.length() == 1) || (tochange[1] == '/')) {
00102 char *tmp = getenv("HOME");
00103 string home = (tmp ? tmp : "/");
00104 tochange.replace(0, 1, home);
00105 }
00106 }
00107 }
00108
00109
00111
00115 bool make_dir(const std::string& dir)
00116 {
00117 int rc;
00118
00119 if (dir.length() < 2) {
00120 return mkdir(dir.c_str(), 0777);
00121 } else {
00122 bool cont(true);
00123 int pos = dir.find('/', 0);
00124 while (cont && (pos != string::npos)) {
00125 cont = (((rc = mkdir(dir.substr(0, pos).c_str(), 0777)) == 0) || (errno == EEXIST));
00126 pos = dir.find('/', pos+1);
00127
00128 }
00129 rc = mkdir(dir.c_str(), 0777);
00130 }
00131
00132 return ((rc == 0) || (errno == EEXIST));
00133 }