This is the firmware for the LaOS - Laser Open Source project. You can use it to drive a laser cutter. For hardware and more information, look at our wiki: http://wiki.laoslaser.org
Dependencies: EthernetNetIf mbed
ConfigFile.cpp
00001 /* 00002 * ConfigFile.cpp 00003 * Simple Config file reader class 00004 * 00005 * Copyright (c) 2011 Peter Brier 00006 * 00007 * This file is part of the LaOS project (see: http://wiki.protospace.nl/index.php/LaOS) 00008 * 00009 * LaOS is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * LaOS is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with LaOS. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 */ 00023 #include "ConfigFile.h" 00024 00025 // Make new config file object 00026 ConfigFile::ConfigFile(char *file) 00027 { 00028 printf("ConfigFile:ConfigFile (%s)\n\r", file); 00029 extern LaosFileSystem sd; 00030 printf("ConfigFile(%s)\n\r", file); 00031 fp =sd.openfile(file, "rb"); 00032 if (fp==NULL) { 00033 printf("Local configfile\n\r"); 00034 char tmpname[32]; 00035 sprintf(tmpname, "/local/%s", file); 00036 printf("name: %s\n\r", tmpname); 00037 fp = fopen(tmpname,"rb"); 00038 } 00039 } 00040 00041 // Destroy a config file (closes the file handle) 00042 ConfigFile::~ConfigFile() 00043 { 00044 printf("~ConfigFile()\n\r"); 00045 if ( fp != NULL ) 00046 fclose(fp); 00047 } 00048 00049 // Read value 00050 bool ConfigFile::Value(char *key, char *value, size_t maxlen, char *def) 00051 { 00052 int m=0,n=0,c,s=0; 00053 char *v = value; 00054 if (fp == NULL) 00055 { 00056 strncpy(value, def, maxlen); 00057 return false; 00058 } 00059 00060 n = strlen(key); 00061 fseek(fp, 0L, SEEK_SET); 00062 while( s != 99 ) 00063 { 00064 c = fgetc(fp); 00065 if ( c == EOF ) 00066 break; 00067 // printf("%d(%d): '%c'\n\r", s, m, c); 00068 switch( s )// sate machine 00069 { 00070 case 0: // (re) start: note: no break; fall through to case 1 00071 m=0; 00072 s=1; 00073 case 1: // read key, skip spaces 00074 if ( c == key[m] ) 00075 m++; 00076 else 00077 s = 0; 00078 if ( c == ';' ) 00079 s = 10; 00080 else if ( c == ' ' || c == '\t' || c == '\n' || c == '\r') 00081 { 00082 if ( n == m ) // key found 00083 { 00084 s = 2; 00085 m = 0; 00086 } 00087 else 00088 s = 0; 00089 } 00090 break; 00091 case 2: // key matched, skip whitepaces upto the first char 00092 if ( c == ';' ) s = 99; 00093 else if ( c != ' ' && c != '\t' ) 00094 { 00095 s = 3; 00096 m = 1; 00097 if ( m < maxlen) 00098 *value++ = c; 00099 } 00100 break; 00101 00102 case 3: // copy value content, upto eol or comment 00103 if ( m == maxlen || c == '\n' || c == '\r' || c == ';' ) 00104 s = 99; 00105 else 00106 { 00107 m++; 00108 *value++ = c; 00109 } 00110 break; 00111 case 10: // skip comments, upto eol or eof 00112 if ( c == '\n' || c == '\r' ) s = 0; 00113 break; 00114 } 00115 } 00116 if ( s == 99 && m > 0) // key found, and value assigned 00117 { 00118 *value = 0; // terminate string 00119 printf("'%s'='%s'\n", key,v); 00120 return true; 00121 } 00122 else 00123 { 00124 strncpy(value, def, maxlen); 00125 printf("'%s'='%s' (default)\n", key,v); 00126 return false; 00127 } 00128 } 00129 00130 00131 // Read int value 00132 bool ConfigFile::Value(char *key, int *value, int def) 00133 { 00134 char val[32]; 00135 bool b = Value(key, val, 31, ""); 00136 if ( b ) 00137 { 00138 *value = atoi(val); 00139 printf("%s: numeric value=%d\n", key, *value); 00140 } 00141 else 00142 { 00143 *value = def; 00144 printf("%s default (%d)\n", key, *value); 00145 } 00146 return b; 00147 }
Generated on Tue Jul 12 2022 21:03:02 by 1.7.2