]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
Standard blurb.
[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 "os.h"
16 #include "support/filetools.h"
17 #include "support/lstrings.h"
18 #include "debug.h"
19
20 #include <windows.h>
21 #include <io.h>
22 #include <sys/cygwin.h>
23
24 using std::endl;
25
26
27 namespace {
28
29 string binpath_;
30 string binname_;
31 string tmpdir_;
32
33 }
34
35
36 namespace os {
37
38 void init(int * /* argc */, char ** argv[])
39 {
40         static bool initialized = false;
41         if (initialized)
42                 return;
43         initialized = true;
44
45         string tmp = *argv[0];
46         binname_ = OnlyFilename(tmp);
47         tmp = ExpandPath(tmp); // This expands ./ and ~/
48
49         if (!is_absolute_path(tmp)) {
50                 string binsearchpath = GetEnvPath("PATH");
51                 // This will make "src/lyx" work always :-)
52                 binsearchpath += ";.";
53                 tmp = *argv[0];
54                 tmp = FileOpenSearch(binsearchpath, tmp);
55         }
56
57         tmp = MakeAbsPath(OnlyPath(tmp));
58
59         // In case we are running in place and compiled with shared libraries
60         if (suffixIs(tmp, "/.libs/"))
61                 tmp.erase(tmp.length()-6, string::npos);
62         binpath_ = tmp;
63 }
64
65
66 void warn(string const & mesg)
67 {
68         MessageBox(0, mesg.c_str(), "LyX error",
69         MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL);
70 }
71
72
73 string current_root()
74 {
75         return "/";
76 }
77
78
79 string::size_type common_path(string const & p1, string const & p2)
80 {
81         string::size_type i = 0;
82         string::size_type       p1_len = p1.length();
83         string::size_type       p2_len = p2.length();
84         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
85                 ++i;
86         if ((i < p1_len && i < p2_len)
87             || (i < p1_len && p1[i] != '/' && i == p2_len)
88             || (i < p2_len && p2[i] != '/' && i == p1_len))
89         {
90                 if (i)
91                         --i;     // here was the last match
92                 while (i && p1[i] != '/')
93                         --i;
94         }
95         return i;
96 }
97
98
99 string slashify_path(string const & p)
100 {
101         return subst(p, '\\', '/');
102 }
103
104
105 string external_path(string const & p)
106 {
107         string dos_path = p;
108         if (is_absolute_path(p)) {
109                 char dp[255];
110                 cygwin_conv_to_full_win32_path(p.c_str(), dp);
111                 dos_path = subst(dp,'\\','/');
112         }
113         lyxerr[Debug::LATEX]
114                 << "<Win32 path correction> ["
115                 << p << "]->>["
116                 << dos_path << ']' << endl;
117         return dos_path;
118 }
119
120
121 // (Claus H.) Parsing the latex log file in an Win32 environment all
122 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
123 // entries to check if any file has been changed we must retranslate
124 // the Win32/DOS pathnames into Cygwin pathnames.
125 string internal_path(string const & p)
126 {
127         char pp[256];
128         cygwin_conv_to_posix_path(p.c_str(), pp);
129         string const posix_path = MakeLatexName(pp);
130         lyxerr[Debug::DEPEND]
131                 << "<Win32 path correction> ["
132                 << p << "]->>["
133                 << posix_path << ']' << endl;
134         return posix_path;
135 }
136
137
138 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
139 // Therefore an absolute path could be either a pathname starting
140 // with a slash (Unix) or a pathname starting with a drive letter
141 // followed by a colon. Because a colon is not valid in pathes in Unix
142 // and at another location in Win32 testing just for the existance
143 // of the colon in the 2nd position seems to be enough!
144 bool is_absolute_path(string const & p)
145 {
146         if (p.empty())
147                 return false;
148
149         bool isDosPath = (p.length() > 1 && p[1] == ':');
150         bool isUnixPath = (p[0] == '/');
151
152         return isDosPath || isUnixPath;
153 }
154
155
156 // returns a string suitable to be passed to popen when
157 // reading a pipe
158 char const * popen_read_mode()
159 {
160         return "r";
161 }
162
163
164 string binpath()
165 {
166         return binpath_;
167 }
168
169
170 string binname()
171 {
172         return binname_;
173 }
174
175
176 void setTmpDir(string const & p)
177 {
178         tmpdir_ = p;
179 }
180
181
182 string getTmpDir()
183 {
184         return tmpdir_;
185 }
186
187
188 shell_type shell()
189 {
190         return UNIX;
191 }
192
193 } // end namespace os