-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2json.cpp
107 lines (94 loc) · 3.08 KB
/
txt2json.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "txt2json.h"
#include "ui_txt2json.h"
#include <QFile>
#include <QTextStream>
#include <vector>
#include <iostream>
using namespace std;
txt2json::txt2json(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::txt2json)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(generate()));
}
txt2json::~txt2json()
{
delete ui;
}
txt2json::generate(){
QString name=ui->lineEdit->text();
QFile FileI(name+".txt");
QFile FileO(name+".json");
if(!FileI.open(QIODevice::ReadOnly | QIODevice::Text)){
cout<<">Fail to open I"<<endl;
return 0;
}
if(!FileO.open(QIODevice::WriteOnly | QIODevice::Text)){
cout<<">Fail to open O"<<endl;
return 0;
}
QTextStream streamI(&FileI);
QTextStream streamO(&FileO);
QString line = streamI.readLine();
streamO<<"{"<<endl;
streamO<<" \"description\" : \"\","<<endl;
streamO<<" \"encoding_version\" : \"0.1\","<<endl;
streamO<<" \"name\" : \""<<line<<"\","<<endl;
streamO<<" \"nodes\" : ["<<endl;
vector<QString> listI;
vector<QString> listO;
int k=0;
line = streamI.readLine();
while(!streamI.atEnd()){
listI.clear();
listO.clear();
cout<<line.toStdString()<<endl;
name = streamI.readLine();
cout<<name.toStdString()<<endl;
line = streamI.readLine();
cout<<line.toStdString()<<endl;
while(line!=""){
cout<<line.toStdString()<<endl;
if(line.at(line.size()-1)=='i')
listI.push_back(line);
else
listO.push_back(line);
line = streamI.readLine();
}
if(k)
streamO<<","<<endl;
streamO<<" {"<<endl;
streamO<<" \"cognitive_function\" : \"Attention\","<<endl;
streamO<<" \"name\" : \""<<name<<"\","<<endl;
streamO<<" \"ports\" : ["<<endl;
for(int i=0;i<listI.size();i++){
streamO<<" {"<<endl;
streamO<<" \"direction\" : \"in\","<<endl;
streamO<<" \"name\" : \""<<listI[i]<<"\""<<endl;
streamO<<" }";
if(i!=listI.size()-1||listO.size())
streamO<<",";
streamO<<endl;
}
for(int i=0;i<listO.size();i++){
streamO<<" {"<<endl;
streamO<<" \"direction\" : \"out\","<<endl;
streamO<<" \"name\" : \""<<listO[i]<<"\""<<endl;
streamO<<" }";
if(i!=listO.size()-1)
streamO<<",";
streamO<<endl;
}
streamO<<" ],"<<endl;
streamO<<" \"position\" : [ 604, -81 ],"<<endl;
streamO<<" \"uuid\" : \"1acc8787-72d6-4db3-a3d5-3770fbc7b4c"<<k<<"\""<<endl;
streamO<<" }";
k++;
}
streamO<<endl;
streamO<<"],"<<endl;
streamO<<"\"uuid\" : \"43cf8ea5-42ed-47f2-a119-831410ae14f9\","<<endl;
streamO<<"\"version\" : \"0.1\""<<endl;
streamO<<"}"<<endl;
}