18 #include <QTemporaryFile>
20 #include "Preference.h"
21 #include "IException.h"
31 FileName::FileName() {
40 FileName::FileName(
const char *file) {
50 FileName::FileName(
const QString &file) {
60 FileName::FileName(
const FileName &other) : m_d(other.m_d) {
85 return QFileInfo(m_d->original(
false)).path();
104 return QFileInfo(
expanded()).path();
123 QString fileNameWithAttribs = QFileInfo(m_d->original(
true)).fileName();
125 int attribStartPos = fileNameWithAttribs.indexOf(
"+");
127 if (attribStartPos != -1)
128 result = fileNameWithAttribs.mid(attribStartPos + 1);
146 return QFileInfo(m_d->original(
false)).completeBaseName();
163 return QFileInfo(m_d->original(
false)).fileName();
179 return QFileInfo(m_d->original(
false)).suffix();
197 return m_d->expanded(
false);
213 return m_d->original(
true);
228 if (result.
extension() != newExtension) {
231 if (attributesStr ==
"")
235 +
"+" + attributesStr);
250 if (attributesStr ==
"")
320 QObject::tr(
"Asked for highest version of file named [%1] in [%2] but there "
321 "are no version sequences in the name")
355 QObject::tr(
"Asked for new version of file named [%1] in [%2] but there "
356 "are no version sequences in the name")
363 result = result.
version(QDate::currentDate());
378 QObject::tr(
"Could not generate unique new version of file named [%1] in "
379 "[%2] because the file [%3] exists")
401 int width = file.count(
"?");
403 if (versionNumber < 0) {
405 QObject::tr(
"FileName does not support negative version numbers in the file name, "
406 "tried to get version [%1] in file named [%2]")
411 if (versionNumber >= pow(10.0, width)) {
413 QObject::tr(
"FileName does not support version numbers greater than what would fit in "
414 "the file name, tried to get version [%1] in file named [%2]")
420 QString &before = splitName.first;
421 QString &after = splitName.second;
423 file = before + QString(
"%1").arg(QString::number(versionNumber), width,
'0') + after;
450 return QFileInfo(
expanded()).exists();
479 QString preppedFileName = QString(
"%1/%2XXXXXX.%3").arg(templateFileName.
path())
481 QTemporaryFile tempFile(preppedFileName);
482 tempFile.setAutoRemove(
false);
484 if (!tempFile.open()) {
486 QObject::tr(
"Could not create a unique temporary file name based on [%1]")
494 QString newTempFileNameStr = templateFileName.
originalPath() +
"/" +
495 QFileInfo(tempFile.fileName()).fileName();
496 result =
FileName(newTempFileNameStr);
541 QString expandedOfThis =
expanded();
542 QString canonicalOfThis = QFileInfo(expandedOfThis).canonicalFilePath();
544 QString expandedOfRhs = rhs.
expanded();
545 QString canonicalOfRhs = QFileInfo(expandedOfRhs).canonicalFilePath();
550 bool equal = (!canonicalOfThis.isEmpty() && canonicalOfThis == canonicalOfRhs);
553 equal = (canonicalOfThis.isEmpty() && canonicalOfRhs.isEmpty() &&
554 expandedOfThis == expandedOfRhs);
569 return !(*
this == rhs);
582 if (fileQDatePattern.contains(
"?")) {
583 QString trueLengthName =
name().replace(QRegExp(
"[{}]"),
"");
584 truncateRange.first = trueLengthName.indexOf(
"?");
585 truncateRange.second = trueLengthName.lastIndexOf(
"?");
586 fileQDatePattern = fileQDatePattern.replace(
"?",
"");
589 QString file =
name();
592 QDate sputnikLaunch(1957, 10, 4);
594 QString before = file.mid(0, file.indexOf(
"{"));
595 QString after = file.mid(file.lastIndexOf(
"}") + 1);
599 nameFilters.append(before +
"*" + after);
603 foreach (QString foundFile, files) {
605 if (truncateRange.first >= 0 && truncateRange.second > truncateRange.first) {
606 foundFile = foundFile.mid(0, truncateRange.first) +
607 foundFile.mid(truncateRange.second + 1);
610 QDate fileDate = QLocale(QLocale::English, QLocale::UnitedStates).toDate(foundFile, fileQDatePattern);
612 if (fileDate.isValid()) {
614 if (fileDate < sputnikLaunch)
615 fileDate = fileDate.addYears(100);
617 if (!result.isValid() || fileDate > result) {
623 if (!result.isValid()) {
625 QObject::tr(
"No existing files found with a date version matching [%1] in "
644 int width = file.count(
"?");
647 QString &before = splitName.first;
648 QString &after = splitName.second;
651 nameFilters.append(before + QString(
"%1").arg(
"", width,
'?') + after);
652 QStringList files =
dir().entryList(nameFilters, QDir::NoFilter, QDir::Name);
654 long foundValue = -1;
655 bool success =
false;
657 for (
int i = files.count() - 1; !success && i >= 0; i--) {
658 foundValue = files[i].mid(before.count(), width).toLong(&success);
666 QObject::tr(
"No existing files found with a numerial version matching [%1] "
680 QString file = QFileInfo(
expanded()).fileName();
682 if (file.contains(QRegExp(
"\\?\\?*[^?][^?]*\\?"))) {
684 QObject::tr(
"Only one numerical version sequence is allowed in a filename; "
685 "there are multiple in [%1]").arg(file),
696 fileDatePattern.replace(QRegExp(
"\\{\\}"),
"");
698 fileDatePattern =
"'" + fileDatePattern.replace(QRegExp(
"[{}]"),
"'") +
"'";
700 QString dated = QDate::currentDate().toString(fileDatePattern);
701 if (file.contains(
"'")) {
703 QObject::tr(
"Date version sequenced file names cannot have single quotes in them; "
704 "the file named [%1] is not usable").arg(file),
707 else if (dated ==
"") {
709 QObject::tr(
"The date version sequence is not usable in the file named [%1]").arg(file),
712 else if (dated == fileDatePattern.replace(QRegExp(
"'"),
"")) {
714 QObject::tr(
"The date version sequences are not recognized in the file named [%1]")
732 file = file.replace(QRegExp(
"[{}]"),
"'");
735 if (file.startsWith(
"'"))
741 if (file.endsWith(
"'"))
742 file = file.mid(0, file.length() - 1);
766 before = file.mid(0, file.indexOf(
"?"));
767 after = file.mid(file.lastIndexOf(
"?") + 1);
777 m_originalFileNameString = NULL;
778 m_expandedFileNameString = NULL;
780 m_originalFileNameString =
new QString;
781 m_expandedFileNameString =
new QString;
790 m_originalFileNameString = NULL;
791 m_expandedFileNameString = NULL;
793 m_originalFileNameString =
new QString(*other.m_originalFileNameString);
794 m_expandedFileNameString =
new QString(*other.m_expandedFileNameString);
801 delete m_originalFileNameString;
802 m_originalFileNameString = NULL;
804 delete m_expandedFileNameString;
805 m_expandedFileNameString = NULL;
818 QString result = *m_originalFileNameString;
821 if (!includeAttributes) {
822 int attributesPos = result.indexOf(
"+");
824 if (attributesPos != -1)
825 result = result.left(attributesPos);
839 *m_originalFileNameString = originalStr;
842 QString expandedStr =
original(
true);
844 int varSearchStartPos = 0;
845 int varStartPos = -1;
848 while((varStartPos = expandedStr.indexOf(
"$", varSearchStartPos)) != -1) {
849 int varEndPos = expandedStr.indexOf(QRegExp(
"[^a-zA-Z{}0-9]"), varStartPos + 1);
851 varEndPos = expandedStr.length();
853 bool variableValid =
false;
854 int varNameLength = varEndPos - varStartPos;
856 if (varNameLength > 0) {
857 QString varName = expandedStr.mid(varStartPos + 1, varEndPos - varStartPos - 1);
859 if (varName.length()) {
860 if (varName[0] ==
'{' && varName[varName.length() - 1] ==
'}')
861 varName = varName.mid(1, varName.length() - 2);
866 if(Preference::Preferences().hasGroup(
"DataDirectory")) {
867 PvlGroup &dataDir = Preference::Preferences().findGroup(
"DataDirectory");
869 varValue = ((QString)dataDir[varName.toStdString().c_str()][0]);
874 if (varValue.isEmpty()) {
876 val = getenv(varName.toStdString().c_str());
877 if(val != NULL) varValue = val;
883 if (!varValue.isEmpty()) {
884 expandedStr = expandedStr.replace(varStartPos, varNameLength, varValue);
885 variableValid =
true;
892 varSearchStartPos = varStartPos;
896 varSearchStartPos = varStartPos + 1;
900 *m_expandedFileNameString = expandedStr;
913 QString result = *m_expandedFileNameString;
915 if (!includeAttributes) {
916 int attributesPos = result.indexOf(
"+");
918 if (attributesPos != -1)
919 result = result.left(attributesPos);