]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
a9c040c6c9580ea2d366569c67e7b313b12564ad
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
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/docstring.h"
17 #include "support/FileName.h"
18 #include "support/lstrings.h"
19
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
23
24 #ifdef __APPLE__
25 #include <Carbon/Carbon.h>
26 #endif
27
28 using namespace std;
29
30 namespace lyx {
31 namespace support {
32 namespace os {
33
34 void init(int, char *[])
35 {}
36
37
38 string current_root()
39 {
40         return "/";
41 }
42
43
44 bool isFilesystemCaseSensitive()
45 {
46 #ifdef __APPLE__
47         return false;
48 #else
49         return true;
50 #endif
51 }
52
53
54 docstring::size_type common_path(docstring const & p1, docstring const & p2)
55 {
56         docstring::size_type i = 0;
57         docstring::size_type const p1_len = p1.length();
58         docstring::size_type const p2_len = p2.length();
59         while (i < p1_len && i < p2_len && p1[i] == 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         return p;
77 }
78
79
80 string internal_path(string const & p)
81 {
82         return p;
83 }
84
85
86 string external_path_list(string const & p)
87 {
88         return p;
89 }
90
91
92 string internal_path_list(string const & p)
93 {
94         return p;
95 }
96
97
98 string latex_path(string const & p)
99 {
100         return p;
101 }
102
103
104 bool is_valid_strftime(string const & p)
105 {
106         string::size_type pos = p.find_first_of('%');
107         while (pos != string::npos) {
108                 if (pos + 1 == string::npos)
109                         break;
110                 if (!containsOnly(p.substr(pos + 1, 1),
111                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
112                         return false;
113                 if (pos + 2 == string::npos)
114                       break;
115                 pos = p.find_first_of('%', pos + 2);
116         }
117         return true;
118 }
119
120
121 char const * popen_read_mode()
122 {
123         return "r";
124 }
125
126
127 string const & nulldev()
128 {
129         static string const nulldev_ = "/dev/null";
130         return nulldev_;
131 }
132
133
134 shell_type shell()
135 {
136         return UNIX;
137 }
138
139
140 char path_separator()
141 {
142         return ':';
143 }
144
145
146 void windows_style_tex_paths(bool)
147 {}
148
149 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
150 {
151 #ifdef __APPLE__
152 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
153         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
154                                         (UInt8 *) ext.c_str(), ext.length(),
155                                         kCFStringEncodingISOLatin1, false);
156         // this is what we would like to do but it seems that the
157         // viewer for PDF is often quicktime...
158         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
159         (void)mode;
160         LSRolesMask role = kLSRolesAll;
161         FSRef outAppRef;
162         OSStatus status =
163                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
164                                         cfs_ext, role, &outAppRef, NULL);
165         CFRelease(cfs_ext);
166
167         return status != kLSApplicationNotFoundErr;
168 #else
169         // silence compiler warnings
170         (void)ext;
171         (void)mode;
172
173         // currently, no default viewer is tried for non-windows system
174         // support for KDE/Gnome/Macintosh may be added later
175         return false;
176 #endif
177 }
178
179
180 bool autoOpenFile(string const & filename, auto_open_mode const mode)
181 {
182 #ifdef __APPLE__
183 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
184         FSRef fileref;
185         OSStatus status =
186                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
187         if (status != 0)
188                 return false;
189
190         // this is what we would like to do but it seems that the
191         // viewer for PDF is often quicktime...
192         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
193         (void)mode;
194         LSRolesMask role = kLSRolesAll;
195         FSRef outAppRef;
196
197         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
198         if (status == kLSApplicationNotFoundErr)
199                 return false;
200
201         LSLaunchFSRefSpec inLaunchSpec;
202         inLaunchSpec.appRef = &outAppRef;
203         inLaunchSpec.numDocs = 1;
204         inLaunchSpec.itemRefs = &fileref;
205         inLaunchSpec.passThruParams = NULL;
206         inLaunchSpec.launchFlags = kLSLaunchDefaults;
207         inLaunchSpec.asyncRefCon = NULL;
208         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
209
210         return status != kLSApplicationNotFoundErr;
211 #else
212         // silence compiler warnings
213         (void)filename;
214         (void)mode;
215
216         // currently, no default viewer is tried for non-windows system
217         // support for KDE/Gnome/Macintosh may be added later
218         return false;
219 #endif
220 }
221
222
223 bool isSameFile(string const & fileone, string const & filetwo)
224 {
225         struct stat st1;
226         struct stat st2;
227
228         if (::stat(fileone.c_str(), &st1) == 0
229             && ::stat(filetwo.c_str(), &st2) == 0) {
230                 return st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev;
231         }
232
233         // One or both files cannot be accessed.
234         return false;
235 }
236
237
238 string real_path(string const & path)
239 {
240         char rpath[PATH_MAX + 1];
241         char * result = realpath(path.c_str(), rpath);
242         return FileName::fromFilesystemEncoding(result ? rpath : path).absFilename();
243 }
244
245 } // namespace os
246 } // namespace support
247 } // namespace lyx