Diffstat (limited to 'noncore/todayplugins/stockticker/libstocks') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/csv.c | 140 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/currency.c | 1 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/lists.h | 1 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/stocks.c | 189 |
4 files changed, 204 insertions, 127 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/csv.c b/noncore/todayplugins/stockticker/libstocks/csv.c index 86d8607..110df7c 100644 --- a/noncore/todayplugins/stockticker/libstocks/csv.c +++ b/noncore/todayplugins/stockticker/libstocks/csv.c | |||
@@ -120,155 +120,199 @@ stock *parse_csv_file(char *csv) | |||
120 | { | 120 | { |
121 | *end_line = 0; | 121 | *end_line = 0; |
122 | 122 | ||
123 | /* Check if symbol valid */ | 123 | /* Check if symbol valid */ |
124 | /* if 1 "N/A" then ok because Indices have N/A for volume */ | 124 | /* if 1 "N/A" then ok because Indices have N/A for volume */ |
125 | /* if 4 "N/A" then ok because Mutual funds have */ | 125 | /* if 4 "N/A" then ok because Mutual funds have */ |
126 | /* if 5 "N/A" then ok because currencies have */ | 126 | /* if 5 "N/A" then ok because currencies have */ |
127 | /* So if >5 then stock not valid */ | 127 | /* So if >5 then stock not valid */ |
128 | 128 | ||
129 | test = line; | 129 | test = line; |
130 | valid = 0; | 130 | valid = 0; |
131 | while ( (test = strstr(test, "N/A")) ) | 131 | while ( (test = strstr(test, "N/A")) ) |
132 | { | 132 | { |
133 | valid ++; | 133 | valid ++; |
134 | test = test +3; | 134 | test = test +3; |
135 | } | 135 | } |
136 | 136 | ||
137 | if (valid < 6) | 137 | if (valid < 6) |
138 | { | 138 | { |
139 | /* This Symbol is valid */ | 139 | /* This Symbol is valid */ |
140 | 140 | ||
141 | StockPtr = malloc_stock(); | 141 | StockPtr = malloc_stock(); |
142 | 142 | ||
143 | ptr = csv_strtok(line, ","); | 143 | ptr = csv_strtok(line, ","); |
144 | if (!ptr) return 0; | 144 | if (!ptr) |
145 | { | ||
146 | free_stock(StockPtr); | ||
147 | return 0; | ||
148 | } | ||
145 | 149 | ||
146 | symbol = (char *)malloc(strlen(ptr)+1); | 150 | symbol = (char *)malloc(strlen(ptr)+1); |
147 | if (symbol==NULL) | 151 | if (symbol==NULL) |
148 | { | 152 | { |
149 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 153 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
150 | ,__FILE__, __LINE__); | 154 | ,__FILE__, __LINE__); |
151 | exit(1); | 155 | exit(1); |
152 | } | 156 | } |
153 | strcpy((char *)(symbol), ptr); | 157 | strcpy((char *)(symbol), ptr); |
154 | StockPtr->Symbol = symbol; | 158 | StockPtr->Symbol = symbol; |
155 | 159 | ||
156 | ptr = csv_strtok(NULL, ","); | 160 | ptr = csv_strtok(NULL, ","); |
157 | if (!ptr) return 0; | 161 | if (!ptr) |
162 | { | ||
163 | free_stock(StockPtr); | ||
164 | return 0; | ||
165 | } | ||
158 | 166 | ||
159 | name = (char *)malloc(strlen(ptr)+1); | 167 | name = (char *)malloc(strlen(ptr)+1); |
160 | if (name==NULL) | 168 | if (name==NULL) |
161 | { | 169 | { |
162 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 170 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
163 | ,__FILE__, __LINE__); | 171 | ,__FILE__, __LINE__); |
164 | exit(1); | 172 | exit(1); |
165 | } | 173 | } |
166 | strcpy((char *)(name), ptr); | 174 | strcpy((char *)(name), ptr); |
167 | StockPtr->Name = name; | 175 | StockPtr->Name = name; |
168 | 176 | ||
169 | ptr = csv_strtok(NULL, ","); | 177 | ptr = csv_strtok(NULL, ","); |
170 | if (!ptr) return 0; | 178 | if (!ptr) |
179 | { | ||
180 | free_stock(StockPtr); | ||
181 | return 0; | ||
182 | } | ||
171 | sscanf(ptr,"%f",&(StockPtr->CurrentPrice)); | 183 | sscanf(ptr,"%f",&(StockPtr->CurrentPrice)); |
172 | 184 | ||
173 | ptr = csv_strtok(NULL, ","); | 185 | ptr = csv_strtok(NULL, ","); |
174 | if (!ptr) return 0; | 186 | if (!ptr) |
187 | { | ||
188 | free_stock(StockPtr); | ||
189 | return 0; | ||
190 | } | ||
175 | 191 | ||
176 | date = (char *)malloc(strlen(ptr)+1); | 192 | date = (char *)malloc(strlen(ptr)+1); |
177 | if (date==NULL) | 193 | if (date==NULL) |
178 | { | 194 | { |
179 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 195 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
180 | ,__FILE__, __LINE__); | 196 | ,__FILE__, __LINE__); |
181 | exit(1); | 197 | exit(1); |
182 | } | 198 | } |
183 | strcpy((char *)(date), ptr); | 199 | strcpy((char *)(date), ptr); |
184 | StockPtr->Date = date; | 200 | StockPtr->Date = date; |
185 | 201 | ||
186 | ptr = csv_strtok(NULL, ","); | 202 | ptr = csv_strtok(NULL, ","); |
187 | if (!ptr) return 0; | 203 | if (!ptr) |
204 | { | ||
205 | free_stock(StockPtr); | ||
206 | return 0; | ||
207 | } | ||
188 | 208 | ||
189 | time = (char *)malloc(strlen(ptr)+1); | 209 | time = (char *)malloc(strlen(ptr)+1); |
190 | if (time==NULL) | 210 | if (time==NULL) |
191 | { | 211 | { |
192 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 212 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
193 | ,__FILE__, __LINE__); | 213 | ,__FILE__, __LINE__); |
194 | exit(1); | 214 | exit(1); |
195 | } | 215 | } |
196 | strcpy((char *)(time), ptr); | 216 | strcpy((char *)(time), ptr); |
197 | StockPtr->Time = time; | 217 | StockPtr->Time = time; |
198 | 218 | ||
199 | ptr = csv_strtok(NULL, ","); | 219 | ptr = csv_strtok(NULL, ","); |
200 | if (!ptr) return 0; | 220 | if (!ptr) |
221 | { | ||
222 | free_stock(StockPtr); | ||
223 | return 0; | ||
224 | } | ||
201 | sscanf(ptr,"%f",&(StockPtr->Variation)); | 225 | sscanf(ptr,"%f",&(StockPtr->Variation)); |
202 | 226 | ||
203 | StockPtr->Pourcentage = 100 * StockPtr->Variation / | 227 | StockPtr->Pourcentage = 100 * StockPtr->Variation / |
204 | (StockPtr->CurrentPrice - StockPtr->Variation); | 228 | (StockPtr->CurrentPrice - StockPtr->Variation); |
205 | 229 | ||
206 | StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation; | 230 | StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation; |
207 | 231 | ||
208 | ptr = csv_strtok(NULL, ","); | 232 | ptr = csv_strtok(NULL, ","); |
209 | if (!ptr) return 0; | 233 | if (!ptr) |
234 | { | ||
235 | free_stock(StockPtr); | ||
236 | return 0; | ||
237 | } | ||
210 | sscanf(ptr,"%f",&(StockPtr->OpenPrice)); | 238 | sscanf(ptr,"%f",&(StockPtr->OpenPrice)); |
211 | 239 | ||
212 | ptr = csv_strtok(NULL, ","); | 240 | ptr = csv_strtok(NULL, ","); |
213 | if (!ptr) return 0; | 241 | if (!ptr) |
242 | { | ||
243 | free_stock(StockPtr); | ||
244 | return 0; | ||
245 | } | ||
214 | sscanf(ptr,"%f",&(StockPtr->MaxPrice)); | 246 | sscanf(ptr,"%f",&(StockPtr->MaxPrice)); |
215 | 247 | ||
216 | ptr = csv_strtok(NULL, ","); | 248 | ptr = csv_strtok(NULL, ","); |
217 | if (!ptr) return 0; | 249 | if (!ptr) |
250 | { | ||
251 | free_stock(StockPtr); | ||
252 | return 0; | ||
253 | } | ||
218 | sscanf(ptr,"%f",&(StockPtr->MinPrice)); | 254 | sscanf(ptr,"%f",&(StockPtr->MinPrice)); |
219 | 255 | ||
220 | ptr = csv_strtok(NULL, ","); | 256 | ptr = csv_strtok(NULL, ","); |
221 | if (!ptr) return 0; | 257 | if (!ptr) |
258 | { | ||
259 | free_stock(StockPtr); | ||
260 | return 0; | ||
261 | } | ||
222 | StockPtr->Volume = atoi(ptr); | 262 | StockPtr->Volume = atoi(ptr); |
223 | 263 | ||
224 | if( !FirstStockPtr ) | 264 | if( !FirstStockPtr ) |
225 | { | 265 | { |
226 | FirstStockPtr = StockPtr; | 266 | FirstStockPtr = StockPtr; |
227 | StockPtr->PreviousStock = 0; | 267 | StockPtr->PreviousStock = 0; |
228 | } | 268 | } |
229 | 269 | ||
230 | StockPtr->NextStock = 0; | 270 | StockPtr->NextStock = 0; |
231 | 271 | ||
232 | if (LastStockPtr) | 272 | if (LastStockPtr) |
233 | { | 273 | { |
234 | LastStockPtr->NextStock = StockPtr; | 274 | LastStockPtr->NextStock = StockPtr; |
235 | StockPtr->PreviousStock = LastStockPtr; | 275 | StockPtr->PreviousStock = LastStockPtr; |
236 | } | 276 | } |
237 | 277 | ||
238 | LastStockPtr = StockPtr; | 278 | LastStockPtr = StockPtr; |
239 | 279 | ||
240 | } | 280 | } |
241 | else | 281 | else |
242 | { | 282 | { |
243 | /* this symbol is not valid */ | 283 | /* this symbol is not valid */ |
244 | /* Set the stock struct just with Symbol, all other are NULL */ | 284 | /* Set the stock struct just with Symbol, all other are NULL */ |
245 | /* This can be used to see if the symbol has been reached are not */ | 285 | /* This can be used to see if the symbol has been reached are not */ |
246 | 286 | ||
247 | StockPtr = malloc_stock(); | 287 | StockPtr = malloc_stock(); |
248 | 288 | ||
249 | ptr = csv_strtok(line, ","); | 289 | ptr = csv_strtok(line, ","); |
250 | if (!ptr) return 0; | 290 | if (!ptr) |
291 | { | ||
292 | free_stock(StockPtr); | ||
293 | return 0; | ||
294 | } | ||
251 | 295 | ||
252 | symbol = (char *)malloc(strlen(ptr)+1); | 296 | symbol = (char *)malloc(strlen(ptr)+1); |
253 | if (symbol==NULL) | 297 | if (symbol==NULL) |
254 | { | 298 | { |
255 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 299 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
256 | ,__FILE__, __LINE__); | 300 | ,__FILE__, __LINE__); |
257 | exit(1); | 301 | exit(1); |
258 | } | 302 | } |
259 | strcpy((char *)(symbol), ptr); | 303 | strcpy((char *)(symbol), ptr); |
260 | StockPtr->Symbol = symbol; | 304 | StockPtr->Symbol = symbol; |
261 | 305 | ||
262 | if( !FirstStockPtr ) | 306 | if( !FirstStockPtr ) |
263 | { | 307 | { |
264 | FirstStockPtr = StockPtr; | 308 | FirstStockPtr = StockPtr; |
265 | StockPtr->PreviousStock = 0; | 309 | StockPtr->PreviousStock = 0; |
266 | } | 310 | } |
267 | 311 | ||
268 | StockPtr->NextStock = 0; | 312 | StockPtr->NextStock = 0; |
269 | 313 | ||
270 | if (LastStockPtr) | 314 | if (LastStockPtr) |
271 | { | 315 | { |
272 | LastStockPtr->NextStock = StockPtr; | 316 | LastStockPtr->NextStock = StockPtr; |
273 | StockPtr->PreviousStock = LastStockPtr; | 317 | StockPtr->PreviousStock = LastStockPtr; |
274 | } | 318 | } |
@@ -307,100 +351,130 @@ stock *parse_csv_history_file(char *csv_file) | |||
307 | stock *StockPtr=NULL; | 351 | stock *StockPtr=NULL; |
308 | stock *LastStockPtr=NULL; | 352 | stock *LastStockPtr=NULL; |
309 | 353 | ||
310 | /* Used to return the pointer to the list */ | 354 | /* Used to return the pointer to the list */ |
311 | stock *FirstStockPtr=NULL; | 355 | stock *FirstStockPtr=NULL; |
312 | 356 | ||
313 | line = csv_file; | 357 | line = csv_file; |
314 | end_line = csv_file; | 358 | end_line = csv_file; |
315 | 359 | ||
316 | /* do not use the first line */ | 360 | /* do not use the first line */ |
317 | if (!(end_line = strstr(line, "\n"))) | 361 | if (!(end_line = strstr(line, "\n"))) |
318 | return 0; | 362 | return 0; |
319 | *end_line = 0; | 363 | *end_line = 0; |
320 | end_line++; | 364 | end_line++; |
321 | line = end_line; | 365 | line = end_line; |
322 | 366 | ||
323 | while ((end_line = strstr(line, "\n"))) | 367 | while ((end_line = strstr(line, "\n"))) |
324 | { | 368 | { |
325 | *end_line = 0; | 369 | *end_line = 0; |
326 | 370 | ||
327 | StockPtr = malloc_stock(); | 371 | StockPtr = malloc_stock(); |
328 | 372 | ||
329 | /* Date */ | 373 | /* Date */ |
330 | ptr = strtok(line, ","); | 374 | ptr = strtok(line, ","); |
331 | if (!ptr) return 0; | 375 | if (!ptr) |
376 | { | ||
377 | free_stock(StockPtr); | ||
378 | free_stock(FirstStockPtr); | ||
379 | free_stock(LastStockPtr); | ||
380 | return 0; | ||
381 | } | ||
332 | 382 | ||
333 | sscanf(ptr,"%d-%3s-%d",&day,smonth,&year); | 383 | sscanf(ptr,"%d-%3s-%d",&day,smonth,&year); |
334 | 384 | ||
335 | i=0; | 385 | i=0; |
336 | 386 | ||
337 | #ifdef __UNIX__ | 387 | #ifdef __UNIX__ |
338 | while((test=strcasecmp(months[i], smonth))) i++; | 388 | while((test=strcasecmp(months[i], smonth))) i++; |
339 | #elif __WINDOWS__ | 389 | #elif __WINDOWS__ |
340 | while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++; | 390 | while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++; |
341 | #endif | 391 | #endif |
342 | 392 | ||
343 | month = i+1; | 393 | month = i+1; |
344 | 394 | ||
345 | date = (char *)malloc(DATE_LENGTH); | 395 | date = (char *)malloc(DATE_LENGTH); |
346 | if (date==NULL) | 396 | if (date==NULL) |
347 | { | 397 | { |
348 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 398 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
349 | ,__FILE__, __LINE__); | 399 | ,__FILE__, __LINE__); |
350 | exit(1); | 400 | exit(1); |
351 | } | 401 | } |
352 | sprintf(date,"%.2d%.2d%.2d", year, month, day); | 402 | sprintf(date,"%.2d%.2d%.2d", year, month, day); |
353 | StockPtr->Date = date; | 403 | StockPtr->Date = date; |
354 | 404 | ||
355 | /* Open */ | 405 | /* Open */ |
356 | ptr = strtok(NULL, ","); | 406 | ptr = strtok(NULL, ","); |
357 | if (!ptr) return 0; | 407 | if (!ptr) |
408 | { | ||
409 | free_stock(StockPtr); | ||
410 | free_stock(FirstStockPtr); | ||
411 | free_stock(LastStockPtr); | ||
412 | return 0; | ||
413 | } | ||
358 | sscanf(ptr,"%f",&(StockPtr->OpenPrice)); | 414 | sscanf(ptr,"%f",&(StockPtr->OpenPrice)); |
359 | 415 | ||
360 | /* High */ | 416 | /* High */ |
361 | ptr = strtok(NULL, ","); | 417 | ptr = strtok(NULL, ","); |
362 | if (!ptr) return 0; | 418 | if (!ptr) |
419 | { | ||
420 | free_stock(StockPtr); | ||
421 | free_stock(FirstStockPtr); | ||
422 | free_stock(LastStockPtr); | ||
423 | return 0; | ||
424 | } | ||
363 | sscanf(ptr,"%f",&(StockPtr->MaxPrice)); | 425 | sscanf(ptr,"%f",&(StockPtr->MaxPrice)); |
364 | 426 | ||
365 | /* Low */ | 427 | /* Low */ |
366 | ptr = strtok(NULL, ","); | 428 | ptr = strtok(NULL, ","); |
367 | if (!ptr) return 0; | 429 | if (!ptr) |
430 | { | ||
431 | free_stock(StockPtr); | ||
432 | free_stock(FirstStockPtr); | ||
433 | free_stock(LastStockPtr); | ||
434 | return 0; | ||
435 | } | ||
368 | sscanf(ptr,"%f",&(StockPtr->MinPrice)); | 436 | sscanf(ptr,"%f",&(StockPtr->MinPrice)); |
369 | 437 | ||
370 | /* Close */ | 438 | /* Close */ |
371 | ptr = strtok(NULL, ","); | 439 | ptr = strtok(NULL, ","); |
372 | if (!ptr) return 0; | 440 | if (!ptr) |
441 | { | ||
442 | free_stock(StockPtr); | ||
443 | free_stock(FirstStockPtr); | ||
444 | free_stock(LastStockPtr); | ||
445 | return 0; | ||
446 | } | ||
373 | sscanf(ptr,"%f",&(StockPtr->LastPrice)); | 447 | sscanf(ptr,"%f",&(StockPtr->LastPrice)); |
374 | 448 | ||
375 | /* Volume */ | 449 | /* Volume */ |
376 | 450 | ||
377 | ptr = strtok(NULL, ","); | 451 | ptr = strtok(NULL, ","); |
378 | if (!ptr) | 452 | if (!ptr) |
379 | /* It seems to be an indice */ | 453 | /* It seems to be an indice */ |
380 | /* No volume for indices */ | 454 | /* No volume for indices */ |
381 | StockPtr->Volume = 0; | 455 | StockPtr->Volume = 0; |
382 | else | 456 | else |
383 | StockPtr->Volume = atoi(ptr); | 457 | StockPtr->Volume = atoi(ptr); |
384 | 458 | ||
385 | if( !FirstStockPtr ) | 459 | if( !FirstStockPtr ) |
386 | { | 460 | { |
387 | FirstStockPtr = StockPtr; | 461 | FirstStockPtr = StockPtr; |
388 | StockPtr->PreviousStock = 0; | 462 | StockPtr->PreviousStock = 0; |
389 | } | 463 | } |
390 | 464 | ||
391 | StockPtr->NextStock = 0; | 465 | StockPtr->NextStock = 0; |
392 | 466 | ||
393 | if (LastStockPtr) | 467 | if (LastStockPtr) |
394 | { | 468 | { |
395 | LastStockPtr->NextStock = StockPtr; | 469 | LastStockPtr->NextStock = StockPtr; |
396 | StockPtr->PreviousStock = LastStockPtr; | 470 | StockPtr->PreviousStock = LastStockPtr; |
397 | } | 471 | } |
398 | 472 | ||
399 | LastStockPtr = StockPtr; | 473 | LastStockPtr = StockPtr; |
400 | 474 | ||
401 | end_line++; | 475 | end_line++; |
402 | line = end_line; | 476 | line = end_line; |
403 | } | 477 | } |
404 | 478 | ||
405 | return (FirstStockPtr); | 479 | return (FirstStockPtr); |
406 | } | 480 | } |
diff --git a/noncore/todayplugins/stockticker/libstocks/currency.c b/noncore/todayplugins/stockticker/libstocks/currency.c index 9a08a9d..e0090e2 100644 --- a/noncore/todayplugins/stockticker/libstocks/currency.c +++ b/noncore/todayplugins/stockticker/libstocks/currency.c | |||
@@ -31,36 +31,37 @@ | |||
31 | /*****************************************************************************/ | 31 | /*****************************************************************************/ |
32 | /* returns the currency exchange rate of "from" currency into */ | 32 | /* returns the currency exchange rate of "from" currency into */ |
33 | /* "into" currency. */ | 33 | /* "into" currency. */ |
34 | /*****************************************************************************/ | 34 | /*****************************************************************************/ |
35 | libstocks_return_code get_currency_exchange(char *from, | 35 | libstocks_return_code get_currency_exchange(char *from, |
36 | char *into, | 36 | char *into, |
37 | float *exchange) | 37 | float *exchange) |
38 | { | 38 | { |
39 | char *symbol; | 39 | char *symbol; |
40 | stock *data; | 40 | stock *data; |
41 | libstocks_return_code error; | 41 | libstocks_return_code error; |
42 | 42 | ||
43 | if((symbol = (char *)malloc(strlen(from)+strlen(into)+3))==NULL) | 43 | if((symbol = (char *)malloc(strlen(from)+strlen(into)+3))==NULL) |
44 | { | 44 | { |
45 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | 45 | fprintf(stderr,"Memory allocating error (%s line %d)\n" |
46 | ,__FILE__, __LINE__); | 46 | ,__FILE__, __LINE__); |
47 | exit(1); | 47 | exit(1); |
48 | } | 48 | } |
49 | 49 | ||
50 | strcpy(symbol, from); | 50 | strcpy(symbol, from); |
51 | strcat(symbol, into); | 51 | strcat(symbol, into); |
52 | strcat(symbol, "=X"); | 52 | strcat(symbol, "=X"); |
53 | 53 | ||
54 | error = get_stocks(symbol, &data); | 54 | error = get_stocks(symbol, &data); |
55 | free(symbol); | ||
55 | if (error) | 56 | if (error) |
56 | { | 57 | { |
57 | *exchange = 0; | 58 | *exchange = 0; |
58 | return(error); | 59 | return(error); |
59 | } | 60 | } |
60 | 61 | ||
61 | free_stocks(data); | 62 | free_stocks(data); |
62 | 63 | ||
63 | *exchange = data->CurrentPrice; | 64 | *exchange = data->CurrentPrice; |
64 | return(error); | 65 | return(error); |
65 | 66 | ||
66 | } | 67 | } |
diff --git a/noncore/todayplugins/stockticker/libstocks/lists.h b/noncore/todayplugins/stockticker/libstocks/lists.h index 0132317..a0eb434 100644 --- a/noncore/todayplugins/stockticker/libstocks/lists.h +++ b/noncore/todayplugins/stockticker/libstocks/lists.h | |||
@@ -9,27 +9,28 @@ | |||
9 | * | 9 | * |
10 | * This library is distributed in the hope that it will be useful, | 10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Library General Public License for more details. | 13 | * Library General Public License for more details. |
14 | * | 14 | * |
15 | * You should have received a copy of the GNU Library General Public | 15 | * You should have received a copy of the GNU Library General Public |
16 | * License along with this library; if not, write to the | 16 | * License along with this library; if not, write to the |
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | * Boston, MA 02111-1307, USA. | 18 | * Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #ifndef __LISTS_H__ | 21 | #ifndef __LISTS_H__ |
22 | #define __LISTS_H__ | 22 | #define __LISTS_H__ |
23 | 23 | ||
24 | #ifndef __LISTS_C__ | 24 | #ifndef __LISTS_C__ |
25 | #define PUBEXT_LISTS extern | 25 | #define PUBEXT_LISTS extern |
26 | #else | 26 | #else |
27 | #define PUBEXT_LISTS | 27 | #define PUBEXT_LISTS |
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #include "stocks.h" | 30 | #include "stocks.h" |
31 | 31 | ||
32 | PUBEXT_LISTS stock *malloc_stock(void); | 32 | PUBEXT_LISTS stock *malloc_stock(void); |
33 | PUBEXT_LISTS void free_stock(stock*); | ||
33 | 34 | ||
34 | 35 | ||
35 | #endif /* __LISTS_H */ | 36 | #endif /* __LISTS_H */ |
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 | |||
@@ -84,114 +84,114 @@ yahoo_source find_yahoo_source(char *symbol) | |||
84 | { | 84 | { |
85 | char *ptr; | 85 | char *ptr; |
86 | int i; | 86 | int i; |
87 | int test; | 87 | int test; |
88 | 88 | ||
89 | ptr = strrchr(symbol, '.'); | 89 | ptr = strrchr(symbol, '.'); |
90 | 90 | ||
91 | /* If no extension we suppose it is a US stock */ | 91 | /* If no extension we suppose it is a US stock */ |
92 | if (!ptr) return YAHOO_US; | 92 | if (!ptr) return YAHOO_US; |
93 | 93 | ||
94 | /* extension is found */ | 94 | /* extension is found */ |
95 | 95 | ||
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 | /*****************************************************************************/ |
119 | libstocks_return_code download_stocks(char *stocks, | 119 | libstocks_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 */ |
186 | /* Choses to fetch European or US depending on the symbol extension */ | 186 | /* Choses to fetch European or US depending on the symbol extension */ |
187 | /* and merges the two lists to one */ | 187 | /* and merges the two lists to one */ |
188 | /* *stocks points to the stocks to fetch */ | 188 | /* *stocks points to the stocks to fetch */ |
189 | /* *stock_datas points to the beginning of the list */ | 189 | /* *stock_datas points to the beginning of the list */ |
190 | /*****************************************************************************/ | 190 | /*****************************************************************************/ |
191 | libstocks_return_code get_stocks(const char *stocks, stock **stock_datas) | 191 | libstocks_return_code get_stocks(const char *stocks, stock **stock_datas) |
192 | { | 192 | { |
193 | char *tok_ptr; | 193 | char *tok_ptr; |
194 | char *eu_quotes=NULL; | 194 | char *eu_quotes=NULL; |
195 | char *eu_quotes_temp=NULL; | 195 | char *eu_quotes_temp=NULL; |
196 | int lgr_eu_quotes=0; | 196 | int lgr_eu_quotes=0; |
197 | 197 | ||
@@ -201,147 +201,148 @@ libstocks_return_code get_stocks(const char *stocks, stock **stock_datas) | |||
201 | 201 | ||
202 | char *symbol; | 202 | char *symbol; |
203 | 203 | ||
204 | yahoo_source source; | 204 | yahoo_source source; |
205 | 205 | ||
206 | int lgr_symbol=0; | 206 | int lgr_symbol=0; |
207 | 207 | ||
208 | libstocks_return_code error; | 208 | libstocks_return_code error; |
209 | 209 | ||
210 | stock *stocks_tmp=NULL; | 210 | stock *stocks_tmp=NULL; |
211 | stock *stocks_tmp2=NULL; | 211 | stock *stocks_tmp2=NULL; |
212 | stock *stocks_getted=NULL; | 212 | stock *stocks_getted=NULL; |
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 | } |