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.c189
1 files changed, 95 insertions, 94 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/stocks.c b/noncore/todayplugins/stockticker/libstocks/stocks.c
index eb04ba9..3a26a47 100644
--- a/noncore/todayplugins/stockticker/libstocks/stocks.c
+++ b/noncore/todayplugins/stockticker/libstocks/stocks.c
@@ -96,90 +96,90 @@ yahoo_source find_yahoo_source(char *symbol)
96 /* Test if it is canadian stock */ 96 /* Test if it is canadian stock */
97 for (i=0; i<YAHOO_US_EXT_NB; i++) 97 for (i=0; i<YAHOO_US_EXT_NB; i++)
98 { 98 {
99 99
100#ifdef __UNIX__ 100#ifdef __UNIX__
101 test = strcasecmp(yahoo_us_ext[i], ptr); 101 test = strcasecmp(yahoo_us_ext[i], ptr);
102#elif __WINDOWS__ 102#elif __WINDOWS__
103 test = _mbsnbicmp(yahoo_us_ext[i], ptr, strlen(yahoo_us_ext[i])); 103 test = _mbsnbicmp(yahoo_us_ext[i], ptr, strlen(yahoo_us_ext[i]));
104#endif 104#endif
105 105
106 if (!test) return YAHOO_US; 106 if (!test) return YAHOO_US;
107 } 107 }
108 108
109 /* We suppose now it is a European stock */ 109 /* We suppose now it is a European stock */
110 return YAHOO_EUROPE; 110 return YAHOO_EUROPE;
111} 111}
112 112
113/*****************************************************************************/ 113/*****************************************************************************/
114/* Gets quotes csv file, parses the file and create the quotes list */ 114/* Gets quotes csv file, parses the file and create the quotes list */
115/* *stocks points to the stocks to fetch */ 115/* *stocks points to the stocks to fetch */
116/* *stock_datas points to the beginning of the list */ 116/* *stock_datas points to the beginning of the list */
117/* count allows to connect to all country servers */ 117/* count allows to connect to all country servers */
118/*****************************************************************************/ 118/*****************************************************************************/
119libstocks_return_code download_stocks(char *stocks, 119libstocks_return_code download_stocks(char *stocks,
120 stock **stock_datas, 120 stock **stock_datas,
121 yahoo_source source) 121 yahoo_source source)
122{ 122{
123 char *stocks_server=NULL; 123 char *stocks_server=NULL;
124 char *url_beg=NULL; 124 char *url_beg=NULL;
125 char *url_end=NULL; 125 char *url_end=NULL;
126 126
127 char *url; 127 char *url;
128 char *data; 128 char *data;
129 129
130 libstocks_return_code error; 130 libstocks_return_code error;
131 131
132#ifdef DEBUG 132#ifdef DEBUG
133 printf("*download_stocks\n"); 133 printf("*download_stocks\n");
134#endif 134#endif
135 135
136 switch (source) 136 switch (source)
137 { 137 {
138 case YAHOO_US: 138 case YAHOO_US:
139 stocks_server = (char *)yahoo_us_stocks_server; 139 stocks_server = (char *)yahoo_us_stocks_server;
140 break; 140 break;
141 141
142 case YAHOO_EUROPE: 142 case YAHOO_EUROPE:
143 stocks_server = (char *)yahoo_eu_stocks_server; 143 stocks_server = (char *)yahoo_eu_stocks_server;
144 break; 144 break;
145 default: 145 default:
146 stocks_server = (char *)yahoo_us_stocks_server; 146 stocks_server = (char *)yahoo_us_stocks_server;
147 break; 147 break;
148 148
149 } 149 }
150 150
151 url_beg = (char *)yahoo_url_beg; 151 url_beg = (char *)yahoo_url_beg;
152 url_end = (char *)yahoo_url_end; 152 url_end = (char *)yahoo_url_end;
153 153
154 url = (char *)malloc(strlen(url_beg) 154 url = (char *)malloc(strlen(url_beg)
155 +strlen(url_end) 155 +strlen(url_end)
156 +strlen(stocks)+1); 156 +strlen(stocks)+1);
157 if (url==NULL) 157 if (url==NULL)
158 { 158 {
159 fprintf(stderr,"Memory allocating error (%s line %d)\n" 159 fprintf(stderr,"Memory allocating error (%s line %d)\n"
160 ,__FILE__, __LINE__); 160 ,__FILE__, __LINE__);
161 exit(1); 161 exit(1);
162 } 162 }
163 163
164 strcpy(url, url_beg); 164 strcpy(url, url_beg);
165 strcat(url, stocks); 165 strcat(url, stocks);
166 strcat(url, url_end); 166 strcat(url, url_end);
167 167
168 error=http_get(url, stocks_server, &data); 168 error=http_get(url, stocks_server, &data);
169 169
170 free(url); 170 free(url);
171 171
172 if (error) return error; 172 if (error) return error;
173 173
174 *stock_datas = parse_csv_file(data); 174 *stock_datas = parse_csv_file(data);
175 175
176 free(data); 176 free(data);
177 177
178 if (!(*stock_datas)) return ERRPCSV; 178 if (!(*stock_datas)) return ERRPCSV;
179 179
180 return 0; 180 return 0;
181 181
182} 182}
183 183
184/*****************************************************************************/ 184/*****************************************************************************/
185/* Gets quotes from yahoo */ 185/* Gets quotes from yahoo */
@@ -213,135 +213,136 @@ libstocks_return_code get_stocks(const char *stocks, stock **stock_datas)
213 stock *last_stock=NULL; 213 stock *last_stock=NULL;
214 214
215#ifdef DEBUG 215#ifdef DEBUG
216 printf("*get_stocks\n"); 216 printf("*get_stocks\n");
217#endif 217#endif
218 218
219 /* to preserve stocks */ 219 /* to preserve stocks */
220 tok_ptr = malloc(strlen(stocks)+1); 220 tok_ptr = malloc(strlen(stocks)+1);
221 if(tok_ptr==NULL) 221 if(tok_ptr==NULL)
222 { 222 {
223 fprintf(stderr,"Memory allocating error (%s line %d)\n" 223 fprintf(stderr,"Memory allocating error (%s line %d)\n"
224 ,__FILE__, __LINE__); 224 ,__FILE__, __LINE__);
225 exit(1); 225 exit(1);
226 } 226 }
227 strcpy(tok_ptr, stocks); 227 strcpy(tok_ptr, stocks);
228 228
229 while( (symbol = strtok(tok_ptr, "+"))!=0) 229 while( (symbol = strtok(tok_ptr, "+"))!=0)
230 { 230 {
231 /* clear tok_ptr for next strtok */ 231 /* clear tok_ptr for next strtok */
232 tok_ptr = NULL; 232 tok_ptr = NULL;
233 233
234 /* look for "." in the symbol */ 234 /* look for "." in the symbol */
235 source = find_yahoo_source(symbol); 235 source = find_yahoo_source(symbol);
236 236
237 switch (source) 237 switch (source)
238 { 238 {
239 case YAHOO_US: 239 case YAHOO_US:
240 240 if (us_quotes)
241 if (us_quotes) 241 {
242 { 242 lgr_us_quotes = strlen(us_quotes);
243 lgr_us_quotes = strlen(us_quotes); 243 lgr_symbol = strlen(symbol);
244 lgr_symbol = strlen(symbol); 244
245 245 us_quotes_temp = malloc(lgr_us_quotes + lgr_symbol +2);
246 us_quotes_temp = malloc(lgr_us_quotes + lgr_symbol +2); 246 if(us_quotes_temp==NULL)
247 if(us_quotes_temp==NULL) 247 {
248 { 248 fprintf(stderr,"Memory allocating error (%s line %d)\n",
249 fprintf(stderr,"Memory allocating error (%s line %d)\n" 249 __FILE__, __LINE__);
250 ,__FILE__, __LINE__); 250 exit(1);
251 exit(1); 251 }
252 } 252 strcpy(us_quotes_temp, us_quotes);
253 strcpy(us_quotes_temp, us_quotes); 253 strcat(us_quotes_temp,"+");
254 strcat(us_quotes_temp,"+"); 254 strcat(us_quotes_temp,symbol);
255 strcat(us_quotes_temp,symbol); 255
256 256 free(us_quotes);
257 free(us_quotes); 257 us_quotes = us_quotes_temp;
258 us_quotes = us_quotes_temp; 258 }
259 } 259 else
260 else 260 {
261 { 261 us_quotes = malloc(strlen(symbol)+1);
262 us_quotes = malloc(strlen(symbol)+1); 262
263 263 if(us_quotes==NULL)
264 if(us_quotes==NULL) 264 {
265 { 265 fprintf(stderr,"Memory allocating error (%s line %d)\n",
266 fprintf(stderr,"Memory allocating error (%s line %d)\n" 266 __FILE__, __LINE__);
267 ,__FILE__, __LINE__); 267 exit(1);
268 exit(1); 268 }
269 } 269 strcpy(us_quotes, symbol);
270 strcpy(us_quotes, symbol); 270 }
271 } 271
272 272 break;
273 break; 273
274 274 case YAHOO_EUROPE:
275 case YAHOO_EUROPE: 275 if (eu_quotes)
276 276 {
277 if (eu_quotes) 277 lgr_eu_quotes = strlen(eu_quotes);
278 { 278 lgr_symbol = strlen(symbol);
279 lgr_eu_quotes = strlen(eu_quotes); 279
280 lgr_symbol = strlen(symbol); 280 eu_quotes_temp = malloc(lgr_eu_quotes + lgr_symbol +2);
281 281 if(eu_quotes_temp==NULL)
282 eu_quotes_temp = malloc(lgr_eu_quotes + lgr_symbol +2); 282 {
283 if(eu_quotes_temp==NULL) 283 fprintf(stderr,"Memory allocating error (%s line %d)\n",
284 { 284 __FILE__, __LINE__);
285 fprintf(stderr,"Memory allocating error (%s line %d)\n" 285 exit(1);
286 ,__FILE__, __LINE__); 286 }
287 exit(1); 287 strcpy(eu_quotes_temp, eu_quotes);
288 } 288 strcat(eu_quotes_temp, "+");
289 strcpy(eu_quotes_temp, eu_quotes); 289 strcat(eu_quotes_temp, symbol);
290 strcat(eu_quotes_temp, "+"); 290
291 strcat(eu_quotes_temp, symbol); 291 free(eu_quotes);
292 292 eu_quotes = eu_quotes_temp;
293 free(eu_quotes); 293 }
294 eu_quotes = eu_quotes_temp; 294 else
295 } 295 {
296 else 296 eu_quotes = malloc(strlen(symbol)+1);
297 { 297 if(eu_quotes==NULL)
298 eu_quotes = malloc(strlen(symbol)+1); 298 {
299 if(eu_quotes==NULL) 299 fprintf(stderr,"Memory allocating error (%s line %d)\n",
300 { 300 __FILE__, __LINE__);
301 fprintf(stderr,"Memory allocating error (%s line %d)\n" 301 exit(1);
302 ,__FILE__, __LINE__); 302 }
303 exit(1); 303 strcpy(eu_quotes, symbol);
304 } 304 }
305 strcpy(eu_quotes, symbol); 305 break;
306 } 306 }
307 break;
308 }
309 } 307 }
310 308
311 free(tok_ptr); 309 free(tok_ptr);
312 310
313 if (us_quotes) 311 if (us_quotes)
314 { 312 {
315 /* Gets us quotes */ 313 /* Gets us quotes */
316 error = download_stocks(us_quotes, &stocks_tmp, YAHOO_US); 314 error = download_stocks(us_quotes, &stocks_tmp, YAHOO_US);
315 free(us_quotes);
317 if (error) return error; 316 if (error) return error;
318 } 317 }
319 318
320 if (eu_quotes) 319 if (eu_quotes)
321 { 320 {
322 /* Gets european quotes */ 321 /* Gets european quotes */
323 error = download_stocks(eu_quotes, &stocks_getted, YAHOO_EUROPE); 322 error = download_stocks(eu_quotes, &stocks_getted, YAHOO_EUROPE);
323 free(eu_quotes);
324 if (error) return error; 324 if (error) return error;
325 325
326 /* concats lists if needed */ 326 /* concats lists if needed */
327 if (stocks_tmp) 327 if (stocks_tmp)
328 { 328 {
329 stocks_tmp2 = stocks_tmp; 329 stocks_tmp2 = stocks_tmp;
330 330
331 while(stocks_tmp2 != NULL) 331 while(stocks_tmp2 != NULL)
332 { 332 {
333 last_stock = stocks_tmp2; 333 last_stock = stocks_tmp2;
334 stocks_tmp2 = next_stock(stocks_tmp2); 334 stocks_tmp2 = next_stock(stocks_tmp2);
335 } 335 }
336 336
337 last_stock->NextStock = stocks_getted; 337 last_stock->NextStock = stocks_getted;
338 stocks_getted->PreviousStock = last_stock; 338 stocks_getted->PreviousStock = last_stock;
339 339
340 } 340 }
341 else (stocks_tmp = stocks_getted); 341 else
342 (stocks_tmp = stocks_getted);
342 } 343 }
343 344
344 *stock_datas = stocks_tmp; 345 *stock_datas = stocks_tmp;
345 346
346 return(0); 347 return(0);
347} 348}