Simple config file class with added support for floats
Fork of ConfigFile by
ConfigFile.h@3:31c936f3b9df, 2011-01-27 (annotated)
- Committer:
- pbrier
- Date:
- Thu Jan 27 21:41:03 2011 +0000
- Revision:
- 3:31c936f3b9df
- Parent:
- 2:ba316099c799
- Child:
- 4:466a2aeaae75
1.11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pbrier | 3:31c936f3b9df | 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 | 3:31c936f3b9df | 21 | */ |
pbrier | 3:31c936f3b9df | 22 | /** |
pbrier | 0:57f3e167586f | 23 | * Reads a setting, based on the key. (case sensitive) |
pbrier | 0:57f3e167586f | 24 | * If the key is not found, the default value is returned. |
pbrier | 0:57f3e167586f | 25 | * |
pbrier | 0:57f3e167586f | 26 | * Simple, not (time) efficient. For every request, the complete file is reset and read again |
pbrier | 3:31c936f3b9df | 27 | * |
pbrier | 0:57f3e167586f | 28 | * file format: |
pbrier | 0:57f3e167586f | 29 | * |
pbrier | 0:57f3e167586f | 30 | * ; comment |
pbrier | 0:57f3e167586f | 31 | * key value [newline || comment] |
pbrier | 0:57f3e167586f | 32 | * |
pbrier | 3:31c936f3b9df | 33 | * @code |
pbrier | 3:31c936f3b9df | 34 | *; This is a test config file |
pbrier | 3:31c936f3b9df | 35 | * ip 192.168.1.1 |
pbrier | 3:31c936f3b9df | 36 | * port 1234 ; the key is "port" the value is "1234". The rest is comment |
pbrier | 3:31c936f3b9df | 37 | *; EOF |
pbrier | 3:31c936f3b9df | 38 | * @endcode |
pbrier | 0:57f3e167586f | 39 | */ |
pbrier | 1:a802df951169 | 40 | |
pbrier | 0:57f3e167586f | 41 | #include "mbed.h" |
pbrier | 0:57f3e167586f | 42 | |
pbrier | 0:57f3e167586f | 43 | #ifndef _CONFIG_FILE_H_ |
pbrier | 0:57f3e167586f | 44 | #define _CONFIG_FILE_H_ |
pbrier | 0:57f3e167586f | 45 | |
pbrier | 1:a802df951169 | 46 | /** Simple config file object |
pbrier | 2:ba316099c799 | 47 | * Only supports reading config files. Tries to limit memory usage. |
pbrier | 1:a802df951169 | 48 | * Note: the file handle is kept open during the lifetime of this object. |
pbrier | 1:a802df951169 | 49 | * To close the file: destroy this ConfigFile object! A simple way is to enclose the creation |
pbrier | 1:a802df951169 | 50 | * of this object inside a code block |
pbrier | 1:a802df951169 | 51 | * Example: |
pbrier | 1:a802df951169 | 52 | * @code |
pbrier | 2:ba316099c799 | 53 | * char ip[16]; |
pbrier | 2:ba316099c799 | 54 | * int port; |
pbrier | 2:ba316099c799 | 55 | * { |
pbrier | 2:ba316099c799 | 56 | * ConfigFile cfg("/local/config.txt"); |
pbrier | 2:ba316099c799 | 57 | * cfg.Value("ip", val, sizeof(val), "192.168.1.10"); |
pbrier | 2:ba316099c799 | 58 | * cfg.Value("port", &port, 80); |
pbrier | 2:ba316099c799 | 59 | * } |
pbrier | 1:a802df951169 | 60 | * @endcode |
pbrier | 1:a802df951169 | 61 | */ |
pbrier | 0:57f3e167586f | 62 | class ConfigFile { |
pbrier | 1:a802df951169 | 63 | public: |
pbrier | 2:ba316099c799 | 64 | /** Make new ConfigFile object. Open config file. |
pbrier | 1:a802df951169 | 65 | * Note: the file handle is kept open during the lifetime of this object. |
pbrier | 1:a802df951169 | 66 | * To close the file: destroy this ConfigFile object! |
pbrier | 1:a802df951169 | 67 | * @param file Filename of the configuration file. |
pbrier | 1:a802df951169 | 68 | */ |
pbrier | 0:57f3e167586f | 69 | ConfigFile(char *name); |
pbrier | 1:a802df951169 | 70 | |
pbrier | 0:57f3e167586f | 71 | ~ConfigFile(); |
pbrier | 1:a802df951169 | 72 | |
pbrier | 1:a802df951169 | 73 | /** Read value. If file is not open, or key does not exist: copy default value (return false) |
pbrier | 1:a802df951169 | 74 | * @param key name of the key in the file |
pbrier | 1:a802df951169 | 75 | * @param value pointer to buffer that receives the value |
pbrier | 1:a802df951169 | 76 | * @param maxlen the maximum length of the value. If the actual value is longer, it is truncated |
pbrier | 1:a802df951169 | 77 | * @param def Default value. If the key is not found in the file, this value is copied. |
pbrier | 1:a802df951169 | 78 | * @return "true" if the key is found "false" is key is not found (default value is returned) |
pbrier | 1:a802df951169 | 79 | */ |
pbrier | 0:57f3e167586f | 80 | bool Value(char *key, char *value, size_t maxlen, char *def); |
pbrier | 1:a802df951169 | 81 | |
pbrier | 1:a802df951169 | 82 | /** Read Integer value. If file is not open, or key does not exist: copy default value (return false) |
pbrier | 1:a802df951169 | 83 | * @param key name of the key in the file |
pbrier | 1:a802df951169 | 84 | * @param value pointer to integer that receives the value |
pbrier | 1:a802df951169 | 85 | * @param def Default value. If the key is not found in the file, this value is copied. |
pbrier | 1:a802df951169 | 86 | * @return "true" if the key is found "false" is key is not found (default value is returned) |
pbrier | 1:a802df951169 | 87 | */ |
pbrier | 0:57f3e167586f | 88 | bool Value(char *key, int *value, int def); |
pbrier | 1:a802df951169 | 89 | |
pbrier | 1:a802df951169 | 90 | private: |
pbrier | 0:57f3e167586f | 91 | FILE *fp; |
pbrier | 1:a802df951169 | 92 | |
pbrier | 0:57f3e167586f | 93 | }; |
pbrier | 0:57f3e167586f | 94 | |
pbrier | 0:57f3e167586f | 95 | #endif |