34 #include <QTemporaryFile> 47 FileName::FileName() {
56 FileName::FileName(
const char *file) {
66 FileName::FileName(
const QString &file) {
76 FileName::FileName(
const FileName &other) : m_d(other.m_d) {
101 return QFileInfo(m_d->original(
false)).
path();
120 return QFileInfo(
expanded()).path();
139 QString fileNameWithAttribs = QFileInfo(m_d->original(
true)).fileName();
141 int attribStartPos = fileNameWithAttribs.indexOf(
"+");
143 if (attribStartPos != -1)
144 result = fileNameWithAttribs.mid(attribStartPos + 1);
162 return QFileInfo(m_d->original(
false)).completeBaseName();
179 return QFileInfo(m_d->original(
false)).fileName();
195 return QFileInfo(m_d->original(
false)).suffix();
213 return m_d->expanded(
false);
229 return m_d->original(
true);
244 if (result.
extension() != newExtension) {
247 if (attributesStr ==
"")
251 +
"+" + attributesStr);
266 if (attributesStr ==
"")
336 QObject::tr(
"Asked for highest version of file named [%1] in [%2] but there " 337 "are no version sequences in the name")
371 QObject::tr(
"Asked for new version of file named [%1] in [%2] but there " 372 "are no version sequences in the name")
379 result = result.
version(QDate::currentDate());
394 QObject::tr(
"Could not generate unique new version of file named [%1] in " 395 "[%2] because the file [%3] exists")
417 int width = file.count(
"?");
419 if (versionNumber < 0) {
421 QObject::tr(
"FileName does not support negative version numbers in the file name, " 422 "tried to get version [%1] in file named [%2]")
427 if (versionNumber >= pow(10.0, width)) {
429 QObject::tr(
"FileName does not support version numbers greater than what would fit in " 430 "the file name, tried to get version [%1] in file named [%2]")
436 QString &before = splitName.first;
437 QString &after = splitName.second;
439 file = before + QString(
"%1").arg(QString::number(versionNumber), width,
'0') + after;
466 return QFileInfo(
expanded()).exists();
495 QString preppedFileName = QString(
"%1/%2XXXXXX.%3").arg(templateFileName.
path())
497 QTemporaryFile tempFile(preppedFileName);
498 tempFile.setAutoRemove(
false);
500 if (!tempFile.open()) {
502 QObject::tr(
"Could not create a unique temporary file name based on [%1]")
510 QString newTempFileNameStr = templateFileName.
originalPath() +
"/" +
511 QFileInfo(tempFile.fileName()).fileName();
512 result =
FileName(newTempFileNameStr);
557 QString expandedOfThis =
expanded();
558 QString canonicalOfThis = QFileInfo(expandedOfThis).canonicalFilePath();
560 QString expandedOfRhs = rhs.
expanded();
561 QString canonicalOfRhs = QFileInfo(expandedOfRhs).canonicalFilePath();
566 bool equal = (!canonicalOfThis.isEmpty() && canonicalOfThis == canonicalOfRhs);
569 equal = (canonicalOfThis.isEmpty() && canonicalOfRhs.isEmpty() &&
570 expandedOfThis == expandedOfRhs);
585 return !(*
this == rhs);
598 if (fileQDatePattern.contains(
"?")) {
599 QString trueLengthName =
name().replace(QRegExp(
"[{}]"),
"");
600 truncateRange.first = trueLengthName.indexOf(
"?");
601 truncateRange.second = trueLengthName.lastIndexOf(
"?");
602 fileQDatePattern = fileQDatePattern.replace(
"?",
"");
605 QString file =
name();
608 QDate sputnikLaunch(1957, 10, 4);
610 QString before = file.mid(0, file.indexOf(
"{"));
611 QString after = file.mid(file.lastIndexOf(
"}") + 1);
615 nameFilters.append(before +
"*" + after);
619 foreach (QString foundFile, files) {
621 if (truncateRange.first >= 0 && truncateRange.second > truncateRange.first) {
622 foundFile = foundFile.mid(0, truncateRange.first) +
623 foundFile.mid(truncateRange.second + 1);
626 QDate fileDate = QLocale(QLocale::English, QLocale::UnitedStates).toDate(foundFile, fileQDatePattern);
628 if (fileDate.isValid()) {
630 if (fileDate < sputnikLaunch)
631 fileDate = fileDate.addYears(100);
633 if (!result.isValid() || fileDate > result) {
639 if (!result.isValid()) {
641 QObject::tr(
"No existing files found with a date version matching [%1] in " 660 int width = file.count(
"?");
663 QString &before = splitName.first;
664 QString &after = splitName.second;
667 nameFilters.append(before + QString(
"%1").arg(
"", width,
'?') + after);
668 QStringList files =
dir().entryList(nameFilters, QDir::NoFilter, QDir::Name);
670 long foundValue = -1;
671 bool success =
false;
673 for (
int i = files.count() - 1; !success && i >= 0; i--) {
674 foundValue = files[i].mid(before.count(), width).toLong(&success);
682 QObject::tr(
"No existing files found with a numerial version matching [%1] " 696 QString file = QFileInfo(
expanded()).fileName();
698 if (file.contains(QRegExp(
"\\?\\?*[^?][^?]*\\?"))) {
700 QObject::tr(
"Only one numerical version sequence is allowed in a filename; " 701 "there are multiple in [%1]").arg(file),
712 fileDatePattern.replace(QRegExp(
"\\{\\}"),
"");
714 fileDatePattern =
"'" + fileDatePattern.replace(QRegExp(
"[{}]"),
"'") +
"'";
716 QString dated = QDate::currentDate().toString(fileDatePattern);
717 if (file.contains(
"'")) {
719 QObject::tr(
"Date version sequenced file names cannot have single quotes in them; " 720 "the file named [%1] is not usable").arg(file),
723 else if (dated ==
"") {
725 QObject::tr(
"The date version sequence is not usable in the file named [%1]").arg(file),
728 else if (dated == fileDatePattern.replace(QRegExp(
"'"),
"")) {
730 QObject::tr(
"The date version sequences are not recognized in the file named [%1]")
748 file = file.replace(QRegExp(
"[{}]"),
"'");
751 if (file.startsWith(
"'"))
757 if (file.endsWith(
"'"))
758 file = file.mid(0, file.length() - 1);
782 before = file.mid(0, file.indexOf(
"?"));
783 after = file.mid(file.lastIndexOf(
"?") + 1);
793 m_originalFileNameString = NULL;
794 m_expandedFileNameString = NULL;
796 m_originalFileNameString =
new QString;
797 m_expandedFileNameString =
new QString;
806 m_originalFileNameString = NULL;
807 m_expandedFileNameString = NULL;
809 m_originalFileNameString =
new QString(*other.m_originalFileNameString);
810 m_expandedFileNameString =
new QString(*other.m_expandedFileNameString);
817 delete m_originalFileNameString;
818 m_originalFileNameString = NULL;
820 delete m_expandedFileNameString;
821 m_expandedFileNameString = NULL;
834 QString result = *m_originalFileNameString;
837 if (!includeAttributes) {
838 int attributesPos = result.indexOf(
"+");
840 if (attributesPos != -1)
841 result = result.left(attributesPos);
855 *m_originalFileNameString = originalStr;
858 QString expandedStr =
original(
true);
860 int varSearchStartPos = 0;
861 int varStartPos = -1;
864 while((varStartPos = expandedStr.indexOf(
"$", varSearchStartPos)) != -1) {
865 int varEndPos = expandedStr.indexOf(QRegExp(
"[^a-zA-Z{}0-9]"), varStartPos + 1);
867 varEndPos = expandedStr.length();
869 bool variableValid =
false;
870 int varNameLength = varEndPos - varStartPos;
872 if (varNameLength > 0) {
873 QString varName = expandedStr.mid(varStartPos + 1, varEndPos - varStartPos - 1);
875 if (varName.length()) {
876 if (varName[0] ==
'{' && varName[varName.length() - 1] ==
'}')
877 varName = varName.mid(1, varName.length() - 2);
882 if(Preference::Preferences().hasGroup(
"DataDirectory")) {
883 PvlGroup &dataDir = Preference::Preferences().findGroup(
"DataDirectory");
885 varValue = ((QString)dataDir[varName.toStdString().c_str()][0]);
890 if (varValue.isEmpty()) {
892 val = getenv(varName.toStdString().c_str());
893 if(val != NULL) varValue = val;
899 if (!varValue.isEmpty()) {
900 expandedStr = expandedStr.replace(varStartPos, varNameLength, varValue);
901 variableValid =
true;
908 varSearchStartPos = varStartPos;
912 varSearchStartPos = varStartPos + 1;
916 *m_expandedFileNameString = expandedStr;
929 QString result = *m_expandedFileNameString;
931 if (!includeAttributes) {
932 int attributesPos = result.indexOf(
"+");
934 if (attributesPos != -1)
935 result = result.left(attributesPos);
QString path() const
Returns the path of the file name.
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
File name manipulation and expansion.
Data()
Data constructor, creates a new Data object.
FileName newVersion() const
Updates the file name to be the latest version.
~Data()
Destroys the Data object.
FileName addExtension(const QString &extension) const
Adds a new extension to the file name.
Namespace for the standard library.
QString name() const
Returns the name of the file excluding the path and the attributes in the file name.
QDate highestVersionDate() const
This looks through the directory of the file and checks for the highest version date of the file that...
bool isDateVersioned() const
Checks if the file name is versioned by date.
QString originalPath() const
Returns the path of the original file name.
QString original(bool includeAttributes) const
Returns the original file name, stored in m_originalFileNameString.
A type of error that occurred when performing an actual I/O operation.
QString fileNameQDatePattern() const
This changes the files format.
QString expanded(bool includeAttributes) const
Returns the expanded file name, stored in m_expandedFileNameString.
Contains multiple PvlContainers.
#define _FILEINFO_
Macro for the filename and line number.
A type of error that cannot be classified as any of the other error types.
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
QString original() const
Returns the full file name including the file path.
long highestVersionNum() const
This looks through the directory of the file and checks for the highest version number of the file th...
QDir dir() const
Returns the path of the file's parent directory as a QDir object.
This is the reference-counted data for FileName.
FileName & operator=(const FileName &rhs)
Clears the current contents of the FileName object and reinitializes it with the argument.
void validateVersioningState() const
This verifies the class invariant when using versioning - that the FileName is in an acceptable state...
bool operator!=(const FileName &rhs)
Compares equality of two FileName objects.
QString baseName() const
Returns the name of the file without the path and without extensions.
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
FileName highestVersion() const
Searches the directory specified in the file name for the highest version of the file name...
~FileName()
Destroys the FileName object.
FileName setExtension(const QString &extension) const
Sets all current file extensions to a new extension in the file name.
FileName()
Constructs an empty FileName object.
Namespace for ISIS/Bullet specific routines.
bool operator==(const FileName &rhs)
Compares equality of two FileName objects.
QString extension() const
Returns the last extension of the file name.
bool isNumericallyVersioned() const
Checks if the file name is versioned numerically.
QString attributes() const
Returns a QString of the attributes in a filename, attributes are expected to be of type CubeAttribut...
QPair< QString, QString > splitNameAroundVersionNum() const
This returns a QPair of the text (before, after) a version number in a file.
FileName version(long versionNumber) const
Returns a FileName object of the same file name but versioned numerically by the number passed in as ...
bool fileExists() const
Returns true if the file exists; false otherwise.
static FileName createTempFile(FileName templateFileName="$TEMPORARY/temp")
Creates a temporary file and returns a FileName object created using the temporary file...
bool isVersioned() const
Checks to see if a file name is versioned by date or numerically.
void setOriginal(const QString &originalStr)
Sets the original file name, stored in m_originalFileNameString.
FileName removeExtension() const
Removes all extensions in the file name.