]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.C
tostr -> convert and some bformat work
[lyx.git] / src / support / os_unix.C
1 /**
2  * \file os_unix.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
20 using std::string;
21
22
23 namespace {
24
25 string binpath_;
26 string binname_;
27 string tmpdir_;
28 string homepath_;
29 string nulldev_;
30
31 }
32
33 namespace lyx {
34 namespace support {
35 namespace os {
36
37 void init(int /*argc*/, char * argv[])
38 {
39         static bool initialized = false;
40         if (initialized)
41                 return;
42         initialized = true;
43
44         string tmp = argv[0];
45         binname_ = OnlyFilename(tmp);
46         tmp = ExpandPath(tmp); // This expands ./ and ~/
47         if (!is_absolute_path(tmp)) {
48                 string binsearchpath = GetEnvPath("PATH");
49                 // This will make "src/lyx" work always :-)
50                 binsearchpath += ";.";
51                 tmp = FileOpenSearch(binsearchpath, tmp);
52         }
53
54         tmp = MakeAbsPath(OnlyPath(tmp));
55
56         // In case we are running in place and compiled with shared libraries
57         if (suffixIs(tmp, "/.libs/"))
58                 tmp.erase(tmp.length() - 6, string::npos);
59         binpath_ = tmp;
60
61         tmpdir_ = "/tmp";
62         homepath_ = GetEnvPath("HOME");
63         nulldev_ = "/dev/null";
64 }
65
66
67 void warn(string const & /*mesg*/)
68 {
69         return;
70 }
71
72
73 string current_root()
74 {
75         return "/";
76 }
77
78
79 string::size_type common_path(string const & p1, string const & p2)
80 {
81         string::size_type i = 0;
82         string::size_type p1_len = p1.length();
83         string::size_type p2_len = p2.length();
84         while (i < p1_len && i < p2_len && p1[i] == p2[i])
85                 ++i;
86         if ((i < p1_len && i < p2_len)
87             || (i < p1_len && p1[i] != '/' && i == p2_len)
88             || (i < p2_len && p2[i] != '/' && i == p1_len))
89         {
90                 if (i)
91                         --i;     // here was the last match
92                 while (i && p1[i] != '/')
93                         --i;
94         }
95         return i;
96 }
97
98
99 string external_path(string const & p)
100 {
101         return p;
102 }
103
104
105 string internal_path(string const & p)
106 {
107         return p;
108 }
109
110
111 bool is_absolute_path(string const & p)
112 {
113         return !p.empty() && p[0] == '/';
114 }
115
116
117 char const * popen_read_mode()
118 {
119         return "r";
120 }
121
122
123 string const & binpath()
124 {
125         return binpath_;
126 }
127
128
129 string const & binname()
130 {
131         return binname_;
132 }
133
134
135 void setTmpDir(string const & p)
136 {
137         tmpdir_ = p;
138 }
139
140
141 string const & getTmpDir()
142 {
143         return tmpdir_;
144 }
145
146
147 string const & homepath()
148 {
149         return homepath_;
150 }
151
152
153 string const & nulldev()
154 {
155         return nulldev_;
156 }
157
158
159 shell_type shell()
160 {
161         return UNIX;
162 }
163
164 } // namespace os
165 } // namespace support
166 } // namespace lyx