move to PIIOTextStream

This commit is contained in:
2022-05-13 13:24:09 +03:00
parent 1028233553
commit f67e3030b9
6 changed files with 217 additions and 188 deletions

View File

@@ -41,6 +41,8 @@ public:
//! \~russian Создает %PIIOBinaryStream для устройства "device" //! \~russian Создает %PIIOBinaryStream для устройства "device"
PIIOBinaryStream(PIIODevice * device): dev(device) {} PIIOBinaryStream(PIIODevice * device): dev(device) {}
void setDevice(PIIODevice * device) {dev = device;}
bool binaryStreamAppendImp(const void * d, size_t s) { bool binaryStreamAppendImp(const void * d, size_t s) {
if (!dev) return false; if (!dev) return false;
return dev->write(d, s); return dev->write(d, s);
@@ -68,6 +70,11 @@ public:
//! \~russian Создает %PIIOTextStream для устройства "device" //! \~russian Создает %PIIOTextStream для устройства "device"
PIIOTextStream(PIIODevice * device): PITextStream<PIIOBinaryStream>(&bin_stream), bin_stream(device){} PIIOTextStream(PIIODevice * device): PITextStream<PIIOBinaryStream>(&bin_stream), bin_stream(device){}
void setDevice(PIIODevice * device) {
bin_stream = PIIOBinaryStream(device);
setStream(&bin_stream);
}
private: private:
PIIOBinaryStream bin_stream; PIIOBinaryStream bin_stream;

View File

@@ -19,6 +19,7 @@
#include "pip.h" #include "pip.h"
#include "picrypt.h" #include "picrypt.h"
#include "piiostream.h"
#include "dispatcherserver.h" #include "dispatcherserver.h"
@@ -72,7 +73,8 @@ int main (int argc, char * argv[]) {
uint max_connections = 1000; uint max_connections = 1000;
if (!PIFile::isExists(conf_path)) { if (!PIFile::isExists(conf_path)) {
PIFile f(conf_path, PIIODevice::ReadWrite); PIFile f(conf_path, PIIODevice::ReadWrite);
f << "ip = " << addr.ipString() << "\n" PIIOTextStream ts(&f);
ts << "ip = " << addr.ipString() << "\n"
<< "port = " << addr.port() << "\n" << "port = " << addr.port() << "\n"
<< "max_connections = " << max_connections << "\n"; << "max_connections = " << max_connections << "\n";
} }

View File

@@ -19,6 +19,7 @@
#include "picli.h" #include "picli.h"
#include "picodeparser.h" #include "picodeparser.h"
#include "piiostream.h"
using namespace PICoutManipulators; using namespace PICoutManipulators;
@@ -118,69 +119,69 @@ PIString toCName(const PIString &s) {
} }
void makeClassInfo(PIFile & f, const PICodeParser::Entity * e) { void makeClassInfo(PIIOTextStream & ts, const PICodeParser::Entity * e) {
f << "\n\t{\n\tClassInfo * ci = new ClassInfo();\n"; ts << "\n\t{\n\tClassInfo * ci = new ClassInfo();\n";
f << "\tci->type = \"" << e->type << "\";\n"; ts << "\tci->type = \"" << e->type << "\";\n";
f << "\tci->name = \"" << e->name << "\";\n"; ts << "\tci->name = \"" << e->name << "\";\n";
f << "\tci->has_name = " << (e->has_name ? "true" : "false") << ";\n"; ts << "\tci->has_name = " << (e->has_name ? "true" : "false") << ";\n";
if (!e->meta.isEmpty()) { if (!e->meta.isEmpty()) {
auto i = e->meta.makeIterator(); auto i = e->meta.makeIterator();
while (i.next()) while (i.next())
f << "\tci->meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n"; ts << "\tci->meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n";
} }
f << "\t(*classesInfo)[ci->name] = ci;\n"; ts << "\t(*classesInfo)[ci->name] = ci;\n";
if (e->parent_scope) { if (e->parent_scope) {
f << "\tpci = " << "classesInfo->value(\"" << e->parent_scope->name << "\", 0);\n"; ts << "\tpci = " << "classesInfo->value(\"" << e->parent_scope->name << "\", 0);\n";
f << "\tif (pci) pci->children_info << ci;\n"; ts << "\tif (pci) pci->children_info << ci;\n";
} }
piForeachC (PICodeParser::Entity * p, e->parents) piForeachC (PICodeParser::Entity * p, e->parents)
f << "\tci->parents << \"" << p->name << "\";\n"; ts << "\tci->parents << \"" << p->name << "\";\n";
if (!e->members.isEmpty()) f << "\n\tTypeInfo ti;\n"; if (!e->members.isEmpty()) ts << "\n\tTypeInfo ti;\n";
piForeachC (PICodeParser::Member & m, e->members) { piForeachC (PICodeParser::Member & m, e->members) {
f << "\tti = TypeInfo(\"" << m.name << "\", \"" << m.type << "\""; ts << "\tti = TypeInfo(\"" << m.name << "\", \"" << m.type << "\"";
if (m.attributes != 0) { if (m.attributes != 0) {
bool fir = true; bool fir = true;
f << ", "; ts << ", ";
if (m.attributes[PICodeParser::Const ]) {if (fir) fir = false; else f << " | "; f << "Const";} if (m.attributes[PICodeParser::Const ]) {if (fir) fir = false; else ts << " | "; ts << "Const";}
if (m.attributes[PICodeParser::Static ]) {if (fir) fir = false; else f << " | "; f << "Static";} if (m.attributes[PICodeParser::Static ]) {if (fir) fir = false; else ts << " | "; ts << "Static";}
if (m.attributes[PICodeParser::Mutable ]) {if (fir) fir = false; else f << " | "; f << "Mutable";} if (m.attributes[PICodeParser::Mutable ]) {if (fir) fir = false; else ts << " | "; ts << "Mutable";}
if (m.attributes[PICodeParser::Volatile]) {if (fir) fir = false; else f << " | "; f << "Volatile";} if (m.attributes[PICodeParser::Volatile]) {if (fir) fir = false; else ts << " | "; ts << "Volatile";}
if (m.attributes[PICodeParser::Inline ]) {if (fir) fir = false; else f << " | "; f << "Inline";} if (m.attributes[PICodeParser::Inline ]) {if (fir) fir = false; else ts << " | "; ts << "Inline";}
if (m.attributes[PICodeParser::Virtual ]) {if (fir) fir = false; else f << " | "; f << "Virtual";} if (m.attributes[PICodeParser::Virtual ]) {if (fir) fir = false; else ts << " | "; ts << "Virtual";}
} else { } else {
if (m.isBitfield()) if (m.isBitfield())
f << ", 0"; ts << ", 0";
} }
if (m.isBitfield()) if (m.isBitfield())
f << ", " << m.bits; ts << ", " << m.bits;
f << ");\n"; ts << ");\n";
if (!m.meta.isEmpty()) { if (!m.meta.isEmpty()) {
for (PICodeParser::MetaMap::const_iterator i = m.meta.begin(); i != m.meta.end(); ++i) for (PICodeParser::MetaMap::const_iterator i = m.meta.begin(); i != m.meta.end(); ++i)
f << "\tti.meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n"; ts << "\tti.meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n";
} }
f << "\tci->variables << ti;\n"; ts << "\tci->variables << ti;\n";
} }
PIString arg; PIString arg;
bool has_fi = false; bool has_fi = false;
piForeachC (PICodeParser::Member & m, e->functions) { piForeachC (PICodeParser::Member & m, e->functions) {
if (e->name.findCWord(m.name) >= 0) continue; if (e->name.findCWord(m.name) >= 0) continue;
if (!has_fi) if (!has_fi)
f << "\n\tFunctionInfo * fi;\n"; ts << "\n\tFunctionInfo * fi;\n";
has_fi = true; has_fi = true;
f << "\tci->functions.push_back(FunctionInfo()); fi = &(ci->functions.back());\n"; ts << "\tci->functions.push_back(FunctionInfo()); fi = &(ci->functions.back());\n";
f << "\tfi->name = \"" << m.name << "\";"; ts << "\tfi->name = \"" << m.name << "\";";
f << " fi->return_type = TypeInfo(\"\", \"" << m.type << "\""; ts << " fi->return_type = TypeInfo(\"\", \"" << m.type << "\"";
if (m.attributes[PICodeParser::Const] || m.attributes[PICodeParser::Static]) { if (m.attributes[PICodeParser::Const] || m.attributes[PICodeParser::Static]) {
bool fir = true; bool fir = true;
f << ", "; ts << ", ";
if (m.attributes[PICodeParser::Const ]) {if (fir) fir = false; else f << " | "; f << "Const";} if (m.attributes[PICodeParser::Const ]) {if (fir) fir = false; else ts << " | "; ts << "Const";}
if (m.attributes[PICodeParser::Static]) {if (fir) fir = false; else f << " | "; f << "Static";} if (m.attributes[PICodeParser::Static]) {if (fir) fir = false; else ts << " | "; ts << "Static";}
} }
f << ");\n"; ts << ");\n";
//piCout << "write func" << m.name; //piCout << "write func" << m.name;
piForeachC (PIString & a, m.arguments_full) { piForeachC (PIString & a, m.arguments_full) {
//piCout << "write arg" << a; //piCout << "write arg" << a;
f << "\tfi->arguments << TypeInfo("; ts << "\tfi->arguments << TypeInfo(";
arg = a; arg = a;
bool con = false; bool con = false;
arg.prepend(" "); arg.prepend(" ");
@@ -189,48 +190,48 @@ void makeClassInfo(PIFile & f, const PICodeParser::Entity * e) {
con = true; con = true;
} }
arg.trim(); arg.trim();
int ts = 0; int i = 0;
for (ts = arg.size_s() - 1; ts > 0; --ts) for (i = arg.size_s() - 1; i > 0; --i)
if (!_isCChar(arg[ts]) && !(arg[ts].isDigit())) break; if (!_isCChar(arg[i]) && !(arg[i].isDigit())) break;
f << "\"" << arg.takeRight(arg.size_s() - ts - 1).trim() << "\", "; ts << "\"" << arg.takeRight(arg.size_s() - i - 1).trim() << "\", ";
f << "\"" << arg.trim() << "\""; ts << "\"" << arg.trim() << "\"";
if (con) f << ", Const"; if (con) ts << ", Const";
f << ");\n"; ts << ");\n";
} }
if (!m.meta.isEmpty()) { if (!m.meta.isEmpty()) {
for (PICodeParser::MetaMap::const_iterator i = m.meta.begin(); i != m.meta.end(); ++i) for (PICodeParser::MetaMap::const_iterator i = m.meta.begin(); i != m.meta.end(); ++i)
f << "\tfi->meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n"; ts << "\tfi->meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n";
} }
} }
f << "\n\t}"; ts << "\n\t}";
} }
void makeEnumInfo(PIFile & f, const PICodeParser::Enum * e) { void makeEnumInfo(PIIOTextStream & ts, const PICodeParser::Enum * e) {
if (e->name.isEmpty()) { if (e->name.isEmpty()) {
f << "\n\tei = (*enumsInfo)[\"\"];\n"; ts << "\n\tei = (*enumsInfo)[\"\"];\n";
} else { } else {
f << "\n\tei = new EnumInfo();\n"; ts << "\n\tei = new EnumInfo();\n";
f << "\t(*enumsInfo)[\"" << e->name << "\"] = ei;\n"; ts << "\t(*enumsInfo)[\"" << e->name << "\"] = ei;\n";
f << "\tei->name = \"" << e->name << "\";\n"; ts << "\tei->name = \"" << e->name << "\";\n";
if (!e->meta.isEmpty()) { if (!e->meta.isEmpty()) {
auto i = e->meta.makeIterator(); auto i = e->meta.makeIterator();
while (i.next()) while (i.next())
f << "\tei->meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n"; ts << "\tei->meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n";
} }
} }
piForeachC (PICodeParser::EnumeratorInfo & m, e->members) { piForeachC (PICodeParser::EnumeratorInfo & m, e->members) {
f << "\tei->members << PICodeInfo::EnumeratorInfo(\"" << m.name << "\", " << m.value << ");\n"; ts << "\tei->members << PICodeInfo::EnumeratorInfo(\"" << m.name << "\", " << m.value << ");\n";
if (!m.meta.isEmpty()) { if (!m.meta.isEmpty()) {
auto i = m.meta.makeIterator(); auto i = m.meta.makeIterator();
while (i.next()) while (i.next())
f << "\tei->members.back().meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n"; ts << "\tei->members.back().meta[\"" << i.key() << "\"] = PIString::fromUTF8(\"" << i.value() << "\");\n";
} }
} }
} }
void writeClassStreamMembersOut(PIFile & f, const PICodeParser::Entity * e, int & cnt, bool simple) { void writeClassStreamMembersOut(PIIOTextStream & ts, const PICodeParser::Entity * e, int & cnt, bool simple) {
PIVector<PICodeParser::Member> ml; PIVector<PICodeParser::Member> ml;
piForeachC (PICodeParser::Member & m, e->members) { piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr || (m.visibility != PICodeParser::Public)) continue; if (m.is_type_ptr || (m.visibility != PICodeParser::Public)) continue;
@@ -246,15 +247,15 @@ void writeClassStreamMembersOut(PIFile & f, const PICodeParser::Entity * e, int
cnt = m.meta.value("id").toInt(); cnt = m.meta.value("id").toInt();
if (m.dims.isEmpty()) { if (m.dims.isEmpty()) {
if (simple) { if (simple) {
f << "\ts << "; ts << "\ts << ";
if (parser.isEnum(m.type)) if (parser.isEnum(m.type))
f << "(int)"; ts << "(int)";
f << "v." << m.name << ";\n"; ts << "v." << m.name << ";\n";
} else { } else {
f << "\tcs << cs.chunk(" << cnt << ", "; ts << "\tcs << cs.chunk(" << cnt << ", ";
if (parser.isEnum(m.type)) if (parser.isEnum(m.type))
f << "(int)"; ts << "(int)";
f << "v." << m.name << ");\n"; ts << "v." << m.name << ");\n";
} }
} else { } else {
PIString ptype = m.type.left(m.type.find('[')).trim(); PIString ptype = m.type.left(m.type.find('[')).trim();
@@ -264,11 +265,11 @@ void writeClassStreamMembersOut(PIFile & f, const PICodeParser::Entity * e, int
size += m.dims[i]; size += m.dims[i];
} }
if (simple) { if (simple) {
f << "\tfor (int i = 0; i < " << size << "; ++i)\n"; ts << "\tfor (int i = 0; i < " << size << "; ++i)\n";
f << "\t\ts << ((const " << ptype << " *)(v." << m.name << "))[i];\n"; ts << "\t\ts << ((const " << ptype << " *)(v." << m.name << "))[i];\n";
} else { } else {
f << "\tcs << cs.chunk(" << cnt << ", PIVector<" << ptype << " >((const " << ptype << " *)(v." << m.name << "), "; ts << "\tcs << cs.chunk(" << cnt << ", PIVector<" << ptype << " >((const " << ptype << " *)(v." << m.name << "), ";
f << size << "));\n"; ts << size << "));\n";
} }
} }
if (is_union) if (is_union)
@@ -277,12 +278,12 @@ void writeClassStreamMembersOut(PIFile & f, const PICodeParser::Entity * e, int
if (is_union) return; if (is_union) return;
piForeachC (PICodeParser::Entity * ce, e->children) { piForeachC (PICodeParser::Entity * ce, e->children) {
if (ce->has_name) continue; if (ce->has_name) continue;
writeClassStreamMembersOut(f, ce, cnt, simple); writeClassStreamMembersOut(ts, ce, cnt, simple);
} }
} }
void writeClassStreamMembersIn(PIFile & f, const PICodeParser::Entity * e, int & cnt, bool simple) { void writeClassStreamMembersIn(PIIOTextStream & ts, const PICodeParser::Entity * e, int & cnt, bool simple) {
PIVector<PICodeParser::Member> ml; PIVector<PICodeParser::Member> ml;
piForeachC (PICodeParser::Member & m, e->members) { piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr || (m.visibility != PICodeParser::Public)) continue; if (m.is_type_ptr || (m.visibility != PICodeParser::Public)) continue;
@@ -298,15 +299,15 @@ void writeClassStreamMembersIn(PIFile & f, const PICodeParser::Entity * e, int &
cnt = m.meta.value("id").toInt(); cnt = m.meta.value("id").toInt();
if (m.dims.isEmpty()) { if (m.dims.isEmpty()) {
if (simple) { if (simple) {
f << "\ts >> "; ts << "\ts >> ";
if (parser.isEnum(m.type)) if (parser.isEnum(m.type))
f << "(int&)"; ts << "(int&)";
f << "v." << m.name << ";\n"; ts << "v." << m.name << ";\n";
} else { } else {
f << "\t\tcase " << cnt << ": cs.get("; ts << "\t\tcase " << cnt << ": cs.get(";
if (parser.isEnum(m.type)) if (parser.isEnum(m.type))
f << "(int&)"; ts << "(int&)";
f << "v." << m.name << "); break;\n"; ts << "v." << m.name << "); break;\n";
} }
} else { } else {
PIString ptype = m.type.left(m.type.find('[')).trim(); PIString ptype = m.type.left(m.type.find('[')).trim();
@@ -316,15 +317,15 @@ void writeClassStreamMembersIn(PIFile & f, const PICodeParser::Entity * e, int &
size += m.dims[i]; size += m.dims[i];
} }
if (simple) { if (simple) {
f << "\tfor (int i = 0; i < " << size << "; ++i)\n"; ts << "\tfor (int i = 0; i < " << size << "; ++i)\n";
f << "\t\ts >> ((" << ptype << " *)(v." << m.name << "))[i];\n"; ts << "\t\ts >> ((" << ptype << " *)(v." << m.name << "))[i];\n";
} else { } else {
f << "\t\tcase " << cnt << ": {\n\t\t\tPIVector<" << ptype << " > d; cs.get(d);\n"; ts << "\t\tcase " << cnt << ": {\n\t\t\tPIVector<" << ptype << " > d; cs.get(d);\n";
f << "\t\t\tint cnt = piMini(d.size_s(), " << size << ");\n"; ts << "\t\t\tint cnt = piMini(d.size_s(), " << size << ");\n";
f << "\t\t\tfor (int i = 0; i < cnt; ++i)\n"; ts << "\t\t\tfor (int i = 0; i < cnt; ++i)\n";
f << "\t\t\t\t((" << ptype << " *)(v." << m.name << "))[i] = d[i];\n"; ts << "\t\t\t\t((" << ptype << " *)(v." << m.name << "))[i] = d[i];\n";
f << "\t\t\t}\n"; ts << "\t\t\t}\n";
f << "\t\t\tbreak;\n"; ts << "\t\t\tbreak;\n";
} }
} }
if (is_union) if (is_union)
@@ -333,7 +334,7 @@ void writeClassStreamMembersIn(PIFile & f, const PICodeParser::Entity * e, int &
if (is_union) return; if (is_union) return;
piForeachC (PICodeParser::Entity * ce, e->children) { piForeachC (PICodeParser::Entity * ce, e->children) {
if (ce->has_name) continue; if (ce->has_name) continue;
writeClassStreamMembersIn(f, ce, cnt, simple); writeClassStreamMembersIn(ts, ce, cnt, simple);
} }
} }
@@ -350,76 +351,76 @@ bool needClassStream(const PICodeParser::Entity * e) {
} }
void makeClassStream(PIFile & f, const PICodeParser::Entity * e) { void makeClassStream(PIIOTextStream & ts, const PICodeParser::Entity * e) {
if (!needClassStream(e)) return; if (!needClassStream(e)) return;
bool simple = e->meta.contains("simple-stream"); bool simple = e->meta.contains("simple-stream");
f << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v) {\n"; ts << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v) {\n";
if (!simple) if (!simple)
f << "\tPIChunkStream cs;\n"; ts << "\tPIChunkStream cs;\n";
int cnt = 0; int cnt = 0;
writeClassStreamMembersOut(f, e, cnt, simple); writeClassStreamMembersOut(ts, e, cnt, simple);
if (!simple) if (!simple)
f << "\ts << cs.data();\n"; ts << "\ts << cs.data();\n";
f << "\treturn s;\n}\n"; ts << "\treturn s;\n}\n";
f << "PIByteArray & operator >>(PIByteArray & s, " << e->name << " & v) {\n"; ts << "PIByteArray & operator >>(PIByteArray & s, " << e->name << " & v) {\n";
if (!simple) { if (!simple) {
f << "\tif (s.size_s() < 4) return s;\n"; ts << "\tif (s.size_s() < 4) return s;\n";
f << "\tPIByteArray csba; s >> csba;\n"; ts << "\tPIByteArray csba; s >> csba;\n";
f << "\tPIChunkStream cs(csba);\n"; ts << "\tPIChunkStream cs(csba);\n";
f << "\twhile (!cs.atEnd()) {\n"; ts << "\twhile (!cs.atEnd()) {\n";
f << "\t\tswitch (cs.read()) {\n"; ts << "\t\tswitch (cs.read()) {\n";
} }
cnt = 0; cnt = 0;
writeClassStreamMembersIn(f, e, cnt, simple); writeClassStreamMembersIn(ts, e, cnt, simple);
if (!simple) if (!simple)
f << "\t\t}\n\t}\n"; ts << "\t\t}\n\t}\n";
f << "\treturn s;\n}\n"; ts << "\treturn s;\n}\n";
} }
void makeClassStreamHeader(PIFile & f, const PICodeParser::Entity * e) { void makeClassStreamHeader(PIIOTextStream & ts, const PICodeParser::Entity * e) {
if (!needClassStream(e)) return; if (!needClassStream(e)) return;
f << "\n"; ts << "\n";
PIStringList sl = e->name.split("::"); PIStringList sl = e->name.split("::");
for (int i = 0; i < sl.size_s() - 1; ++i) for (int i = 0; i < sl.size_s() - 1; ++i)
f << "namespace " << sl[i] << " {"; ts << "namespace " << sl[i] << " {";
f << e->type << " " << sl.back() << ";"; ts << e->type << " " << sl.back() << ";";
if (sl.size_s() > 1) f << PIString('}').repeat(sl.size_s() - 1); if (sl.size_s() > 1) ts << PIString('}').repeat(sl.size_s() - 1);
f << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v);"; ts << "\nPIByteArray & operator <<(PIByteArray & s, const " << e->name << " & v);";
f << "\nPIByteArray & operator >>(PIByteArray & s, " << e->name << " & v);\n"; ts << "\nPIByteArray & operator >>(PIByteArray & s, " << e->name << " & v);\n";
} }
void makeGetterType(PIFile & f, const PICodeParser::Entity * e) { void makeGetterType(PIIOTextStream & ts, const PICodeParser::Entity * e) {
if (!needClassStream(e)) return; if (!needClassStream(e)) return;
f << "\nconst char * getterType" << toCName(e->name) << "(const char * name) {\n"; ts << "\nconst char * getterType" << toCName(e->name) << "(const char * name) {\n";
f << "\tif (!name) return \"\";\n"; ts << "\tif (!name) return \"\";\n";
piForeachC (PICodeParser::Member & m, e->members) { piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr || m.isBitfield() || !m.dims.isEmpty() || (m.visibility != PICodeParser::Public)) if (m.is_type_ptr || m.isBitfield() || !m.dims.isEmpty() || (m.visibility != PICodeParser::Public))
continue; continue;
f << "\tif (strcmp(name, \"" << m.name << "\") == 0) return \"" << m.type << "\";\n"; ts << "\tif (strcmp(name, \"" << m.name << "\") == 0) return \"" << m.type << "\";\n";
} }
f << "\treturn \"\";\n}\n"; ts << "\treturn \"\";\n}\n";
} }
void makeGetterValue(PIFile & f, const PICodeParser::Entity * e) { void makeGetterValue(PIIOTextStream & ts, const PICodeParser::Entity * e) {
if (!needClassStream(e)) return; if (!needClassStream(e)) return;
f << "\nPIByteArray getterValue" << toCName(e->name) << "(const void * p, const char * name) {\n"; ts << "\nPIByteArray getterValue" << toCName(e->name) << "(const void * p, const char * name) {\n";
f << "\tPIByteArray ret;\n"; ts << "\tPIByteArray ret;\n";
f << "\tif (!p || !name) return ret;\n"; ts << "\tif (!p || !name) return ret;\n";
f << "\t" << e->name << " * o = (" << e->name << "*)p;\n"; ts << "\t" << e->name << " * o = (" << e->name << "*)p;\n";
piForeachC (PICodeParser::Member & m, e->members) { piForeachC (PICodeParser::Member & m, e->members) {
if (m.is_type_ptr || m.isBitfield() || !m.dims.isEmpty() || (m.visibility != PICodeParser::Public)) if (m.is_type_ptr || m.isBitfield() || !m.dims.isEmpty() || (m.visibility != PICodeParser::Public))
continue; continue;
f << "\tif (strcmp(name, \"" << m.name << "\") == 0) {serialize(ret, o->" << m.name << "); return ret;}\n"; ts << "\tif (strcmp(name, \"" << m.name << "\") == 0) {serialize(ret, o->" << m.name << "); return ret;}\n";
} }
f << "\treturn ret;\n}\n"; ts << "\treturn ret;\n}\n";
} }
void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool meta, bool enums, bool streams, bool texts, bool getters) { bool writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool meta, bool enums, bool streams, bool texts, bool getters) {
PIString defname = "CCM_" + PIString::fromNumber(out.hash()) + "_H"; PIString defname = "CCM_" + PIString::fromNumber(out.hash()) + "_H";
PISet<PIString> inc_files; PISet<PIString> inc_files;
piForeachC (PICodeParser::Entity * e, parser.entities) piForeachC (PICodeParser::Entity * e, parser.entities)
@@ -434,63 +435,67 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
PIFile f(out + ".cpp"); PIFile f(out + ".cpp");
f.clear(); f.clear();
f.open(PIIODevice::WriteOnly); if (!f.open(PIIODevice::WriteOnly)) {
f << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n\n"); piCout << "Error: can`t open out file" << f.path();
f << "#include <string.h>\n"; return false;
}
PIIOTextStream ts(&f);
ts << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n\n");
ts << "#include <string.h>\n";
if (streams || texts) if (streams || texts)
f << "#include <pichunkstream.h>\n"; ts << "#include <pichunkstream.h>\n";
f << "#include \"" << out << ".h\""; ts << "#include \"" << out << ".h\"";
f << inc_string << "\n"; ts << inc_string << "\n";
f << "\nusing namespace PICodeInfo;\n\n"; ts << "\nusing namespace PICodeInfo;\n\n";
if (meta || enums || getters) { if (meta || enums || getters) {
if (getters) { if (getters) {
f << "\n\n// Getter funtions\n"; ts << "\n\n// Getter funtions\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (!e->has_name || e->name.startsWith("_PI")) continue; if (!e->has_name || e->name.startsWith("_PI")) continue;
makeGetterType(f, e); makeGetterType(ts, e);
makeGetterValue(f, e); makeGetterValue(ts, e);
} }
} }
f << "\n\n// Metainformation\n\n__ClassInfo_" << defname << "_Initializer__::__ClassInfo_" << defname << "_Initializer__() {\n"; ts << "\n\n// Metainformation\n\n__ClassInfo_" << defname << "_Initializer__::__ClassInfo_" << defname << "_Initializer__() {\n";
f << "\tif (_inited_) return;\n\t_inited_ = true;\n\n"; ts << "\tif (_inited_) return;\n\t_inited_ = true;\n\n";
if (meta) { if (meta) {
f << "\tClassInfo * pci = new ClassInfo();\n"; ts << "\tClassInfo * pci = new ClassInfo();\n";
f << "\t(*classesInfo)[\"\"] = pci;\n"; ts << "\t(*classesInfo)[\"\"] = pci;\n";
} }
if (enums && !parser.enums.isEmpty()) { if (enums && !parser.enums.isEmpty()) {
f << "\tEnumInfo * ei;\n"; ts << "\tEnumInfo * ei;\n";
f << "\t(*enumsInfo)[\"\"] = new EnumInfo();\n"; ts << "\t(*enumsInfo)[\"\"] = new EnumInfo();\n";
} }
if (meta) { if (meta) {
f << "\n\n// Classes\n"; ts << "\n\n// Classes\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (e->name.startsWith("_PI")) continue; if (e->name.startsWith("_PI")) continue;
makeClassInfo(f, e); makeClassInfo(ts, e);
} }
} }
if (enums) { if (enums) {
f << "\n// Enums\n"; ts << "\n// Enums\n";
piForeachC (PICodeParser::Enum & e, parser.enums) piForeachC (PICodeParser::Enum & e, parser.enums)
makeEnumInfo(f, &e); makeEnumInfo(ts, &e);
} }
if (getters) { if (getters) {
f << "\n// Getters\n"; ts << "\n// Getters\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (!needClassStream(e)) continue; if (!needClassStream(e)) continue;
if (!e->has_name || e->name.startsWith("_PI")) continue; if (!e->has_name || e->name.startsWith("_PI")) continue;
f << "\t(*accessValueFunctions)[\"" << e->name << "\"] = getterValue" << toCName(e->name) << ";\n"; ts << "\t(*accessValueFunctions)[\"" << e->name << "\"] = getterValue" << toCName(e->name) << ";\n";
f << "\t(*accessTypeFunctions)[\"" << e->name << "\"] = getterType" << toCName(e->name) << ";\n"; ts << "\t(*accessTypeFunctions)[\"" << e->name << "\"] = getterType" << toCName(e->name) << ";\n";
} }
} }
f << "}\n"; ts << "}\n";
f << "\n\nbool __ClassInfo_" << defname << "_Initializer__::_inited_ = false;\n"; ts << "\n\nbool __ClassInfo_" << defname << "_Initializer__::_inited_ = false;\n";
} }
if (streams) { if (streams) {
f << "\n\n// Stream operators\n"; ts << "\n\n// Stream operators\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (!e->has_name || e->name.startsWith("_PI")) continue; if (!e->has_name || e->name.startsWith("_PI")) continue;
makeClassStream(f, e); makeClassStream(ts, e);
} }
} }
@@ -499,28 +504,33 @@ void writeModel(PICodeParser & parser, PICLI & cli, const PIString out, bool met
f.setPath(out + ".h"); f.setPath(out + ".h");
f.clear(); f.clear();
f.open(PIIODevice::WriteOnly); if (!f.open(PIIODevice::WriteOnly)) {
f << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n"); piCout << "Error: can`t open out file" << f.path();
f << "// Execute command:\n"; return false;
}
ts << "// Generated by \"PIP Code model generator\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n");
ts << "// Execute command:\n";
piForeachC (PIString & _a, cli.rawArguments()) piForeachC (PIString & _a, cli.rawArguments())
f << "// \"" << _a << "\"\n"; ts << "// \"" << _a << "\"\n";
f << "\n"; ts << "\n";
f << "#ifndef " << defname << "\n#define " << defname << "\n\n"; ts << "#ifndef " << defname << "\n#define " << defname << "\n\n";
f << "#include <pivariant.h>\n#include <picodeinfo.h>"; ts << "#include <pivariant.h>\n#include <picodeinfo.h>";
if (streams) { if (streams) {
f << "\n\n// Stream operators\n"; ts << "\n\n// Stream operators\n";
piForeachC (PICodeParser::Entity * e, parser.entities) { piForeachC (PICodeParser::Entity * e, parser.entities) {
if (!e->has_name || e->name.startsWith("_PI")) continue; if (!e->has_name || e->name.startsWith("_PI")) continue;
makeClassStreamHeader(f, e); makeClassStreamHeader(ts, e);
} }
} }
if (meta || enums || getters) { if (meta || enums || getters) {
f << "\n\n// Metainformation\n\nclass __ClassInfo_" << defname << "_Initializer__ {\n"; ts << "\n\n// Metainformation\n\nclass __ClassInfo_" << defname << "_Initializer__ {\n";
f << "public:\n\t__ClassInfo_" << defname << "_Initializer__();\n\tstatic bool _inited_;\n};\n"; ts << "public:\n\t__ClassInfo_" << defname << "_Initializer__();\n\tstatic bool _inited_;\n};\n";
f << "\nstatic __ClassInfo_" << defname << "_Initializer__ __classinfo_" << defname.toLowerCase() << "_initializer__;\n"; ts << "\nstatic __ClassInfo_" << defname << "_Initializer__ __classinfo_" << defname.toLowerCase() << "_initializer__;\n";
} }
f << "\n\n#endif // " << defname << "\n"; ts << "\n\n#endif // " << defname << "\n";
f.close(); f.close();
return true;
} }
@@ -562,11 +572,12 @@ int main(int argc, char * argv[]) {
piCout << Cyan << Bold << "Parsing done"; piCout << Cyan << Bold << "Parsing done";
piCout << Cyan << Bold << "Writing code model ..."; piCout << Cyan << Bold << "Writing code model ...";
bool all = cli.hasArgument("All"); bool all = cli.hasArgument("All");
writeModel(parser, cli, cli.argumentValue("output"), cli.hasArgument("Metainfo") || all, if (!writeModel(parser, cli, cli.argumentValue("output"), cli.hasArgument("Metainfo") || all,
cli.hasArgument("Enum") || all, cli.hasArgument("Enum") || all,
cli.hasArgument("Stream") || all, cli.hasArgument("Stream") || all,
cli.hasArgument("Text") || all, cli.hasArgument("Text") || all,
cli.hasArgument("Getter") || all); cli.hasArgument("Getter") || all))
return 1;
piCout << Cyan << Bold << "Writing done"; piCout << Cyan << Bold << "Writing done";
if (cli.hasArgument("print") || cli.hasArgument("Print")) { if (cli.hasArgument("print") || cli.hasArgument("Print")) {
bool womain = cli.hasArgument("print"); bool womain = cli.hasArgument("print");

View File

@@ -17,9 +17,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <picli.h> #include "picli.h"
#include <pidir.h> #include "pidir.h"
#include <piprocess.h> #include "piprocess.h"
#include "piiostream.h"
#define DELIM "::" #define DELIM "::"
@@ -735,7 +736,8 @@ int main(int argc, char * argv[]) {
lp = "Resources/lang"; lp = "Resources/lang";
} }
//piCout << pp; //piCout << pp;
qtc << "[Paths]\n\tPlugins = " << pp << "\n\tTranslations = " << lp << "\n"; PIIOTextStream ts(&qtc);
ts << "[Paths]\n\tPlugins = " << pp << "\n\tTranslations = " << lp << "\n";
} }
} }
} }

View File

@@ -1,5 +1,6 @@
#include "generator.h" #include "generator.h"
#include "piresourcesstorage.h" #include "piresourcesstorage.h"
#include "piiostream.h"
PIString initName(const PIString & n) { PIString initName(const PIString & n) {
@@ -25,7 +26,8 @@ bool generate(const PIString & init_name, PIFile & file, const PIVector<ParserSe
} }
} }
if (fv.isEmpty()) return false; if (fv.isEmpty()) return false;
file << "#include \"piresourcesstorage.h\"\n\nstatic const uchar " << dataname << "[] = {\n"; PIIOTextStream ts(&file);
ts << "#include \"piresourcesstorage.h\"\n\nstatic const uchar " << dataname << "[] = {\n";
bool first = true; bool first = true;
int rcnt = -1; int rcnt = -1;
llong curoff = 0, curfile = 0; llong curoff = 0, curfile = 0;
@@ -40,45 +42,45 @@ bool generate(const PIString & init_name, PIFile & file, const PIVector<ParserSe
curfile += readed.size_s(); curfile += readed.size_s();
for (int i = 0; i < readed.size_s(); ++i) { for (int i = 0; i < readed.size_s(); ++i) {
if (!first) if (!first)
file << ','; ts << ',';
first = false; first = false;
if (++rcnt >= 32) { if (++rcnt >= 32) {
file << '\n'; ts << '\n';
rcnt = 0; rcnt = 0;
} }
file << int(readed[i]); ts << int(readed[i]);
} }
} }
e.size = curfile; e.size = curfile;
curoff += curfile; curoff += curfile;
} }
file << "\n};\n"; ts << "\n};\n";
PIByteArray dba; PIByteArray dba;
dba << fv; dba << fv;
file << "\nstatic const uchar " << descname << "[] = {\n"; ts << "\nstatic const uchar " << descname << "[] = {\n";
first = true; first = true;
rcnt = -1; rcnt = -1;
for (int i = 0; i < dba.size_s(); ++i) { for (int i = 0; i < dba.size_s(); ++i) {
if (!first) if (!first)
file << ','; ts << ',';
first = false; first = false;
if (++rcnt >= 32) { if (++rcnt >= 32) {
file << '\n'; ts << '\n';
rcnt = 0; rcnt = 0;
} }
file << int(dba[i]); ts << int(dba[i]);
} }
file << "\n};\n"; ts << "\n};\n";
file << "\nvoid " << funcname << "() {\n"; ts << "\nvoid " << funcname << "() {\n";
file << "\tPIResourcesStorage::instance()->registerSection(" << dataname << ", " << descname << ", sizeof(" << descname << "));\n"; ts << "\tPIResourcesStorage::instance()->registerSection(" << dataname << ", " << descname << ", sizeof(" << descname << "));\n";
file << "}\n"; ts << "}\n";
file << "\nclass " << icname << " {\n"; ts << "\nclass " << icname << " {\n";
file << "public:\n\t" << icname << "() {\n"; ts << "public:\n\t" << icname << "() {\n";
file << "\t\t" << funcname << "();\n"; ts << "\t\t" << funcname << "();\n";
file << "\t}\n"; ts << "\t}\n";
file << "} _pirc_" << fcname << "_initializer_;\n"; ts << "} _pirc_" << fcname << "_initializer_;\n";
return true; return true;
} }

View File

@@ -20,6 +20,7 @@
#include "parser.h" #include "parser.h"
#include "generator.h" #include "generator.h"
#include "picli.h" #include "picli.h"
#include "piiostream.h"
using namespace PICoutManipulators; using namespace PICoutManipulators;
@@ -76,12 +77,16 @@ int main (int argc, char * argv[]) {
if (!out_file.isEmpty()) { if (!out_file.isEmpty()) {
if (outf.open(out_file, PIIODevice::ReadWrite)) { if (outf.open(out_file, PIIODevice::ReadWrite)) {
outf.clear(); outf.clear();
} else piCout << "Error: can`t open out file"; } else {
outf << "// Generated by \"PIP Resources Compiler\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n"); piCout << "Error: can`t open out file" << out_file;
outf << "// Execute command:\n"; return 1;
}
PIIOTextStream ts(&outf);
ts << "// Generated by \"PIP Resources Compiler\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n");
ts << "// Execute command:\n";
piForeachC (PIString & _a, cli.rawArguments()) piForeachC (PIString & _a, cli.rawArguments())
outf << "// \"" << _a << "\"\n"; ts << "// \"" << _a << "\"\n";
outf << "\n"; ts << "\n";
if (!generate(init_name, outf, files)) { if (!generate(init_name, outf, files)) {
piCout << "Error: generate fail"; piCout << "Error: generate fail";
return 1; return 1;