A test program for ConfigFile library.

Dependencies:   mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Test program for configuration file interface class (Version 0.0.1)
00003  *
00004  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00005  * http://shinta.main.jp/
00006  */
00007 #include "mbed.h"
00008 
00009 #include "ConfigFile.h"
00010 
00011 ConfigFile cfg;
00012 LocalFileSystem local("local");
00013 
00014 /**
00015  * View the list.
00016  */
00017 void viewlist(void) {
00018     /*
00019      * If you need to check all parameters.
00020      */
00021     const int cnt = cfg.getCount();
00022     char buf_key[BUFSIZ];
00023     char buf_value[BUFSIZ];
00024     for (int i = 0; i < cnt; i++) {
00025         if (cfg.getKeyAndValue(i, buf_key, sizeof(buf_key), buf_value, sizeof(buf_value))) {
00026             printf("No.%3d:'%s'='%s'\n", i, buf_key, buf_value);
00027         } else {
00028             printf("No.%3d:Failure to get a configuration.\n", i);
00029         }
00030     }
00031 }
00032 
00033 /**
00034  * ==================================================
00035  * Input file (input.cfg)
00036  * ==================================================
00037  * #
00038  * # Configuration file for mbed.
00039  * #
00040  *
00041  * MyKey1=This is a value for key1.
00042  * MyKey2=Value 2
00043  *
00044  * Message1 = This is a test message no.1
00045  *  Message2 = This is a test message no.2
00046  *   Message3 = This is a test message no.3
00047  *
00048  * ==================================================
00049  * Output file (output1.cfg)
00050  * ==================================================
00051  * MyKey1=This is a value for key1.
00052  * MyKey2=Value 2
00053  * Message1 = This is a test message no.1
00054  *  Message2  =  This is a test message no.2
00055  *   Message3   =   This is a test message no.3
00056  *
00057  * ==================================================
00058  * Output file (output2.txt)
00059  * ==================================================
00060  * # This is a configuration file for my application.
00061  * ABC=123
00062  * DEF=456
00063  *
00064  * ==================================================
00065  * Console output
00066  * ==================================================
00067  * 'MyKey1'='This is a value for key1.'
00068  * 'MyKey2'='Value 2'
00069  * 'Message1 '=' This is a test message no.1'
00070  * ' Message2  '='  This is a test message no.2'
00071  * '  Message3   '='   This is a test message no.3'
00072  *
00073  */
00074 int main() {
00075 
00076     char *key1 = "MyKey1";
00077     char *key2 = "MyKey2";
00078     char *key3 = "Message1 ";
00079     char *key4 = " Message2  ";
00080     char *key5 = "  Message3   ";
00081     char value[BUFSIZ];
00082 
00083     /*
00084      * Read a configuration file from a mbed.
00085      */
00086     cfg.read("/local/input.cfg");
00087 
00088     /*
00089      * Read a configuration value.
00090      */
00091     if (cfg.getValue(key1, &value[0], sizeof(value))) {
00092         printf("'%s'='%s'\n", key1, value);
00093     }
00094 
00095     if (cfg.getValue(key2, &value[0], sizeof(value))) {
00096         printf("'%s'='%s'\n", key2, value);
00097     }
00098 
00099     if (cfg.getValue(key3, &value[0], sizeof(value))) {
00100         printf("'%s'='%s'\n", key3, value);
00101     }
00102 
00103     if (cfg.getValue(key4, &value[0], sizeof(value))) {
00104         printf("'%s'='%s'\n", key4, value);
00105     }
00106 
00107     if (cfg.getValue(key5, &value[0], sizeof(value))) {
00108         printf("'%s'='%s'\n", key5, value);
00109     }
00110 
00111     /*
00112      * Write a configuration file to a mbed.
00113      */
00114     cfg.write("/local/output1.cfg");
00115 
00116     /*
00117      * Remove all configurations.
00118      */
00119     cfg.removeAll();
00120 
00121     /*
00122      * Write a configuration value.
00123      */
00124     cfg.setValue("ABC", "123");
00125     cfg.setValue("DEF", "456");
00126 
00127     /*
00128      * Write a configuration file to a mbed.
00129      */
00130     cfg.write("/local/output2.cfg", "# This is a configuration file for my application.");
00131 
00132     while (1) {
00133     }
00134 }