diff --git a/cd_utils/cdutils_core.h b/cd_utils/cdutils_core.h index 3b9d274..ab24df7 100644 --- a/cd_utils/cdutils_core.h +++ b/cd_utils/cdutils_core.h @@ -55,7 +55,7 @@ private: static const char app_config[], pult_config[]; PIConnection connection; - PIMap > k_sections; // [enum KSection][index] = CDUtils::KSection + }; diff --git a/cd_utils/cdutils_k.cpp b/cd_utils/cdutils_k.cpp index c8abb3b..867a6d3 100644 --- a/cd_utils/cdutils_k.cpp +++ b/cd_utils/cdutils_k.cpp @@ -1,11 +1,15 @@ #include "cdutils_k.h" #include "cdutils_core.h" +#include "piconfig.h" +#include "pifile.h" using namespace CDUtils; KInterface::KInterface() { core = Core::instance(); + k_file = PIStringAscii("k.dat"); + k_file_size = 0; CONNECTU(core, K_Sended, this, sended); CONNECTU(core, K_Received, this, received); } @@ -41,6 +45,11 @@ const KSection & KInterface::root() const { } +int KInterface::count(bool recursive) const { + return core->k_.count(recursive); +} + + void KInterface::send() { core->K_Send(); } @@ -50,6 +59,15 @@ void KInterface::request() { } +bool KInterface::configure(const PIString & config, const PIString & sect) { + PIConfig conf(config, PIIODevice::ReadOnly); + PIConfig::Entry & e(conf.getValue(sect)); + bool ret = false; + k_file = e.getValue("file", "k.dat", &ret); + return ret; +} + + void KInterface::write(PIIODevice * d) { core->k_write(d); } @@ -68,3 +86,20 @@ void KInterface::parse(PIIODevice * d) { void KInterface::update(PIIODevice * d) { core->k_update(d); } + + +void KInterface::readFile() { + if (k_file.isEmpty()) return; + PIFile f(k_file, PIIODevice::ReadOnly); + read(&f); + k_file_size = f.size(); +} + + +void KInterface::writeFile() { + if (k_file.isEmpty()) return; + PIFile f(k_file, PIIODevice::ReadWrite); + f.clear(); + write(&f); + k_file_size = f.size(); +} diff --git a/cd_utils/cdutils_k.h b/cd_utils/cdutils_k.h index 0352edb..34bd175 100644 --- a/cd_utils/cdutils_k.h +++ b/cd_utils/cdutils_k.h @@ -23,16 +23,27 @@ public: const KSection section(int v) const; const KSection & root() const; + int count(bool recursive = true) const; + const PIString file() const {return k_file;} + int fileSize() const {return k_file_size;} + EVENT(sended) EVENT(received) EVENT_HANDLER(void, send); EVENT_HANDLER(void, request); + bool configure(const PIString & config, const PIString & sect = PIStringAscii("k")); void write(PIIODevice * d); void read(PIIODevice * d); void parse(PIIODevice * d); void update(PIIODevice * d); + + void readFile(); + void writeFile(); + private: Core * core; + PIString k_file; + int k_file_size; }; diff --git a/cd_utils/cdutils_k_parser.cpp b/cd_utils/cdutils_k_parser.cpp index 609c003..da01956 100644 --- a/cd_utils/cdutils_k_parser.cpp +++ b/cd_utils/cdutils_k_parser.cpp @@ -18,17 +18,21 @@ enum Phase { }; -void removeComment(PIString & line, PIString * comment = 0) { +void removeComment(PIString & line, PIString * type, PIString * comment) { int ci = line.find("//"); if (ci >= 0) { if (comment) *comment = line.right(line.size_s() - ci - 2).trim(); line.cutRight(line.size_s() - ci).trim(); + if (type && comment && !line.isEmpty()) { + *type = comment->takeLeft(1); + comment->trim(); + } } } -void parseEnumLine(PIString & line, int * value, PIString * comment = 0) { - removeComment(line, comment); +void parseEnumLine(PIString & line, int * value, PIString * type, PIString * comment) { + removeComment(line, type, comment); int ci = line.find("="); if (ci >= 0) { if (value) *value = line.right(line.size_s() - ci - 1).trim().toInt(); @@ -54,15 +58,19 @@ void parseInsert(PIString line, PIString & alias, PIStringList & out) { } -PIVector enumValues(const PIString & e, const PIMap & sections) { +PIVector enumValues(const PIString & e, const PIMap & sections, PIStringList & enames) { PIVector ret; + enames.clear(); if (sections.contains(e)) { ret = sections[e].indexes(); + enames = sections[e].index_names(); } else { int v = e.toInt(); if (v < 2) return ret; - for (int i = 0; i < v; ++i) + for (int i = 0; i < v; ++i) { ret << i; + enames << PIString::fromNumber(i); + } } return ret; } @@ -75,9 +83,13 @@ KSection KParser::parse(PIIODevice * d) { KType ck; PIMap sections; PIMap enum_values, cevalues; - PIString content, line, alias, comment; + PIString content, line, alias, type, comment; PIStringList iarr; - if (PIStringAscii(d->className()) == PIStringAscii("PIFile")) content = ((PIFile*)d)->readAll(); + if (PIStringAscii(d->className()) == PIStringAscii("PIFile")) { + PIByteArray c = ((PIFile*)d)->readAll(); + c << uchar(0); + content = PIString::fromUTF8((const char *)c.data()); + } if (PIStringAscii(d->className()) == PIStringAscii("PIIOString")) content = *(((PIIOString*)d)->string()); PIIOString ios(&content); //int phase = 0; @@ -85,14 +97,15 @@ KSection KParser::parse(PIIODevice * d) { while ((cind = content.find("enum", cind)) >= 0) { ios.seek(cind); line = ios.readLine().trim(); + type.clear(); comment.clear(); - removeComment(line, &comment); + removeComment(line, &type, &comment); if (line.find("{") < 0) { cind += 4; continue; } line.cutLeft(line.find("enum") + 4).trim(); - line.cutRight(line.size_s() - line.find("{")).trim(); + line.cutRight(line.size_s() - line.find("{")).trim(); if (line.isEmpty()) { cind += 4; continue; @@ -105,7 +118,7 @@ KSection KParser::parse(PIIODevice * d) { while (!ios.isEnd()) { line = ios.readLine().trim(); comment.clear(); - removeComment(line, &comment); + removeComment(line, &type, &comment); if (line.find("}") >= 0) break; if (line.isEmpty()) { if (comment.find("=") >= 0) { @@ -124,12 +137,15 @@ KSection KParser::parse(PIIODevice * d) { KSection is = sections.value(iarr.take_front()), ts; int ibpos = is.name.size_s(); piForeachRC (PIString & a, iarr) { - PIVector evals = enumValues(a, sections); + PIStringList enames; + PIVector evals = enumValues(a, sections, enames); //piCout << a << evals; - piForeachC (int e, evals) - ts.section(e) = is; + for (int i = 0; i < evals.size_s(); ++i) { + ts.section(evals[i]) = is; + ts.section(evals[i]).index_name = enames[i]; + } ts.name = is.name; - ts.name.insert(ibpos, PIString("[") << evals.size_s() << "]"); + ts.name.insert(ibpos, PIString("[") << a << "]"); is = ts; ts = KSection(); } @@ -137,9 +153,9 @@ KSection KParser::parse(PIIODevice * d) { } } } else { - parseEnumLine(line, &cev, &comment); - //piCout << line << "=" << cev << "//" << comment; - ck = KType(cev, line, "", "", comment); + parseEnumLine(line, &cev, &type, &comment); + piCout << line << "=" << cev << "//" << type << comment; + ck = KType(cev, line, type, "", "", comment); cs[cev] = ck; cevalues[line] = cev; ++cev; diff --git a/cd_utils/cdutils_k_types.cpp b/cd_utils/cdutils_k_types.cpp index 9d0293d..671f182 100644 --- a/cd_utils/cdutils_k_types.cpp +++ b/cd_utils/cdutils_k_types.cpp @@ -12,9 +12,10 @@ KType::KType() { } -KType::KType(int i, const PIString & n, const PIString & v, const PIString & f, const PIString & c) { +KType::KType(int i, const PIString & n, const PIString & t, const PIString & v, const PIString & f, const PIString & c) { index_ = i; name_ = n; + type_ = t; value_s = v; formula_ = f; comment_ = c; @@ -24,6 +25,26 @@ KType::KType(int i, const PIString & n, const PIString & v, const PIString & f, } +int KSection::count(bool recursive) const { + int ret = k.size_s(); + if (recursive) { + PIMap::const_iterator i; + for (i = s.begin(); i != s.end(); ++i) + ret += i->second.count(recursive); + } + return ret; +} + + +PIStringList KSection::index_names() const { + PIStringList ret; + PIMap::iterator i; + for (i = k.begin(); i != k.end(); ++i) + ret << i.value().name(); + return ret; +} + + void KSection::write(PIIODevice * d, const PIString & prefix) { if (!d) return; if (k.isEmpty() && s.isEmpty()) return; @@ -32,14 +53,14 @@ void KSection::write(PIIODevice * d, const PIString & prefix) { if (prefix.isEmpty()) l = "[k]"; else l = "[" + prefix + ".k]"; l += "\n"; - d->write(l.toByteArray()); + d->write(l.toUTF8()); PIMap::iterator i; for (i = k.begin(); i != k.end(); ++i) { KType & ck(i.value()); l.clear(); l << ck.index() << ".f = " << ck.formula() << " #s " << ck.comment() << "\n"; - l << ck.index() << ".v = " << ck.value() << " #s " << ck.name() << "\n"; - d->write(l.toByteArray()); + l << ck.index() << ".v = " << ck.value() << " #s " << ck.type() << " " << ck.name() << "\n"; + d->write(l.toUTF8()); } } if (!s.isEmpty()) { @@ -52,7 +73,7 @@ void KSection::write(PIIODevice * d, const PIString & prefix) { } if (prefix.isEmpty()) { l = "[]\n"; - d->write(l.toByteArray()); + d->write(l.toUTF8()); } } @@ -65,7 +86,9 @@ void KSection::read(const void * ep) { for (int i = 0; i < kl.childCount(); ++i) { const PIConfig::Entry * ke(kl.child(i)); int kid = ke->name().toInt(); - k[kid] = KType(kid, ke->getValue("v").comment(), ke->getValue("v").value(), ke->getValue("f").value(), ke->getValue("f").comment()); + PIString n = ke->getValue("v").comment(); + PIString t = n.takeLeft(1); + k[kid] = KType(kid, n.trim(), t, ke->getValue("v").value(), ke->getValue("f").value(), ke->getValue("f").comment()); } PIConfig::Entry & sl = e.getValue("s"); for (int i = 0; i < sl.childCount(); ++i) { diff --git a/cd_utils/cdutils_k_types.h b/cd_utils/cdutils_k_types.h index 4e10d72..ec336c1 100644 --- a/cd_utils/cdutils_k_types.h +++ b/cd_utils/cdutils_k_types.h @@ -16,9 +16,10 @@ class KType { friend class ::CD_Pult; public: KType(); - KType(int i, const PIString & n, const PIString & v, const PIString & f, const PIString & c); + KType(int i, const PIString & n, const PIString & t, const PIString & v, const PIString & f, const PIString & c); int index() const {return index_;} PIString name() const {return name_;} + PIString type() const {return type_;} PIString value() const {return value_s;} PIString formula() const {return formula_;} PIString comment() const {return comment_;} @@ -31,7 +32,7 @@ public: private: int index_; - PIString name_; + PIString name_, type_; PIString value_s, formula_, comment_; double value_d; int value_i; @@ -54,9 +55,12 @@ public: const KSection section(int v) const {return s[v];} bool isEmpty() const {return k.isEmpty() && s.isEmpty();} + int count(bool recursive = true) const; PIVector indexes() const {return k.keys();} + PIStringList index_names() const; PIString name; + PIString index_name; private: KSection(PIMap k_, PIMap s_) { @@ -68,7 +72,7 @@ private: void update(KSection & v, bool keep_names); bool isSameStructure(KSection & v); - PIMap k; + mutable PIMap k; PIMap s; }; diff --git a/cd_utils/pult/cd_pult.cpp b/cd_utils/pult/cd_pult.cpp index 3f5c228..344f099 100644 --- a/cd_utils/pult/cd_pult.cpp +++ b/cd_utils/pult/cd_pult.cpp @@ -58,6 +58,8 @@ config(piqt(config_), QIODevice::ReadWrite) { connect(ui->checkKHideExpressions, SIGNAL(toggled(bool)), this, SLOT(filterTree())); connect(ui->lineKSearch, SIGNAL(textChanged(QString)), this, SLOT(filterTree())); session.load(); + K.configure(config_); + K.readFile(); updateKDesc(); updateCDesc(); timer_diag.start(40); @@ -70,14 +72,14 @@ CD_Pult::~CD_Pult() { void CD_Pult::loading(QPIConfig & conf) { - kdesc_file = conf.getValue("kdesc_file").stringValue(); - cdesc_file = conf.getValue("cdesc_file").stringValue(); + kdesc_file = Q2PIString(conf.getValue("kdesc_file").stringValue()); + cdesc_file = Q2PIString(conf.getValue("cdesc_file").stringValue()); } void CD_Pult::saving(QPIConfig & conf) { - conf.setValue("kdesc_file", kdesc_file); - conf.setValue("cdesc_file", cdesc_file); + conf.setValue("kdesc_file", PI2QString(kdesc_file)); + conf.setValue("cdesc_file", PI2QString(cdesc_file)); } @@ -206,15 +208,19 @@ void CD_Pult::makeTreeSection(KSection & ks, QTreeWidgetItem * pi) { const KType & ck(ki.value()); ti->setText(0, QString::number(ck.index())); ti->setText(1, PI2QString(ck.name())); - ti->setText(2, PI2QString(ck.formula())); - ti->setText(4, PI2QString(ck.comment())); + ti->setText(2, typeName(PI2QString(ck.type()))); + ti->setText(3, PI2QString(ck.formula())); + ti->setText(5, PI2QString(ck.comment())); } PIMap::iterator si; for (si = ks.s.begin(); si != ks.s.end(); ++si) { QTreeWidgetItem * ti = new QTreeWidgetItem(pi); const KSection & cs(si.value()); - ti->setText(0, QString("[%1]").arg(si.key())); - ti->setText(1, PI2QString(cs.name)); + QString sn("[%1]"); + if (cs.index_name.isEmpty()) sn = sn.arg(si.key()); + else sn = sn.arg(PI2QString(cs.index_name)); + ti->setText(0, sn); + ti->setText(2, PI2QString(cs.name)); makeTreeSection(const_cast(cs), ti); } } @@ -273,32 +279,32 @@ void CD_Pult::on_buttonHideAll_clicked() { void CD_Pult::on_buttonRead_clicked() { - //coeffs.readCoeffs(); - //addToList(trUtf8("Read K file \"%1\": %2 coeffs, %3 bytes").arg(PI2QString(coeffs.fileName())).arg(K.size_s()).arg(coeffs.k_content.size_s()), Qt::darkMagenta); + K.readFile(); + addToList(trUtf8("Readed K file \"%1\": %2 coeffs, %3 bytes").arg(PI2QString(K.file())).arg(K.count()).arg(K.fileSize()), Qt::darkMagenta); updateTree(); } void CD_Pult::on_buttonWrite_clicked() { - //coeffs.writeCoeffs(); - //addToList(trUtf8("Write K file \"%1\": %2 coeffs, %3 bytes").arg(PI2QString(coeffs.fileName())).arg(K.size_s()).arg(coeffs.k_content.size_s()), Qt::darkMagenta); + K.writeFile(); + addToList(trUtf8("Written K file \"%1\": %2 coeffs, %3 bytes").arg(PI2QString(K.file())).arg(K.count()).arg(K.fileSize()), Qt::darkMagenta); } void CD_Pult::on_buttonSetKDesc_clicked() { - QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with K description"), kdesc_file, "C/C++ header files(*.h *.hpp);;All files(*)"); + QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with K description"), PI2QString(kdesc_file), "C/C++ header files(*.h *.hpp);;All files(*)"); if (ret.isEmpty()) return; - kdesc_file = QDir::current().relativeFilePath(ret); - PIFile f(Q2PIString(kdesc_file), PIIODevice::ReadOnly); + kdesc_file = Q2PIString(QDir::current().relativeFilePath(ret)); + PIFile f(kdesc_file, PIIODevice::ReadOnly); K.update(&f); updateKDesc(true); } void CD_Pult::on_buttonSetCDesc_clicked() { - QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with C description"), cdesc_file, "C/C++ header files(*.h *.hpp);;All files(*)"); + QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with C description"), PI2QString(cdesc_file), "C/C++ header files(*.h *.hpp);;All files(*)"); if (ret.isEmpty()) return; - cdesc_file = QDir::current().relativeFilePath(ret); + cdesc_file = Q2PIString(QDir::current().relativeFilePath(ret)); updateCDesc(); } diff --git a/cd_utils/pult/cd_pult.h b/cd_utils/pult/cd_pult.h index b420817..e8644b9 100644 --- a/cd_utils/pult/cd_pult.h +++ b/cd_utils/pult/cd_pult.h @@ -60,7 +60,7 @@ private: Ui::CD_Pult * ui; PIString config_, name_x, name_c; - QString kdesc_file, cdesc_file, xdesc_file; + PIString kdesc_file, cdesc_file, xdesc_file; QMenu log_menu; QTime tm, ctm; QTimer timer_diag; diff --git a/cd_utils/pult/cd_pult.ui b/cd_utils/pult/cd_pult.ui index 4f51a38..a04f3cf 100644 --- a/cd_utils/pult/cd_pult.ui +++ b/cd_utils/pult/cd_pult.ui @@ -321,6 +321,11 @@ Name + + + Type + + Expression @@ -331,11 +336,6 @@ Calculated - - - Type - - Comment