Practical library series : Configuration File

Languages

日本語版はこちら: 実用ライブラリシリーズ:設定ファイル

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

Features

  • Available 'Key' and the 'Value' configuration set.
  • Supported read and write.
  • You can write comments in a configuration file. The library will ignore the comments.

Library

Class structure

The class is only

ConfigFile

in this library.

Projects

The library project is ConfigFile. And the test program is ConfigFile_TestProgram.

The library project ConfigFile hasn't mbed library in the project. So you can import the link of the library.

You can run the test program ConfigFile_TestProgram if you want to check the functions.

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.
#
# 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.read("/local/output.cfg")) {
        error("Failure to write a configuration file.\n");
    }
}

Summary

You can use this library class if you want to read configuration variables or store a configuration variables.


5 comments

22 Sep 2010

Good Utility,

Thanks,

 

Warm Regards,

Boseji

http://m8051.blogspot.com

23 Sep 2010

Dear Mr. Boseji-san

Thank you for your comments. :)

Best regards

Shin.

22 Jun 2015
Hi Shin, I use this with an 8GB SanDisk micro SD card, on a Seeed Arch Max. Reading is fast, but writing takes about 4 seconds. Do you know what can be the reason for writing that slow? Best regards, Jack.
20 Sep 2016 . Edited: 20 Sep 2016
Hello, I used this library once, it worked very well... But now I am having problems when I am instantiating the USBSerial.h class and trying to use the ConfigFile in the same program... There is anything in the ConfigFile library that could be disturbing the USBSerial or vice-versa? I don't see the relation between the USB Pins in MBED LPC1768 and the LocalFileSystem... Best Regards,
11 Sep 2019

It looks like aline that is:

valx=-3.0 

 

will work in a config_file,  but the line:

valx = -3.0

 

  will fail.  The space after the key, but before the '=' and the space after the '='

goof up the program. It does not ignore that whitespace. I guess I expected better handling of whitespace.

You need to log in to post a comment