]> git.lyx.org Git - lyx.git/blob - src/support/filename.C
Fix another instance of filename encoding problems
[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         BOOST_ASSERT(!contains(name_, '\\'));
49 }
50
51
52 void FileName::set(string const & name)
53 {
54         name_ = name;
55         BOOST_ASSERT(absolutePath(name_));
56         BOOST_ASSERT(!contains(name_, '\\'));
57 }
58
59
60 void FileName::erase()
61 {
62         name_.erase();
63 }
64
65
66 string const FileName::toFilesystemEncoding() const
67 {
68         QByteArray const encoded = QFile::encodeName(toqstr(name_));
69         return string(encoded.begin(), encoded.end());
70 }
71
72
73 FileName const FileName::fromFilesystemEncoding(string const & name)
74 {
75         QByteArray const encoded(name.c_str(), name.length());
76         return FileName(fromqstr(QFile::decodeName(encoded)));
77 }
78
79
80 bool operator==(FileName const & lhs, FileName const & rhs)
81 {
82         return lhs.absFilename() == rhs.absFilename();
83 }
84
85
86 bool operator!=(FileName const & lhs, FileName const & rhs)
87 {
88         return lhs.absFilename() != rhs.absFilename();
89 }
90
91
92 bool operator<(FileName const & lhs, FileName const & rhs)
93 {
94         return lhs.absFilename() < rhs.absFilename();
95 }
96
97
98 bool operator>(FileName const & lhs, FileName const & rhs)
99 {
100         return lhs.absFilename() > rhs.absFilename();
101 }
102
103
104 std::ostream & operator<<(std::ostream & os, FileName const & filename)
105 {
106         return os << filename.absFilename();
107 }
108
109
110 DocFileName::DocFileName()
111         : save_abs_path_(true)
112 {}
113
114
115 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
116         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
117 {}
118
119
120 void DocFileName::set(string const & name, string const & buffer_path)
121 {
122         save_abs_path_ = absolutePath(name);
123         name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
124         zipped_valid_ = false;
125 }
126
127
128 void DocFileName::erase()
129 {
130         name_.erase();
131         zipped_valid_ = false;
132 }
133
134
135 string const DocFileName::relFilename(string const & path) const
136 {
137         return makeRelPath(name_, path);
138 }
139
140
141 string const DocFileName::outputFilename(string const & path) const
142 {
143         return save_abs_path_ ? name_ : makeRelPath(name_, path);
144 }
145
146
147 string const DocFileName::mangledFilename(std::string const & dir) const
148 {
149         // We need to make sure that every DocFileName instance for a given
150         // filename returns the same mangled name.
151         typedef map<string, string> MangledMap;
152         static MangledMap mangledNames;
153         MangledMap::const_iterator const it = mangledNames.find(name_);
154         if (it != mangledNames.end())
155                 return (*it).second;
156
157         // Now the real work
158         string mname = os::internal_path(name_);
159         // Remove the extension.
160         mname = changeExtension(name_, string());
161         // Replace '/' in the file name with '_'
162         mname = subst(mname, "/", "_");
163         // Replace '.' in the file name with '_'
164         mname = subst(mname, ".", "_");
165         // Replace ' ' in the file name with '_'
166         mname = subst(mname, " ", "_");
167         // Replace ':' in the file name with '_'
168         mname = subst(mname, ":", "_");
169         // Add the extension back on
170         mname = changeExtension(mname, getExtension(name_));
171
172         // Prepend a counter to the filename. This is necessary to make
173         // the mangled name unique.
174         static int counter = 0;
175         std::ostringstream s;
176         s << counter++ << mname;
177         mname = s.str();
178
179         // MiKTeX's YAP (version 2.4.1803) crashes if the file name
180         // is longer than about 160 characters. MiKTeX's pdflatex
181         // is even pickier. A maximum length of 100 has been proven to work.
182         // If dir.size() > max length, all bets are off for YAP. We truncate
183         // the filename nevertheless, keeping a minimum of 10 chars.
184
185         string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
186
187         // If the mangled file name is too long, hack it to fit.
188         // We know we're guaranteed to have a unique file name because
189         // of the counter.
190         if (mname.size() > max_length) {
191                 int const half = (int(max_length) / 2) - 2;
192                 if (half > 0) {
193                         mname = mname.substr(0, half) + "___" +
194                                 mname.substr(mname.size() - half);
195                 }
196         }
197
198         mangledNames[name_] = mname;
199         return mname;
200 }
201
202
203 bool DocFileName::isZipped() const
204 {
205         if (!zipped_valid_) {
206                 zipped_ = zippedFile(*this);
207                 zipped_valid_ = true;
208         }
209         return zipped_;
210 }
211
212
213 string const DocFileName::unzippedFilename() const
214 {
215         return unzippedFileName(name_);
216 }
217
218
219 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
220 {
221         return lhs.absFilename() == rhs.absFilename() &&
222                 lhs.saveAbsPath() == rhs.saveAbsPath();
223 }
224
225
226 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
227 {
228         return !(lhs == rhs);
229 }
230
231 } // namespace support
232 } // namespace lyx