File manager

Dependencies:   SDFileSystem

Dependents:   RwSDCard_Xml_GPS

FileManager.cpp

Committer:
nsrwsurasak
Date:
2016-05-10
Revision:
0:a27e0d3581d1
Child:
1:1f1f2b99756b

File content as of revision 0:a27e0d3581d1:

#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;

static void removeSpaces(char* s , int size);
static void getXmlText(char *str, char *ret);


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);
}
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(char date[], char time[])
{
    FILE *fp  = fopen(GPS_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
        printf("\r\n Writing to Gps Log File....");

        fprintf(fp, "%s,%s\n",date,time);  // print formatted string to file (CSV)

        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, "Start 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
}
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;
}