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