18#include <QTemporaryFile>
20#include "Preference.h"
21#include "IException.h"
42 m_d->setOriginal(file);
52 m_d->setOriginal(file);
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);
581 QPair<int, int> truncateRange(-1, -1);
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 numerical 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);
770 return QPair<QString, QString>(before, after);
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");
868 if(dataDir.hasKeyword(varName)) {
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);
This is the reference-counted data for FileName.
QString original(bool includeAttributes) const
Returns the original file name, stored in m_originalFileNameString.
Data()
Data constructor, creates a new Data object.
QString expanded(bool includeAttributes) const
Returns the expanded file name, stored in m_expandedFileNameString.
~Data()
Destroys the Data object.
void setOriginal(const QString &originalStr)
Sets the original file name, stored in m_originalFileNameString.
File name manipulation and expansion.
QString path() const
Returns the path of the file name.
bool isDateVersioned() const
Checks if the file name is versioned by date.
~FileName()
Destroys the FileName object.
QPair< QString, QString > splitNameAroundVersionNum() const
This returns a QPair of the text (before, after) a version number in a file.
bool isNumericallyVersioned() const
Checks if the file name is versioned numerically.
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
FileName()
Constructs an empty FileName object.
bool fileExists() const
Returns true if the file exists; false otherwise.
FileName setExtension(const QString &extension) const
Sets all current file extensions to a new extension in the file name.
QString baseName() const
Returns the name of the file without the path and without extensions.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
QDate highestVersionDate() const
This looks through the directory of the file and checks for the highest version date of the file that...
static FileName createTempFile(FileName templateFileName="$TEMPORARY/temp")
Creates a temporary file and returns a FileName object created using the temporary file.
FileName highestVersion() const
Searches the directory specified in the file name for the highest version of the file name.
QString original() const
Returns the full file name including the file path.
FileName newVersion() const
Updates the file name to be the latest version.
FileName & operator=(const FileName &rhs)
Clears the current contents of the FileName object and reinitializes it with the argument.
long highestVersionNum() const
This looks through the directory of the file and checks for the highest version number of the file th...
QString fileNameQDatePattern() const
This changes the files format.
QString extension() const
Returns the last extension of the file name.
bool operator!=(const FileName &rhs)
Compares equality of two FileName objects.
QString originalPath() const
Returns the path of the original file name.
void validateVersioningState() const
This verifies the class invariant when using versioning - that the FileName is in an acceptable state...
FileName removeExtension() const
Removes all extensions in the file name.
QString attributes() const
Returns a QString of the attributes in a filename, attributes are expected to be of type CubeAttribut...
bool operator==(const FileName &rhs)
Compares equality of two FileName objects.
FileName addExtension(const QString &extension) const
Adds a new extension to the file name.
bool isVersioned() const
Checks to see if a file name is versioned by date or numerically.
FileName version(long versionNumber) const
Returns a FileName object of the same file name but versioned numerically by the number passed in as ...
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
@ Unknown
A type of error that cannot be classified as any of the other error types.
@ Io
A type of error that occurred when performing an actual I/O operation.
Contains multiple PvlContainers.
This is free and unencumbered software released into the public domain.
Namespace for the standard library.