Logo ROOT  
Reference Guide
cygpath.h
Go to the documentation of this file.
1/* @(#)build/win:$Id$ */
2
3/*************************************************************************
4 * Copyright (C) 1995-2010, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_CygPath
12#include <stdio.h>
13#include <stdlib.h>
14#include <direct.h>
15#include <string>
16
17static const char *GetCygwinRootDir() {
18 // Get the install location of cygwin.
19 static char buf[512] = {0};
20
21 if (!buf[0]) {
22 char pathbuffer[_MAX_PATH] = {0};
23 // Search for cygpath in PATH environment variable
24 _searchenv( "cygpath.exe", "PATH", pathbuffer );
25 if( *pathbuffer == '\0' ) {
26 sprintf(buf, "%c:", _getdrive());
27 return buf;
28 }
29 FILE *pipe = _popen( "cygpath -m /", "rt" );
30
31 if (!pipe) return 0;
32 fgets(buf, sizeof(buf), pipe);
33 int len = strlen(buf);
34 while (buf[len - 1] == '\n' || buf[len - 1] == '\r') {
35 buf[len - 1] = 0;
36 }
37 if (!feof(pipe)) _pclose(pipe);
38 else fprintf(stderr, "GetCygwinRootDir() error: Failed to read the pipe to the end.\n");
39 }
40 return buf;
41}
42
43static bool FromCygToNativePath(std::string& path) {
44 // Convert a cygwin path (/cygdrive/x/...,/home)
45 // to a native Windows path. Return whether the path was changed.
46 static std::string cygRoot;
47 size_t posCygDrive = path.find("/cygdrive/");
48 if (posCygDrive != std::string::npos) {
49 path[posCygDrive] = path[posCygDrive + 10];
50 path[posCygDrive + 1] = ':';
51 path.erase(posCygDrive + 2, 9);
52 return true;
53 } else {
54 size_t posHome = path.find("/home/");
55 if (posHome != std::string::npos) {
56 size_t posColumn = path.find(":");
57 if (posColumn != std::string::npos && posColumn > 0) {
58 // Don't convert C:/home or even C:/cygwin/home
59 if (path[posColumn - 1] >= 'A' && path[posColumn - 1] <= 'Z')
60 return false;
61 if (path[posColumn - 1] >= 'a' && path[posColumn - 1] <= 'z')
62 return false;
63 }
64 if (cygRoot.empty()) {
65 cygRoot = GetCygwinRootDir();
66 size_t len = cygRoot.length();
67 if (cygRoot[len - 1] == '/') {
68 cygRoot.erase(len - 1);
69 }
70 }
71 path.insert(posHome, cygRoot);
72 return true;
73 }
74 }
75 return false;
76}
77
78#endif // ROOT_CygPath
static bool FromCygToNativePath(std::string &path)
Definition: cygpath.h:43
static const char * GetCygwinRootDir()
Definition: cygpath.h:17