File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

FileManager.cpp

Committer:
Lucyjungz
Date:
2016-05-10
Revision:
2:18e004a47f52
Parent:
1:1f1f2b99756b
Child:
3:6e08d0bba1bb

File content as of revision 2:18e004a47f52:

#include "mbed.h"
#include "FileManager.h"
#include "SDFileSystem.h"

char m_GpsInterval[XMLTEXT_SIZE];
char m_DataInterval[XMLTEXT_SIZE];
Variable_Data_TypeDef m_varList[MAX_VAR];
unsigned int m_amountVarList = 0;

static void removeSpaces(char* s , int size);
static void getXmlText(char *str, char *ret);
static void generateFileNameWithTime(time_t timestamp, char * file_name);

static void removeSpaces(char* s , int size)
{
    char* cpy = s;  // an alias to iterate through s without moving s
    char* temp = s;

    for (int i = 0 ; i < size ; i++) {
        if (*cpy != ' ')
            *temp++ = *cpy;
        cpy++;
    }
    *temp = 0;
    return;
}
static void getXmlText(char *str, char *ret)
{
    int size = strlen(str);
    int i;
    bool begin_text = false;
    char * ret_addr = ret;
    memset (ret,' ',XMLTEXT_SIZE);

    for(i = 0; i < size ; i++) {

        if (*str == '>') {
            begin_text = true;
        } else if (begin_text && *str == '<') {
            begin_text = false;
            break;
        } else if (begin_text && *str != ' ') {
            *ret = *str;
            ret++;
        }

        str++;
    }
    removeSpaces(ret_addr, XMLTEXT_SIZE);
}
static void generateFileNameWithTime(time_t timestamp, char * file_name)
{
    char str[5];
    struct tm * ptm;
    
    ptm = localtime ( &timestamp );  
    sprintf(str,"%04d", ptm->tm_year-100 + 2000);
    memcpy(&file_name[4], str, 4);
    
    sprintf(str,"%02d", ptm->tm_mon+1);
    memcpy(&file_name[9], str, 2);
    
    sprintf(str,"%02d", ptm->tm_mday);
    memcpy(&file_name[12], str, 2); 
}

void readSetupFile()
{
    // now open file for reading
    FILE *fp = fopen(SETUP_FILE_NAME, "r");

    if (fp == NULL) {  // if it can't open the file then print error message
        printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME);
    } else {  // opened file so can write
//        fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&)
//        serial.printf("Read %d from file.\n",stored_top_score);

        ReadingFileState  state = STATE_FINDING;
        char buf[1024];
        while (fgets(buf, sizeof(buf), fp) != NULL) {
            if (strstr (buf,DATA_TAG)) {
                state = STATE_FOUND_DATA;
            } else if (strstr (buf,GPS_TAG)) {
                state = STATE_FOUND_GPS;
            } else if (strstr (buf,UPDATE_INTERVAL_TAG)) {
                if (state == STATE_FOUND_GPS) {
                    getXmlText(buf, m_GpsInterval);
                    printf("\r\n-found GPS interval %s ", m_GpsInterval);
                    state = STATE_FINDING;
                } else if(state == STATE_FOUND_DATA) {
                    getXmlText(buf, m_DataInterval);
                    printf("\r\n-found Data interval %s ", m_DataInterval);
                    state = STATE_FINDING;
                }
            }
        }
        fclose(fp);  // ensure you close the file after reading
    }
}
void logGPSData(time_t timestamp ,char lat[], char longti[])
{
    char file_name[] = GPS_LOG_FILE_NAME;
    
    generateFileNameWithTime(timestamp,file_name);
    FILE *fp  = fopen(file_name, "a");

    if (fp == NULL) {  // if it can't open the file then print error message
        printf("Error! Unable to open file %s!\n",file_name);
    } else {  // opened file so can write
        printf("\r\n Writing to Gps Log File (%s)....",file_name);

        fprintf(fp, "%d,%s,%s\n",timestamp,lat,longti);  // print formatted string to file (CSV)

        printf("Done");
        fclose(fp);  // ensure you close the file after writing
    }
}
void logRMSData(time_t timestamp ,float * var, int size)
{
    char file_name[] = RTL_LOG_FILE_NAME;
    
    generateFileNameWithTime(timestamp,file_name);
    if (!is_file_exist(file_name))
    {
        logRMSHeader(timestamp);
    }   
    FILE *fp  = fopen(file_name, "a");

    if (fp == NULL) {  // if it can't open the file then print error message
        printf("Error! Unable to open file %s!\n",file_name);
    } else {  // opened file so can write
        printf("\r\n Writing to Log File (%s)....",file_name);

        fprintf(fp, "%d",timestamp);  // print Timestamp

        for(int i = 0; i < size; i++)
        {
            fprintf(fp, ",%f",var[i]);  // print variable list
        }
        fprintf(fp, "\n");  // print new line

        printf("Done");
        fclose(fp);  // ensure you close the file after writing
    }
}
void logRMSHeader(time_t timestamp)
{
    char file_name[] = RTL_LOG_FILE_NAME;
    
    generateFileNameWithTime(timestamp,file_name);
    

    FILE *fp  = fopen(file_name, "a");

    if (fp == NULL) {  // if it can't open the file then print error message
        printf("Error! Unable to open file %s!\n",file_name);
    } else {  // opened file so can write
        printf("\r\n Writing to Log File (%s)....",file_name);

        fprintf(fp, "%s",RMS_HEADER_TIME);  // print variable list
        for(int i = 0; i < m_amountVarList; i++)
        {
            fprintf(fp, ",%s",m_varList[i].varName);  // print variable list
        }
        fprintf(fp, "\n");  // print new line

        printf("Done");
        fclose(fp);  // ensure you close the file after writing
    }
}
void logSystemData(float gps_interval)
{
    FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a");

    if (fp == NULL) {  // if it can't open the file then print error message
        printf("Error! Unable to open file!\n");
    } else {  // opened file so can write
        fprintf(fp, "\nStart Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches
        fclose(fp);  // ensure you close the file after writing
    }
}
void delete_file(char filename[])
{
    printf("Deleting file '%s'...",filename);
    FILE *fp = fopen(filename, "r");  // try and open file
    if (fp != NULL) {  // if it does open...
        fclose(fp);    // close it
        remove(filename);  // and then delete
        printf("Done!\n");
    }
    // if we can't open it, it doesn't exist and so we can't delete it
}
bool is_file_exist(char filename[])
{
    bool exist = false;
    FILE *fp = fopen(filename, "r");  // try and open file
    if (fp != NULL) {  // if it does open...
        fclose(fp);    // close it
        exist = true;
    }
    
    return exist;
}
int GPSInterval()
{
    //Return whether or not CRC is enabled
    return atoi( m_GpsInterval );
}
int DataInterval()
{
    //Return whether or not CRC is enabled
    return atoi( m_DataInterval );
}

Variable_Data_TypeDef * readVarFile()
{
    // now open file for reading
    FILE *fp = fopen(VARIABLE_FILE_NAME, "r");

    if (fp == NULL) {  // if it can't open the file then print error message
        printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME);
        return NULL;
    } else {  // opened file so can write

        char buf[1024];
        int index = 0;
        memset(m_varList, ' ', sizeof(m_varList));
        while (fgets(buf, sizeof(buf), fp) != NULL) {
            if (strstr (buf,VAR_NAME_TAG)) {
                getXmlText(buf , m_varList[index].varName);

            } else if (strstr (buf,VAR_ADDR_TAG)) {
                getXmlText(buf , m_varList[index].varAddress);
                index++;
            }

        }
        fclose(fp);  // ensure you close the file after reading
        m_amountVarList = index;
        return m_varList;
    }
}
int getAmountVarList()
{
    return m_amountVarList;
}
Variable_Data_TypeDef * getVarList()
{
    return m_varList;
}