]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
The package reworking.
[lyx.git] / src / support / os_win32.C
1 /**
2  * \file os_win32.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16
17 #include "debug.h"
18
19 #include <windows.h>
20 #include <io.h>
21
22 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
23 # include <sys/cygwin.h>
24
25 #elif defined(_WIN32)
26 # include <direct.h> // _getdrive
27 #endif
28
29 using std::endl;
30 using std::string;
31
32
33 namespace lyx {
34 namespace support {
35 namespace os {
36
37 void init(int, char *[])
38 {}
39
40
41 string current_root()
42 {
43 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
44         return string("/");
45
46 #else
47         // _getdrive returns the current drive (1=A, 2=B, and so on).
48         char const drive = ::_getdrive() + 'A' - 1;
49         return string(1, drive) + ":/";
50 #endif
51 }
52
53
54 string::size_type common_path(string const & p1, string const & p2)
55 {
56         string::size_type i = 0;
57         string::size_type       p1_len = p1.length();
58         string::size_type       p2_len = p2.length();
59         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
60                 ++i;
61         if ((i < p1_len && i < p2_len)
62             || (i < p1_len && p1[i] != '/' && i == p2_len)
63             || (i < p2_len && p2[i] != '/' && i == p1_len))
64         {
65                 if (i)
66                         --i;     // here was the last match
67                 while (i && p1[i] != '/')
68                         --i;
69         }
70         return i;
71 }
72
73
74 string external_path(string const & p)
75 {
76         string dos_path;
77
78 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
79         // Translate from cygwin path syntax to dos path syntax
80         if (is_absolute_path(p)) {
81                 char dp[PATH_MAX];
82                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
83                 dos_path = !dp ? "" : dp;
84         }
85
86         else return p;
87 #else // regular Win32
88         dos_path = p;
89 #endif
90         
91         //No backslashes in LaTeX files
92         dos_path = subst(dos_path,'\\','/');
93
94         lyxerr[Debug::LATEX]
95                 << "<Win32 path correction> ["
96                 << p << "]->>["
97                 << dos_path << ']' << endl;
98         return dos_path;
99 }
100
101
102 // (Claus H.) Parsing the latex log file in an Win32 environment all
103 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
104 // entries to check if any file has been changed we must retranslate
105 // the Win32/DOS pathnames into Cygwin pathnames.
106 string internal_path(string const & p)
107 {
108 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
109         char posix_path[PATH_MAX];
110         posix_path[0] = '\0';
111         cygwin_conv_to_posix_path(p.c_str(), posix_path);
112         return posix_path;
113 #else
114         return subst(p,"\\","/");
115 #endif
116 }
117
118
119 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
120 // Therefore an absolute path could be either a pathname starting
121 // with a slash (Unix) or a pathname starting with a drive letter
122 // followed by a colon. Because a colon is not valid in pathes in Unix
123 // and at another location in Win32 testing just for the existance
124 // of the colon in the 2nd position seems to be enough!
125 bool is_absolute_path(string const & p)
126 {
127         if (p.empty())
128                 return false;
129
130         bool isDosPath = (p.length() > 1 && p[1] == ':');
131         bool isUnixPath = (p[0] == '/');
132
133         return isDosPath || isUnixPath;
134 }
135
136
137 // returns a string suitable to be passed to popen when
138 // reading a pipe
139 char const * popen_read_mode()
140 {
141         return "r";
142 }
143
144
145 string const & nulldev()
146 {
147 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
148         static string const nulldev_ = "/dev/null";
149 #else
150         static string const nulldev_ = "nul";
151 #endif
152         return nulldev_;
153 }
154
155
156 shell_type shell()
157 {
158 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
159         return UNIX;
160 #else
161         return CMD_EXE;
162 #endif
163 }
164
165 } // namespace os
166 } // namespace support
167 } // namespace lyx