BlinkenArea - GitList
Repositories
Blog
Wiki
JEtherPix
Code
Commits
Branches
Tags
Search
Tree:
42cf6f6
Branches
Tags
master
JEtherPix
org
blinkenarea
JFlexiPix
Config.java
start of implementation (half done)
Stefan Schuermans
commited
42cf6f6
at 2011-09-11 07:59:34
Config.java
Blame
History
Raw
/* JFlexiPix - Java implementation of FlexiPix output library * * Copyright 2010-2011 Stefan Schuermans <stefan blinkenarea org> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.blinkenarea.JFlexiPix; import java.io.*; import java.util.*; import java.util.regex.*; /// configuration parser class Config { /** * @brief constructor * @param[in] display display to configure * @param[in] messageIf message interface to report messages to or null */ Config(Display display, MessageIf messageIf) { // save constructor parameters m_display = display; m_messageIf = messageIf; // compile regular expression patterns // empty lines: " # comment" m_patternEmpty = Pattern.compile("^" + "\\s*" + "(?:#.*)?" + "$"); // extract setting and value from " setting = value # comment" m_patternLine = Pattern.compile("^" + "\\s*" + "([^\\s=#](?:[^=#]*[^\\s=#])?)" + "\\s*=\\s*" + "([^\\s=#](?:[^=#]*[^\\s=#])?)" + "\\s*" + "(?:#.*)?" + "$"); } /** * @brief process distributor from config file * * @param[in] settingPart2 second half of setting to process * @param[in] value value of setting */ void procDistri(String settingPart2, String value) throws Exception { // TODO } /** * @brief process mapping from config file * * @param[in] settingPart2 second half of setting to process * @param[in] value value of setting */ void procMapping(String settingPart2, String value) throws Exception { // TODO } /** * @brief process pixel from config file * * @param[in] txtPixel text of pixel to process * @param[in] dist number of distributor * @param[in] out number of output * @param[in] pix number of pixel */ void procPixel(String txtPixel, int dist, int out, int pix) throws Exception { // TODO } /** * @brief process output from config file * * @param[in] settingPart2 second half of setting to process * @param[in] value value of setting */ void procOutput(String settingPart2, String value) throws Exception { // TODO } /** * @brief process setting from config file * * @param[in] setting setting to process * @param[in] value value of setting */ void procSetting(String setting, String value) throws Exception { // TODO // DEBUG System.out.println("setting=" + setting + " value=" + value + "\n"); } /** * @brief process line from config file * * @param[in] line line to process */ void procLine(String line) throws Exception { Matcher matcher; String setting, value; // ignore empty lines: " # comment" matcher = m_patternEmpty.matcher(line); if (matcher.find()) return; // extract setting and value from " setting = value # comment" matcher = m_patternLine.matcher(line); if (!matcher.find()) { // line cannot be parsed if (m_messageIf != null) m_messageIf.message(MsgType.Warn, String.format("invalid line %d in config file," + " ignored\n", m_lineNo)); return; } setting = matcher.group(1); value = matcher.group(2); // process setting procSetting(setting, value); } /** * @brief process config file * * @param[in] configFileName name of config file to read */ void procFile(String configFileName) throws Exception { FileReader fr; BufferedReader br; String line; // check file name if (configFileName.length() == 0) { String txt = "no config file specified"; if (m_messageIf != null) m_messageIf.message(MsgType.Err, txt + "\n"); throw new Exception(txt); } if (m_messageIf != null) m_messageIf.message(MsgType.Info, "using config file \"" + configFileName + "\"\n"); // open file try { fr = new FileReader(configFileName); br = new BufferedReader(fr); } catch (FileNotFoundException e) { String txt = "cannot open config file \"" + configFileName + "\" for reading: " + e.getMessage() + "\n"; if (m_messageIf != null) m_messageIf.message(MsgType.Err, txt + "\n"); throw new Exception(txt); } // read lines and process them m_lineNo = 1; while ((line = br.readLine()) != null) { procLine(line); ++m_lineNo; } // close file br.close(); fr.close(); if (m_messageIf != null) m_messageIf.message(MsgType.Info, String.format("%dx%d input format, ", m_display.m_size.m_x, m_display.m_size.m_y) + String.format("%d distributors, ", m_display.m_distriCnt ) + String.format("%d outputs, ", m_display.m_outputCnt ) + String.format("%d pixels, ", m_display.m_pixelCnt) + "\n"); } Display m_display; ///< display to configure MessageIf m_messageIf; ///< message interface to report messages to or null int m_lineNo; ///< current line in config file Pattern m_patternEmpty; ///< empty lines Pattern m_patternLine; ///< setting lines }