stock.cpp
6.21 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "jframe.h"
#include "jtextfield.h"
#include "jlabel.h"
#include "jstringtokenizer.h"
#include "jstringutils.h"
#include "jsocket.h"
#include "jsocketexception.h"
#include <sstream>
class Stock : public jgui::Frame{
private:
jgui::TextField *acao;
jgui::Label *ldata,
*lcotacao,
*lvariacao,
*lanterior,
*labertura,
*lminimo,
*lmaximo,
*lvolume,
*vdata,
*vcotacao,
*vvariacao,
*vanterior,
*vabertura,
*vminimo,
*vmaximo,
*vvolume;
public:
Stock(int x, int y):
jgui::Frame("Stock", x, y, 500, 400)
{
int px = 0,
py = 0,
pw = DEFAULT_COMPONENT_WIDTH,
pr = DEFAULT_COMPONENT_WIDTH,
ph = DEFAULT_COMPONENT_HEIGHT,
gap = 5;
acao = new jgui::TextField(px, py+0*(ph+gap), (pw+pr+gap), ph);
ldata = new jgui::Label("Data", px, py+1*(ph+gap), pw, ph);
lcotacao = new jgui::Label("Cotacao", px, py+2*(ph+gap), pw, ph);
lvariacao = new jgui::Label("Variacao", px, py+3*(ph+gap), pw, ph);
lanterior = new jgui::Label("Anterior", px, py+4*(ph+gap), pw, ph);
labertura = new jgui::Label("Abertura", px, py+5*(ph+gap), pw, ph);
lminimo = new jgui::Label("Minimo", px, py+6*(ph+gap), pw, ph);
lmaximo = new jgui::Label("Maximo", px, py+7*(ph+gap), pw, ph);
lvolume = new jgui::Label("Volume", px, py+8*(ph+gap), pw, ph);
vdata = new jgui::Label("--/--/--", px+(pw+gap), py+1*(ph+gap), pr, ph);
vcotacao = new jgui::Label("0.00", px+(pw+gap), py+2*(ph+gap), pr, ph);
vvariacao = new jgui::Label("0.00%", px+(pw+gap), py+3*(ph+gap), pr, ph);
vanterior = new jgui::Label("0.00", px+(pw+gap), py+4*(ph+gap), pr, ph);
vabertura = new jgui::Label("0.00", px+(pw+gap), py+5*(ph+gap), pr, ph);
vminimo = new jgui::Label("0.00", px+(pw+gap), py+6*(ph+gap), pr, ph);
vmaximo = new jgui::Label("0.00", px+(pw+gap), py+7*(ph+gap), pr, ph);
vvolume = new jgui::Label("0.00", px+(pw+gap), py+8*(ph+gap), pr, ph);
Add(acao);
Add(ldata);
Add(lcotacao);
Add(lvariacao);
Add(lanterior);
Add(labertura);
Add(lminimo);
Add(lmaximo);
Add(lvolume);
Add(vdata);
Add(vcotacao);
Add(vvariacao);
Add(vanterior);
Add(vabertura);
Add(vminimo);
Add(vmaximo);
Add(vvolume);
acao->RequestFocus();
Pack();
}
virtual ~Stock()
{
delete acao;
delete ldata;
delete lcotacao;
delete lvariacao;
delete lanterior;
delete labertura;
delete lminimo;
delete lmaximo;
delete lvolume;
delete vdata;
delete vcotacao;
delete vvariacao;
delete vanterior;
delete vabertura;
delete vminimo;
delete vmaximo;
delete vvolume;
}
std::map<std::string, std::string> RequestQuotes(std::string stock)
{
std::map<std::string, std::string> quotes;
std::ostringstream o;
char receive[4098];
int count = 0;
o << "GET /d/quotes.csv?s=" << acao->GetText() << "&f=snd1t1l1c1p2poghvt HTTP/1.0\r\n\r\n";
try {
jsocket::Socket c("download.finance.yahoo.com", 80);
c.Send((char *)o.str().c_str(), o.str().size());
do {
count = count + (int)c.Receive((receive+count), 4096);
} while (true);
} catch (...) {
}
receive[count] = '\0';
jcommon::StringTokenizer lines(std::string(receive), "\r\n\r\n", jcommon::JTT_STRING, false);
jcommon::StringTokenizer tokens(lines.GetToken(1), ",", jcommon::JTT_STRING, false);
quotes["nome"] = tokens.GetToken(1);
quotes["data"] = jcommon::StringUtils::ReplaceString(tokens.GetToken(2), "\"", "");
quotes["hora"] = tokens.GetToken(3);
quotes["cotacao"] = tokens.GetToken(4);
quotes["variacao"] = jcommon::StringUtils::ReplaceString(tokens.GetToken(6), "\"", "");
quotes["anterior"] = tokens.GetToken(7);
quotes["abertura"] = tokens.GetToken(8);
quotes["minimo"] = tokens.GetToken(9);
quotes["maximo"] = tokens.GetToken(10);
quotes["volume"] = tokens.GetToken(11);
return quotes;
}
virtual bool ProcessEvent(jgui::KeyEvent *event)
{
if (event->GetType() != jgui::JKT_PRESSED) {
return false;
}
if (event->GetSymbol() == jgui::JKS_ENTER && GetFocusOwner() == acao) {
std::map<std::string, std::string> quotes = RequestQuotes(acao->GetText() + ".sa");
vdata->SetText(quotes["data"]);
// vhora->SetText(quotes["hora"]);
vcotacao->SetText(quotes["cotacao"]);
vvariacao->SetText(quotes["variacao"]);
vanterior->SetText(quotes["anterior"]);
vabertura->SetText(quotes["abertura"]);
vminimo->SetText(quotes["minimo"]);
vmaximo->SetText(quotes["maximo"]);
vvolume->SetText(quotes["volume"]);
if (quotes["variacao"].find("-") != std::string::npos) {
vvariacao->SetForegroundColor(0xf0, 0x00, 0x00, 0xff);
} else {
vvariacao->SetForegroundColor(0x00, 0xf0, 0x00, 0xff);
}
}
return true;
}
};
int main()
{
Stock stock(100, 100);
stock.Show();
return 0;
}