-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDataReader.cpp
59 lines (45 loc) · 1.58 KB
/
MyDataReader.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
#include <QtGui>
#include "MyDataReader.h"
#include <QRegExp>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <iostream>
#include <fstream>
using namespace std;
// gets the file and gets the data from the website
MyDataReader::MyDataReader(string url, ifstream& infile){
this->in = &infile;
manager = new QNetworkAccessManager(this);
// signal to get the data from the website.
connect(manager, SIGNAL(finished(QNetworkReply *)),this, SLOT(replyFinished(QNetworkReply *)));
QString qurl = QString::fromStdString(url);
manager->get(QNetworkRequest(QUrl(qurl)));
}
// one of the most important functions of the project. gets the ids of the coins from the website.
void MyDataReader::replyFinished(QNetworkReply *reply) {
QString data = (QString) reply->readAll();
string token;
std::vector<std::string> ids = {};
while((this->in)->peek() != EOF) {
getline(*(this->in), token);
// regex part if symbol is given.
string pat = "([a-zA-Z\\-\\d]+)\",\"symbol\":\"" + token +"\"";
QString pattern = QString::fromStdString(pat);
QRegExp rx(pattern);
int pos = 0;
if(rx.indexIn(data, pos) != -1){
ids.push_back(rx.cap(1).toStdString());
continue;
}
// regex part if name is given.
string pat2 = "([a-zA-Z\\-\\d]+)\",\"symbol\":\"([a-zA-Z\\-\\d\\s]+)\",\"name\":\"" + token + "\"";
QString pattern2 = QString::fromStdString(pat2);
QRegExp rx2(pattern2);
if(rx2.indexIn(data, pos) != -1){
ids.push_back(rx2.cap(1).toStdString());
}
}
// done signal.
emit done(&ids);
}