move filename utils into shared library

This commit is contained in:
Seth Alves 2015-03-12 10:23:57 -07:00
parent b431ce289c
commit 9e6c289ab8
5 changed files with 42 additions and 39 deletions

View file

@ -39,6 +39,7 @@
#include <PacketHeaders.h>
#include <SharedUtil.h>
#include <Shape.h>
#include <PathUtils.h>
#include "CoverageMap.h"
#include "OctreeConstants.h"
@ -47,7 +48,7 @@
#include "ViewFrustum.h"
QVector<QString> persistExtensions = {"svo", "json"};
QVector<QString> PERSIST_EXTENSIONS = {"svo", "json"};
float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeScale) {
return voxelSizeScale / powf(2, renderLevel);
@ -1851,7 +1852,7 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* element,
bool Octree::readFromFile(const char* fileName) {
bool fileOk = false;
QString qFileName = findMostRecentPersist(fileName);
QString qFileName = findMostRecentFileExtension(fileName, PERSIST_EXTENSIONS);
QFile file(qFileName);
fileOk = file.open(QIODevice::ReadOnly);
@ -2063,7 +2064,7 @@ bool Octree::readJSONFromStream(unsigned long streamLength, QDataStream& inputSt
void Octree::writeToFile(const char* fileName, OctreeElement* element, QString persistAsFileType) {
// make the sure file extension makes sense
QString qFileName = fileNameWithoutExtension(QString(fileName), persistExtensions) + "." + persistAsFileType;
QString qFileName = fileNameWithoutExtension(QString(fileName), PERSIST_EXTENSIONS) + "." + persistAsFileType;
QByteArray byteArray = qFileName.toUtf8();
const char* cFileName = byteArray.constData();
@ -2193,29 +2194,3 @@ bool Octree::countOctreeElementsOperation(OctreeElement* element, void* extraDat
void Octree::cancelImport() {
_stopImport = true;
}
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions) {
foreach (const QString possibleExtension, possibleExtensions) {
if (fileName.endsWith(possibleExtension) ||
fileName.endsWith(possibleExtension.toUpper()) ||
fileName.endsWith(possibleExtension.toLower())) {
return fileName.left(fileName.count() - possibleExtension.count() - 1);
}
}
return fileName;
}
QString findMostRecentPersist(const QString& originalFileName) {
QString sansExt = fileNameWithoutExtension(originalFileName, persistExtensions);
QString newestFileName = originalFileName;
QDateTime newestTime = QDateTime::fromMSecsSinceEpoch(0);
foreach (QString possibleExtension, persistExtensions) {
QString fileName = sansExt + "." + possibleExtension;
QFileInfo fileInfo(fileName);
if (fileInfo.exists() && fileInfo.lastModified() > newestTime) {
newestFileName = fileName;
newestTime = fileInfo.lastModified();
}
}
return newestFileName;
}

View file

@ -36,7 +36,7 @@ class Shape;
#include <QReadWriteLock>
extern QVector<QString> persistExtensions;
extern QVector<QString> PERSIST_EXTENSIONS;
/// derive from this class to use the Octree::recurseTreeWithOperator() method
class RecurseOctreeOperator {
@ -407,10 +407,4 @@ protected:
float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeScale);
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions);
// XXX rename this
QString findMostRecentPersist(const QString& originalFileName);
#endif // hifi_Octree_h

View file

@ -24,6 +24,7 @@
#include <PerfStat.h>
#include <SharedUtil.h>
#include <PathUtils.h>
#include "OctreePersistThread.h"
@ -46,7 +47,7 @@ OctreePersistThread::OctreePersistThread(Octree* tree, const QString& filename,
parseSettings(settings);
// in case the persist filename has an extension that doesn't match the file type
QString sansExt = fileNameWithoutExtension(_filename, persistExtensions);
QString sansExt = fileNameWithoutExtension(_filename, PERSIST_EXTENSIONS);
_filename = sansExt + "." + _persistAsFileType;
}
@ -352,7 +353,7 @@ void OctreePersistThread::rollOldBackupVersions(const BackupRule& rule) {
backupExtensionN.replace(QString("%N"), QString::number(n));
backupExtensionNplusOne.replace(QString("%N"), QString::number(n+1));
QString backupFilenameN = findMostRecentPersist(_filename) + backupExtensionN;
QString backupFilenameN = findMostRecentFileExtension(_filename, PERSIST_EXTENSIONS) + backupExtensionN;
QString backupFilenameNplusOne = _filename + backupExtensionNplusOne;
QFile backupFileN(backupFilenameN);

View file

@ -11,6 +11,9 @@
#include <QCoreApplication>
#include <QString>
#include <QVector>
#include <QDateTime>
#include <QFileInfo>
#include "PathUtils.h"
@ -23,3 +26,30 @@ QString& PathUtils::resourcesPath() {
#endif
return staticResourcePath;
}
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions) {
foreach (const QString possibleExtension, possibleExtensions) {
if (fileName.endsWith(possibleExtension) ||
fileName.endsWith(possibleExtension.toUpper()) ||
fileName.endsWith(possibleExtension.toLower())) {
return fileName.left(fileName.count() - possibleExtension.count() - 1);
}
}
return fileName;
}
QString findMostRecentFileExtension(const QString& originalFileName, QVector<QString> possibleExtensions) {
QString sansExt = fileNameWithoutExtension(originalFileName, possibleExtensions);
QString newestFileName = originalFileName;
QDateTime newestTime = QDateTime::fromMSecsSinceEpoch(0);
foreach (QString possibleExtension, possibleExtensions) {
QString fileName = sansExt + "." + possibleExtension;
QFileInfo fileInfo(fileName);
if (fileInfo.exists() && fileInfo.lastModified() > newestTime) {
newestFileName = fileName;
newestTime = fileInfo.lastModified();
}
}
return newestFileName;
}

View file

@ -19,4 +19,7 @@ namespace PathUtils {
QString& resourcesPath();
}
#endif // hifi_PathUtils_h
QString fileNameWithoutExtension(const QString& fileName, const QVector<QString> possibleExtensions);
QString findMostRecentFileExtension(const QString& originalFileName, QVector<QString> possibleExtensions);
#endif // hifi_PathUtils_h