Simple config file class with added support for floats

Fork of ConfigFile by peter brier

Committer:
pbrier
Date:
Thu Jan 27 21:36:01 2011 +0000
Revision:
2:ba316099c799
Parent:
1:a802df951169
Child:
3:31c936f3b9df
1.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pbrier 2:ba316099c799 1 /**
pbrier 0:57f3e167586f 2 * ConfigFile.cpp
pbrier 0:57f3e167586f 3 * Simple Config file reader class
pbrier 0:57f3e167586f 4 *
pbrier 0:57f3e167586f 5 * Copyright (c) 2011 Peter Brier
pbrier 0:57f3e167586f 6 *
pbrier 0:57f3e167586f 7 * This file is part of the LaOS project (see: http://wiki.protospace.nl/index.php/LaOS)
pbrier 0:57f3e167586f 8 *
pbrier 0:57f3e167586f 9 * LaOS is free software: you can redistribute it and/or modify
pbrier 0:57f3e167586f 10 * it under the terms of the GNU General Public License as published by
pbrier 0:57f3e167586f 11 * the Free Software Foundation, either version 3 of the License, or
pbrier 0:57f3e167586f 12 * (at your option) any later version.
pbrier 0:57f3e167586f 13 *
pbrier 0:57f3e167586f 14 * LaOS is distributed in the hope that it will be useful,
pbrier 0:57f3e167586f 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
pbrier 0:57f3e167586f 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
pbrier 0:57f3e167586f 17 * GNU General Public License for more details.
pbrier 0:57f3e167586f 18 *
pbrier 0:57f3e167586f 19 * You should have received a copy of the GNU General Public License
pbrier 0:57f3e167586f 20 * along with LaOS. If not, see <http://www.gnu.org/licenses/>.
pbrier 0:57f3e167586f 21 *
pbrier 0:57f3e167586f 22 * Reads a setting, based on the key. (case sensitive)
pbrier 0:57f3e167586f 23 * If the key is not found, the default value is returned.
pbrier 0:57f3e167586f 24 *
pbrier 0:57f3e167586f 25 * Simple, not (time) efficient. For every request, the complete file is reset and read again
pbrier 0:57f3e167586f 26 * file format:
pbrier 0:57f3e167586f 27 *
pbrier 0:57f3e167586f 28 * ; comment
pbrier 0:57f3e167586f 29 * key value [newline || comment]
pbrier 0:57f3e167586f 30 *
pbrier 0:57f3e167586f 31 *
pbrier 0:57f3e167586f 32 * note: NULL key does nothing
pbrier 0:57f3e167586f 33 */
pbrier 1:a802df951169 34
pbrier 0:57f3e167586f 35 #include "mbed.h"
pbrier 0:57f3e167586f 36
pbrier 0:57f3e167586f 37 #ifndef _CONFIG_FILE_H_
pbrier 0:57f3e167586f 38 #define _CONFIG_FILE_H_
pbrier 0:57f3e167586f 39
pbrier 1:a802df951169 40 /** Simple config file object
pbrier 2:ba316099c799 41 * Only supports reading config files. Tries to limit memory usage.
pbrier 1:a802df951169 42 * Note: the file handle is kept open during the lifetime of this object.
pbrier 1:a802df951169 43 * To close the file: destroy this ConfigFile object! A simple way is to enclose the creation
pbrier 1:a802df951169 44 * of this object inside a code block
pbrier 1:a802df951169 45 * Example:
pbrier 1:a802df951169 46 * @code
pbrier 2:ba316099c799 47 * char ip[16];
pbrier 2:ba316099c799 48 * int port;
pbrier 2:ba316099c799 49 * {
pbrier 2:ba316099c799 50 * ConfigFile cfg("/local/config.txt");
pbrier 2:ba316099c799 51 * cfg.Value("ip", val, sizeof(val), "192.168.1.10");
pbrier 2:ba316099c799 52 * cfg.Value("port", &port, 80);
pbrier 2:ba316099c799 53 * }
pbrier 1:a802df951169 54 * @endcode
pbrier 1:a802df951169 55 */
pbrier 0:57f3e167586f 56 class ConfigFile {
pbrier 1:a802df951169 57 public:
pbrier 2:ba316099c799 58 /** Make new ConfigFile object. Open config file.
pbrier 1:a802df951169 59 * Note: the file handle is kept open during the lifetime of this object.
pbrier 1:a802df951169 60 * To close the file: destroy this ConfigFile object!
pbrier 1:a802df951169 61 * @param file Filename of the configuration file.
pbrier 1:a802df951169 62 */
pbrier 0:57f3e167586f 63 ConfigFile(char *name);
pbrier 1:a802df951169 64
pbrier 0:57f3e167586f 65 ~ConfigFile();
pbrier 1:a802df951169 66
pbrier 1:a802df951169 67 /** Read value. If file is not open, or key does not exist: copy default value (return false)
pbrier 1:a802df951169 68 * @param key name of the key in the file
pbrier 1:a802df951169 69 * @param value pointer to buffer that receives the value
pbrier 1:a802df951169 70 * @param maxlen the maximum length of the value. If the actual value is longer, it is truncated
pbrier 1:a802df951169 71 * @param def Default value. If the key is not found in the file, this value is copied.
pbrier 1:a802df951169 72 * @return "true" if the key is found "false" is key is not found (default value is returned)
pbrier 1:a802df951169 73 */
pbrier 0:57f3e167586f 74 bool Value(char *key, char *value, size_t maxlen, char *def);
pbrier 1:a802df951169 75
pbrier 1:a802df951169 76 /** Read Integer value. If file is not open, or key does not exist: copy default value (return false)
pbrier 1:a802df951169 77 * @param key name of the key in the file
pbrier 1:a802df951169 78 * @param value pointer to integer that receives the value
pbrier 1:a802df951169 79 * @param def Default value. If the key is not found in the file, this value is copied.
pbrier 1:a802df951169 80 * @return "true" if the key is found "false" is key is not found (default value is returned)
pbrier 1:a802df951169 81 */
pbrier 0:57f3e167586f 82 bool Value(char *key, int *value, int def);
pbrier 1:a802df951169 83
pbrier 1:a802df951169 84 private:
pbrier 0:57f3e167586f 85 FILE *fp;
pbrier 1:a802df951169 86
pbrier 0:57f3e167586f 87 };
pbrier 0:57f3e167586f 88
pbrier 0:57f3e167586f 89 #endif