00001
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <stdio.h>
00048 #include <string.h>
00049 #include <ctype.h>
00050
00051 #include "fsaccess.h"
00052 #include "config_file.h"
00053 #include "cptime.h"
00054 #include "shell.h"
00055
00056
00057 #ifdef FREERTOS_USED
00058 #include "FreeRTOS.h"
00059 #include "semphr.h"
00060 #include "task.h"
00061 #include "portmacro.h"
00062 #include "tracedump.h"
00063 #endif
00064
00065
00066
00067 #include "compiler.h"
00068
00069
00080 signed short config_file_set_value(char * filename, int ac, signed char *av[])
00081 {
00082 int size, i;
00083 char * pcDataToWrite = NULL;
00084 char * pcDataTemp = NULL;
00085 char * pcBegin = NULL;
00086 char * pcEnd = NULL;
00087 char * pcEq = NULL;
00088 signed short xreturn = -1;
00089 size_t FileLen;
00090 int fd;
00091 char pcTempoDate[17];
00092
00093 #ifdef FREERTOS_USED
00094 pcDataToWrite = pvPortMalloc(MAX_CONFIG_FILE_SIZE);
00095 #else
00096 pcDataToWrite = malloc(MAX_CONFIG_FILE_SIZE);
00097 #endif
00098 if (pcDataToWrite != NULL)
00099 {
00100 if ((fd = open(filename, (O_RDONLY))) >= 0)
00101 {
00102 if ((size = fsaccess_file_get_size(fd)) > 0)
00103 {
00104
00105 if (read(fd, pcDataToWrite, size) > 0)
00106 {
00107 pcDataToWrite[size] = '\0';
00108 for (i = 0 ; i < ac ; i+=2)
00109 {
00110
00111 pcBegin = strstr(pcDataToWrite, (char *)av[i]);
00112 if( NULL == pcBegin )
00113 {
00114 strcat( pcDataToWrite, CRLF );
00115 strcat( pcDataToWrite, (char *)av[i] );
00116 strcat( pcDataToWrite, "=" );
00117 strcat( pcDataToWrite, (char *)av[i+1] );
00118 strcat( pcDataToWrite, CRLF );
00119 }
00120 else
00121 {
00122
00123 pcEq = strpbrk(pcBegin, "=");
00124
00125 pcEnd = strpbrk(pcBegin, "\n");
00126 if( NULL == pcEnd )
00127 {
00128 pcEq++;
00129
00130 strcpy( pcEq, (char *)av[i+1] );
00131 }
00132 else
00133 {
00134 pcEnd++;
00135 if( ( pcEnd - pcDataToWrite ) < size )
00136 {
00137 #ifdef FREERTOS_USED
00138 if ((pcDataTemp = pvPortMalloc(MAX_CONFIG_FILE_SIZE)) != NULL)
00139 #else
00140 if ((pcDataTemp = malloc(MAX_CONFIG_FILE_SIZE)) != NULL)
00141 #endif
00142 {
00143 strcpy( pcDataTemp, pcEnd );
00144 }
00145 }
00146 pcEq++;
00147
00148 strcpy( pcEq, (char *)av[i+1] );
00149 strcat( pcEq, CRLF );
00150 if (pcDataTemp != NULL)
00151 {
00152
00153 strcat( pcEq, pcDataTemp );
00154 #ifdef FREERTOS_USED
00155 vPortFree(pcDataTemp);
00156 #else
00157 free(pcDataTemp);
00158 #endif
00159 }
00160 }
00161 }
00162 }
00163 }
00164 }
00165 close(fd);
00166 }
00167 if ((fd = open(filename, O_WRONLY)) >= 0)
00168 {
00169 FileLen = strlen( pcDataToWrite );
00170 if( write( fd, pcDataToWrite, FileLen ) == FileLen )
00171 {
00172 xreturn = 0;
00173 }
00174
00175 v_cptime_GetDateInFatStringFormat( pcTempoDate );
00176
00177 nav_file_dateset( (FS_STRING)pcTempoDate, FS_DATE_LAST_WRITE );
00178
00179 close(fd);
00180 }
00181 #ifdef FREERTOS_USED
00182 vPortFree(pcDataToWrite);
00183 #else
00184 free(pcDataToWrite);
00185 #endif
00186 }
00187
00188 return( xreturn );
00189 }
00190
00191
00201 signed short config_file_get_value(char * filename, char * pcField, char *pcValue)
00202 {
00203 int ErrorConfig = -1;
00204 int size;
00205 portCHAR * pcDataRead = NULL;
00206 portCHAR * current;
00207 portCHAR * end;
00208 int fd;
00209
00210 if ((fd = open(filename, O_RDONLY)) >= 0)
00211 {
00212
00213 size = fsaccess_file_get_size(fd);
00214
00215 if ((size > 0) && (size <= MAX_CONFIG_FILE_SIZE))
00216 {
00217 #ifdef FREERTOS_USED
00218 if ((pcDataRead = pvPortMalloc(size+1)) != NULL)
00219 #else
00220 if ((pcDataRead = malloc(size+1)) != NULL)
00221 #endif
00222 {
00223 if (read(fd,pcDataRead,size) == size)
00224 {
00225 pcDataRead[size] = '\0';
00226
00227 current = strstr(pcDataRead, pcField );
00228 if (current != NULL)
00229 {
00230
00231 current = strpbrk(current, "=" );
00232 if( NULL != current )
00233 {
00234 current++;
00235
00236
00237 end = strpbrk(current, "\r\n" );
00238 if( NULL != end )
00239 *end = '\0';
00240
00241
00242 strcpy(pcValue, current);
00243 ErrorConfig = 0;
00244 }
00245
00246
00247 }
00248 }
00249
00250 #ifdef FREERTOS_USED
00251 vPortFree(pcDataRead);
00252 #else
00253 free(pcDataRead);
00254 #endif
00255 }
00256 }
00257 close(fd);
00258 }
00259 return (ErrorConfig);
00260 }
00261