You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.1 KiB
163 lines
4.1 KiB
#include "storageProvider.h"
|
|
|
|
#define LOG(X) m_pLog->Log( "storageProvider", this, X )
|
|
#define LOG_DEBUG( X ) m_pLog->Log( "storageProvider", this, _LOG_DEBUG, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "storageProvider", this, _LOG_ERROR, X )
|
|
|
|
storageProvider* storageProvider::_instance = 0;
|
|
|
|
/**
|
|
* Returns the single instance of the object.
|
|
*/
|
|
storageProvider* storageProvider::getInstance(CSTR Base) {
|
|
// check if the instance has been created yet
|
|
if (0 == _instance) {
|
|
// if not, then create it
|
|
_instance = new storageProvider(Base);
|
|
}
|
|
// return the single instance
|
|
return _instance;
|
|
};
|
|
|
|
|
|
storageProvider::storageProvider(CSTR BasePath) :m_mCounterProtectorMutex(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
LOG("Start Storage");
|
|
m_pCWD = (CSTR) malloc(MAX_PATH_LEN);
|
|
m_pBasePath = (CSTR) malloc(MAX_PATH_LEN);
|
|
m_pEntryFolder = (CSTR) malloc( 9 );
|
|
m_pSeriesBase = (CSTR) malloc( 33 );
|
|
m_nSeriesCounter = 0;
|
|
if( m_pBasePath!= NULL ) {
|
|
_getcwd(m_pCWD, MAX_PATH_LEN );
|
|
//strncpy( m_pBasePath, BasePath, 1024 );
|
|
sprintf(m_pBasePath, "%s/%s", m_pCWD, BasePath);
|
|
#ifdef DEBUG
|
|
char Buffer[256];
|
|
sprintf(Buffer,"Base Path OK [%s]", m_pBasePath );
|
|
LOG(Buffer);
|
|
#endif
|
|
startNewEntry();
|
|
} else {
|
|
LOG_ERROR("No Base Path Provided!");
|
|
};
|
|
};
|
|
|
|
|
|
storageProvider::~storageProvider(){
|
|
if( m_pCWD!=NULL ) {
|
|
free(m_pCWD);
|
|
};
|
|
if( m_pBasePath!=NULL ) {
|
|
free(m_pBasePath);
|
|
};
|
|
if( m_pSeriesBase!=NULL ) {
|
|
free(m_pSeriesBase);
|
|
};
|
|
if( m_pEntryFolder!= NULL ) {
|
|
free(m_pEntryFolder);
|
|
};
|
|
};
|
|
|
|
bool storageProvider::startNewSeries( void ){
|
|
char temp[128];
|
|
//#ifdef DEBUG
|
|
char Buffer[256];
|
|
//#endif
|
|
#ifdef __linux__
|
|
sprintf( temp, "%lld", getMilliseconds() );
|
|
#else
|
|
sprintf( temp, "%I64u", getMilliseconds());
|
|
#endif
|
|
//#ifdef DEBUG
|
|
sprintf(Buffer,"Stor: %s", temp);
|
|
LOG(Buffer);
|
|
//#endif
|
|
sprintf( m_pSeriesBase, "%s", MD5String( temp ) );
|
|
#ifdef DEBUG
|
|
sprintf(Buffer,"Stor Ser Base: %s", m_pSeriesBase);
|
|
LOG(Buffer);
|
|
#endif
|
|
m_nSeriesCounter = 0;
|
|
return true;
|
|
};
|
|
|
|
bool storageProvider::startNewEntry( void ){
|
|
char Buffer[256];
|
|
if( fileExist( m_pBasePath ) == 0 ) {
|
|
sprintf( Buffer, "No BasePath[%s] Detected", m_pBasePath );
|
|
LOG(Buffer);
|
|
return false;
|
|
};
|
|
sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y"));
|
|
sprintf( Buffer,"Test Base: %s", m_pCWD );
|
|
LOG(Buffer);
|
|
int RetVal;
|
|
if( fileExist( m_pCWD ) == 0 ){
|
|
#ifdef __linux__
|
|
RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
|
|
#else
|
|
RetVal = _mkdir( m_pCWD );
|
|
#endif
|
|
if( RetVal!= 0 ) {
|
|
return false;
|
|
};
|
|
};
|
|
sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m"));
|
|
sprintf( Buffer,"Test Base: %s", m_pCWD );
|
|
LOG(Buffer);
|
|
if( fileExist( m_pCWD ) == 0 ){
|
|
#ifdef __linux__
|
|
RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
|
|
#else
|
|
RetVal = _mkdir( m_pCWD );
|
|
#endif
|
|
if( RetVal!= 0 ) {
|
|
return false;
|
|
};
|
|
};
|
|
sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m/%d"));
|
|
sprintf( Buffer,"Test Base: %s", m_pCWD );
|
|
LOG(Buffer);
|
|
if( fileExist( m_pCWD ) == 0 ){
|
|
#ifdef __linux__
|
|
RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
|
|
#else
|
|
RetVal = _mkdir( m_pCWD );
|
|
#endif
|
|
if( RetVal!= 0 ) {
|
|
return false;
|
|
};
|
|
};
|
|
sprintf( m_pEntryFolder, "%s", getTStr((CSTR)"%H%M%S"));
|
|
char tVal[1024];
|
|
sprintf( tVal, "%s/%s", m_pCWD , m_pEntryFolder );
|
|
sprintf( Buffer,"Test Base: %s", tVal );
|
|
LOG(Buffer);
|
|
#ifdef __linux__
|
|
RetVal = mkdir( tVal, STORAGE_DIR_MODE );
|
|
#else
|
|
RetVal = _mkdir( tVal );
|
|
#endif
|
|
if( RetVal!= 0 ) {
|
|
return false;
|
|
};
|
|
return startNewSeries();
|
|
};
|
|
|
|
CSTR storageProvider::getNewFileName( void ){
|
|
char tVal[1024];
|
|
CSTR RetVal;
|
|
m_mCounterProtectorMutex.lock();
|
|
m_nSeriesCounter++;
|
|
sprintf( tVal, "%s/%s/%s_%d", m_pCWD, m_pEntryFolder, m_pSeriesBase, m_nSeriesCounter );
|
|
RetVal = (CSTR) malloc( strlen(tVal)+1);
|
|
strncpy( RetVal, tVal, strlen(tVal) );
|
|
RetVal[strlen(tVal)] = (char)0;
|
|
m_mCounterProtectorMutex.unlock();
|
|
return RetVal;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_DEBUG
|
|
#undef LOG_ERROR
|