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