00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ROOT_CygPath
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <direct.h>
00015 #include <string>
00016
00017 static const char *GetCygwinRootDir() {
00018
00019 static char buf[512] = {0};
00020
00021 if (!buf[0]) {
00022 char pathbuffer[_MAX_PATH] = {0};
00023
00024 _searchenv( "cygpath.exe", "PATH", pathbuffer );
00025 if( *pathbuffer == '\0' ) {
00026 sprintf(buf, "%c:", _getdrive());
00027 return buf;
00028 }
00029 FILE *pipe = _popen( "cygpath -m /", "rt" );
00030
00031 if (!pipe) return 0;
00032 fgets(buf, sizeof(buf), pipe);
00033 int len = strlen(buf);
00034 while (buf[len - 1] == '\n' || buf[len - 1] == '\r') {
00035 buf[len - 1] = 0;
00036 }
00037 if (!feof(pipe)) _pclose(pipe);
00038 else fprintf(stderr, "GetCygwinRootDir() error: Failed to read the pipe to the end.\n");
00039 }
00040 return buf;
00041 }
00042
00043 static bool FromCygToNativePath(std::string& path) {
00044
00045
00046 static std::string cygRoot;
00047 size_t posCygDrive = path.find("/cygdrive/");
00048 if (posCygDrive != std::string::npos) {
00049 path[posCygDrive] = path[posCygDrive + 10];
00050 path[posCygDrive + 1] = ':';
00051 path.erase(posCygDrive + 2, 9);
00052 return true;
00053 } else {
00054 size_t posHome = path.find("/home/");
00055 if (posHome != std::string::npos) {
00056 size_t posColumn = path.find(":");
00057 if (posColumn != std::string::npos && posColumn > 0) {
00058
00059 if (path[posColumn - 1] >= 'A' && path[posColumn - 1] <= 'Z')
00060 return false;
00061 if (path[posColumn - 1] >= 'a' && path[posColumn - 1] <= 'z')
00062 return false;
00063 }
00064 if (cygRoot.empty()) {
00065 cygRoot = GetCygwinRootDir();
00066 size_t len = cygRoot.length();
00067 if (cygRoot[len - 1] == '/') {
00068 cygRoot.erase(len - 1);
00069 }
00070 }
00071 path.insert(posHome, cygRoot);
00072 return true;
00073 }
00074 }
00075 return false;
00076 }
00077
00078 #endif // ROOT_CygPath