config_file.c File Reference


Detailed Description

Management of the config file access.

This file manages the accesses to the config files.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file config_file.c.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "fsaccess.h"
#include "config_file.h"
#include "cptime.h"
#include "shell.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#include "portmacro.h"
#include "tracedump.h"
#include "compiler.h"

Go to the source code of this file.

Functions

signed short config_file_get_value (char *filename, char *pcField, char *pcValue)
 get module config, by parsing config file.
signed short config_file_set_value (char *filename, int ac, signed char *av[])
 This fonction replace a (or many) line(s) in a file.


Function Documentation

signed short config_file_get_value ( char *  filename,
char *  pcField,
char *  pcValue 
)

get module config, by parsing config file.

Parameters:
filename Input. file to read into.
pcField Input. field to find in file.
pcValue Output. value read for this field, as string.
Returns:
short 0 if success, -1 otherwise.

Definition at line 201 of file config_file.c.

References MAX_CONFIG_FILE_SIZE.

Referenced by b_joystick_init(), b_light_init(), b_potentiometer_init(), b_pushb1_init(), b_pushb2_init(), b_pushb3_init(), b_temperature_init(), portTASK_FUNCTION(), and prvEthernetConfigureInterface().

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     /* get the file size */
00213     size = fsaccess_file_get_size(fd);
00214     /* if size is consistant */
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           // Look for the requested field.
00227           current = strstr(pcDataRead, pcField );
00228           if (current != NULL)
00229           {
00230             /* goto = */
00231             current = strpbrk(current, "=" );
00232             if( NULL != current )
00233             {   // The equal sign was found
00234               current++; // Goto just after the = sign.
00235               // Look for the end of line 
00236               // (Well-formed config file rule is : one param=value pair per line)
00237               end = strpbrk(current, "\r\n" );
00238               if( NULL != end ) 
00239                 *end = '\0';
00240               // Else the param is the last one in the config file and we'll use
00241               // the '\0' of pcDataRead).               
00242               strcpy(pcValue, current);
00243               ErrorConfig = 0;
00244             }
00245             // else '=' sign not found => the config file is not well-formed.
00246             // (Well-formed config file rule is, per line: param=value).
00247           }
00248         }// read succeeds
00249         /* free buffer */
00250 #ifdef FREERTOS_USED
00251         vPortFree(pcDataRead);
00252 #else
00253         free(pcDataRead);
00254 #endif
00255       }// alloc succeeds
00256     }// size is OK
00257     close(fd);
00258   }// open OK
00259   return (ErrorConfig);
00260 }

signed short config_file_set_value ( char *  filename,
int  ac,
signed char *  av[] 
)

This fonction replace a (or many) line(s) in a file.

Syntax of the line should be :

field=value\r\n 

Parameters:
filename Input. The filename to write into.
ac Input. The argument counter. For this command, should be 1.
av Input. The argument vector.
Returns:
short : 0 if success or -1 else

Definition at line 80 of file config_file.c.

References CRLF, MAX_CONFIG_FILE_SIZE, pcTempoDate, and v_cptime_GetDateInFatStringFormat().

Referenced by e_ethernet_cmd_set_config(), e_joystick_set_config(), e_light_set_config(), e_potentiometer_set_config(), e_smtpclient_cmd_set_config(), e_temperature_set_config(), e_webserver_cmd_set_config(), and prv_e_pushb_set_config().

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         /* get data from file */
00105         if (read(fd, pcDataToWrite, size) > 0)
00106         {
00107           pcDataToWrite[size] = '\0';
00108           for (i = 0 ; i < ac ; i+=2)
00109           {
00110             /* search the field in the file */
00111             pcBegin = strstr(pcDataToWrite, (char *)av[i]);
00112             if( NULL == pcBegin )
00113             {  // The field is not yet in the file.
00114               strcat( pcDataToWrite, CRLF );  // Add a new line for this field.
00115               strcat( pcDataToWrite, (char *)av[i] ); // Add the field.
00116               strcat( pcDataToWrite, "=" );   // Add the =
00117               strcat( pcDataToWrite, (char *)av[i+1] );  // Add the value. 
00118               strcat( pcDataToWrite, CRLF );  // Add the CRLF. 
00119             }
00120             else
00121             {
00122               /* Search for the = sign. */
00123               pcEq = strpbrk(pcBegin, "=");
00124               /* search for end of line */
00125               pcEnd = strpbrk(pcBegin, "\n");
00126               if( NULL == pcEnd )
00127               { // no crlf, we're at the end of the file.
00128                 pcEq++; // WARNING NOTE: we assume the cfg file is well-formed,
00129                         // i.e. there must be an equal sign after the field name then a value.
00130                 strcpy( pcEq, (char *)av[i+1] );  // Add the value. 
00131               }
00132               else
00133               {
00134                 pcEnd++;
00135                 if( ( pcEnd - pcDataToWrite ) < size )
00136                 { // There are other fields after this one.
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++; // WARNING NOTE: we assume the cfg file is well-formed,
00147                         // i.e. there must be an equal sign after the field name then a value.
00148                 strcpy( pcEq, (char *)av[i+1] );  // Add the value. 
00149                 strcat( pcEq, CRLF );
00150                 if (pcDataTemp != NULL)
00151                 {
00152                   /* add old data to the buffer */
00153                   strcat( pcEq, pcDataTemp );
00154 #ifdef FREERTOS_USED
00155                   vPortFree(pcDataTemp);
00156 #else
00157                   free(pcDataTemp);
00158 #endif
00159                 }
00160               }
00161             }
00162           }// for number of args
00163         }//read succeeds
00164       }// size > 0
00165       close(fd); 
00166     }// open succeeds
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       // Get the current time in the "YYYYMMDDHHMMSSMS" string format.
00175       v_cptime_GetDateInFatStringFormat( pcTempoDate );
00176       // Set the file date.
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   }// malloc succeeds
00187 
00188   return( xreturn );
00189 }


Generated on Fri Feb 19 02:22:43 2010 for AVR32 - Control Panel demonstration. by  doxygen 1.5.5