ConfigFile

Overview

Some applications need variable configurations.

For example, The configuration of account information for Twitter...

It's not good for publish if I implemented a program with a account information for Twitter, and it's not secure.

So, in this document we will show you a configuration file helper class for local file system on mbed.

You can use a variable configuration with it.

Basic concept

  • A configuration set consist from a key and a value.

http://mbed.org/media/uploads/shintamainjp/_scaled_keyvalue.png

How to use it?

Configuration file

We need a configuration file.

  • A comment line started by #.
  • Empty lines have not meaning.
  • Key=Value based configuration.
  • A space character in a side of a key and a value have a meaning.

Configurtion file example

#
# Configuration file for mbed.
#

MyKey1=This is a value for key1.
MyKey2=Value 2

Message1 = This is a test message no.1
 Message2  =  This is a test message no.2
  Message3   =   This is a test message no.3

Reading

This example codes read a file named input.cfg on mbed local file system.

#include "mbed.h"
#include "ConfigFile.h"
 
LocalFileSystem local("local");
ConfigFile cfg;

int main(void) {
    char *key = "MyKey";
    char value[BUFSIZ];
    /*
     * Read a configuration file from a mbed.
     */
    if (!cfg.read("/local/input.cfg")) {
        error("Failure to read a configuration file.\n");
    }

    /*
     * Get a configuration value.
     */
    if (cfg.getValue(key, &value[0], sizeof(value))) {
        printf("'%s'='%s'\n", key, value);
    }
}
  • Ignore comments in a configuration file.
  • Ignore empty lines in a configuration file.
  • Keep spaces in side of keys and values.

Writing

This example codes write a configuration to a file.

#include "mbed.h"
#include "ConfigFile.h"
 
LocalFileSystem local("local");
ConfigFile cfg;

int main(void) {

    /*
     * Set a configuration value.
     */
    if (!cfg.setValue("MyKey", "TestValue")) {
        error("Failure to set a value.\n");
    }    

    /*
     * Write to a file.
     */
    if (!cfg.write("/local/output.cfg")) {
        error("Failure to write a configuration file.\n");
    }
}

Library API

» Import this library into a program

Public Member Functions

  ConfigFile ()
  Create a configuration file class.
  ~ConfigFile ()
  Destroy a configuration file class.
bool  getValue (char *key, char *value, size_t siz)
  Get a value for a key.
bool  setValue (char *key, char *value)
  Set a set of a key and value.
bool  remove (char *key)
  Remove a config.
bool  removeAll (void)
  Remove all config.
int  getCount ()
  Get a number of configuration sets.
bool  getKeyAndValue (int index, char *key, size_t keybufsiz, char *value, size_t valuebufsiz)
  Get a key and a value.
bool  read (char *file)
  Read from the target file.
bool  write (char *file, char *header=NULL, FileFormat ff=UNIX)
  Write from the target file.

Example application

» Import this programConfigFile_TestProgram

A test program for ConfigFile library.

References





3 comments:

11 Jun 2012

Hey this is terribly handy! Thank you.

12 Sep 2012

Hi, nice tool! Is it possible to make it a bit more *.ini like? So you can group those keys. And an integrated parser for int, float, HEX, ... would be very fine.

Let me know, if you want to integrate some of this features.

18 Dec 2012

Hi! This is a handy tool but I have a problem with it: I can't change the config file with an text editor once it is on the mbed. I guives strange errors like file in use. I deliberately put the config file object into it's own scope so the desructor would be called after I've read all my keys:

{
        ConfigFile cfg;
        if (!cfg.read("/local/serv.cfg")) {
            error("Failure to read a configuration file.\n");
        }

        if (cfg.getValue ....
}

Any idea why? Thanks a lot for the help! Edit: To answer myself: Seems like there was a strange problem with a broken file in the mbed filesystem. It works well after changing the name of the configfile. And I've checked the source: the file gets closed at the end of the read call, so my {..} block was useless ...