OTQtConfigFileEditor.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00079 import();
00080 is_initialized_ = true;
00081 }
00082
00083
00084 void OTQtConfigFileEditor::import() {
00085
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
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
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
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
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
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 }
00239
00240 #endif // USE_OTQT
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256