Simple config file class with added support for floats

Fork of ConfigFile by peter brier

Committer:
pbrier
Date:
Thu Jan 27 21:23:19 2011 +0000
Revision:
1:a802df951169
Parent:
0:57f3e167586f
Child:
2:ba316099c799
version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pbrier 0:57f3e167586f 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 1:a802df951169 41 * Note: the file handle is kept open during the lifetime of this object.
pbrier 1:a802df951169 42 * To close the file: destroy this ConfigFile object! A simple way is to enclose the creation
pbrier 1:a802df951169 43 * of this object inside a code block
pbrier 1:a802df951169 44 * Example:
pbrier 1:a802df951169 45 * @code
pbrier 1:a802df951169 46 * bla
pbrier 1:a802df951169 47 * @endcode
pbrier 1:a802df951169 48 */
pbrier 0:57f3e167586f 49 class ConfigFile {
pbrier 1:a802df951169 50 public:
pbrier 1:a802df951169 51 /// Make new config file object
pbrier 1:a802df951169 52 /** Open config file.
pbrier 1:a802df951169 53 * Note: the file handle is kept open during the lifetime of this object.
pbrier 1:a802df951169 54 * To close the file: destroy this ConfigFile object!
pbrier 1:a802df951169 55 * @param file Filename of the configuration file.
pbrier 1:a802df951169 56 */
pbrier 0:57f3e167586f 57 ConfigFile(char *name);
pbrier 1:a802df951169 58
pbrier 0:57f3e167586f 59 ~ConfigFile();
pbrier 1:a802df951169 60
pbrier 1:a802df951169 61 /** Read value. If file is not open, or key does not exist: copy default value (return false)
pbrier 1:a802df951169 62 * @param key name of the key in the file
pbrier 1:a802df951169 63 * @param value pointer to buffer that receives the value
pbrier 1:a802df951169 64 * @param maxlen the maximum length of the value. If the actual value is longer, it is truncated
pbrier 1:a802df951169 65 * @param def Default value. If the key is not found in the file, this value is copied.
pbrier 1:a802df951169 66 * @return "true" if the key is found "false" is key is not found (default value is returned)
pbrier 1:a802df951169 67 */
pbrier 0:57f3e167586f 68 bool Value(char *key, char *value, size_t maxlen, char *def);
pbrier 1:a802df951169 69
pbrier 1:a802df951169 70 /** Read Integer value. If file is not open, or key does not exist: copy default value (return false)
pbrier 1:a802df951169 71 * @param key name of the key in the file
pbrier 1:a802df951169 72 * @param value pointer to integer that receives the value
pbrier 1:a802df951169 73 * @param def Default value. If the key is not found in the file, this value is copied.
pbrier 1:a802df951169 74 * @return "true" if the key is found "false" is key is not found (default value is returned)
pbrier 1:a802df951169 75 */
pbrier 0:57f3e167586f 76 bool Value(char *key, int *value, int def);
pbrier 1:a802df951169 77
pbrier 1:a802df951169 78 private:
pbrier 0:57f3e167586f 79 FILE *fp;
pbrier 1:a802df951169 80
pbrier 0:57f3e167586f 81 };
pbrier 0:57f3e167586f 82
pbrier 0:57f3e167586f 83 #endif