|
@@ -0,0 +1,363 @@
|
|
|
+#include "protointerface.h"
|
|
|
+
|
|
|
+#include <QDir>
|
|
|
+#include <QFile>
|
|
|
+
|
|
|
+protointerface::protointerface(std::string strprotopath,std::string strmsgtypepath,std::string strprofolder)
|
|
|
+{
|
|
|
+ mstrprotopath = strprotopath;
|
|
|
+ mstrmsgtypepath = strmsgtypepath;
|
|
|
+ mstrprofolder = strprofolder;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+std::vector<std::string> protointerface::GetMessageName(std::string strfilepath,std::vector<std::string> & xvectormsgname)
|
|
|
+{
|
|
|
+ std::vector<std::string> xvector;
|
|
|
+
|
|
|
+ QFile xFile;
|
|
|
+
|
|
|
+ xFile.setFileName(strfilepath.data());
|
|
|
+ if(!xFile.open(QIODevice::ReadOnly))
|
|
|
+ {
|
|
|
+ return xvector;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ QByteArray ba = xFile.readAll();
|
|
|
+
|
|
|
+ xFile.close();
|
|
|
+
|
|
|
+ QList<QByteArray> baline =ba.split('\n');//x.split(QRegExp("\n ")) ;//ba.split('\n');
|
|
|
+ int nline = baline.size();
|
|
|
+ int i;
|
|
|
+ bool bFindPackName = false;
|
|
|
+ std::string strclasspre = "";
|
|
|
+ std::string strnamepre = "";
|
|
|
+ for(i=0;i<nline;i++)
|
|
|
+ {
|
|
|
+ QString x(baline[i]);
|
|
|
+ x.trimmed();
|
|
|
+// QList<QByteArray> badata = baline[i].split('\t');
|
|
|
+
|
|
|
+ QStringList badata = x.split(QRegExp("[\t ,;{]+"));
|
|
|
+ if(bFindPackName == false)
|
|
|
+ {
|
|
|
+ if(badata[0] == "package")
|
|
|
+ {
|
|
|
+// qDebug("package name is %s",QString(badata[1]).toLatin1().data());
|
|
|
+ bFindPackName = true;
|
|
|
+ QString xpac(badata[1]);
|
|
|
+ xpac.trimmed();
|
|
|
+ QStringList bapack = xpac.split(QRegExp("[.]"));
|
|
|
+ unsigned int j;
|
|
|
+ for(j=0;j<bapack.size();j++)
|
|
|
+ {
|
|
|
+ strclasspre = strclasspre + QString(bapack[j]).toStdString() + "::";
|
|
|
+ strnamepre = strnamepre + QString(bapack[j]).toStdString() + "_";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(badata[0] == "message")
|
|
|
+ {
|
|
|
+ // qDebug("message name is %s",QString(badata[1]).toLatin1().data());
|
|
|
+ std::string strclassname = strclasspre + QString(badata[1]).toStdString();
|
|
|
+ std::string strxname = strnamepre + QString(badata[1]).toStdString();
|
|
|
+// qDebug("class name is %s",strclassname.data());
|
|
|
+ xvector.push_back(strclassname);
|
|
|
+ xvectormsgname.push_back(strxname);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return xvector;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<std::string> protointerface::GetFileNameVector(std::string strpath,std::string strsuff)
|
|
|
+{
|
|
|
+ std::vector<std::string> xvector;
|
|
|
+ QDir dir(strpath.data());
|
|
|
+
|
|
|
+ QStringList nameFilters;
|
|
|
+ nameFilters<<strsuff.data();
|
|
|
+
|
|
|
+ QStringList files = dir.entryList(nameFilters,QDir::Files,QDir::Name);
|
|
|
+
|
|
|
+ unsigned int i;
|
|
|
+ for(i=0;i<files.size();i++)
|
|
|
+ {
|
|
|
+ xvector.push_back(QString(files.at(i)).toStdString());
|
|
|
+ }
|
|
|
+
|
|
|
+ return xvector;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+bool protointerface::ccodeisexist(std::string strhname)
|
|
|
+{
|
|
|
+ std::string strpath = mstrmsgtypepath;
|
|
|
+ strpath = strpath + "/" + strhname;
|
|
|
+ QFile xFile;
|
|
|
+ xFile.setFileName(strpath.data());
|
|
|
+
|
|
|
+ if(xFile.exists())
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+void protointerface::WriteCodeLine(std::string strline,QFile * pFile)
|
|
|
+{
|
|
|
+ char str[1000];
|
|
|
+ snprintf(str,1000,"%s\n",strline.data());
|
|
|
+ pFile->write(str);
|
|
|
+}
|
|
|
+
|
|
|
+void protointerface::Proc()
|
|
|
+{
|
|
|
+
|
|
|
+ QFile xFileout;
|
|
|
+ QString strprofolder = mstrprofolder.data();
|
|
|
+ strprofolder = strprofolder + "/ivprotoif";
|
|
|
+ QDir xDir;
|
|
|
+ xDir.mkpath(strprofolder);
|
|
|
+ xFileout.setFileName(strprofolder + "/ivprotoif.cpp");
|
|
|
+ if(!xFileout.open(QIODevice::ReadWrite|QIODevice::Truncate))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ QFile xFileHeader;
|
|
|
+ xFileHeader.setFileName(strprofolder + "/ivprotoif.h");
|
|
|
+ if(!xFileHeader.open(QIODevice::ReadWrite|QIODevice::Truncate))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ QFile xFilePro;
|
|
|
+ xFilePro.setFileName(strprofolder + "/ivprotoif.pro");
|
|
|
+ if(!xFilePro.open(QIODevice::ReadWrite|QIODevice::Truncate))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ std::vector<std::string> xvectorproto = GetFileNameVector(mstrprotopath,"*.proto");
|
|
|
+
|
|
|
+ std::vector<std::string> xvectorclassname;
|
|
|
+ std::vector<std::string > xvectorname;
|
|
|
+ std::vector<std::string> xvectorprotoname;
|
|
|
+
|
|
|
+ unsigned int i;
|
|
|
+ for(i=0;i<xvectorproto.size();i++)
|
|
|
+ {
|
|
|
+ std::vector<std::string> xven;
|
|
|
+ QString strtemp(xvectorproto[i].data());
|
|
|
+ strtemp = strtemp.left(strtemp.size() - 6);
|
|
|
+
|
|
|
+ std::string strheadarfilename = strtemp.toStdString() + ".pb.h";
|
|
|
+ if(!ccodeisexist(strheadarfilename))
|
|
|
+ {
|
|
|
+ qDebug("proto %s is not generate c++ code.",xvectorproto[i].data());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ xvectorprotoname.push_back(strtemp.toStdString());
|
|
|
+// qDebug("header file is %s",strheadarfilename.data());
|
|
|
+ char strline[1000];
|
|
|
+ snprintf(strline,1000,"#include \"%s\"\n",strheadarfilename.data());
|
|
|
+ xFileout.write(strline);
|
|
|
+// qDebug("%d:%s",i,xvectorproto[i].data());
|
|
|
+ std::vector<std::string> xvectorcla = GetMessageName(mstrprotopath+"/" + xvectorproto[i],xven);
|
|
|
+ unsigned int j;
|
|
|
+ for(j=0;j<xvectorcla.size();j++)
|
|
|
+ {
|
|
|
+ xvectorclassname.push_back(xvectorcla[j]);
|
|
|
+ xvectorname.push_back(xven[j]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ char strline[1000];
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+ WriteCodeLine("namespace iv",&xFileout);
|
|
|
+ WriteCodeLine("{",&xFileout);
|
|
|
+ WriteCodeLine("\tstruct protomsgtype",&xFileout);
|
|
|
+ WriteCodeLine("\t{",&xFileout);
|
|
|
+ WriteCodeLine("\t\tstd::string mstrtypename;",&xFileout);
|
|
|
+ WriteCodeLine("\t\t::google::protobuf::Message * mpmsg;",&xFileout);
|
|
|
+ WriteCodeLine("\t};",&xFileout);
|
|
|
+ WriteCodeLine("}",&xFileout);
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+
|
|
|
+
|
|
|
+ snprintf(strline,1000,"#define MSGCOUNT %d\n",xvectorclassname.size());xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"const int typecount = %d;\n",xvectorclassname.size());xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"iv::protomsgtype gmsgtype[MSGCOUNT];\n");xFileout.write(strline);
|
|
|
+
|
|
|
+// snprintf(strline,1000,"{\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"void InitMsgType()\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"{\n");xFileout.write(strline);
|
|
|
+ for(i=0;i<xvectorclassname.size();i++)
|
|
|
+ {
|
|
|
+
|
|
|
+ snprintf(strline,1000,"\tgmsgtype[%d].mstrtypename = \"%s\";\n",
|
|
|
+ i,xvectorclassname[i].data());
|
|
|
+ xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"\tgmsgtype[%d].mpmsg = (::google::protobuf::Message *)(new %s);\n",
|
|
|
+ i,xvectorclassname[i].data());
|
|
|
+ xFileout.write(strline);
|
|
|
+ }
|
|
|
+ snprintf(strline,1000,"}\n");xFileout.write(strline);
|
|
|
+
|
|
|
+ snprintf(strline,1000,"\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"std::shared_ptr<::google::protobuf::Message > GetProtoMessage(char * strdata,int nsize,unsigned int protoindex)\n");
|
|
|
+ xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"{\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"\tstd::shared_ptr<::google::protobuf::Message > msg_ptr = NULL;\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"\tif(protoindex >= MSGCOUNT)\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"\t{\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"\t\treturn msg_ptr;\n");xFileout.write(strline);
|
|
|
+ snprintf(strline,1000,"\t}\n");xFileout.write(strline);
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+
|
|
|
+ WriteCodeLine("\tmsg_ptr =std::shared_ptr<::google::protobuf::Message>(gmsgtype[protoindex].mpmsg->New(NULL));",&xFileout);
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+ WriteCodeLine("\tif(msg_ptr->ParseFromArray(strdata,nsize))",&xFileout);
|
|
|
+ WriteCodeLine("\t{",&xFileout);
|
|
|
+ WriteCodeLine("\treturn msg_ptr;",&xFileout);
|
|
|
+ WriteCodeLine("\t}",&xFileout);
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+ WriteCodeLine("\treturn NULL;",&xFileout);
|
|
|
+ WriteCodeLine("}",&xFileout);
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+
|
|
|
+
|
|
|
+ WriteCodeLine("std::vector<unsigned int> GetProbProtoIndex(char * strdata,int nsize,std::vector<std::string > & xvectorprobname)",&xFileout);
|
|
|
+ WriteCodeLine("{",&xFileout);
|
|
|
+ WriteCodeLine("\tstd::vector<unsigned int> xvectorrtn;",&xFileout);
|
|
|
+ WriteCodeLine("\txvectorrtn.clear();",&xFileout);
|
|
|
+ WriteCodeLine("\txvectorprobname.clear();",&xFileout);
|
|
|
+ WriteCodeLine("\tunsigned int i;",&xFileout);
|
|
|
+ WriteCodeLine("\tfor(i=0;i<MSGCOUNT;i++)",&xFileout);
|
|
|
+ WriteCodeLine("\t{",&xFileout);
|
|
|
+ WriteCodeLine("\t\tif(gmsgtype[i].mpmsg->ParseFromArray(strdata,nsize))",&xFileout);
|
|
|
+ WriteCodeLine("\t\t{",&xFileout);
|
|
|
+ WriteCodeLine("\t\t\txvectorrtn.push_back(i);",&xFileout);
|
|
|
+ WriteCodeLine("\t\t\txvectorprobname.push_back(gmsgtype[i].mstrtypename);",&xFileout);
|
|
|
+ WriteCodeLine("\t\t}",&xFileout);
|
|
|
+ WriteCodeLine("\t}",&xFileout);
|
|
|
+ WriteCodeLine("\treturn xvectorrtn;",&xFileout);
|
|
|
+ WriteCodeLine("}",&xFileout);
|
|
|
+ WriteCodeLine("",&xFileout);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// for(i=0;i<xvectorclassname.size();i++)
|
|
|
+// {
|
|
|
+
|
|
|
+// snprintf(strline,1000,"\t%s\t%s;\n",xvectorclassname[i].data(),xvectorname[i].data());
|
|
|
+// // qDebug("%s %s",xvectorclassname[i].data(),xvectorname[i].data());
|
|
|
+// xFileout.write(strline);
|
|
|
+// }
|
|
|
+
|
|
|
+ xFileout.close();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ WriteCodeLine("#ifndef IVPROTOIF_H",&xFileHeader);
|
|
|
+ WriteCodeLine("#define IVPROTOIF_H",&xFileHeader);
|
|
|
+ WriteCodeLine("",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <QtCore/qglobal.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/stubs/common.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/stubs/port.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/stubs/once.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/io/coded_stream.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/wire_format_lite_inl.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/descriptor.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/generated_message_reflection.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/reflection_ops.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("#include <google/protobuf/wire_format.h>",&xFileHeader);
|
|
|
+ WriteCodeLine("",&xFileHeader);
|
|
|
+ WriteCodeLine("#if defined(PROTOINTERFACE_LIBRARY)",&xFileHeader);
|
|
|
+ WriteCodeLine("# define PROTOINTERFACESHARED_EXPORT Q_DECL_EXPORT",&xFileHeader);
|
|
|
+ WriteCodeLine("#else",&xFileHeader);
|
|
|
+ WriteCodeLine("# define PROTOINTERFACESHARED_EXPORT Q_DECL_IMPORT",&xFileHeader);
|
|
|
+ WriteCodeLine("#endif",&xFileHeader);
|
|
|
+ WriteCodeLine("",&xFileHeader);
|
|
|
+ WriteCodeLine("",&xFileHeader);
|
|
|
+ WriteCodeLine("void PROTOINTERFACESHARED_EXPORT InitMsgType();",&xFileHeader);
|
|
|
+ WriteCodeLine("std::shared_ptr<::google::protobuf::Message > PROTOINTERFACESHARED_EXPORT GetProtoMessage(char * strdata,int nsize,unsigned int protoindex);",&xFileHeader);
|
|
|
+ WriteCodeLine("std::vector<unsigned int> PROTOINTERFACESHARED_EXPORT GetProbProtoIndex(char * strdata,int nsize,std::vector<std::string > & xvectorprobname);",&xFileHeader);
|
|
|
+
|
|
|
+ WriteCodeLine("#endif",&xFileHeader);
|
|
|
+ WriteCodeLine("",&xFileHeader);
|
|
|
+ xFileHeader.close();
|
|
|
+
|
|
|
+ WriteCodeLine("QT -= gui", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("TEMPLATE = lib", &xFilePro);
|
|
|
+ WriteCodeLine("DEFINES += PROTOINTERFACE_LIBRARY", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("CONFIG += c++11", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("SOURCES += ivprotoif.cpp \\", &xFilePro);
|
|
|
+ for(i=0;i<xvectorprotoname.size();i++)
|
|
|
+ {
|
|
|
+ char strtemp[1000];
|
|
|
+ if(i<(xvectorprotoname.size()-1))
|
|
|
+ {
|
|
|
+ snprintf(strtemp,1000,"\t%s/%s.pb.cc \\",mstrmsgtypepath.data(),
|
|
|
+ xvectorprotoname[i].data());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ snprintf(strtemp,1000,"\t%s/%s.pb.cc",mstrmsgtypepath.data(),
|
|
|
+ xvectorprotoname[i].data());
|
|
|
+ }
|
|
|
+ WriteCodeLine(strtemp,&xFilePro);
|
|
|
+ }
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("HEADERS += ivprotoif.h \\", &xFilePro);
|
|
|
+ for(i=0;i<xvectorprotoname.size();i++)
|
|
|
+ {
|
|
|
+ char strtemp[1000];
|
|
|
+ if(i<(xvectorprotoname.size()-1))
|
|
|
+ {
|
|
|
+ snprintf(strtemp,1000,"\t%s/%s.pb.h \\",mstrmsgtypepath.data(),
|
|
|
+ xvectorprotoname[i].data());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ snprintf(strtemp,1000,"\t%s/%s.pb.h ",mstrmsgtypepath.data(),
|
|
|
+ xvectorprotoname[i].data());
|
|
|
+ }
|
|
|
+ WriteCodeLine(strtemp,&xFilePro);
|
|
|
+ }
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("DEFINES += QT_DEPRECATED_WARNINGS", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("CONFIG += plugin", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ snprintf(strline,1000,"INCLUDEPATH += %s",mstrmsgtypepath.data());
|
|
|
+ WriteCodeLine(strline,&xFilePro);
|
|
|
+ WriteCodeLine("LIBS+= -lprotobuf", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+ WriteCodeLine("", &xFilePro);
|
|
|
+
|
|
|
+ xFilePro.close();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|