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