OpenTracker

An Open Architecture for Reconfigurable Tracking based on XML | Contact

OTQtConfigFileEditor.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00045 #include <dllinclude.h>
00046 #if USE_OTQT
00047 
00048 #include "OTQtConfigFileEditor.h"
00049 #include "QtMouseEventCalibModule.h"
00050 #include <stdexcept>
00051 
00052 namespace ot {
00053 
00054 //--------------------------------------------------------------------------------
00055 OTQtConfigFileEditor::OTQtConfigFileEditor(std::string & input_filename)
00056   : is_initialized_(false),
00057     input_filename_(input_filename)
00058 {
00059 }
00060 
00061 //--------------------------------------------------------------------------------
00062 OTQtConfigFileEditor::~OTQtConfigFileEditor()
00063 {
00064   lines_.clear();
00065 }
00066 
00067 
00068 //--------------------------------------------------------------------------------
00069 OTQtConfigFileEditor::OTQtConfigFileEditor(OTQtConfigFileEditor const & ref)
00070   : is_initialized_(ref.is_initialized_),
00071     input_filename_(ref.input_filename_),
00072     lines_(ref.lines_)
00073 {
00074 }
00075 
00076 //--------------------------------------------------------------------------------
00077 void OTQtConfigFileEditor::init() {
00078   // import input config file
00079   import();
00080   is_initialized_ = true;
00081 }
00082 
00083 //--------------------------------------------------------------------------------
00084 void OTQtConfigFileEditor::import() {
00085   // open input file for reading
00086   FILE * fp = fopen(input_filename_.c_str(), "r");
00087   if (fp == NULL) {
00088     std::string err_msg = "Could not read/import config inputfile '";
00089     err_msg += input_filename_;
00090     err_msg += "'.";
00091     throw std::invalid_argument(err_msg);
00092   }
00093   // read file line per line
00094   char c;
00095   std::string one_line;
00096   while ((c = fgetc(fp)) != EOF) {
00097     if (c == '\n' || c == '\r') {
00098       lines_.push_back(one_line);
00099       one_line.clear();
00100       continue;
00101     }
00102     one_line += c;
00103   }
00104   lines_.push_back(one_line);
00105   fclose(fp);
00106 }
00107 
00108 //--------------------------------------------------------------------------------
00109 int OTQtConfigFileEditor::countOfExpr(std::string expr) const {
00110   int count = 0;
00111   for (ConstLineIterator iter = lines_.begin(); iter != lines_.end(); iter++) {
00112     if (iter->find(expr) != std::string::npos)
00113       count++;
00114   }
00115   return count;
00116 }
00117 
00118 //--------------------------------------------------------------------------------
00119 int OTQtConfigFileEditor::firstLineNoOfExpr(std::string expr, int start_index) const {
00120   int line_index = -1;
00121   for (ConstLineIterator iter = lines_.begin(); iter != lines_.end(); iter++) {
00122     line_index++;
00123     if (line_index < start_index)
00124       continue;
00125     if (iter->find(expr) != std::string::npos)
00126       return line_index;
00127   }
00128   return -1;
00129 }
00130 
00131 //--------------------------------------------------------------------------------
00132 bool OTQtConfigFileEditor::containsExpr(std::string expr) const {
00133   return (firstLineNoOfExpr(expr) >= 0);
00134 }
00135 
00136 //--------------------------------------------------------------------------------
00137 std::string const & OTQtConfigFileEditor::getLine(int index) const {
00138   if (index < 0 || index >= getLineCount()) {
00139     throw std::invalid_argument("OTQtConfigFileEditor::getLine():: Invalid line index.");
00140   }
00141   ConstLineIterator it = lines_.begin();
00142   int count = 0;
00143   while (it != lines_.end()) {
00144     if (count == index) {
00145       break;
00146     }
00147     count++; it++;
00148   }
00149   return (*it);
00150 }
00151 
00152 //--------------------------------------------------------------------------------
00153 bool OTQtConfigFileEditor::killLine(int index)
00154 {
00155   LineIterator it = lines_.begin();
00156   int count = 0;
00157   while (it != lines_.end()) {
00158     if (count == index) {
00159       lines_.erase(it);
00160       return true;
00161     }
00162     count++; it++;
00163   }
00164   return false;
00165 }
00166 
00167 //--------------------------------------------------------------------------------
00168 bool OTQtConfigFileEditor::insertLines(int index_at, LineList & ins_lines, int num_indent_spaces)
00169 {
00170   if (num_indent_spaces > 0) {
00171     char const SPACE = ' ';
00172 #ifdef WIN32
00173     char * indent = new char[num_indent_spaces + 1];
00174 #else
00175     char indent[num_indent_spaces + 1];
00176 #endif
00177     for (int i = 0; i < num_indent_spaces; i++) {
00178       indent[i] = SPACE;
00179     }
00180     indent[num_indent_spaces] = '\0';
00181     // add indent spaces
00182     LineIterator it = ins_lines.begin();
00183     while (it != ins_lines.end()) {
00184       (*it) = std::string(indent) + (*it);
00185       it++;
00186     }
00187 #ifdef WIN32
00188     delete indent;
00189 #endif
00190   }
00191   // insert
00192   LineIterator it = lines_.begin();
00193   int count = 0;
00194   while (it != lines_.end()) {
00195     if (index_at == count) {
00196       lines_.insert(it, ins_lines.begin(), ins_lines.end());
00197       return true;
00198     }
00199     count++; it++;
00200   }
00201   return false;
00202 }
00203 
00204 //--------------------------------------------------------------------------------
00205 void OTQtConfigFileEditor::writeToStdOut() const
00206 {
00207   fprintf(stdout, "----- OT configuration START -----\n");
00208   ConstLineIterator it = lines_.begin();
00209   while (it != lines_.end()) {
00210     fprintf(stdout, "%s\n", (*it).c_str());
00211     it++;
00212   }
00213   fprintf(stdout, "----- OT configuration EOF -----\n");
00214   fflush(stdout);
00215 }
00216 
00217 //--------------------------------------------------------------------------------
00218 void OTQtConfigFileEditor::writeToFile(std::string const & output_filename) const
00219 {
00220   // open file for writing
00221   FILE * fp = fopen(output_filename.c_str(), "w");
00222   if (fp == NULL) {
00223     std::string err_msg = "Could not write config outputfile '";
00224     err_msg += output_filename;
00225     err_msg += "'.";
00226     throw std::invalid_argument(err_msg);
00227   }
00228   // write output file line per line
00229   ConstLineIterator it = lines_.begin();
00230   while (it != lines_.end()) {
00231     fprintf(fp, "%s\n", (*it).c_str());
00232     it++;
00233   }
00234   fflush(fp);
00235   fclose(fp);
00236 }
00237 
00238 } // namespace ot
00239 
00240 #endif // USE_OTQT
00241 
00242 /* 
00243  * ------------------------------------------------------------
00244  *   End of OTQtConfigFileEditor.cxx
00245  * ------------------------------------------------------------
00246  *   Automatic Emacs configuration follows.
00247  *   Local Variables:
00248  *   mode:c++
00249  *   c-basic-offset: 4
00250  *   eval: (c-set-offset 'substatement-open 0)
00251  *   eval: (c-set-offset 'case-label '+)
00252  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00253  *   eval: (setq indent-tabs-mode nil)
00254  *   End:
00255  * ------------------------------------------------------------ 
00256  */

copyright (c) 2006 Graz University of Technology