cygpath.h

Go to the documentation of this file.
00001 /* @(#)build/win:$Id: cygpath.h 36947 2010-11-25 16:06:55Z rdm $ */
00002 
00003 /*************************************************************************
00004  * Copyright (C) 1995-2010, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
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    // Get the install location of cygwin.
00019    static char buf[512] = {0};
00020 
00021    if (!buf[0]) {
00022       char pathbuffer[_MAX_PATH] = {0};
00023       // Search for cygpath in PATH environment variable
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    // Convert a cygwin path (/cygdrive/x/...,/home)
00045    // to a native Windows path. Return whether the path was changed.
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             // Don't convert C:/home or even C:/cygwin/home
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

Generated on Tue Jul 5 14:12:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1