Laser cutter software
Dependencies: EthernetNetIf mbed SDFileSystem
ConfigFile/ConfigFile.cpp@0:18ead85c200b, 2011-08-11 (annotated)
- Committer:
- pbrier
- Date:
- Thu Aug 11 09:33:15 2011 +0000
- Revision:
- 0:18ead85c200b
Laos server
Who changed what in which revision?
User | Revision | Line number | New 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 | */ |
pbrier | 0:18ead85c200b | 23 | #include "ConfigFile.h" |
pbrier | 0:18ead85c200b | 24 | |
pbrier | 0:18ead85c200b | 25 | // Make new config file object |
pbrier | 0:18ead85c200b | 26 | ConfigFile::ConfigFile(char *file) |
pbrier | 0:18ead85c200b | 27 | { |
pbrier | 0:18ead85c200b | 28 | // printf("ConfigFile(%s)\n\r", file); |
pbrier | 0:18ead85c200b | 29 | fp = fopen(file,"rb"); |
pbrier | 0:18ead85c200b | 30 | } |
pbrier | 0:18ead85c200b | 31 | |
pbrier | 0:18ead85c200b | 32 | // Destroy a config file (closes the file handle) |
pbrier | 0:18ead85c200b | 33 | ConfigFile::~ConfigFile() |
pbrier | 0:18ead85c200b | 34 | { |
pbrier | 0:18ead85c200b | 35 | // printf("~ConfigFile()\n\r"); |
pbrier | 0:18ead85c200b | 36 | if ( fp != NULL ) |
pbrier | 0:18ead85c200b | 37 | fclose(fp); |
pbrier | 0:18ead85c200b | 38 | } |
pbrier | 0:18ead85c200b | 39 | |
pbrier | 0:18ead85c200b | 40 | // Read value |
pbrier | 0:18ead85c200b | 41 | bool ConfigFile::Value(char *key, char *value, size_t maxlen, char *def) |
pbrier | 0:18ead85c200b | 42 | { |
pbrier | 0:18ead85c200b | 43 | int m=0,n=0,c,s=0; |
pbrier | 0:18ead85c200b | 44 | if (fp == NULL) |
pbrier | 0:18ead85c200b | 45 | { |
pbrier | 0:18ead85c200b | 46 | strncpy(value, def, maxlen); |
pbrier | 0:18ead85c200b | 47 | return false; |
pbrier | 0:18ead85c200b | 48 | } |
pbrier | 0:18ead85c200b | 49 | // printf("Value()\n\r"); |
pbrier | 0:18ead85c200b | 50 | n = strlen(key); |
pbrier | 0:18ead85c200b | 51 | fseek(fp, 0L, SEEK_SET); |
pbrier | 0:18ead85c200b | 52 | while( s != 99 ) |
pbrier | 0:18ead85c200b | 53 | { |
pbrier | 0:18ead85c200b | 54 | c = fgetc(fp); |
pbrier | 0:18ead85c200b | 55 | if ( c == EOF ) |
pbrier | 0:18ead85c200b | 56 | break; |
pbrier | 0:18ead85c200b | 57 | // printf("%d: '%c'\n\r", s, c); |
pbrier | 0:18ead85c200b | 58 | switch( s )// sate machine |
pbrier | 0:18ead85c200b | 59 | { |
pbrier | 0:18ead85c200b | 60 | case 0: // (re) start: note: no break; fall through to case 1 |
pbrier | 0:18ead85c200b | 61 | m=0; |
pbrier | 0:18ead85c200b | 62 | s=1; |
pbrier | 0:18ead85c200b | 63 | case 1: // read key, skip spaces |
pbrier | 0:18ead85c200b | 64 | if ( c == key[m] ) |
pbrier | 0:18ead85c200b | 65 | m++; |
pbrier | 0:18ead85c200b | 66 | else |
pbrier | 0:18ead85c200b | 67 | s = 0; |
pbrier | 0:18ead85c200b | 68 | if ( c == ';' ) |
pbrier | 0:18ead85c200b | 69 | s = 10; |
pbrier | 0:18ead85c200b | 70 | else if ( c == ' ' || c == '\t' || c == '\n' || c == '\r') |
pbrier | 0:18ead85c200b | 71 | { |
pbrier | 0:18ead85c200b | 72 | if ( n == m ) // key found |
pbrier | 0:18ead85c200b | 73 | { |
pbrier | 0:18ead85c200b | 74 | s = 2; |
pbrier | 0:18ead85c200b | 75 | m = 0; |
pbrier | 0:18ead85c200b | 76 | } |
pbrier | 0:18ead85c200b | 77 | else |
pbrier | 0:18ead85c200b | 78 | s = 0; |
pbrier | 0:18ead85c200b | 79 | } |
pbrier | 0:18ead85c200b | 80 | break; |
pbrier | 0:18ead85c200b | 81 | case 2: // key matched, skip whitepaces upto the first char |
pbrier | 0:18ead85c200b | 82 | if ( c == ';' ) s = 99; |
pbrier | 0:18ead85c200b | 83 | else if ( c != ' ' && c != '\t' ) |
pbrier | 0:18ead85c200b | 84 | { |
pbrier | 0:18ead85c200b | 85 | s = 3; |
pbrier | 0:18ead85c200b | 86 | m = 1; |
pbrier | 0:18ead85c200b | 87 | if ( m < maxlen) |
pbrier | 0:18ead85c200b | 88 | *value++ = c; |
pbrier | 0:18ead85c200b | 89 | } |
pbrier | 0:18ead85c200b | 90 | break; |
pbrier | 0:18ead85c200b | 91 | |
pbrier | 0:18ead85c200b | 92 | case 3: // copy value content, upto eol or comment |
pbrier | 0:18ead85c200b | 93 | if ( m == maxlen || c == '\n' || c == '\r' || c == ';' ) |
pbrier | 0:18ead85c200b | 94 | s = 99; |
pbrier | 0:18ead85c200b | 95 | else |
pbrier | 0:18ead85c200b | 96 | { |
pbrier | 0:18ead85c200b | 97 | m++; |
pbrier | 0:18ead85c200b | 98 | *value++ = c; |
pbrier | 0:18ead85c200b | 99 | } |
pbrier | 0:18ead85c200b | 100 | break; |
pbrier | 0:18ead85c200b | 101 | case 10: // skip comments, upto eol or eof |
pbrier | 0:18ead85c200b | 102 | if ( c == '\n' || c == '\r' ) s = 0; |
pbrier | 0:18ead85c200b | 103 | break; |
pbrier | 0:18ead85c200b | 104 | } |
pbrier | 0:18ead85c200b | 105 | } |
pbrier | 0:18ead85c200b | 106 | if ( s == 99 && m > 1) // key found, and value assigned |
pbrier | 0:18ead85c200b | 107 | { |
pbrier | 0:18ead85c200b | 108 | *value = 0; // terminate string |
pbrier | 0:18ead85c200b | 109 | return true; |
pbrier | 0:18ead85c200b | 110 | } |
pbrier | 0:18ead85c200b | 111 | else |
pbrier | 0:18ead85c200b | 112 | { |
pbrier | 0:18ead85c200b | 113 | strncpy(value, def, maxlen); |
pbrier | 0:18ead85c200b | 114 | return false; |
pbrier | 0:18ead85c200b | 115 | } |
pbrier | 0:18ead85c200b | 116 | } |
pbrier | 0:18ead85c200b | 117 | |
pbrier | 0:18ead85c200b | 118 | // Read int value |
pbrier | 0:18ead85c200b | 119 | bool ConfigFile::Value(char *key, int *value, int def) |
pbrier | 0:18ead85c200b | 120 | { |
pbrier | 0:18ead85c200b | 121 | char val[16]; |
pbrier | 0:18ead85c200b | 122 | bool b = Value(key, val, 12, ""); |
pbrier | 0:18ead85c200b | 123 | if ( b ) |
pbrier | 0:18ead85c200b | 124 | *value = atoi(val); |
pbrier | 0:18ead85c200b | 125 | else |
pbrier | 0:18ead85c200b | 126 | { |
pbrier | 0:18ead85c200b | 127 | *value = def; |
pbrier | 0:18ead85c200b | 128 | } |
pbrier | 0:18ead85c200b | 129 | return b; |
pbrier | 0:18ead85c200b | 130 | } |