summaryrefslogtreecommitdiff
path: root/noncore/todayplugins/stockticker/libstocks/stocks.c
Unidiff
Diffstat (limited to 'noncore/todayplugins/stockticker/libstocks/stocks.c') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/todayplugins/stockticker/libstocks/stocks.c347
1 files changed, 347 insertions, 0 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/stocks.c b/noncore/todayplugins/stockticker/libstocks/stocks.c
new file mode 100644
index 0000000..eb04ba9
--- a/dev/null
+++ b/noncore/todayplugins/stockticker/libstocks/stocks.c
@@ -0,0 +1,347 @@
1/* libstocks - Library to get current stock quotes from Yahoo Finance
2 *
3 * Copyright (C) 2000 Eric Laeuffer
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21#define __STOCKS_C__
22
23#include <stdio.h>
24#include <string.h>
25#include <malloc.h>
26#include <stdlib.h>
27
28#ifdef __WINDOWS__
29#include <mbstring.h>
30#endif
31
32#include "http.h"
33#include "csv.h"
34
35#include "stocks.h"
36
37/*
38s = symbol
39n = name
40l1 = last trade
41d1 = date
42t1 = time
43c1 = variation
44o = open
45h = higher price
46g = lower price
47v = volume
48*/
49
50const char yahoo_us_stocks_server[]="finance.yahoo.com";
51const char yahoo_eu_stocks_server[]="finance.yahoo.com";
52//const char yahoo_eu_stocks_server[]="fr.finance.yahoo.com";
53
54const char yahoo_url_beg[]="/d/quotes.csv?s=";
55const char yahoo_url_end[]="&f=snl1d1t1c1ohgv&e=.csv";
56
57typedef enum {
58 YAHOO_EUROPE,
59 YAHOO_US
60} yahoo_source;
61
62#define YAHOO_US_EXT_NB 11
63const char *yahoo_us_ext[YAHOO_US_EXT_NB] =
64{
65 ".US", /* United States */
66 ".TO", /* Canada */
67 ".M", /* Canada */
68 ".V", /* Canada */
69 ".AL", /* Canada */
70 ".MX", /* Mexico */
71 ".SA", /* Brasil */
72 ".BA", /* Argentina */
73 ".CR", /* Venezuela */
74 ".SN", /* Chili */
75 ".AX" /* Australia */
76};
77
78/*****************************************************************************/
79/* Finds, according to the symbol extension, the http source of the quotes. */
80/* Actually just finance.yahoo.com and fr.finance.yahoo.com are supported. */
81/* The function returns the country source (US or EUROPE). */
82/*****************************************************************************/
83yahoo_source find_yahoo_source(char *symbol)
84{
85 char *ptr;
86 int i;
87 int test;
88
89 ptr = strrchr(symbol, '.');
90
91 /* If no extension we suppose it is a US stock */
92 if (!ptr) return YAHOO_US;
93
94 /* extension is found */
95
96 /* Test if it is canadian stock */
97 for (i=0; i<YAHOO_US_EXT_NB; i++)
98 {
99
100#ifdef __UNIX__
101 test = strcasecmp(yahoo_us_ext[i], ptr);
102#elif __WINDOWS__
103 test = _mbsnbicmp(yahoo_us_ext[i], ptr, strlen(yahoo_us_ext[i]));
104#endif
105
106 if (!test) return YAHOO_US;
107 }
108
109 /* We suppose now it is a European stock */
110 return YAHOO_EUROPE;
111}
112
113/*****************************************************************************/
114/* Gets quotes csv file, parses the file and create the quotes list */
115/* *stocks points to the stocks to fetch */
116/* *stock_datas points to the beginning of the list */
117/* count allows to connect to all country servers */
118/*****************************************************************************/
119libstocks_return_code download_stocks(char *stocks,
120 stock **stock_datas,
121 yahoo_source source)
122{
123 char *stocks_server=NULL;
124 char *url_beg=NULL;
125 char *url_end=NULL;
126
127 char *url;
128 char *data;
129
130 libstocks_return_code error;
131
132#ifdef DEBUG
133 printf("*download_stocks\n");
134#endif
135
136 switch (source)
137 {
138 case YAHOO_US:
139 stocks_server = (char *)yahoo_us_stocks_server;
140 break;
141
142 case YAHOO_EUROPE:
143 stocks_server = (char *)yahoo_eu_stocks_server;
144 break;
145 default:
146 stocks_server = (char *)yahoo_us_stocks_server;
147 break;
148
149 }
150
151 url_beg = (char *)yahoo_url_beg;
152 url_end = (char *)yahoo_url_end;
153
154 url = (char *)malloc(strlen(url_beg)
155 +strlen(url_end)
156 +strlen(stocks)+1);
157 if (url==NULL)
158 {
159 fprintf(stderr,"Memory allocating error (%s line %d)\n"
160 ,__FILE__, __LINE__);
161 exit(1);
162 }
163
164 strcpy(url, url_beg);
165 strcat(url, stocks);
166 strcat(url, url_end);
167
168 error=http_get(url, stocks_server, &data);
169
170 free(url);
171
172 if (error) return error;
173
174 *stock_datas = parse_csv_file(data);
175
176 free(data);
177
178 if (!(*stock_datas)) return ERRPCSV;
179
180 return 0;
181
182}
183
184/*****************************************************************************/
185/* Gets quotes from yahoo */
186/* Choses to fetch European or US depending on the symbol extension */
187/* and merges the two lists to one */
188/* *stocks points to the stocks to fetch */
189/* *stock_datas points to the beginning of the list */
190/*****************************************************************************/
191libstocks_return_code get_stocks(const char *stocks, stock **stock_datas)
192{
193 char *tok_ptr;
194 char *eu_quotes=NULL;
195 char *eu_quotes_temp=NULL;
196 int lgr_eu_quotes=0;
197
198 char *us_quotes=NULL;
199 char *us_quotes_temp=NULL;
200 int lgr_us_quotes=0;
201
202 char *symbol;
203
204 yahoo_source source;
205
206 int lgr_symbol=0;
207
208 libstocks_return_code error;
209
210 stock *stocks_tmp=NULL;
211 stock *stocks_tmp2=NULL;
212 stock *stocks_getted=NULL;
213 stock *last_stock=NULL;
214
215#ifdef DEBUG
216 printf("*get_stocks\n");
217#endif
218
219 /* to preserve stocks */
220 tok_ptr = malloc(strlen(stocks)+1);
221 if(tok_ptr==NULL)
222 {
223 fprintf(stderr,"Memory allocating error (%s line %d)\n"
224 ,__FILE__, __LINE__);
225 exit(1);
226 }
227 strcpy(tok_ptr, stocks);
228
229 while( (symbol = strtok(tok_ptr, "+"))!=0)
230 {
231 /* clear tok_ptr for next strtok */
232 tok_ptr = NULL;
233
234 /* look for "." in the symbol */
235 source = find_yahoo_source(symbol);
236
237 switch (source)
238 {
239 case YAHOO_US:
240
241 if (us_quotes)
242 {
243 lgr_us_quotes = strlen(us_quotes);
244 lgr_symbol = strlen(symbol);
245
246 us_quotes_temp = malloc(lgr_us_quotes + lgr_symbol +2);
247 if(us_quotes_temp==NULL)
248 {
249 fprintf(stderr,"Memory allocating error (%s line %d)\n"
250 ,__FILE__, __LINE__);
251 exit(1);
252 }
253 strcpy(us_quotes_temp, us_quotes);
254 strcat(us_quotes_temp,"+");
255 strcat(us_quotes_temp,symbol);
256
257 free(us_quotes);
258 us_quotes = us_quotes_temp;
259 }
260 else
261 {
262 us_quotes = malloc(strlen(symbol)+1);
263
264 if(us_quotes==NULL)
265 {
266 fprintf(stderr,"Memory allocating error (%s line %d)\n"
267 ,__FILE__, __LINE__);
268 exit(1);
269 }
270 strcpy(us_quotes, symbol);
271 }
272
273 break;
274
275 case YAHOO_EUROPE:
276
277 if (eu_quotes)
278 {
279 lgr_eu_quotes = strlen(eu_quotes);
280 lgr_symbol = strlen(symbol);
281
282 eu_quotes_temp = malloc(lgr_eu_quotes + lgr_symbol +2);
283 if(eu_quotes_temp==NULL)
284 {
285 fprintf(stderr,"Memory allocating error (%s line %d)\n"
286 ,__FILE__, __LINE__);
287 exit(1);
288 }
289 strcpy(eu_quotes_temp, eu_quotes);
290 strcat(eu_quotes_temp, "+");
291 strcat(eu_quotes_temp, symbol);
292
293 free(eu_quotes);
294 eu_quotes = eu_quotes_temp;
295 }
296 else
297 {
298 eu_quotes = malloc(strlen(symbol)+1);
299 if(eu_quotes==NULL)
300 {
301 fprintf(stderr,"Memory allocating error (%s line %d)\n"
302 ,__FILE__, __LINE__);
303 exit(1);
304 }
305 strcpy(eu_quotes, symbol);
306 }
307 break;
308 }
309 }
310
311 free(tok_ptr);
312
313 if (us_quotes)
314 {
315 /* Gets us quotes */
316 error = download_stocks(us_quotes, &stocks_tmp, YAHOO_US);
317 if (error) return error;
318 }
319
320 if (eu_quotes)
321 {
322 /* Gets european quotes */
323 error = download_stocks(eu_quotes, &stocks_getted, YAHOO_EUROPE);
324 if (error) return error;
325
326 /* concats lists if needed */
327 if (stocks_tmp)
328 {
329 stocks_tmp2 = stocks_tmp;
330
331 while(stocks_tmp2 != NULL)
332 {
333 last_stock = stocks_tmp2;
334 stocks_tmp2 = next_stock(stocks_tmp2);
335 }
336
337 last_stock->NextStock = stocks_getted;
338 stocks_getted->PreviousStock = last_stock;
339
340 }
341 else (stocks_tmp = stocks_getted);
342 }
343
344 *stock_datas = stocks_tmp;
345
346 return(0);
347}