]> git.lyx.org Git - lyx.git/blob - src/support/FileName.cpp
e35546860db9f51e40328e12efa0eefaeb640ce0
[lyx.git] / src / support / FileName.cpp
1 /**
2  * \file FileName.cpp
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/FileNameList.h"
15
16 #include "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/lyxlib.h"
20 #include "support/os.h"
21 #include "support/qstring_helpers.h"
22
23 #include <QDateTime>
24 #include <QDir>
25 #include <QFile>
26 #include <QFileInfo>
27 #include <QList>
28 #include <QTime>
29
30 #include <boost/assert.hpp>
31
32 #include <map>
33 #include <sstream>
34 #include <fstream>
35 #include <algorithm>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 #endif
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifdef HAVE_DIRECT_H
47 # include <direct.h>
48 #endif
49 #ifdef _WIN32
50 # include <windows.h>
51 #endif
52
53 #include <fcntl.h>
54 #include <cerrno>
55
56 using namespace std;
57
58 namespace lyx {
59 namespace support {
60
61
62 /////////////////////////////////////////////////////////////////////
63 //
64 // FileName::Private
65 //
66 /////////////////////////////////////////////////////////////////////
67
68 struct FileName::Private
69 {
70         Private() {}
71
72         Private(string const & abs_filename) : fi(toqstr(abs_filename))
73         {
74                 fi.setCaching(fi.exists() ? true : false);
75         }
76         ///
77         QFileInfo fi;
78 };
79
80 /////////////////////////////////////////////////////////////////////
81 //
82 // FileName
83 //
84 /////////////////////////////////////////////////////////////////////
85
86
87 FileName::FileName() : d(new Private)
88 {
89 }
90
91
92 FileName::FileName(string const & abs_filename)
93         : d(abs_filename.empty() ? new Private : new Private(abs_filename))
94 {
95 }
96
97
98 FileName::~FileName()
99 {
100         delete d;
101 }
102
103
104 FileName::FileName(FileName const & rhs) : d(new Private)
105 {
106         d->fi = rhs.d->fi;
107 }
108
109
110 FileName & FileName::operator=(FileName const & rhs)
111 {
112         d->fi = rhs.d->fi;
113         return *this;
114 }
115
116
117 bool FileName::empty() const
118 {
119         return d->fi.absoluteFilePath().isEmpty();
120 }
121
122
123 string FileName::absFilename() const
124 {
125         return fromqstr(d->fi.absoluteFilePath());
126 }
127
128
129 void FileName::set(string const & name)
130 {
131         d->fi.setFile(toqstr(name));
132         BOOST_ASSERT(d->fi.isAbsolute());
133 }
134
135
136 void FileName::erase()
137 {
138         d->fi = QFileInfo();
139 }
140
141
142 bool FileName::copyTo(FileName const & name, bool overwrite) const
143 {
144         if (overwrite)
145                 QFile::remove(name.d->fi.absoluteFilePath());
146         bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
147         if (!success)
148                 lyxerr << "FileName::copyTo(): Could not copy file "
149                         << *this << " to " << name << endl;
150         return success;
151 }
152
153
154 bool FileName::renameTo(FileName const & name) const
155 {
156         bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
157         if (!success)
158                 LYXERR0("Could not rename file " << *this << " to " << name);
159         return success;
160 }
161
162
163 bool FileName::moveTo(FileName const & name) const
164 {
165         if (name.exists() && !name.removeFile())
166                 return false;
167
168         bool success = QFile::rename(d->fi.absoluteFilePath(),
169                 name.d->fi.absoluteFilePath());
170         if (!success)
171                 LYXERR0("Could not move file " << *this << " to " << name);
172         return success;
173 }
174
175
176 bool FileName::changePermission(unsigned long int mode) const
177 {
178         if (!isWritable()) {
179                 LYXERR0("File " << *this << " is not writable!");
180                 return false;
181         }
182
183 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
184         if (::chmod(toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
185                 LYXERR0("File " << *this << ": cannot change permission to "
186                         << mode << ".");
187                 return false;
188         }
189 #endif
190         return true;
191 }
192
193
194 string FileName::toFilesystemEncoding() const
195 {
196         QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
197         return string(encoded.begin(), encoded.end());
198 }
199
200
201 FileName FileName::fromFilesystemEncoding(string const & name)
202 {
203         QByteArray const encoded(name.c_str(), name.length());
204         return FileName(fromqstr(QFile::decodeName(encoded)));
205 }
206
207
208 bool FileName::exists() const
209 {
210         return d->fi.exists();
211 }
212
213
214 bool FileName::isSymLink() const
215 {
216         return d->fi.isSymLink();
217 }
218
219
220 bool FileName::isFileEmpty() const
221 {
222         return d->fi.size() == 0;
223 }
224
225
226 bool FileName::isDirectory() const
227 {
228         return d->fi.isDir();
229 }
230
231
232 bool FileName::isReadOnly() const
233 {
234         return d->fi.isReadable() && !d->fi.isWritable();
235 }
236
237
238 bool FileName::isReadableDirectory() const
239 {
240         return d->fi.isDir() && d->fi.isReadable();
241 }
242
243
244 string FileName::onlyFileName() const
245 {
246         return support::onlyFilename(absFilename());
247 }
248
249
250 FileName FileName::onlyPath() const
251 {
252         return FileName(support::onlyPath(absFilename()));
253 }
254
255
256 bool FileName::isReadableFile() const
257 {
258         return d->fi.isFile() && d->fi.isReadable();
259 }
260
261
262 bool FileName::isWritable() const
263 {
264         return d->fi.isWritable();
265 }
266
267
268 bool FileName::isDirWritable() const
269 {
270         LYXERR(Debug::FILES, "isDirWriteable: " << *this);
271
272         FileName const tmpfl(tempName(*this, "lyxwritetest"));
273
274         if (tmpfl.empty())
275                 return false;
276
277         tmpfl.removeFile();
278         return true;
279 }
280
281
282 FileNameList FileName::dirList(string const & ext) const
283 {
284         FileNameList dirlist;
285         if (!isDirectory()) {
286                 LYXERR0("Directory '" << *this << "' does not exist!");
287                 return dirlist;
288         }
289
290         QDir dir = d->fi.absoluteDir();
291
292         if (!ext.empty()) {
293                 QString filter;
294                 switch (ext[0]) {
295                 case '.': filter = "*" + toqstr(ext); break;
296                 case '*': filter = toqstr(ext); break;
297                 default: filter = "*." + toqstr(ext);
298                 }
299                 dir.setNameFilters(QStringList(filter));
300                 LYXERR(Debug::FILES, "filtering on extension "
301                         << fromqstr(filter) << " is requested.");
302         }
303
304         QFileInfoList list = dir.entryInfoList();
305         for (int i = 0; i != list.size(); ++i) {
306                 FileName fi(fromqstr(list.at(i).absoluteFilePath()));
307                 dirlist.push_back(fi);
308                 LYXERR(Debug::FILES, "found file " << fi);
309         }
310
311         return dirlist;
312 }
313
314
315 FileName FileName::tempName(FileName const & dir, string const & mask)
316 {
317         return support::tempName(dir, mask);
318 }
319
320
321 time_t FileName::lastModified() const
322 {
323         return d->fi.lastModified().toTime_t();
324 }
325
326
327 bool FileName::chdir() const
328 {
329         return QDir::setCurrent(d->fi.absoluteFilePath());
330 }
331
332
333 extern unsigned long sum(char const * file);
334
335 unsigned long FileName::checksum() const
336 {
337         if (!exists()) {
338                 LYXERR0("File \"" << absFilename() << "\" does not exist!");
339                 return 0;
340         }
341         // a directory may be passed here so we need to test it. (bug 3622)
342         if (isDirectory()) {
343                 LYXERR0('"' << absFilename() << "\" is a directory!");
344                 return 0;
345         }
346         if (!lyxerr.debugging(Debug::FILES))
347                 return sum(absFilename().c_str());
348
349         QTime t;
350         t.start();
351         unsigned long r = sum(absFilename().c_str());
352         lyxerr << "Checksumming \"" << absFilename() << "\" lasted "
353                 << t.elapsed() << " ms." << endl;
354         return r;
355 }
356
357
358 bool FileName::removeFile() const
359 {
360         bool const success = QFile::remove(d->fi.absoluteFilePath());
361         if (!success && exists())
362                 LYXERR0("Could not delete file " << *this);
363         return success;
364 }
365
366
367 static bool rmdir(QFileInfo const & fi)
368 {
369         QDir dir(fi.absoluteFilePath());
370         QFileInfoList list = dir.entryInfoList();
371         bool success = true;
372         for (int i = 0; i != list.size(); ++i) {
373                 if (list.at(i).fileName() == ".")
374                         continue;
375                 if (list.at(i).fileName() == "..")
376                         continue;
377                 bool removed;
378                 if (list.at(i).isDir()) {
379                         LYXERR(Debug::FILES, "Removing dir " 
380                                 << fromqstr(list.at(i).absoluteFilePath()));
381                         removed = rmdir(list.at(i));
382                 }
383                 else {
384                         LYXERR(Debug::FILES, "Removing file " 
385                                 << fromqstr(list.at(i).absoluteFilePath()));
386                         removed = dir.remove(list.at(i).fileName());
387                 }
388                 if (!removed) {
389                         success = false;
390                         LYXERR0("Could not delete "
391                                 << fromqstr(list.at(i).absoluteFilePath()));
392                 }
393         } 
394         QDir parent = fi.absolutePath();
395         success &= parent.rmdir(fi.fileName());
396         return success;
397 }
398
399
400 bool FileName::destroyDirectory() const
401 {
402         bool const success = rmdir(d->fi);
403         if (!success)
404                 LYXERR0("Could not delete " << *this);
405
406         return success;
407 }
408
409
410 static int mymkdir(char const * pathname, unsigned long int mode)
411 {
412         // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
413 #if HAVE_MKDIR
414 # if MKDIR_TAKES_ONE_ARG
415         // MinGW32
416         return ::mkdir(pathname);
417         // FIXME: "Permissions of created directories are ignored on this system."
418 # else
419         // POSIX
420         return ::mkdir(pathname, mode_t(mode));
421 # endif
422 #elif defined(_WIN32)
423         // plain Windows 32
424         return CreateDirectory(pathname, 0) != 0 ? 0 : -1;
425         // FIXME: "Permissions of created directories are ignored on this system."
426 #elif HAVE__MKDIR
427         return ::_mkdir(pathname);
428         // FIXME: "Permissions of created directories are ignored on this system."
429 #else
430 #   error "Don't know how to create a directory on this system."
431 #endif
432
433 }
434
435
436 bool FileName::createDirectory(int permission) const
437 {
438         BOOST_ASSERT(!empty());
439         return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
440 }
441
442
443 // adapted from zlib-1.2.3/contrib/minizip/miniunz.c
444 static int makedir(char * newdir, unsigned long int mode)
445 {
446         char *buffer;
447         char *p;
448         int     len = (int)strlen(newdir);
449
450         if (len <= 0)
451                 return 1;
452
453         buffer = (char*)malloc(len+1);
454         strcpy(buffer,newdir);
455
456         if (buffer[len-1] == '/')
457                 buffer[len-1] = '\0';
458         if (mymkdir(buffer, mode) == 0) {
459                 free(buffer);
460                 return 0;
461         }
462
463         p = buffer + 1;
464         while (1) {
465                 char hold;
466
467                 while(*p && *p != '\\' && *p != '/')
468                         p++;
469                 hold = *p;
470                 *p = 0;
471                 if (mymkdir(buffer, mode) != 0) {
472                         free(buffer);
473                         return 1;
474                 }
475                 if (hold == 0)
476                         break;
477                 *p++ = hold;
478         }
479         free(buffer);
480         return 0;
481 }
482
483
484 bool FileName::createPath() const
485 {
486         BOOST_ASSERT(!empty());
487         if (isDirectory())
488                 return true;
489
490         QDir dir;
491         bool success = dir.mkpath(d->fi.absoluteFilePath());
492         if (!success)
493                 LYXERR0("Cannot create path '" << *this << "'!");
494         return success;
495 }
496
497
498 docstring const FileName::absoluteFilePath() const
499 {
500         return qstring_to_ucs4(d->fi.absoluteFilePath());
501 }
502
503
504 docstring FileName::displayName(int threshold) const
505 {
506         return makeDisplayPath(absFilename(), threshold);
507 }
508
509
510 docstring FileName::fileContents(string const & encoding) const
511 {
512         if (!isReadableFile()) {
513                 LYXERR0("File '" << *this << "' is not redable!");
514                 return docstring();
515         }
516
517         QFile file(d->fi.absoluteFilePath());
518         if (!file.open(QIODevice::ReadOnly)) {
519                 LYXERR0("File '" << *this
520                         << "' could not be opened in read only mode!");
521                 return docstring();
522         }
523         QByteArray contents = file.readAll();
524         file.close();
525
526         if (contents.isEmpty()) {
527                 LYXERR(Debug::FILES, "File '" << *this
528                         << "' is either empty or some error happened while reading it.");
529                 return docstring();
530         }
531
532         QString s;
533         if (encoding.empty() || encoding == "UTF-8")
534                 s = QString::fromUtf8(contents.data());
535         else if (encoding == "ascii")
536                 s = QString::fromAscii(contents.data());
537         else if (encoding == "local8bit")
538                 s = QString::fromLocal8Bit(contents.data());
539         else if (encoding == "latin1")
540                 s = QString::fromLatin1(contents.data());
541
542         return qstring_to_ucs4(s);
543 }
544
545
546 void FileName::changeExtension(string const & extension)
547 {
548         // FIXME: use Qt native methods...
549         string const oldname = absFilename();
550         string::size_type const last_slash = oldname.rfind('/');
551         string::size_type last_dot = oldname.rfind('.');
552         if (last_dot < last_slash && last_slash != string::npos)
553                 last_dot = string::npos;
554
555         string ext;
556         // Make sure the extension starts with a dot
557         if (!extension.empty() && extension[0] != '.')
558                 ext= '.' + extension;
559         else
560                 ext = extension;
561
562         set(oldname.substr(0, last_dot) + ext);
563 }
564
565
566 string FileName::guessFormatFromContents() const
567 {
568         // the different filetypes and what they contain in one of the first lines
569         // (dots are any characters).           (Herbert 20020131)
570         // AGR  Grace...
571         // BMP  BM...
572         // EPS  %!PS-Adobe-3.0 EPSF...
573         // FIG  #FIG...
574         // FITS ...BITPIX...
575         // GIF  GIF...
576         // JPG  JFIF
577         // PDF  %PDF-...
578         // PNG  .PNG...
579         // PBM  P1... or P4     (B/W)
580         // PGM  P2... or P5     (Grayscale)
581         // PPM  P3... or P6     (color)
582         // PS   %!PS-Adobe-2.0 or 1.0,  no "EPSF"!
583         // SGI  \001\332...     (decimal 474)
584         // TGIF %TGIF...
585         // TIFF II... or MM...
586         // XBM  ..._bits[]...
587         // XPM  /* XPM */    sometimes missing (f.ex. tgif-export)
588         //      ...static char *...
589         // XWD  \000\000\000\151        (0x00006900) decimal 105
590         //
591         // GZIP \037\213        http://www.ietf.org/rfc/rfc1952.txt
592         // ZIP  PK...                   http://www.halyava.ru/document/ind_arch.htm
593         // Z    \037\235                UNIX compress
594
595         // paranoia check
596         if (empty() || !isReadableFile())
597                 return string();
598
599         ifstream ifs(toFilesystemEncoding().c_str());
600         if (!ifs)
601                 // Couldn't open file...
602                 return string();
603
604         // gnuzip
605         static string const gzipStamp = "\037\213";
606
607         // PKZIP
608         static string const zipStamp = "PK";
609
610         // compress
611         static string const compressStamp = "\037\235";
612
613         // Maximum strings to read
614         int const max_count = 50;
615         int count = 0;
616
617         string str;
618         string format;
619         bool firstLine = true;
620         while ((count++ < max_count) && format.empty()) {
621                 if (ifs.eof()) {
622                         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
623                                 << "\tFile type not recognised before EOF!");
624                         break;
625                 }
626
627                 getline(ifs, str);
628                 string const stamp = str.substr(0, 2);
629                 if (firstLine && str.size() >= 2) {
630                         // at first we check for a zipped file, because this
631                         // information is saved in the first bytes of the file!
632                         // also some graphic formats which save the information
633                         // in the first line, too.
634                         if (prefixIs(str, gzipStamp)) {
635                                 format =  "gzip";
636
637                         } else if (stamp == zipStamp) {
638                                 format =  "zip";
639
640                         } else if (stamp == compressStamp) {
641                                 format =  "compress";
642
643                         // the graphics part
644                         } else if (stamp == "BM") {
645                                 format =  "bmp";
646
647                         } else if (stamp == "\001\332") {
648                                 format =  "sgi";
649
650                         // PBM family
651                         // Don't need to use str.at(0), str.at(1) because
652                         // we already know that str.size() >= 2
653                         } else if (str[0] == 'P') {
654                                 switch (str[1]) {
655                                 case '1':
656                                 case '4':
657                                         format =  "pbm";
658                                     break;
659                                 case '2':
660                                 case '5':
661                                         format =  "pgm";
662                                     break;
663                                 case '3':
664                                 case '6':
665                                         format =  "ppm";
666                                 }
667                                 break;
668
669                         } else if ((stamp == "II") || (stamp == "MM")) {
670                                 format =  "tiff";
671
672                         } else if (prefixIs(str,"%TGIF")) {
673                                 format =  "tgif";
674
675                         } else if (prefixIs(str,"#FIG")) {
676                                 format =  "fig";
677
678                         } else if (prefixIs(str,"GIF")) {
679                                 format =  "gif";
680
681                         } else if (str.size() > 3) {
682                                 int const c = ((str[0] << 24) & (str[1] << 16) &
683                                                (str[2] << 8)  & str[3]);
684                                 if (c == 105) {
685                                         format =  "xwd";
686                                 }
687                         }
688
689                         firstLine = false;
690                 }
691
692                 if (!format.empty())
693                     break;
694                 else if (contains(str,"EPSF"))
695                         // dummy, if we have wrong file description like
696                         // %!PS-Adobe-2.0EPSF"
697                         format = "eps";
698
699                 else if (contains(str, "Grace"))
700                         format = "agr";
701
702                 else if (contains(str, "JFIF"))
703                         format = "jpg";
704
705                 else if (contains(str, "%PDF"))
706                         format = "pdf";
707
708                 else if (contains(str, "PNG"))
709                         format = "png";
710
711                 else if (contains(str, "%!PS-Adobe")) {
712                         // eps or ps
713                         ifs >> str;
714                         if (contains(str,"EPSF"))
715                                 format = "eps";
716                         else
717                             format = "ps";
718                 }
719
720                 else if (contains(str, "_bits[]"))
721                         format = "xbm";
722
723                 else if (contains(str, "XPM") || contains(str, "static char *"))
724                         format = "xpm";
725
726                 else if (contains(str, "BITPIX"))
727                         format = "fits";
728         }
729
730         if (!format.empty()) {
731                 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
732                 return format;
733         }
734
735         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
736                 << "\tCouldn't find a known format!");
737         return string();
738 }
739
740
741 bool FileName::isZippedFile() const
742 {
743         string const type = guessFormatFromContents();
744         return contains("gzip zip compress", type) && !type.empty();
745 }
746
747
748 docstring const FileName::relPath(string const & path) const
749 {
750         // FIXME UNICODE
751         return makeRelPath(absoluteFilePath(), from_utf8(path));
752 }
753
754
755 bool operator==(FileName const & lhs, FileName const & rhs)
756 {
757         return lhs.absFilename() == rhs.absFilename();
758 }
759
760
761 bool operator!=(FileName const & lhs, FileName const & rhs)
762 {
763         return lhs.absFilename() != rhs.absFilename();
764 }
765
766
767 bool operator<(FileName const & lhs, FileName const & rhs)
768 {
769         return lhs.absFilename() < rhs.absFilename();
770 }
771
772
773 bool operator>(FileName const & lhs, FileName const & rhs)
774 {
775         return lhs.absFilename() > rhs.absFilename();
776 }
777
778
779 ostream & operator<<(ostream & os, FileName const & filename)
780 {
781         return os << filename.absFilename();
782 }
783
784
785 /////////////////////////////////////////////////////////////////////
786 //
787 // DocFileName
788 //
789 /////////////////////////////////////////////////////////////////////
790
791
792 DocFileName::DocFileName()
793         : save_abs_path_(true)
794 {}
795
796
797 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
798         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
799 {}
800
801
802 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
803         : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
804 {}
805
806
807 void DocFileName::set(string const & name, string const & buffer_path)
808 {
809         save_abs_path_ = absolutePath(name);
810         FileName::set(save_abs_path_ ? name : makeAbsPath(name, buffer_path).absFilename());
811         zipped_valid_ = false;
812 }
813
814
815 void DocFileName::erase()
816 {
817         FileName::erase();
818         zipped_valid_ = false;
819 }
820
821
822 string DocFileName::relFilename(string const & path) const
823 {
824         // FIXME UNICODE
825         return to_utf8(relPath(path));
826 }
827
828
829 string DocFileName::outputFilename(string const & path) const
830 {
831         return save_abs_path_ ? absFilename() : relFilename(path);
832 }
833
834
835 string DocFileName::mangledFilename(string const & dir) const
836 {
837         // We need to make sure that every DocFileName instance for a given
838         // filename returns the same mangled name.
839         typedef map<string, string> MangledMap;
840         static MangledMap mangledNames;
841         MangledMap::const_iterator const it = mangledNames.find(absFilename());
842         if (it != mangledNames.end())
843                 return (*it).second;
844
845         string const name = absFilename();
846         // Now the real work
847         string mname = os::internal_path(name);
848         // Remove the extension.
849         mname = support::changeExtension(name, string());
850         // The mangled name must be a valid LaTeX name.
851         // The list of characters to keep is probably over-restrictive,
852         // but it is not really a problem.
853         // Apart from non-ASCII characters, at least the following characters
854         // are forbidden: '/', '.', ' ', and ':'.
855         // On windows it is not possible to create files with '<', '>' or '?'
856         // in the name.
857         static string const keep = "abcdefghijklmnopqrstuvwxyz"
858                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
859                                    "+,-0123456789;=";
860         string::size_type pos = 0;
861         while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
862                 mname[pos++] = '_';
863         // Add the extension back on
864         mname = support::changeExtension(mname, getExtension(name));
865
866         // Prepend a counter to the filename. This is necessary to make
867         // the mangled name unique.
868         static int counter = 0;
869         ostringstream s;
870         s << counter++ << mname;
871         mname = s.str();
872
873         // MiKTeX's YAP (version 2.4.1803) crashes if the file name
874         // is longer than about 160 characters. MiKTeX's pdflatex
875         // is even pickier. A maximum length of 100 has been proven to work.
876         // If dir.size() > max length, all bets are off for YAP. We truncate
877         // the filename nevertheless, keeping a minimum of 10 chars.
878
879         string::size_type max_length = max(100 - ((int)dir.size() + 1), 10);
880
881         // If the mangled file name is too long, hack it to fit.
882         // We know we're guaranteed to have a unique file name because
883         // of the counter.
884         if (mname.size() > max_length) {
885                 int const half = (int(max_length) / 2) - 2;
886                 if (half > 0) {
887                         mname = mname.substr(0, half) + "___" +
888                                 mname.substr(mname.size() - half);
889                 }
890         }
891
892         mangledNames[absFilename()] = mname;
893         return mname;
894 }
895
896
897 bool DocFileName::isZipped() const
898 {
899         if (!zipped_valid_) {
900                 zipped_ = isZippedFile();
901                 zipped_valid_ = true;
902         }
903         return zipped_;
904 }
905
906
907 string DocFileName::unzippedFilename() const
908 {
909         return unzippedFileName(absFilename());
910 }
911
912
913 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
914 {
915         return lhs.absFilename() == rhs.absFilename()
916                 && lhs.saveAbsPath() == rhs.saveAbsPath();
917 }
918
919
920 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
921 {
922         return !(lhs == rhs);
923 }
924
925 } // namespace support
926 } // namespace lyx