3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "support/FileName.h"
14 #include "support/FileNameList.h"
16 #include "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/lassert.h"
19 #include "support/lstrings.h"
20 #include "support/qstring_helpers.h"
21 #include "support/os.h"
22 #include "support/Package.h"
23 #include "support/qstring_helpers.h"
30 #include <QTemporaryFile>
33 #include <boost/crc.hpp>
34 #include <boost/scoped_array.hpp>
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
62 // Three implementations of checksum(), depending on having mmap support or not.
63 #if defined(HAVE_MMAP) && defined(HAVE_MUNMAP)
66 #endif // SUM_WITH_MMAP
70 // OK, this is ugly, but it is the only workaround I found to compile
71 // with gcc (any version) on a system which uses a non-GNU toolchain.
72 // The problem is that gcc uses a weak symbol for a particular
73 // instantiation and that the system linker usually does not
74 // understand those weak symbols (seen on HP-UX, tru64, AIX and
75 // others). Thus we force an explicit instanciation of this particular
77 template struct boost::detail::crc_table_t<32, 0x04C11DB7, true>;
82 /////////////////////////////////////////////////////////////////////
86 /////////////////////////////////////////////////////////////////////
88 struct FileName::Private
92 Private(string const & abs_filename) : fi(toqstr(abs_filename))
94 name = fromqstr(fi.absoluteFilePath());
95 fi.setCaching(fi.exists() ? true : false);
100 // There seems to be a bug in Qt >= 4.2.0 and < 4.5.0, that causes problems with
101 // QFileInfo::refresh() on *nix. So we recreate the object in that case.
102 #if defined(_WIN32) || (QT_VERSION >= 0x040500)
105 fi = QFileInfo(fi.absoluteFilePath());
111 bool isFilesystemEqual(QString const & lhs, QString const & rhs)
113 return QString::compare(lhs, rhs, os::isFilesystemCaseSensitive() ?
114 Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
117 /// The absolute file name in UTF-8 encoding.
123 /////////////////////////////////////////////////////////////////////
127 /////////////////////////////////////////////////////////////////////
130 FileName::FileName() : d(new Private)
135 FileName::FileName(string const & abs_filename)
136 : d(abs_filename.empty() ? new Private : new Private(abs_filename))
138 //LYXERR(Debug::FILES, "FileName(" << abs_filename << ')');
139 LASSERT(empty() || isAbsolute(d->name), /**/);
143 FileName::~FileName()
149 FileName::FileName(FileName const & rhs) : d(new Private)
151 d->name = rhs.d->name;
156 FileName::FileName(FileName const & rhs, string const & suffix) : d(new Private)
162 FileName & FileName::operator=(FileName const & rhs)
166 d->name = rhs.d->name;
172 bool FileName::empty() const
174 return d->name.empty();
178 bool FileName::isAbsolute(string const & name)
180 QFileInfo fi(toqstr(name));
181 return fi.isAbsolute();
185 string FileName::absFilename() const
191 string FileName::realPath() const
193 return os::real_path(toFilesystemEncoding());
197 void FileName::set(string const & name)
199 d->fi.setFile(toqstr(name));
200 d->name = fromqstr(d->fi.absoluteFilePath());
201 //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
202 LASSERT(empty() || isAbsolute(d->name), /**/);
206 void FileName::set(FileName const & rhs, string const & suffix)
208 if (!rhs.d->fi.isDir())
209 d->fi.setFile(rhs.d->fi.filePath() + toqstr(suffix));
211 d->fi.setFile(QDir(rhs.d->fi.absoluteFilePath()), toqstr(suffix));
212 d->name = fromqstr(d->fi.absoluteFilePath());
213 //LYXERR(Debug::FILES, "FileName::set(" << d->name << ')');
214 LASSERT(empty() || isAbsolute(d->name), /**/);
218 void FileName::erase()
225 bool FileName::copyTo(FileName const & name) const
227 LYXERR(Debug::FILES, "Copying " << name);
228 QFile::remove(name.d->fi.absoluteFilePath());
229 bool success = QFile::copy(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
231 LYXERR0("FileName::copyTo(): Could not copy file "
232 << *this << " to " << name);
237 bool FileName::renameTo(FileName const & name) const
239 bool success = QFile::rename(d->fi.absoluteFilePath(), name.d->fi.absoluteFilePath());
241 LYXERR0("Could not rename file " << *this << " to " << name);
246 bool FileName::moveTo(FileName const & name) const
248 QFile::remove(name.d->fi.absoluteFilePath());
250 bool success = QFile::rename(d->fi.absoluteFilePath(),
251 name.d->fi.absoluteFilePath());
253 LYXERR0("Could not move file " << *this << " to " << name);
258 bool FileName::changePermission(unsigned long int mode) const
260 #if defined (HAVE_CHMOD) && defined (HAVE_MODE_T)
261 if (::chmod(toFilesystemEncoding().c_str(), mode_t(mode)) != 0) {
262 LYXERR0("File " << *this << ": cannot change permission to "
271 string FileName::toFilesystemEncoding() const
273 // FIXME: This doesn't work on Windows for non ascii file names with Qt < 4.4.
274 // Provided that Windows package uses Qt4.4, this isn't a problem.
275 QByteArray const encoded = QFile::encodeName(d->fi.absoluteFilePath());
276 return string(encoded.begin(), encoded.end());
280 FileName FileName::fromFilesystemEncoding(string const & name)
282 QByteArray const encoded(name.c_str(), name.length());
283 return FileName(fromqstr(QFile::decodeName(encoded)));
287 bool FileName::exists() const
289 return !empty() && d->fi.exists();
293 bool FileName::isSymLink() const
295 return !empty() && d->fi.isSymLink();
299 bool FileName::isFileEmpty() const
301 LASSERT(!empty(), return true);
302 return d->fi.size() == 0;
306 bool FileName::isDirectory() const
308 return !empty() && d->fi.isDir();
312 bool FileName::isReadOnly() const
314 LASSERT(!empty(), return true);
315 return d->fi.isReadable() && !d->fi.isWritable();
319 bool FileName::isReadableDirectory() const
321 return isDirectory() && d->fi.isReadable();
325 string FileName::onlyFileName() const
327 return fromqstr(d->fi.fileName());
331 string FileName::onlyFileNameWithoutExt() const
333 return fromqstr(d->fi.completeBaseName());
337 string FileName::extension() const
339 return fromqstr(d->fi.suffix());
343 bool FileName::hasExtension(const string & ext)
345 return Private::isFilesystemEqual(d->fi.suffix(), toqstr(ext));
349 FileName FileName::onlyPath() const
354 path.d->fi.setFile(d->fi.path());
355 path.d->name = fromqstr(path.d->fi.absoluteFilePath());
360 bool FileName::isReadableFile() const
362 return !empty() && d->fi.isFile() && d->fi.isReadable();
366 bool FileName::isWritable() const
368 return !empty() && d->fi.isWritable();
372 bool FileName::isDirWritable() const
374 LASSERT(isDirectory(), return false);
375 QFileInfo tmp(QDir(d->fi.absoluteFilePath()), "lyxwritetest");
376 QTemporaryFile qt_tmp(tmp.absoluteFilePath());
378 LYXERR(Debug::FILES, "Directory " << *this << " is writable");
381 LYXERR(Debug::FILES, "Directory " << *this << " is not writable");
386 FileNameList FileName::dirList(string const & ext) const
388 FileNameList dirlist;
389 if (!isDirectory()) {
390 LYXERR0("Directory '" << *this << "' does not exist!");
394 // If the directory is specified without a trailing '/', absoluteDir()
395 // would return the parent dir, so we must use absoluteFilePath() here.
396 QDir dir = d->fi.absoluteFilePath();
401 case '.': filter = "*" + toqstr(ext); break;
402 case '*': filter = toqstr(ext); break;
403 default: filter = "*." + toqstr(ext);
405 dir.setNameFilters(QStringList(filter));
406 LYXERR(Debug::FILES, "filtering on extension "
407 << fromqstr(filter) << " is requested.");
410 QFileInfoList list = dir.entryInfoList();
411 for (int i = 0; i != list.size(); ++i) {
412 FileName fi(fromqstr(list.at(i).absoluteFilePath()));
413 dirlist.push_back(fi);
414 LYXERR(Debug::FILES, "found file " << fi);
421 static string createTempFile(QString const & mask)
423 QTemporaryFile qt_tmp(mask);
425 string const temp_file = fromqstr(qt_tmp.fileName());
426 LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
429 LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
430 << qt_tmp.fileTemplate());
435 FileName FileName::tempName(FileName const & temp_dir, string const & mask)
437 QFileInfo tmp_fi(QDir(temp_dir.d->fi.absoluteFilePath()), toqstr(mask));
438 LYXERR(Debug::FILES, "Temporary file in " << tmp_fi.absoluteFilePath());
439 return FileName(createTempFile(tmp_fi.absoluteFilePath()));
443 FileName FileName::tempName(string const & mask)
445 return tempName(package().temp_dir(), mask);
449 FileName FileName::getcwd()
451 // return makeAbsPath("."); would create an infinite loop
453 return FileName(fromqstr(fi.absoluteFilePath()));
457 FileName FileName::tempPath()
459 return FileName(os::internal_path(fromqstr(QDir::tempPath())));
463 time_t FileName::lastModified() const
465 // QFileInfo caches information about the file. So, in case this file has
466 // been touched between the object creation and now, we refresh the file
469 return d->fi.lastModified().toTime_t();
473 bool FileName::chdir() const
475 return QDir::setCurrent(d->fi.absoluteFilePath());
479 unsigned long FileName::checksum() const
481 unsigned long result = 0;
484 //LYXERR0("File \"" << absFilename() << "\" does not exist!");
487 // a directory may be passed here so we need to test it. (bug 3622)
489 LYXERR0('"' << absFilename() << "\" is a directory!");
493 // This is used in the debug output at the end of the method.
495 if (lyxerr.debugging(Debug::FILES))
498 #if QT_VERSION >= 0x999999
499 // First version of checksum uses Qt4.4 mmap support.
500 // FIXME: This code is not ready with Qt4.4.2,
501 // see http://bugzilla.lyx.org/show_bug.cgi?id=5293
502 // FIXME: should we check if the MapExtension extension is supported?
503 // see QAbstractFileEngine::supportsExtension() and
504 // QAbstractFileEngine::MapExtension)
505 QFile qf(fi.filePath());
506 if (!qf.open(QIODevice::ReadOnly))
508 qint64 size = fi.size();
509 uchar * ubeg = qf.map(0, size);
510 uchar * uend = ubeg + size;
511 boost::crc_32_type ucrc;
512 ucrc.process_block(ubeg, uend);
515 result = ucrc.checksum();
519 string const encoded = toFilesystemEncoding();
520 char const * file = encoded.c_str();
523 //LYXERR(Debug::FILES, "using mmap (lightning fast)");
525 int fd = open(file, O_RDONLY);
532 void * mm = mmap(0, info.st_size, PROT_READ,
534 // Some platforms have the wrong type for MAP_FAILED (compaq cxx).
535 if (mm == reinterpret_cast<void*>(MAP_FAILED)) {
540 char * beg = static_cast<char*>(mm);
541 char * end = beg + info.st_size;
543 boost::crc_32_type crc;
544 crc.process_block(beg, end);
545 result = crc.checksum();
547 munmap(mm, info.st_size);
550 #else // no SUM_WITH_MMAP
552 //LYXERR(Debug::FILES, "lyx::sum() using istreambuf_iterator (fast)");
553 ifstream ifs(file, ios_base::in | ios_base::binary);
557 istreambuf_iterator<char> beg(ifs);
558 istreambuf_iterator<char> end;
559 boost::crc_32_type crc;
560 crc = for_each(beg, end, crc);
561 result = crc.checksum();
563 #endif // SUM_WITH_MMAP
566 LYXERR(Debug::FILES, "Checksumming \"" << absFilename() << "\" "
567 << result << " lasted " << t.elapsed() << " ms.");
572 bool FileName::removeFile() const
574 bool const success = QFile::remove(d->fi.absoluteFilePath());
576 if (!success && exists())
577 LYXERR0("Could not delete file " << *this);
582 static bool rmdir(QFileInfo const & fi)
584 QDir dir(fi.absoluteFilePath());
585 QFileInfoList list = dir.entryInfoList();
587 for (int i = 0; i != list.size(); ++i) {
588 if (list.at(i).fileName() == ".")
590 if (list.at(i).fileName() == "..")
593 if (list.at(i).isDir()) {
594 LYXERR(Debug::FILES, "Removing dir "
595 << fromqstr(list.at(i).absoluteFilePath()));
596 removed = rmdir(list.at(i));
599 LYXERR(Debug::FILES, "Removing file "
600 << fromqstr(list.at(i).absoluteFilePath()));
601 removed = dir.remove(list.at(i).fileName());
605 LYXERR0("Could not delete "
606 << fromqstr(list.at(i).absoluteFilePath()));
609 QDir parent = fi.absolutePath();
610 success &= parent.rmdir(fi.fileName());
615 bool FileName::destroyDirectory() const
617 bool const success = rmdir(d->fi);
619 LYXERR0("Could not delete " << *this);
625 // Only used in non Win32 platforms
626 static int mymkdir(char const * pathname, unsigned long int mode)
628 // FIXME: why don't we have mode_t in lyx::mkdir prototype ??
630 # if MKDIR_TAKES_ONE_ARG
632 return ::mkdir(pathname);
633 // FIXME: "Permissions of created directories are ignored on this system."
636 return ::mkdir(pathname, mode_t(mode));
638 #elif defined(_WIN32)
640 return CreateDirectory(pathname, 0) != 0 ? 0 : -1;
641 // FIXME: "Permissions of created directories are ignored on this system."
643 return ::_mkdir(pathname);
644 // FIXME: "Permissions of created directories are ignored on this system."
646 # error "Don't know how to create a directory on this system."
652 bool FileName::createDirectory(int permission) const
654 LASSERT(!empty(), return false);
656 // FIXME: "Permissions of created directories are ignored on this system."
659 return mymkdir(toFilesystemEncoding().c_str(), permission) == 0;
664 bool FileName::createPath() const
666 LASSERT(!empty(), return false);
667 LYXERR(Debug::FILES, "creating path '" << *this << "'.");
672 bool success = dir.mkpath(d->fi.absoluteFilePath());
674 LYXERR0("Cannot create path '" << *this << "'!");
679 docstring const FileName::absoluteFilePath() const
681 return qstring_to_ucs4(d->fi.absoluteFilePath());
685 docstring FileName::displayName(int threshold) const
687 return makeDisplayPath(absFilename(), threshold);
691 docstring FileName::fileContents(string const & encoding) const
693 if (!isReadableFile()) {
694 LYXERR0("File '" << *this << "' is not redable!");
698 QFile file(d->fi.absoluteFilePath());
699 if (!file.open(QIODevice::ReadOnly)) {
700 LYXERR0("File '" << *this
701 << "' could not be opened in read only mode!");
704 QByteArray contents = file.readAll();
707 if (contents.isEmpty()) {
708 LYXERR(Debug::FILES, "File '" << *this
709 << "' is either empty or some error happened while reading it.");
714 if (encoding.empty() || encoding == "UTF-8")
715 s = QString::fromUtf8(contents.data());
716 else if (encoding == "ascii")
717 s = QString::fromAscii(contents.data());
718 else if (encoding == "local8bit")
719 s = QString::fromLocal8Bit(contents.data());
720 else if (encoding == "latin1")
721 s = QString::fromLatin1(contents.data());
723 return qstring_to_ucs4(s);
727 void FileName::changeExtension(string const & extension)
729 // FIXME: use Qt native methods...
730 string const oldname = absFilename();
731 string::size_type const last_slash = oldname.rfind('/');
732 string::size_type last_dot = oldname.rfind('.');
733 if (last_dot < last_slash && last_slash != string::npos)
734 last_dot = string::npos;
737 // Make sure the extension starts with a dot
738 if (!extension.empty() && extension[0] != '.')
739 ext= '.' + extension;
743 set(oldname.substr(0, last_dot) + ext);
747 string FileName::guessFormatFromContents() const
749 // the different filetypes and what they contain in one of the first lines
750 // (dots are any characters). (Herbert 20020131)
753 // EPS %!PS-Adobe-3.0 EPSF...
760 // PBM P1... or P4 (B/W)
761 // PGM P2... or P5 (Grayscale)
762 // PPM P3... or P6 (color)
763 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
764 // SGI \001\332... (decimal 474)
766 // TIFF II... or MM...
768 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
769 // ...static char *...
770 // XWD \000\000\000\151 (0x00006900) decimal 105
772 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
773 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
774 // Z \037\235 UNIX compress
777 if (empty() || !isReadableFile())
780 ifstream ifs(toFilesystemEncoding().c_str());
782 // Couldn't open file...
786 static string const gzipStamp = "\037\213";
789 static string const zipStamp = "PK";
792 static string const compressStamp = "\037\235";
794 // Maximum strings to read
795 int const max_count = 50;
800 bool firstLine = true;
801 while ((count++ < max_count) && format.empty()) {
803 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
804 << "\tFile type not recognised before EOF!");
809 string const stamp = str.substr(0, 2);
810 if (firstLine && str.size() >= 2) {
811 // at first we check for a zipped file, because this
812 // information is saved in the first bytes of the file!
813 // also some graphic formats which save the information
814 // in the first line, too.
815 if (prefixIs(str, gzipStamp)) {
818 } else if (stamp == zipStamp) {
821 } else if (stamp == compressStamp) {
825 } else if (stamp == "BM") {
828 } else if (stamp == "\001\332") {
832 // Don't need to use str.at(0), str.at(1) because
833 // we already know that str.size() >= 2
834 } else if (str[0] == 'P') {
850 } else if ((stamp == "II") || (stamp == "MM")) {
853 } else if (prefixIs(str,"%TGIF")) {
856 } else if (prefixIs(str,"#FIG")) {
859 } else if (prefixIs(str,"GIF")) {
862 } else if (str.size() > 3) {
863 int const c = ((str[0] << 24) & (str[1] << 16) &
864 (str[2] << 8) & str[3]);
875 else if (contains(str,"EPSF"))
876 // dummy, if we have wrong file description like
877 // %!PS-Adobe-2.0EPSF"
880 else if (contains(str, "Grace"))
883 else if (contains(str, "JFIF"))
886 else if (contains(str, "%PDF"))
889 else if (contains(str, "PNG"))
892 else if (contains(str, "%!PS-Adobe")) {
895 if (contains(str,"EPSF"))
901 else if (contains(str, "_bits[]"))
904 else if (contains(str, "XPM") || contains(str, "static char *"))
907 else if (contains(str, "BITPIX"))
911 // Dia knows also compressed form
912 if ((format == "gzip") && (!compare_ascii_no_case(extension(), "dia")))
915 if (!format.empty()) {
916 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
920 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
921 << "\tCouldn't find a known format!");
926 bool FileName::isZippedFile() const
928 string const type = guessFormatFromContents();
929 return contains("gzip zip compress", type) && !type.empty();
933 docstring const FileName::relPath(string const & path) const
936 return makeRelPath(absoluteFilePath(), from_utf8(path));
940 // Note: According to Qt, QFileInfo::operator== is undefined when
941 // both files do not exist (Qt4.5 gives true for all non-existent
942 // files, while Qt4.4 compares the filenames).
944 // http://www.qtsoftware.com/developer/task-tracker/
945 // index_html?id=248471&method=entry.
946 bool equivalent(FileName const & l, FileName const & r)
948 // FIXME: In future use Qt.
949 // Qt 4.4: We need to solve this warning from Qt documentation:
950 // * Long and short file names that refer to the same file on Windows are
951 // treated as if they referred to different files.
952 // This is supposed to be fixed for Qt5.
953 FileName const lhs(os::internal_path(l.absFilename()));
954 FileName const rhs(os::internal_path(r.absFilename()));
957 // QFileInfo::operator==() returns false if the two QFileInfo are empty.
961 // Avoid unnecessary checks below.
967 if (!lhs.d->fi.isSymLink() && !rhs.d->fi.isSymLink()) {
968 // Qt already checks if the filesystem is case sensitive or not.
969 // see note above why the extra check with fileName is needed.
970 return lhs.d->fi == rhs.d->fi
971 && lhs.d->fi.fileName() == rhs.d->fi.fileName();
974 // FIXME: When/if QFileInfo support symlink comparison, remove this code.
975 QFileInfo fi1(lhs.d->fi);
977 fi1 = QFileInfo(fi1.symLinkTarget());
978 QFileInfo fi2(rhs.d->fi);
980 fi2 = QFileInfo(fi2.symLinkTarget());
981 // see note above why the extra check with fileName is needed.
982 return fi1 == fi2 && fi1.fileName() == fi2.fileName();
986 bool operator==(FileName const & lhs, FileName const & rhs)
988 return os::isFilesystemCaseSensitive()
989 ? lhs.absFilename() == rhs.absFilename()
990 : !QString::compare(toqstr(lhs.absFilename()),
991 toqstr(rhs.absFilename()), Qt::CaseInsensitive);
995 bool operator!=(FileName const & lhs, FileName const & rhs)
997 return !(operator==(lhs, rhs));
1001 bool operator<(FileName const & lhs, FileName const & rhs)
1003 return lhs.absFilename() < rhs.absFilename();
1007 bool operator>(FileName const & lhs, FileName const & rhs)
1009 return lhs.absFilename() > rhs.absFilename();
1013 ostream & operator<<(ostream & os, FileName const & filename)
1015 return os << filename.absFilename();
1019 /////////////////////////////////////////////////////////////////////
1023 /////////////////////////////////////////////////////////////////////
1026 DocFileName::DocFileName()
1027 : save_abs_path_(true)
1031 DocFileName::DocFileName(string const & abs_filename, bool save_abs)
1032 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
1036 DocFileName::DocFileName(FileName const & abs_filename, bool save_abs)
1037 : FileName(abs_filename), save_abs_path_(save_abs), zipped_valid_(false)
1041 void DocFileName::set(string const & name, string const & buffer_path)
1043 save_abs_path_ = isAbsolute(name);
1045 FileName::set(name);
1047 FileName::set(makeAbsPath(name, buffer_path).absFilename());
1048 zipped_valid_ = false;
1052 void DocFileName::erase()
1055 zipped_valid_ = false;
1059 string DocFileName::relFilename(string const & path) const
1062 return to_utf8(relPath(path));
1066 string DocFileName::outputFilename(string const & path) const
1068 return save_abs_path_ ? absFilename() : relFilename(path);
1072 string DocFileName::mangledFilename(string const & dir) const
1074 // We need to make sure that every DocFileName instance for a given
1075 // filename returns the same mangled name.
1076 typedef map<string, string> MangledMap;
1077 static MangledMap mangledNames;
1078 MangledMap::const_iterator const it = mangledNames.find(absFilename());
1079 if (it != mangledNames.end())
1080 return (*it).second;
1082 string const name = absFilename();
1083 // Now the real work
1084 string mname = os::internal_path(name);
1085 // Remove the extension.
1086 mname = support::changeExtension(name, string());
1087 // The mangled name must be a valid LaTeX name.
1088 // The list of characters to keep is probably over-restrictive,
1089 // but it is not really a problem.
1090 // Apart from non-ASCII characters, at least the following characters
1091 // are forbidden: '/', '.', ' ', and ':'.
1092 // On windows it is not possible to create files with '<', '>' or '?'
1094 static string const keep = "abcdefghijklmnopqrstuvwxyz"
1095 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1097 string::size_type pos = 0;
1098 while ((pos = mname.find_first_not_of(keep, pos)) != string::npos)
1100 // Add the extension back on
1101 mname = support::changeExtension(mname, getExtension(name));
1103 // Prepend a counter to the filename. This is necessary to make
1104 // the mangled name unique.
1105 static int counter = 0;
1107 s << counter++ << mname;
1110 // MiKTeX's YAP (version 2.4.1803) crashes if the file name
1111 // is longer than about 160 characters. MiKTeX's pdflatex
1112 // is even pickier. A maximum length of 100 has been proven to work.
1113 // If dir.size() > max length, all bets are off for YAP. We truncate
1114 // the filename nevertheless, keeping a minimum of 10 chars.
1116 string::size_type max_length = max(100 - ((int)dir.size() + 1), 10);
1118 // If the mangled file name is too long, hack it to fit.
1119 // We know we're guaranteed to have a unique file name because
1121 if (mname.size() > max_length) {
1122 int const half = (int(max_length) / 2) - 2;
1124 mname = mname.substr(0, half) + "___" +
1125 mname.substr(mname.size() - half);
1129 mangledNames[absFilename()] = mname;
1134 bool DocFileName::isZipped() const
1136 if (!zipped_valid_) {
1137 zipped_ = isZippedFile();
1138 zipped_valid_ = true;
1144 string DocFileName::unzippedFilename() const
1146 return unzippedFileName(absFilename());
1150 bool operator==(DocFileName const & lhs, DocFileName const & rhs)
1152 return static_cast<FileName const &>(lhs)
1153 == static_cast<FileName const &>(rhs)
1154 && lhs.saveAbsPath() == rhs.saveAbsPath();
1158 bool operator!=(DocFileName const & lhs, DocFileName const & rhs)
1160 return !(lhs == rhs);
1163 } // namespace support