Skip to content

Commit

Permalink
改动二进制文件
Browse files Browse the repository at this point in the history
  • Loading branch information
hu419087137 committed Sep 23, 2017
1 parent ccf898e commit 6ead5d1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
26 changes: 20 additions & 6 deletions ProtoBufTool/ProtobufThread.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ProtobufThread.h"


#include <ostream>
using namespace std;
ProtobufThread::ProtobufThread(QObject *parent) : BaseThread(parent)
{

Expand Down Expand Up @@ -60,6 +60,16 @@ void ProtobufThread::setMsgByteArray(const QByteArray &msgByteArray)
_msgByteArray = msgByteArray;
}

QString ProtobufThread::outBinaryFilePath() const
{
return _outBinaryFilePath;
}

void ProtobufThread::setOutBinaryFilePath(const QString &outBinaryFilePath)
{
_outBinaryFilePath = outBinaryFilePath;
}

QString ProtobufThread::jsonData() const
{
return _jsonData;
Expand All @@ -85,7 +95,7 @@ void ProtobufThread::run()

//---------
switch (this->threadType()) {
case 0://proto->json
case 0://proto::message->json
{
const google::protobuf::FileDescriptor* filedescriptor = importer.Import(protoFileName);
if (filedescriptor) {
Expand Down Expand Up @@ -124,7 +134,7 @@ void ProtobufThread::run()
}
}
break;
case 1://json->proto
case 1://json->proto::message
{
QString jsonStr;
if(_jsonIsFile){
Expand All @@ -148,8 +158,12 @@ void ProtobufThread::run()
google::protobuf::util::Status convertStatus = google::protobuf::util::JsonStringToMessage(jsonStr.toStdString(),msg);
if (convertStatus.ok()) {
qDebug()<<"convertStatus.ok()";
setMsgByteArray(QByteArray::fromStdString(msg->SerializeAsString()));

_outBinaryFilePath = "./outStream.message";
std::ofstream ostram(_outBinaryFilePath.toStdString(),std::ios::binary);
if(!msg->SerializeToOstream(&ostram)){
setLastError("导出地图错误: "+QString::fromStdString(msg->DebugString()));
}
// setMsgByteArray(QByteArray::fromStdString(msg->SerializeToOstream()));
}else {
setLastError("JsonStringToMessage error: "+ QString::fromStdString(convertStatus.ToString()));
}
Expand Down
4 changes: 4 additions & 0 deletions ProtoBufTool/ProtobufThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ class ProtobufThread : public BaseThread
QByteArray msgByteArray() const;
void setMsgByteArray(const QByteArray &msgByteArray);

QString outBinaryFilePath() const;
void setOutBinaryFilePath(const QString &outBinaryFilePath);

private:
QFileInfo _protoFileInfo;
QString _packageName;
QString _messageName;
QString _binaryMessageName;
QString _jsonData;
QString _outBinaryFilePath;
bool _jsonIsFile = false;
bool _prettyJSON = true;
QByteArray _msgByteArray;
Expand Down
31 changes: 16 additions & 15 deletions ProtoBufTool/ProtobufWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,24 @@ void ProtobufWidget::slotProtobufThreadFinish()
break;
case 1:
{
QString path = QFileDialog::getSaveFileName(this, tr("message"), QString("./untitled.message"), tr("message(*.message)"));
if (!path.isEmpty()) {
QFile file(path);
if(file.open(QIODevice::WriteOnly)){
if(!pProtobufThread->msgByteArray().isEmpty()){
// QString path = QFileDialog::getSaveFileName(this, tr("message"), QString("./untitled.message"), tr("message(*.message)"));
// if (!path.isEmpty()) {
// QFile file(path);
// if(file.open(QIODevice::WriteOnly)){
// if(!pProtobufThread->msgByteArray().isEmpty()){

file.write(pProtobufThread->msgByteArray());
// file.write(pProtobufThread->msgByteArray());

}else{
ui->label_status->setText(tr("message二进制文件数据为空."));
}
file.close();
}else{
ui->label_status->setText(QString("打开【%1】文件失败.").arg(path));
}
ui->label_status->setText(QString("已保存:%1").arg(path));
}
// }else{
// ui->label_status->setText(tr("message二进制文件数据为空."));
// }
// file.close();
// }else{
// ui->label_status->setText(QString("打开【%1】文件失败.").arg(path));
// }
// ui->label_status->setText(QString("已保存:%1").arg(path));
ui->label_status->setText(QString("已保存:%1").arg(pProtobufThread->outBinaryFilePath()));
// }
}
break;
default:
Expand Down
7 changes: 7 additions & 0 deletions ProtoBufTool/ProtobufWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>注:.proto文件中有import &quot;AAA/xxx.proto&quot;相对路径下可以索引(也可以把xxx.proto拷贝到当前路径下import改为-&gt; import&quot;xxx.proto&quot;转换)</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down
2 changes: 1 addition & 1 deletion SeerTCPTest.pro
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ HEADERS += SCTcpToolWidget.h \
FORMS += SCTcpToolWidget.ui \
ProtoBufTool/ProtobufWidget.ui

RC_FILE = ICO.rc
win32:RC_FILE = ICO.rc

0 comments on commit 6ead5d1

Please sign in to comment.