File manager
FileManager.cpp@0:a27e0d3581d1, 2016-05-10 (annotated)
- Committer:
- nsrwsurasak
- Date:
- Tue May 10 06:35:01 2016 +0000
- Revision:
- 0:a27e0d3581d1
- Child:
- 1:1f1f2b99756b
FileManager
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nsrwsurasak | 0:a27e0d3581d1 | 1 | #include "FileManager.h" |
nsrwsurasak | 0:a27e0d3581d1 | 2 | #include "SDFileSystem.h" |
nsrwsurasak | 0:a27e0d3581d1 | 3 | |
nsrwsurasak | 0:a27e0d3581d1 | 4 | char m_GpsInterval[XMLTEXT_SIZE]; |
nsrwsurasak | 0:a27e0d3581d1 | 5 | char m_DataInterval[XMLTEXT_SIZE]; |
nsrwsurasak | 0:a27e0d3581d1 | 6 | Variable_Data_TypeDef m_varList[MAX_VAR]; |
nsrwsurasak | 0:a27e0d3581d1 | 7 | unsigned int m_amountVarList; |
nsrwsurasak | 0:a27e0d3581d1 | 8 | |
nsrwsurasak | 0:a27e0d3581d1 | 9 | static void removeSpaces(char* s , int size); |
nsrwsurasak | 0:a27e0d3581d1 | 10 | static void getXmlText(char *str, char *ret); |
nsrwsurasak | 0:a27e0d3581d1 | 11 | |
nsrwsurasak | 0:a27e0d3581d1 | 12 | |
nsrwsurasak | 0:a27e0d3581d1 | 13 | static void removeSpaces(char* s , int size) |
nsrwsurasak | 0:a27e0d3581d1 | 14 | { |
nsrwsurasak | 0:a27e0d3581d1 | 15 | char* cpy = s; // an alias to iterate through s without moving s |
nsrwsurasak | 0:a27e0d3581d1 | 16 | char* temp = s; |
nsrwsurasak | 0:a27e0d3581d1 | 17 | |
nsrwsurasak | 0:a27e0d3581d1 | 18 | for (int i = 0 ; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 19 | if (*cpy != ' ') |
nsrwsurasak | 0:a27e0d3581d1 | 20 | *temp++ = *cpy; |
nsrwsurasak | 0:a27e0d3581d1 | 21 | cpy++; |
nsrwsurasak | 0:a27e0d3581d1 | 22 | } |
nsrwsurasak | 0:a27e0d3581d1 | 23 | *temp = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 24 | return; |
nsrwsurasak | 0:a27e0d3581d1 | 25 | } |
nsrwsurasak | 0:a27e0d3581d1 | 26 | static void getXmlText(char *str, char *ret) |
nsrwsurasak | 0:a27e0d3581d1 | 27 | { |
nsrwsurasak | 0:a27e0d3581d1 | 28 | int size = strlen(str); |
nsrwsurasak | 0:a27e0d3581d1 | 29 | int i; |
nsrwsurasak | 0:a27e0d3581d1 | 30 | bool begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 31 | char * ret_addr = ret; |
nsrwsurasak | 0:a27e0d3581d1 | 32 | memset (ret,' ',XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 33 | |
nsrwsurasak | 0:a27e0d3581d1 | 34 | for(i = 0; i < size ; i++) { |
nsrwsurasak | 0:a27e0d3581d1 | 35 | |
nsrwsurasak | 0:a27e0d3581d1 | 36 | if (*str == '>') { |
nsrwsurasak | 0:a27e0d3581d1 | 37 | begin_text = true; |
nsrwsurasak | 0:a27e0d3581d1 | 38 | } else if (begin_text && *str == '<') { |
nsrwsurasak | 0:a27e0d3581d1 | 39 | begin_text = false; |
nsrwsurasak | 0:a27e0d3581d1 | 40 | break; |
nsrwsurasak | 0:a27e0d3581d1 | 41 | } else if (begin_text && *str != ' ') { |
nsrwsurasak | 0:a27e0d3581d1 | 42 | *ret = *str; |
nsrwsurasak | 0:a27e0d3581d1 | 43 | ret++; |
nsrwsurasak | 0:a27e0d3581d1 | 44 | } |
nsrwsurasak | 0:a27e0d3581d1 | 45 | |
nsrwsurasak | 0:a27e0d3581d1 | 46 | str++; |
nsrwsurasak | 0:a27e0d3581d1 | 47 | } |
nsrwsurasak | 0:a27e0d3581d1 | 48 | removeSpaces(ret_addr, XMLTEXT_SIZE); |
nsrwsurasak | 0:a27e0d3581d1 | 49 | } |
nsrwsurasak | 0:a27e0d3581d1 | 50 | void readSetupFile() |
nsrwsurasak | 0:a27e0d3581d1 | 51 | { |
nsrwsurasak | 0:a27e0d3581d1 | 52 | // now open file for reading |
nsrwsurasak | 0:a27e0d3581d1 | 53 | FILE *fp = fopen(SETUP_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 54 | |
nsrwsurasak | 0:a27e0d3581d1 | 55 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 56 | printf("\nError! Unable to open file! %s \n", SETUP_FILE_NAME); |
nsrwsurasak | 0:a27e0d3581d1 | 57 | } else { // opened file so can write |
nsrwsurasak | 0:a27e0d3581d1 | 58 | // fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
nsrwsurasak | 0:a27e0d3581d1 | 59 | // serial.printf("Read %d from file.\n",stored_top_score); |
nsrwsurasak | 0:a27e0d3581d1 | 60 | |
nsrwsurasak | 0:a27e0d3581d1 | 61 | ReadingFileState state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 62 | char buf[1024]; |
nsrwsurasak | 0:a27e0d3581d1 | 63 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
nsrwsurasak | 0:a27e0d3581d1 | 64 | if (strstr (buf,DATA_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 65 | state = STATE_FOUND_DATA; |
nsrwsurasak | 0:a27e0d3581d1 | 66 | } else if (strstr (buf,GPS_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 67 | state = STATE_FOUND_GPS; |
nsrwsurasak | 0:a27e0d3581d1 | 68 | } else if (strstr (buf,UPDATE_INTERVAL_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 69 | if (state == STATE_FOUND_GPS) { |
nsrwsurasak | 0:a27e0d3581d1 | 70 | getXmlText(buf, m_GpsInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 71 | printf("\r\n-found GPS interval %s ", m_GpsInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 72 | state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 73 | } else if(state == STATE_FOUND_DATA) { |
nsrwsurasak | 0:a27e0d3581d1 | 74 | getXmlText(buf, m_DataInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 75 | printf("\r\n-found Data interval %s ", m_DataInterval); |
nsrwsurasak | 0:a27e0d3581d1 | 76 | state = STATE_FINDING; |
nsrwsurasak | 0:a27e0d3581d1 | 77 | } |
nsrwsurasak | 0:a27e0d3581d1 | 78 | } |
nsrwsurasak | 0:a27e0d3581d1 | 79 | } |
nsrwsurasak | 0:a27e0d3581d1 | 80 | fclose(fp); // ensure you close the file after reading |
nsrwsurasak | 0:a27e0d3581d1 | 81 | } |
nsrwsurasak | 0:a27e0d3581d1 | 82 | } |
nsrwsurasak | 0:a27e0d3581d1 | 83 | void logGPSData(char date[], char time[]) |
nsrwsurasak | 0:a27e0d3581d1 | 84 | { |
nsrwsurasak | 0:a27e0d3581d1 | 85 | FILE *fp = fopen(GPS_LOG_FILE_NAME, "a"); |
nsrwsurasak | 0:a27e0d3581d1 | 86 | |
nsrwsurasak | 0:a27e0d3581d1 | 87 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 88 | printf("Error! Unable to open file!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 89 | } else { // opened file so can write |
nsrwsurasak | 0:a27e0d3581d1 | 90 | printf("\r\n Writing to Gps Log File...."); |
nsrwsurasak | 0:a27e0d3581d1 | 91 | |
nsrwsurasak | 0:a27e0d3581d1 | 92 | fprintf(fp, "%s,%s\n",date,time); // print formatted string to file (CSV) |
nsrwsurasak | 0:a27e0d3581d1 | 93 | |
nsrwsurasak | 0:a27e0d3581d1 | 94 | printf("Done"); |
nsrwsurasak | 0:a27e0d3581d1 | 95 | fclose(fp); // ensure you close the file after writing |
nsrwsurasak | 0:a27e0d3581d1 | 96 | } |
nsrwsurasak | 0:a27e0d3581d1 | 97 | } |
nsrwsurasak | 0:a27e0d3581d1 | 98 | void logSystemData(float gps_interval) |
nsrwsurasak | 0:a27e0d3581d1 | 99 | { |
nsrwsurasak | 0:a27e0d3581d1 | 100 | FILE *fp = fopen(MINIRMS_LOG_FILE_NAME, "a"); |
nsrwsurasak | 0:a27e0d3581d1 | 101 | |
nsrwsurasak | 0:a27e0d3581d1 | 102 | |
nsrwsurasak | 0:a27e0d3581d1 | 103 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 104 | printf("Error! Unable to open file!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 105 | } else { // opened file so can write |
nsrwsurasak | 0:a27e0d3581d1 | 106 | fprintf(fp, "Start Mini-RMS System with Gps Interval = %f",gps_interval); // ensure data type matches |
nsrwsurasak | 0:a27e0d3581d1 | 107 | fclose(fp); // ensure you close the file after writing |
nsrwsurasak | 0:a27e0d3581d1 | 108 | } |
nsrwsurasak | 0:a27e0d3581d1 | 109 | } |
nsrwsurasak | 0:a27e0d3581d1 | 110 | void delete_file(char filename[]) |
nsrwsurasak | 0:a27e0d3581d1 | 111 | { |
nsrwsurasak | 0:a27e0d3581d1 | 112 | printf("Deleting file '%s'...",filename); |
nsrwsurasak | 0:a27e0d3581d1 | 113 | FILE *fp = fopen(filename, "r"); // try and open file |
nsrwsurasak | 0:a27e0d3581d1 | 114 | if (fp != NULL) { // if it does open... |
nsrwsurasak | 0:a27e0d3581d1 | 115 | fclose(fp); // close it |
nsrwsurasak | 0:a27e0d3581d1 | 116 | remove(filename); // and then delete |
nsrwsurasak | 0:a27e0d3581d1 | 117 | printf("Done!\n"); |
nsrwsurasak | 0:a27e0d3581d1 | 118 | } |
nsrwsurasak | 0:a27e0d3581d1 | 119 | // if we can't open it, it doesn't exist and so we can't delete it |
nsrwsurasak | 0:a27e0d3581d1 | 120 | } |
nsrwsurasak | 0:a27e0d3581d1 | 121 | int GPSInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 122 | { |
nsrwsurasak | 0:a27e0d3581d1 | 123 | //Return whether or not CRC is enabled |
nsrwsurasak | 0:a27e0d3581d1 | 124 | return atoi( m_GpsInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 125 | } |
nsrwsurasak | 0:a27e0d3581d1 | 126 | int DataInterval() |
nsrwsurasak | 0:a27e0d3581d1 | 127 | { |
nsrwsurasak | 0:a27e0d3581d1 | 128 | //Return whether or not CRC is enabled |
nsrwsurasak | 0:a27e0d3581d1 | 129 | return atoi( m_DataInterval ); |
nsrwsurasak | 0:a27e0d3581d1 | 130 | } |
nsrwsurasak | 0:a27e0d3581d1 | 131 | |
nsrwsurasak | 0:a27e0d3581d1 | 132 | Variable_Data_TypeDef * readVarFile() |
nsrwsurasak | 0:a27e0d3581d1 | 133 | { |
nsrwsurasak | 0:a27e0d3581d1 | 134 | // now open file for reading |
nsrwsurasak | 0:a27e0d3581d1 | 135 | FILE *fp = fopen(VARIABLE_FILE_NAME, "r"); |
nsrwsurasak | 0:a27e0d3581d1 | 136 | |
nsrwsurasak | 0:a27e0d3581d1 | 137 | if (fp == NULL) { // if it can't open the file then print error message |
nsrwsurasak | 0:a27e0d3581d1 | 138 | printf("\nError! Unable to open file! %s \n", VARIABLE_FILE_NAME); |
nsrwsurasak | 0:a27e0d3581d1 | 139 | return NULL; |
nsrwsurasak | 0:a27e0d3581d1 | 140 | } else { // opened file so can write |
nsrwsurasak | 0:a27e0d3581d1 | 141 | |
nsrwsurasak | 0:a27e0d3581d1 | 142 | char buf[1024]; |
nsrwsurasak | 0:a27e0d3581d1 | 143 | int index = 0; |
nsrwsurasak | 0:a27e0d3581d1 | 144 | memset(m_varList, ' ', sizeof(m_varList)); |
nsrwsurasak | 0:a27e0d3581d1 | 145 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
nsrwsurasak | 0:a27e0d3581d1 | 146 | if (strstr (buf,VAR_NAME_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 147 | getXmlText(buf , m_varList[index].varName); |
nsrwsurasak | 0:a27e0d3581d1 | 148 | |
nsrwsurasak | 0:a27e0d3581d1 | 149 | } else if (strstr (buf,VAR_ADDR_TAG)) { |
nsrwsurasak | 0:a27e0d3581d1 | 150 | getXmlText(buf , m_varList[index].varAddress); |
nsrwsurasak | 0:a27e0d3581d1 | 151 | index++; |
nsrwsurasak | 0:a27e0d3581d1 | 152 | } |
nsrwsurasak | 0:a27e0d3581d1 | 153 | |
nsrwsurasak | 0:a27e0d3581d1 | 154 | } |
nsrwsurasak | 0:a27e0d3581d1 | 155 | fclose(fp); // ensure you close the file after reading |
nsrwsurasak | 0:a27e0d3581d1 | 156 | m_amountVarList = index; |
nsrwsurasak | 0:a27e0d3581d1 | 157 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 158 | } |
nsrwsurasak | 0:a27e0d3581d1 | 159 | } |
nsrwsurasak | 0:a27e0d3581d1 | 160 | int getAmountVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 161 | { |
nsrwsurasak | 0:a27e0d3581d1 | 162 | return m_amountVarList; |
nsrwsurasak | 0:a27e0d3581d1 | 163 | } |
nsrwsurasak | 0:a27e0d3581d1 | 164 | Variable_Data_TypeDef * getVarList() |
nsrwsurasak | 0:a27e0d3581d1 | 165 | { |
nsrwsurasak | 0:a27e0d3581d1 | 166 | return m_varList; |
nsrwsurasak | 0:a27e0d3581d1 | 167 | } |
nsrwsurasak | 0:a27e0d3581d1 | 168 | |
nsrwsurasak | 0:a27e0d3581d1 | 169 |