]> git.lyx.org Git - lyx.git/blob - src/support/filename.C
remove unused stuff
[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
18 #include <boost/assert.hpp>
19
20 #include <map>
21 #include <sstream>
22 #include <algorithm>
23
24
25 using std::map;
26 using std::string;
27
28
29 namespace lyx {
30 namespace support {
31
32
33 FileName::FileName()
34 {}
35
36
37 FileName::~FileName()
38 {}
39
40
41 FileName::FileName(string const & abs_filename)
42         : name_(abs_filename)
43 {
44         BOOST_ASSERT(absolutePath(name_));
45 }
46
47
48 void FileName::set(string const & name)
49 {
50         name_ = name;
51         BOOST_ASSERT(absolutePath(name_));
52 }
53
54
55 void FileName::erase()
56 {
57         name_.erase();
58 }
59
60
61 string const FileName::toFilesystemEncoding() const
62 {
63         // FIXME UNICODE: correct encoding not implemented yet
64         return name_;
65 }
66
67
68 bool operator==(FileName const & lhs, FileName const & rhs)
69 {
70         return lhs.absFilename() == rhs.absFilename();
71 }
72
73
74 bool operator!=(FileName const & lhs, FileName const & rhs)
75 {
76         return lhs.absFilename() != rhs.absFilename();
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 std::ostream & operator<<(std::ostream & os, FileName const & filename)
93 {
94         return os << filename.absFilename();
95 }
96
97
98 DocFileName::DocFileName()
99         : save_abs_path_(true)
100 {}
101
102
103 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
104         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
105 {}
106
107
108 void DocFileName::set(string const & name, string const & buffer_path)
109 {
110         save_abs_path_ = absolutePath(name);
111         name_ = save_abs_path_ ? name : makeAbsPath(name, buffer_path);
112         zipped_valid_ = false;
113 }
114
115
116 void DocFileName::erase()
117 {
118         name_.erase();
119         zipped_valid_ = false;
120 }
121
122
123 string const DocFileName::relFilename(string const & path) const
124 {
125         return makeRelPath(name_, path);
126 }
127
128
129 string const DocFileName::outputFilename(string const & path) const
130 {
131         return save_abs_path_ ? name_ : makeRelPath(name_, path);
132 }
133
134
135 string const DocFileName::mangledFilename(std::string const & dir) const
136 {
137         // We need to make sure that every DocFileName instance for a given
138         // filename returns the same mangled name.
139         typedef map<string, string> MangledMap;
140         static MangledMap mangledNames;
141         MangledMap::const_iterator const it = mangledNames.find(name_);
142         if (it != mangledNames.end())
143                 return (*it).second;
144
145         // Now the real work
146         string mname = os::internal_path(name_);
147         // Remove the extension.
148         mname = changeExtension(name_, string());
149         // Replace '/' in the file name with '_'
150         mname = subst(mname, "/", "_");
151         // Replace '.' in the file name with '_'
152         mname = subst(mname, ".", "_");
153         // Replace ' ' in the file name with '_'
154         mname = subst(mname, " ", "_");
155         // Replace ':' in the file name with '_'
156         mname = subst(mname, ":", "_");
157         // Add the extension back on
158         mname = changeExtension(mname, getExtension(name_));
159
160         // Prepend a counter to the filename. This is necessary to make
161         // the mangled name unique.
162         static int counter = 0;
163         std::ostringstream s;
164         s << counter++ << mname;
165         mname = s.str();
166
167         // MiKTeX's YAP (version 2.4.1803) crashes if the file name
168         // is longer than about 160 characters. MiKTeX's pdflatex
169         // is even pickier. A maximum length of 100 has been proven to work.
170         // If dir.size() > max length, all bets are off for YAP. We truncate
171         // the filename nevertheless, keeping a minimum of 10 chars.
172
173         string::size_type max_length = std::max(100 - ((int)dir.size() + 1), 10);
174
175         // If the mangled file name is too long, hack it to fit.
176         // We know we're guaranteed to have a unique file name because
177         // of the counter.
178         if (mname.size() > max_length) {
179                 int const half = (int(max_length) / 2) - 2;
180                 if (half > 0) {
181                         mname = mname.substr(0, half) + "___" +
182                                 mname.substr(mname.size() - half);
183                 }
184         }
185
186         mangledNames[name_] = mname;
187         return mname;
188 }
189
190
191 bool DocFileName::isZipped() const
192 {
193         if (!zipped_valid_) {
194                 zipped_ = zippedFile(*this);
195                 zipped_valid_ = true;
196         }
197         return zipped_;
198 }
199
200
201 string const DocFileName::unzippedFilename() const
202 {
203         return unzippedFileName(name_);
204 }
205
206
207 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
208 {
209         return lhs.absFilename() == rhs.absFilename() &&
210                 lhs.saveAbsPath() == rhs.saveAbsPath();
211 }
212
213
214 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
215 {
216         return !(lhs == rhs);
217 }
218
219 } // namespace support
220 } // namespace lyx