checkVersion.cpp
3.32 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* \author Ezequiel Silva
* \ email eseveriano@gmail.com
* \date março 2016
*/
#include <time.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <IOSTREAM>
#include <windows.h>
#include <thread>
using namespace std;
HINSTANCE hDLL1;
char* pPathTempJson;
char* pPathVlibrasJson;
int codDownload = -1;
//Assinatura do método itd_downloadfile utilizado na dll
typedef int (WINAPI*download)(char * arg1, char * arg2);
download itd_downloadfile;
//ofstream versionFileCheck;
//usado para representar as versoes tanto da api quanto local
typedef struct Versions
{
char versionDicionario[7]={'0','0','0','0','0','0','\0'};
char versionPlayer[4]={'0','0','0','\0'};
} Version;
// usado para carregar os jsons de versoes
Version loadVersion(char* pPathTempJson)
{
Version version;
string line_in_str;
ifstream file_versionApi;
int i;
try
{
file_versionApi.open(pPathTempJson);
while ( getline (file_versionApi, line_in_str) )
{
if(strstr(line_in_str.c_str(), "playerVersion")){
i = 0;
for (char & c : line_in_str)
if(isdigit(c))
version.versionPlayer[i++]=c;
version.versionPlayer[i]='\0';
}
else if(strstr(line_in_str.c_str(), "dictionaryVersion")){
i = 0;
for (char & c : line_in_str)
if(isdigit(c))
version.versionDicionario[i++]=c;
version.versionDicionario[i]='\0';
}
}
file_versionApi.close();
}
catch (exception e)
{
cout<<"-1"<<endl;
}
return version;
}
void downloadFileJson(){
codDownload = itd_downloadfile("http://vlibras.lavid.ufpb.br/api/dicionario/0.0.0_0.0.0?type=json",pPathTempJson);
}
int main(){
//versionFileCheck.open ("c:\\VLibras\\update\\version\\version.txt");
pPathTempJson ="C:\\VLibras\\update\\download\\VLibrasVersionApi.json";
pPathVlibrasJson = "c:\\VLibras\\version.json";
//Carrega a dll
hDLL1 = LoadLibrary("c:\\VLibras\\update\\plugins\\itdownload.dll");
//Relaciona método local com o método implementado na dll
itd_downloadfile=(download)GetProcAddress((HMODULE)hDLL1, "itd_downloadfile");
if (hDLL1){
//faz download do json de versao
thread tDownloadFileJson(downloadFileJson);
clock_t time_end;
time_end = clock() + 3000 * CLOCKS_PER_SEC/1000;
while (clock() < time_end){
if(codDownload != -1)
break;
}
if(codDownload == -1 ){
cout<<"-1"<<endl;
goto fim;
}
tDownloadFileJson.join();
//verifica se existe os dois arquivos de versao
if(!ifstream(pPathTempJson) || !ifstream(pPathVlibrasJson) || (codDownload > 0 )){
cout<<"-1"<<endl;
goto fim;
}
Version versionApi = loadVersion(pPathTempJson);
Version versionLocal = loadVersion(pPathVlibrasJson);
// compara as versoes (return '0' para nao tem attualizacao, '1' para player e '2' para dicionario)
if(atoi(versionApi.versionPlayer)> atoi(versionLocal.versionPlayer))
cout<<"1"<<endl;
//versionFileCheck << "1";
else if(atoi(versionApi.versionDicionario)> atoi(versionLocal.versionDicionario))
cout<<"2"<<endl;
//versionFileCheck << "2";
else
cout<<"0"<<endl;
//versionFileCheck << "0";
}
else
cout<<"-1"<<endl;
//versionFileCheck << "-1";
fim:
//versionFileCheck.close();
//Libera dll
FreeLibrary((HMODULE)hDLL1);
return 1;
}