]> git.lyx.org Git - lyx.git/blob - src/support/filename.C
* src/encoding.C (latexChar,read):
[lyx.git] / src / support / filename.C
1 /**
2  * \file filename.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/filename.h"
14 #include "support/filetools.h"
15 #include "support/lstrings.h"
16 #include "support/os.h"
17 #include "support/qstring_helpers.h"
18
19 #include <QFile>
20
21 #include <boost/assert.hpp>
22
23 #include <map>
24 #include <sstream>
25 #include <algorithm>
26
27
28 using std::map;
29 using std::string;
30
31
32 namespace lyx {
33 namespace support {
34
35
36 FileName::FileName()
37 {}
38
39
40 FileName::~FileName()
41 {}
42
43
44 FileName::FileName(string const & abs_filename)
45         : name_(abs_filename)
46 {
47         BOOST_ASSERT(empty() || absolutePath(name_));
48 #if defined(_WIN32)
49         BOOST_ASSERT(!contains(name_, '\\'));
50 #endif
51 }
52
53
54 void FileName::set(string const & name)
55 {
56         name_ = name;
57         BOOST_ASSERT(absolutePath(name_));
58 #if defined(_WIN32)
59         BOOST_ASSERT(!contains(name_, '\\'));
60 #endif
61 }
62
63
64 void FileName::erase()
65 {
66         name_.erase();
67 }
68
69
70 string const FileName::toFilesystemEncoding() const
71 {
72         QByteArray const encoded = QFile::encodeName(toqstr(name_));
73         return string(encoded.begin(), encoded.end());
74 }
75
76
77 FileName const FileName::fromFilesystemEncoding(string const & name)
78 {
79         QByteArray const encoded(name.c_str(), name.length());
80         return FileName(fromqstr(QFile::decodeName(encoded)));
81 }
82
83
84 bool operator==(FileName const & lhs, FileName const & rhs)
85 {
86         return lhs.absFilename() == rhs.absFilename();
87 }
88
89
90 bool operator!=(FileName const & lhs, FileName const & rhs)
91 {
92         return lhs.absFilename() != rhs.absFilename();
93 }
94
95
96 bool operator<(FileName const & lhs, FileName const & rhs)
97 {
98         return lhs.absFilename() < rhs.absFilename();
99 }
100
101
102 bool operator>(FileName const & lhs, FileName const & rhs)
103 {
104         return lhs.absFilename() > rhs.absFilename();
105 }
106
107
108 std::ostream & operator<<(std::ostream & os, FileName const & filename)
109 {
110         return os << filename.absFilename();
111 }
112
113
114 DocFileName::DocFileName()
115         : save_abs_path_(true)
116 {}
117
118
119 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
120         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
121 {}
122
123
124 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
125         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
126 {}
127
128
129 void DocFileName::set(string const & name, string const & buffer_path)
130 {
131         save_abs_path_ = absolutePath(name);
132         name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename();
133         zipped_valid_ = false;
134 }
135
136
137 void DocFileName::erase()
138 {
139         name_.erase();
140         zipped_valid_ = false;
141 }
142
143
144 string const DocFileName::relFilename(string const & path) const
145 {
146         // FIXME UNICODE
147         return to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
148 }
149
150
151 string const DocFileName::outputFilename(string const & path) const
152 {
153         // FIXME UNICODE
154         return save_abs_path_ ? name_ : to_utf8(makeRelPath(from_utf8(name_), from_utf8(path)));
155 }
156
157
158 string const DocFileName::mangledFilename(std::string const & dir) const
159 {
160         // We need to make sure that every DocFileName instance for a given
161         // filename returns the same mangled name.
162         typedef map<string, string> MangledMap;
163         static MangledMap mangledNames;
164         MangledMap::const_iterator const it = mangledNames.find(name_);
165         if (it != mangledNames.end())
166                 return (*it).second;
167
168         // Now the real work
169         string mname = os::internal_path(name_);
170         // Remove the extension.
171         mname = changeExtension(name_, string());
172         // The mangled name must be a valid LaTeX name.
173         // The list of characters to keep is probably over-restrictive,
174         // but it is not really a problem.
175         // Apart from non-ASCII characters, at least the following characters
176         // are forbidden: '/', '.', ' ', and ':'.
177         // On windows it is not possible to create files with '<', '>' or '?'
178         // in the name.
179         static string const keep = "abcdefghijklmnopqrstuvwxyz"
180                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
181                                    "+,-0123456789;=";
182         string::size_type pos = 0;
183         while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
184                 mname[pos++] = '_';
185         // Add the extension back on
186         mname = changeExtension(mname, getExtension(name_));
187
188         // Prepend a counter to the filename. This is necessary to make
189         // the mangled name unique.
190         static int counter = 0;
191         std::ostringstream s;
192         s << counter++ << mname;
193         mname = s.str();
194
195         // MiKTeX's YAP (version 2.4.1803) crashes if the file name
196         // is longer than about 160 characters. MiKTeX's pdflatex
197         // is even pickier. A maximum length of 100 has been proven to work.
198         // If dir.size() > max length, all bets are off for YAP. We truncate
199         // the filename nevertheless, keeping a minimum of 10 chars.
200
201         string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
202
203         // If the mangled file name is too long, hack it to fit.
204         // We know we're guaranteed to have a unique file name because
205         // of the counter.
206         if (mname.size() > max_length) {
207                 int const half = (int(max_length) / 2) - 2;
208                 if (half > 0) {
209                         mname = mname.substr(0, half) + "___" +
210                                 mname.substr(mname.size() - half);
211                 }
212         }
213
214         mangledNames[name_] = mname;
215         return mname;
216 }
217
218
219 bool DocFileName::isZipped() const
220 {
221         if (!zipped_valid_) {
222                 zipped_ = zippedFile(*this);
223                 zipped_valid_ = true;
224         }
225         return zipped_;
226 }
227
228
229 string const DocFileName::unzippedFilename() const
230 {
231         return unzippedFileName(name_);
232 }
233
234
235 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
236 {
237         return lhs.absFilename() == rhs.absFilename() &&
238                 lhs.saveAbsPath() == rhs.saveAbsPath();
239 }
240
241
242 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
243 {
244         return !(lhs == rhs);
245 }
246
247 } // namespace support
248 } // namespace lyx