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