Laser cutter software

Dependencies:   EthernetNetIf mbed SDFileSystem

Committer:
pbrier
Date:
Thu Aug 11 09:33:15 2011 +0000
Revision:
0:18ead85c200b
Laos server

Who changed what in which revision?

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