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.

187 lines
4.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
  1. #include "storageProvider.h"
  2. #define LOG(X) m_pLog->Log( "storageProvider", this, X )
  3. #define LOG_DEBUG( X ) m_pLog->Log( "storageProvider", this, _LOG_DEBUG, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "storageProvider", this, _LOG_ERROR, X )
  5. storageProvider* storageProvider::_instance = 0;
  6. /**
  7. * Returns the single instance of the object.
  8. */
  9. storageProvider* storageProvider::getInstance(CSTR Base) {
  10. // check if the instance has been created yet
  11. if (0 == _instance) {
  12. // if not, then create it
  13. _instance = new storageProvider(Base);
  14. }
  15. // return the single instance
  16. return _instance;
  17. };
  18. storageProvider::storageProvider(CSTR BasePath) :m_mCounterProtectorMutex(){
  19. m_pLog = basicLogCollection::getInstance();
  20. LOG("Start Storage");
  21. m_pCWD = (CSTR) malloc(MAX_PATH_LEN);
  22. m_pBasePath = (CSTR) malloc(MAX_PATH_LEN);
  23. m_pEntryFolder = (CSTR) malloc( 9 );
  24. m_pSeriesBase = (CSTR) malloc( 33 );
  25. m_nSeriesCounter = 0;
  26. m_bStorageInitComplete = false;
  27. if( m_pBasePath!= NULL ) {
  28. #ifdef __linux__
  29. getcwd(m_pCWD, MAX_PATH_LEN );
  30. #else
  31. _getcwd(m_pCWD, MAX_PATH_LEN );
  32. #endif
  33. //strncpy( m_pBasePath, BasePath, 1024 );
  34. sprintf(m_pBasePath, "%s/%s", m_pCWD, BasePath);
  35. #ifdef DEBUG
  36. char Buffer[256];
  37. sprintf(Buffer,"Base Path OK [%s]", m_pBasePath );
  38. LOG(Buffer);
  39. #endif
  40. //startNewEntry();
  41. } else {
  42. LOG_ERROR("No Base Path Provided!");
  43. };
  44. };
  45. storageProvider::~storageProvider(){
  46. if( m_pCWD!=NULL ) {
  47. free(m_pCWD);
  48. };
  49. if( m_pBasePath!=NULL ) {
  50. free(m_pBasePath);
  51. };
  52. if( m_pSeriesBase!=NULL ) {
  53. free(m_pSeriesBase);
  54. };
  55. if( m_pEntryFolder!= NULL ) {
  56. free(m_pEntryFolder);
  57. };
  58. };
  59. bool storageProvider::startNewSeries( void ){
  60. char temp[128];
  61. //#ifdef DEBUG
  62. char Buffer[256];
  63. //#endif
  64. #ifdef __linux__
  65. sprintf( temp, "%lld", getMilliseconds() );
  66. #else
  67. sprintf( temp, "%I64u", getMilliseconds());
  68. #endif
  69. //#ifdef DEBUG
  70. sprintf(Buffer,"Stor: %s", temp);
  71. LOG(Buffer);
  72. //#endif
  73. sprintf( m_pSeriesBase, "%s", MD5String( temp ) );
  74. #ifdef DEBUG
  75. sprintf(Buffer,"Stor Ser Base: %s", m_pSeriesBase);
  76. LOG(Buffer);
  77. #endif
  78. m_nSeriesCounter = 0;
  79. m_bStorageInitComplete = true;
  80. return m_bStorageInitComplete;
  81. };
  82. bool storageProvider::startNewEntry( void ){
  83. char Buffer[256];
  84. if( fileExist( m_pBasePath ) == 0 ) {
  85. sprintf( Buffer, "No BasePath[%s] Detected", m_pBasePath );
  86. LOG(Buffer);
  87. return false;
  88. };
  89. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y"));
  90. sprintf( Buffer,"Test Base: %s", m_pCWD );
  91. LOG(Buffer);
  92. int RetVal;
  93. if( fileExist( m_pCWD ) == 0 ){
  94. #ifdef __linux__
  95. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  96. #else
  97. RetVal = _mkdir( m_pCWD );
  98. #endif
  99. if( RetVal!= 0 ) {
  100. return false;
  101. };
  102. };
  103. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m"));
  104. sprintf( Buffer,"Test Base: %s", m_pCWD );
  105. LOG(Buffer);
  106. if( fileExist( m_pCWD ) == 0 ){
  107. #ifdef __linux__
  108. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  109. #else
  110. RetVal = _mkdir( m_pCWD );
  111. #endif
  112. if( RetVal!= 0 ) {
  113. return false;
  114. };
  115. };
  116. sprintf( m_pCWD, "%s/%s", m_pBasePath ,getTStr((CSTR)"%Y/%m/%d"));
  117. sprintf( Buffer,"Test Base: %s", m_pCWD );
  118. LOG(Buffer);
  119. if( fileExist( m_pCWD ) == 0 ){
  120. #ifdef __linux__
  121. RetVal = mkdir( m_pCWD, STORAGE_DIR_MODE );
  122. #else
  123. RetVal = _mkdir( m_pCWD );
  124. #endif
  125. if( RetVal!= 0 ) {
  126. return false;
  127. };
  128. };
  129. sprintf( m_pEntryFolder, "%s", getTStr((CSTR)"%H%M%S"));
  130. char tVal[1024];
  131. sprintf( tVal, "%s/%s", m_pCWD , m_pEntryFolder );
  132. sprintf( Buffer,"Test Base: %s", tVal );
  133. LOG(Buffer);
  134. #ifdef __linux__
  135. RetVal = mkdir( tVal, STORAGE_DIR_MODE );
  136. #else
  137. RetVal = _mkdir( tVal );
  138. #endif
  139. if( RetVal!= 0 ) {
  140. return false;
  141. };
  142. return startNewSeries();
  143. };
  144. CSTR storageProvider::getCurrentFilePath( void ){
  145. char tVal[1024];
  146. CSTR RetVal;
  147. m_mCounterProtectorMutex.lock();
  148. sprintf(tVal, "%s/%s", m_pCWD, m_pEntryFolder );
  149. RetVal = (CSTR)malloc(strlen(tVal) + 1);
  150. if (RetVal != NULL) {
  151. strncpy(RetVal, tVal, strlen(tVal));
  152. RetVal[strlen(tVal)] = (char)0;
  153. };
  154. m_mCounterProtectorMutex.unlock();
  155. return RetVal;
  156. };
  157. CSTR storageProvider::getNewFileName( CSTR JustFileName ){
  158. char tVal[1024];
  159. CSTR RetVal;
  160. m_mCounterProtectorMutex.lock();
  161. if (!m_bStorageInitComplete) startNewEntry();
  162. m_nSeriesCounter++;
  163. if(JustFileName!=NULL)sprintf(JustFileName, "%s_%d", m_pSeriesBase, m_nSeriesCounter);
  164. sprintf( tVal, "%s/%s/%s_%d", m_pCWD, m_pEntryFolder, m_pSeriesBase, m_nSeriesCounter );
  165. RetVal = (CSTR) malloc( strlen(tVal)+1);
  166. if (RetVal != NULL) {
  167. strncpy(RetVal, tVal, strlen(tVal));
  168. RetVal[strlen(tVal)] = (char)0;
  169. };
  170. m_mCounterProtectorMutex.unlock();
  171. return RetVal;
  172. };
  173. #undef LOG
  174. #undef LOG_DEBUG
  175. #undef LOG_ERROR