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