File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

Revision:
33:dd67a980e9d8
Parent:
32:69b7c870d69c
Child:
34:097b1f6828b7
--- a/FileManager.cpp	Tue Sep 13 09:22:31 2016 +0000
+++ b/FileManager.cpp	Wed Sep 14 02:07:06 2016 +0000
@@ -283,13 +283,18 @@
         /* Indicate LED Status (ON)*/
         FILEMANAGER_SetLedStatus(true);
         
+        #if DEBUG
         /* Print some message for information */
         printf("\r\n Writing to Gps Log File (%s)....",file_name);
-
+        #endif
+        
         /* Write the line with lattitude and longtitude */
         fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti); 
 
+        #if DEBUG
         printf("Done");
+        #endif
+        
         /* Close file once it done */
         fclose(fp);  
         
@@ -335,20 +340,20 @@
     #if DEBUG
     printf("\r\n File name is generated \n");
     #endif
-    if (*p_headerRequired || !FILEMANAGER_IsFileExist(file_name))
-    {
-        /* If file is not exist, log the header */
-        FILEMANAGER_LogRMSHeader(timestamp);
-        
-        /* Clear flag once header has been written */
-        *p_headerRequired = false;
-    }   
+ 
     #if DEBUG
     printf("\r\n Going to Open RMS File For write \n");
     #endif
     /* Open file with "APPEND" mode */
     FILE *fp  = fopen(file_name, "a");
-
+    if (*p_headerRequired || !FILEMANAGER_IsFileExist(file_name))
+    {
+        /* If file is not exist, log the header */
+        FILEMANAGER_LogRMSHeader(fp, timestamp, file_name);
+        
+        /* Clear flag once header has been written */
+        *p_headerRequired = false;
+    }  
     if (fp == NULL) 
     {  
         /* In case of error, print the error message */
@@ -366,9 +371,11 @@
         t.reset();
         t.start();
         #endif
+        
+        #if DEBUG
         /* Print some message for information */
         printf("\r\n Writing to Log File (%s)....",file_name);
-
+        #endif
         /* Write timestamp */
         fprintf(fp, "%d,%d",timestamp,msec);
 
@@ -380,7 +387,9 @@
         /* Write new line as we done */
         fprintf(fp, "\n");
 
+        #if DEBUG
         printf("Done");
+        #endif
         /* Close the file */
         fclose(fp);
         #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
@@ -398,66 +407,32 @@
  * @param timestamp - time structure to get Date 
  * @retval None
  */
-void FILEMANAGER_LogRMSHeader(time_t timestamp)
+void FILEMANAGER_LogRMSHeader(FILE *fp, time_t timestamp, char file_name[])
 {   
-
-    /* Get File name */
-    char file_name[] = RTL_LOG_FILE_NAME;
-    
-    /* Generate file name with time */
-    FILEMANAGER_GenerateFileNameWithTime(timestamp,file_name);
+    #if DEBUG
+    /* opened file so can write */
+    printf("\r\n Writing Header to Log File (%s)....",file_name);
+    #endif
+    fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
     
-    /* Open file in append mode */
-    FILE *fp  = fopen(file_name, "a");
-
-    if (fp == NULL) 
-    {  
-        /* In case of error, print the error message */
-        printf("Error! Unable to open file %s!\n",file_name);
-        
-        /* Indicate LED Status (OFF)*/
-        FILEMANAGER_SetLedStatus(false);
-    } 
-    else 
-    {  
-        /* Indicate LED Status (ON)*/
-        FILEMANAGER_SetLedStatus(true);
-        
-        /* opened file so can write */
-        printf("\r\n Writing to Log File (%s)....",file_name);
-
-        /* Write the header to the file  */
-        fprintf(fp, "%s,%s",RMS_HEADER_TIME,RMS_HEADER_MSECOND);
-        
-        for(int i = 0; i < m_amountVarList; i++)
-        {
-            fprintf(fp, ",%s",m_varList[i].varName); 
-        }
-        /* Write new line as done */
-        fprintf(fp, "\n");
-        
-        /* Write the timestamp unit to the file  */
-        fprintf(fp, "-,-");
-        
-        
-        /* Write the unit of variables to the file  */
-        for(int i = 0; i < m_amountVarList; i++)
-        {
-            fprintf(fp, ",%s",m_varList[i].varUnit); 
-        }
-        
-        /* Write new line as done */
-        fprintf(fp, "\n");
-
-        printf("Done");
-        
-        /* Close the file */
-        fclose(fp);  
-        
-        #if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
-        free(fp);
-        #endif
+    for(int i = 0; i < m_amountVarList; i++)
+    {
+        fprintf(fp, ",%s",m_varList[i].varName); 
     }
+    /* Write new line as done */
+    fprintf(fp, "\n-,-");    
+    
+    /* Write the unit of variables to the file  */
+    for(int i = 0; i < m_amountVarList; i++)
+    {
+        fprintf(fp, ",%s",m_varList[i].varUnit); 
+    }
+    
+    /* Write new line as done */
+    fprintf(fp, "\n");
+    #if DEBUG
+    printf("Done");
+    #endif
 }
 /**
  * @brief Function to log Mini RMS System Data
@@ -508,9 +483,16 @@
  */
 void FILEMANAGER_Deletefile(char filename[])
 {
+    #if UART_FILE_ACCESS
     FILEMANAGER_AddPrefixFilename(filename, m_bufFileName);
+    #endif
     printf("Deleting file '%s'...",filename);
+    
+    #if UART_FILE_ACCESS
     FILE *fp = fopen(m_bufFileName, "r");  // try and open file
+    #else
+    FILE *fp = fopen(filename, "r");  // try and open file
+    #endif
     if (fp != NULL) {  // if it does open...
         fclose(fp);    // close it
         
@@ -518,7 +500,11 @@
         free(fp);
         #endif
         
+        #if UART_FILE_ACCESS
         remove(m_bufFileName);  // and then delete
+        #else
+        remove(filename);  // and then delete
+        #endif
         printf("Done!\n");
     }
     // if we can't open it, it doesn't exist and so we can't delete it