Commit 21448366 authored by Robert Menzel's avatar Robert Menzel

cleanups in Utils and Base, some functions moved and/or changed namespaces

parent 095a4dc7
/***********************************************************************
* Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
* All rights reserved. *
* Distributed under the terms of the MIT License (see LICENSE.TXT). *
**********************************************************************/
#ifndef ACGL_BASE_FILEOPERATIONS_HH
#define ACGL_BASE_FILEOPERATIONS_HH
#include <ACGL/ACGL.hh>
#include <string>
namespace ACGL{
namespace Base{
namespace FileOperations
{
bool rawData(const std::string& _filename, char*& _pData, ACGL::long_t& _size);
}
} // Base
} // ACGL
#endif // ACGL_BASE_FILEOPERATIONS_HH
......@@ -17,7 +17,7 @@
#include <ACGL/Base/Singleton.hh>
#include <ACGL/OpenGL/Controller/DataControlFile.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <map>
#include <string>
......@@ -67,7 +67,7 @@ public:
{
std::string file;
std::string extension;
ACGL::Base::StringOperations::splitFileExtension(_filename, file, extension);
ACGL::Utils::StringHelpers::splitFileExtension(_filename, file, extension);
typename FactoryMap::const_iterator existingCreator = mFactoryMap.find(extension);
if (existingCreator != mFactoryMap.end()) {
return existingCreator->second(_filename);
......
......@@ -50,7 +50,7 @@ protected:
struct ImportedShader {
std::string fileName;
Base::FileHelpers::FileModificationTime modificatonTime;
Utils::FileHelpers::FileModificationTime modificatonTime;
};
std::vector< ImportedShader > mImportedShaders; // in case the shader imports additional shaders files
......
......@@ -26,7 +26,7 @@
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Macros.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/OpenGL/Objects/RenderBuffer.hh>
......
......@@ -11,7 +11,7 @@
#include <ACGL/Resource/BasicCreateController.hh>
#include <ACGL/Resource/BasicUpdateController.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <string>
......@@ -41,8 +41,8 @@ public:
const std::string& getFullFilePath(void) const { return mFullFilePath; }
protected:
inline bool fileIsUpToDate(void) { return Base::FileHelpers::getFileModificationTime(mFullFilePath) == mFileModificationTime; }
inline void updateFileModificationTime(void) { mFileModificationTime = Base::FileHelpers::getFileModificationTime(mFullFilePath); }
inline bool fileIsUpToDate(void) { return Utils::FileHelpers::getFileModificationTime(mFullFilePath) == mFileModificationTime; }
inline void updateFileModificationTime(void) { mFileModificationTime = Utils::FileHelpers::getFileModificationTime(mFullFilePath); }
// =================================================================================================== \/
// ============================================================================================ FIELDS \/
......@@ -51,7 +51,7 @@ protected:
std::string mFilename;
std::string mFullFilePath;
Base::FileHelpers::FileModificationTime mFileModificationTime;
Utils::FileHelpers::FileModificationTime mFileModificationTime;
};
} // Resource
......
......@@ -18,7 +18,7 @@
#include <ACGL/Resource/BasicCreateController.hh>
#include <ACGL/Resource/BasicUpdateController.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <string>
#include <sstream>
......@@ -72,14 +72,14 @@ protected:
//! adds another file, this is protected to derived classes can expose specific versions which do more than just remembering one file
void addFile (const std::string &_fileName) {
mFileNames.push_back( _fileName );
mFileModificationTime.push_back( Base::FileHelpers::FileModificationTime() );
mFileModificationTime.push_back( Utils::FileHelpers::FileModificationTime() );
}
//! returns true iff _all_ files are up to date
bool filesAreUpToDate(void) {
bool b = true;
for (unsigned int i = 0; i < mFileNames.size(); ++i) {
b &= ( Base::FileHelpers::getFileModificationTime( mBasePath+mFileNames[i] ) == mFileModificationTime[i] );
b &= ( Utils::FileHelpers::getFileModificationTime( mBasePath+mFileNames[i] ) == mFileModificationTime[i] );
}
return b;
}
......@@ -87,7 +87,7 @@ protected:
//! updates _all_ modification times
void updateFileModificationTimes(void) {
for (unsigned int i = 0; i < mFileNames.size(); ++i) {
mFileModificationTime[i] = Base::FileHelpers::getFileModificationTime( mBasePath+mFileNames[i]);
mFileModificationTime[i] = Utils::FileHelpers::getFileModificationTime( mBasePath+mFileNames[i]);
}
}
......@@ -99,7 +99,7 @@ protected:
std::string mBasePath;
std::string mResourceName;
std::vector<Base::FileHelpers::FileModificationTime> mFileModificationTime;
std::vector<Utils::FileHelpers::FileModificationTime> mFileModificationTime;
};
} // Resource
......
......@@ -10,6 +10,7 @@
#include <string>
#include <vector>
// to get file modification times:
#include <time.h>
#ifdef _MSC_VER
# include <sys/utime.h>
......@@ -18,45 +19,27 @@
#endif
#include <sys/stat.h>
#include <ACGL/ACGL.hh>
#include <ACGL/Utils/StringHelpers.hh>
namespace ACGL{
namespace Base{
namespace Utils{
namespace FileHelpers
{
/*
* A small collection of helperfunctions to handle files and strings.
* A small collection of helperfunctions to handle files. See StringHelpers for functions to
* handle filenames.
*/
/*
* Reads a file to a char buffer created with new[], the caller has to delete the buffer.
* Will return a NULL pointer if the file does not exist or there were other errors opening the file.
*/
//char *readTextFile( const std::string &fileName );
/*
* Checks if a string ends with a certain ending. Can be used to check for fileendings:
* if (stringEndsWith( "foobar.txt", ".txt" )) cout << "textfile";
*/
bool stringEndsWith( const std::string &theString, const std::string &theEnding );
/*
* Returns the lowercase fileending if the filename has a '.' in it, otherwise
* an empty string.
*/
std::string getFileEnding( const std::string &_fileName );
/*
* Checks if a string begins with a certain prefix.
*/
//bool stringBeginsWith( const std::string &theString, const std::string &thePrefix );
/*
* Splits a string at the delim char.
*/
void splitString(const std::string &s, char delim, std::vector<std::string> &elems);
char *readTextFile( const std::string &fileName );
bool rawData(const std::string& _filename, char*& _pData, ACGL::long_t& _size);
/*
* On Desktops this will return the input string. But on iOS it will calculate the absolute
......@@ -64,8 +47,6 @@ namespace FileHelpers
*/
std::string getDeviceDependentPathFor( const std::string &resource );
typedef time_t FileModificationTime;
/*
......
......@@ -20,9 +20,9 @@
#include <string>
namespace ACGL{
namespace Base{
namespace Utils{
namespace StringOperations
namespace StringHelpers
{
//! splits the filename _full into the filename without the extension and just the extension
bool splitFileExtension (const std::string& _full, std::string& _file, std::string& _extension );
......@@ -33,6 +33,12 @@ namespace StringOperations
//! returns true iff the _string starts with a given single char prefix (quicker than the string version above)
bool startsWith (const std::string& _string, const char _prefix);
/*
* Checks if a string ends with a certain ending. Can be used to check for fileendings:
* if (endsWith( "foobar.txt", ".txt" )) cout << "textfile";
*/
bool endsWith( const std::string &theString, const std::string &theEnding );
std::vector<std::string> split (const std::string& _string, char _splitChar, bool _skipEmptyStrings = true);
......@@ -58,16 +64,24 @@ namespace StringOperations
return t;
}
//! converts a string in the form "(23,42)" to a glm::uvec2. Reverses the functionality of toString<glm::uvec2>( foo ).
glm::uvec2 toUvec2( const std::string& _string );
//! converts a string in the form "(23.0,42.0)" to a glm::vec2. Reverses the functionality of toString<glm::vec2>( foo ).
glm::vec2 toVec2( const std::string& _string );
glm::vec3 toVec3( const std::string& _string );
glm::vec4 toVec4( const std::string& _string );
glm::mat3 toMat3( const std::string& _string );
/*
* Returns the lowercase fileending if the filename has a '.' in it, otherwise
* an empty string.
*/
std::string getFileEnding( const std::string &_fileName );
}
} // Base
} // Utils
} // ACGL
#endif // ACGL_BASE_STRINGOPERATIONS_HH
/***********************************************************************
* Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
* All rights reserved. *
* Distributed under the terms of the MIT License (see LICENSE.TXT). *
**********************************************************************/
#include <ACGL/Base/FileOperations.hh>
#include <cstdio>
#include <cstdlib>
using namespace std;
using namespace ACGL;
using namespace ACGL::Utils;
using namespace ACGL::Base;
bool FileOperations::rawData(const std::string& _filename, char*& _pData, long_t& _size)
{
FILE* pFile;
size_t result;
pFile = fopen(_filename.c_str(), "rb");
if(pFile==NULL)
{
warning() << "Opening error!" << std::endl;
return false;
}
// obtain file size:
fseek(pFile, 0, SEEK_END);
_size = ftell(pFile);
rewind(pFile);
#ifdef __ANDROID__
_pData = new char[_size];
#else
try
{
// allocate memory to contain the whole file:
_pData = new char[_size];
}
catch(...)
{
error() << "Memory error!" << std::endl;
fclose (pFile);
return false;
}
#endif
// copy the file into the buffer:
result = fread(_pData, 1, _size, pFile);
if ( (long_t)result != _size)
{
error() << "Reading error!" << std::endl;
fclose (pFile);
return false;
}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (pFile);
return true;
}
......@@ -10,7 +10,7 @@
#include <ACGL/OpenGL/Controller/ArrayBufferControl.hh>
#include <ACGL/OpenGL/Controller/ElementArrayBufferControl.hh>
#include <ACGL/OpenGL/Controller/ArrayBufferControl.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Base/StringHelpers.hh>
#include <vector>
#include <iostream>
......@@ -72,9 +72,9 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
while(fileStream.good())
{
if(StringOperations::startsWith(line, "a"))
if(StringHelpers::startsWith(line, "a"))
{
std::vector<std::string> elements = StringOperations::split(line, ' ');
std::vector<std::string> elements = StringHelpers::split(line, ' ');
//The first occurence of vt gives us the number of texture coordinates per vertex.
if(attributeElements == 0)
{
......@@ -85,7 +85,7 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
if((uint_t)elements.size() == attributeElements)
{
for(uint_t i = 1; i < attributeElements; ++i)
sharedAttributeVector.push_back(StringOperations::to<GLfloat>(elements[i], &elementOK));
sharedAttributeVector.push_back(StringHelpers::to<GLfloat>(elements[i], &elementOK));
}
else
{
......@@ -96,7 +96,7 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
lineOK = false;
}
}
else if(StringOperations::startsWith(line, "f"))
else if(StringHelpers::startsWith(line, "f"))
{
//As soon as the first f occurs, we assume that all vertex
//positions, normals and tex coords have been defined!
......@@ -125,16 +125,16 @@ bool ArrayBufferControlFileATB::loadATB(SharedArrayBuffer& _arrayBuffer)
uint_t index = 0;
while(fileStream.good())
{
if(StringOperations::startsWith(line, "f"))
if(StringHelpers::startsWith(line, "f"))
{
std::vector<std::string> elements = StringOperations::split(line, ' ');
std::vector<std::string> elements = StringHelpers::split(line, ' ');
uint_t uiPolygonSize = elements.size() - 1;
if(uiPolygonSize > 2)
{
for(size_t i = 1; i < elements.size(); ++i)
{
index++;
uint_t sharedIndex = StringOperations::to<uint_t>(elements[i], &elementOK);
uint_t sharedIndex = StringHelpers::to<uint_t>(elements[i], &elementOK);
for(uint_t i = 0; i < attributeDimension; ++i)
dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndex + i]);
}
......
......@@ -20,7 +20,7 @@ GeometryDataControlFileATB::GeometryDataControlFileATB(const std::string& _filen
{
// Set the default attribute name
std::string filenameBase, filenameExtension;
ACGL::Base::StringOperations::splitFileExtension(mFilename, filenameBase, filenameExtension);
ACGL::Utils::StringHelpers::splitFileExtension(mFilename, filenameBase, filenameExtension);
mAttributeName = "a" + filenameBase;
}
......@@ -49,9 +49,9 @@ bool GeometryDataControlFileATB::load(SharedGeometryData& _geometry) const
while(fileStream.good())
{
if(StringOperations::startsWith(line, "a"))
if(StringHelpers::startsWith(line, "a"))
{
std::vector<std::string> elements = StringOperations::split(line, ' ');
std::vector<std::string> elements = StringHelpers::split(line, ' ');
//The first occurence of vt gives us the number of texture coordinates per vertex.
if(attributeElements == 0)
{
......@@ -62,7 +62,7 @@ bool GeometryDataControlFileATB::load(SharedGeometryData& _geometry) const
if((uint_t)elements.size() == attributeElements)
{
for(uint_t i = 1; i < attributeElements; ++i)
sharedAttributeVector.push_back(StringOperations::to<GLfloat>(elements[i], &elementOK));
sharedAttributeVector.push_back(StringHelpers::to<GLfloat>(elements[i], &elementOK));
}
else
{
......@@ -73,7 +73,7 @@ bool GeometryDataControlFileATB::load(SharedGeometryData& _geometry) const
lineOK = false;
}
}
else if(StringOperations::startsWith(line, "f"))
else if(StringHelpers::startsWith(line, "f"))
{
//As soon as the first f occurs, we assume that all vertex
//positions, normals and tex coords have been defined!
......@@ -102,25 +102,25 @@ bool GeometryDataControlFileATB::load(SharedGeometryData& _geometry) const
uint_t index = 0;
while(fileStream.good())
{
if(StringOperations::startsWith(line, "f"))
if(StringHelpers::startsWith(line, "f"))
{
std::vector<std::string> elements = StringOperations::split(line, ' ');
std::vector<std::string> elements = StringHelpers::split(line, ' ');
uint_t uiPolygonSize = elements.size() - 1;
if(uiPolygonSize > 2)
{
for(size_t i = 1; i < elements.size(); ++i)
{
index++;
uint_t sharedIndex = StringOperations::to<uint_t>(elements[i], &elementOK);
uint_t sharedIndex = StringHelpers::to<uint_t>(elements[i], &elementOK);
if(i > 3)
{
// triangulate polygons with number of vertices > 3
uint_t sharedIndexFirst = StringOperations::to<uint_t>(elements[1], &elementOK);
uint_t sharedIndexFirst = StringHelpers::to<uint_t>(elements[1], &elementOK);
for(uint_t d = 0; d < attributeDimension; ++d)
dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndexFirst + d]);
uint_t sharedIndexPrev = StringOperations::to<uint_t>(elements[i-1], &elementOK);
uint_t sharedIndexPrev = StringHelpers::to<uint_t>(elements[i-1], &elementOK);
for(uint_t d = 0; d < attributeDimension; ++d)
dataVector.push_back(sharedAttributeVector[attributeDimension * sharedIndexPrev + d]);
}
......
......@@ -7,7 +7,7 @@
#include <ACGL/OpenGL/Controller/GeometryDataControlFileOBJ.hh>
#include <ACGL/OpenGL/Controller/DataControlFileFactory.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <ACGL/OpenGL/Data/GeometryDataLoadStore.hh>
......
......@@ -5,10 +5,10 @@
**********************************************************************/
#include <ACGL/OpenGL/Controller/ShaderControlFile.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <ACGL/OpenGL/Tools.hh>
using namespace ACGL::Base;
using namespace ACGL::Utils;
using namespace ACGL::OpenGL;
SharedShader ShaderControlFile::create(void)
......@@ -41,7 +41,7 @@ bool ShaderControlFile::update(SharedShader& shader)
{
bool shaderIsUpToDate = fileIsUpToDate();
for (unsigned int i = 0; i < mImportedShaders.size(); ++i) {
if ( Base::FileHelpers::getFileModificationTime(mImportedShaders[i].fileName) != mImportedShaders[i].modificatonTime ) {
if ( Utils::FileHelpers::getFileModificationTime(mImportedShaders[i].fileName) != mImportedShaders[i].modificatonTime ) {
// imported shader changed
shaderIsUpToDate = false;
break; // one changed imported shader is enough to justify a full shader reload
......
......@@ -10,11 +10,12 @@
#include <ACGL/Resource/FileManager.hh>
#include <ACGL/OpenGL/Managers.hh>
#include <ACGL/Base/Settings.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <sstream>
#include <algorithm>
using namespace ACGL::Utils;
using namespace ACGL::Base;
using namespace ACGL::OpenGL;
......
......@@ -5,11 +5,13 @@
**********************************************************************/
#include <ACGL/OpenGL/Data/GeometryDataLoadStore.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <ACGL/Utils/StringHelpers.hh>
using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;
using namespace ACGL::Utils::StringHelpers;
namespace ACGL{
namespace OpenGL{
......@@ -21,7 +23,7 @@ namespace OpenGL{
SharedGeometryData loadGeometryData(const std::string& _filename)
{
// lower case file ending:
std::string fileEnding = Base::FileHelpers::getFileEnding(_filename);
std::string fileEnding = getFileEnding(_filename);
if(fileEnding == "obj")
{
......
......@@ -6,7 +6,7 @@
#include <ACGL/OpenGL/Data/GeometryDataLoadStore.hh>
#include <ACGL/Math/Math.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <fstream>
#include <string>
......@@ -59,7 +59,7 @@ namespace
std::string vertexString;
while(stream >> vertexString)
{
std::vector<std::string> componentsString = ACGL::Base::StringOperations::split(vertexString, '/', false);
std::vector<std::string> componentsString = ACGL::Utils::StringHelpers::split(vertexString, '/', false);
IndexTuple indexTuple;
for(int i = 0; i < std::min<int>(componentsString.size(), 3); ++i)
......
......@@ -6,7 +6,7 @@
#include <ACGL/OpenGL/Data/GeometryDataLoadStore.hh>
#include <ACGL/Math/Math.hh>
#include <ACGL/Base/StringOperations.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <fstream>
#include <string>
......@@ -61,7 +61,7 @@ namespace
std::string vertexString;
while(stream >> vertexString)
{
std::vector<std::string> componentsString = ACGL::Base::StringOperations::split(vertexString, '/', false);
std::vector<std::string> componentsString = ACGL::Utils::StringHelpers::split(vertexString, '/', false);
IndexTuple indexTuple;
for(int i = 0; i < std::min<int>(componentsString.size(), 3); ++i)
......
......@@ -6,7 +6,8 @@
#include <ACGL/OpenGL/Data/TextureDataLoadStore.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <ACGL/Utils/StringHelpers.hh>
#include <ACGL/Utils/Memory.hh>
// for the screenshot function:
......@@ -44,6 +45,7 @@ using namespace Imath;
using namespace ACGL;
using namespace ACGL::OpenGL;
using namespace ACGL::Utils;
using namespace ACGL::Utils::StringHelpers;
namespace ACGL{
namespace OpenGL{
......@@ -55,7 +57,7 @@ namespace OpenGL{
SharedTextureData loadTextureData(const std::string &_filename)
{
// lower case file ending:
std::string fileEnding = Base::FileHelpers::getFileEnding( _filename );
std::string fileEnding = getFileEnding( _filename );
if (fileEnding == "png") {
return loadTextureDataFromLodepng( _filename );
......@@ -84,7 +86,7 @@ SharedTextureData loadTextureData(const std::string &_filename)
bool saveTextureData(const SharedTextureData &_textureData, const std::string &_filename)
{
// lower case file ending:
std::string fileEnding = Base::FileHelpers::getFileEnding( _filename );
std::string fileEnding = getFileEnding( _filename );
if (fileEnding == "png") {
return saveTextureDataToLodepng( _textureData, _filename );
......@@ -125,7 +127,7 @@ bool saveScreenshotWithDate(const std::string& _prefix, const std::string& _file
static int i = 0;
i++;
std::string filename = _prefix + timestring + "_" + ACGL::Base::StringOperations::toString(i) + "." + _fileEnding;
std::string filename = _prefix + timestring + "_" + ACGL::Utils::StringHelpers::toString(i) + "." + _fileEnding;
return saveScreenshot(filename);
}
......
......@@ -5,7 +5,8 @@
**********************************************************************/
#include <ACGL/OpenGL/Data/TextureLoadStore.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <ACGL/Utils/StringHelpers.hh>
using namespace ACGL;
using namespace ACGL::OpenGL;
......@@ -16,7 +17,7 @@ namespace OpenGL{
SharedTexture2D loadTexture2D( const std::string &_filename )
{
std::string fileEnding = Base::FileHelpers::getFileEnding(_filename);
std::string fileEnding = StringHelpers::getFileEnding(_filename);
if(fileEnding == "dds")
{
return loadTexture2DFromDDS(_filename);
......
......@@ -6,7 +6,8 @@
#include <ACGL/OpenGL/Data/VertexArrayObjectLoadStore.hh>
#include <ACGL/OpenGL/Data/ArrayBufferLoadStore.hh>
#include <ACGL/Base/FileHelpers.hh>
#include <ACGL/Utils/FileHelpers.hh>
#include <ACGL/Utils/StringHelpers.hh>
using namespace ACGL;
using namespace ACGL::OpenGL;
......@@ -18,7 +19,7 @@ namespace OpenGL{
SharedVertexArrayObject loadVertexArrayObject(const std::string& _filename, bool _caching)
{