summaryrefslogtreecommitdiff
authorllornkcor <llornkcor>2002-10-31 20:33:18 (UTC)
committer llornkcor <llornkcor>2002-10-31 20:33:18 (UTC)
commit88310c7207b84c79dbb11ab3c3afeded1874c10b (patch) (unidiff)
tree6933dd40e2333854523e25edbefdf00b78145300
parent73b03f932dd43d684e67db1837bf506ee015f2eb (diff)
downloadopie-88310c7207b84c79dbb11ab3c3afeded1874c10b.zip
opie-88310c7207b84c79dbb11ab3c3afeded1874c10b.tar.gz
opie-88310c7207b84c79dbb11ab3c3afeded1874c10b.tar.bz2
fix
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/todayplugins/stockticker/libstocks/csv.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/csv.c b/noncore/todayplugins/stockticker/libstocks/csv.c
index 27bcce6..f45af52 100644
--- a/noncore/todayplugins/stockticker/libstocks/csv.c
+++ b/noncore/todayplugins/stockticker/libstocks/csv.c
@@ -1,403 +1,403 @@
1/* libstocks - Library to get current stock quotes from Yahoo Finance 1/* libstocks - Library to get current stock quotes from Yahoo Finance
2 * 2 *
3 * Copyright (C) 2000 Eric Laeuffer 3 * Copyright (C) 2000 Eric Laeuffer
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public 6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either 7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version. 8 * version 2 of the License, or (at your option) any later version.
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#define __CSV_C__ 21#define __CSV_C__
22#ifdef __UNIX__ 22#define __UNIX__
23 23
24#include <string.h> 24#include <string.h>
25#include <stdlib.h> 25#include <stdlib.h>
26#include <stdio.h> 26#include <stdio.h>
27 27
28#ifdef __WINDOWS__ 28#ifdef __WINDOWS__
29#include <mbstring.h> 29#include <mbstring.h>
30#endif 30#endif
31 31
32#include "csv.h" 32#include "csv.h"
33#include "stocks.h" 33#include "stocks.h"
34#include "lists.h" 34#include "lists.h"
35 35
36#define DATE_LENGTH 7 /*YYMMDD*/ 36#define DATE_LENGTH 7 /*YYMMDD*/
37 37
38const char *months[12]= 38const char *months[12]=
39{ 39{
40 "Jan", 40 "Jan",
41 "Feb", 41 "Feb",
42 "Mar", 42 "Mar",
43 "Apr", 43 "Apr",
44 "May", 44 "May",
45 "Jun", 45 "Jun",
46 "Jul", 46 "Jul",
47 "Aug", 47 "Aug",
48 "Sep", 48 "Sep",
49 "Oct", 49 "Oct",
50 "Nov", 50 "Nov",
51 "Dec" 51 "Dec"
52}; 52};
53 53
54/*****************************************************************************/ 54/*****************************************************************************/
55/* Replacement of the strtok function. This one forgets "delim" when it is */ 55/* Replacement of the strtok function. This one forgets "delim" when it is */
56/* between two commas. */ 56/* between two commas. */
57/* Thanks to Julio Lucas who has told me the bug and has proposed me a patch */ 57/* Thanks to Julio Lucas who has told me the bug and has proposed me a patch */
58/*****************************************************************************/ 58/*****************************************************************************/
59char *csv_strtok(char *s, char *delim) 59char *csv_strtok(char *s, char *delim)
60{ 60{
61 static char *next=NULL; 61 static char *next=NULL;
62 char *temp, *first; 62 char *temp, *first;
63 int comma=0; 63 int comma=0;
64 64
65 if (s!=NULL) first=s; 65 if (s!=NULL) first=s;
66 else first=next; 66 else first=next;
67 67
68 temp=first; 68 temp=first;
69 if (*temp=='\0') return NULL; 69 if (*temp=='\0') return NULL;
70 70
71 while (*temp!='\0' && ((*temp!=*delim) || comma)) 71 while (*temp!='\0' && ((*temp!=*delim) || comma))
72 { 72 {
73 if (*temp=='"') comma ^= 1; 73 if (*temp=='"') comma ^= 1;
74 temp++; 74 temp++;
75 } 75 }
76 76
77 if (*temp=='\0') next=temp; 77 if (*temp=='\0') next=temp;
78 else 78 else
79 { 79 {
80 *temp='\0'; 80 *temp='\0';
81 next=temp+1; 81 next=temp+1;
82 } 82 }
83 83
84 return first; 84 return first;
85} 85}
86 86
87/*****************************************************************************/ 87/*****************************************************************************/
88/* Parses the csv file and return a list of stocks structure. */ 88/* Parses the csv file and return a list of stocks structure. */
89/* *csv points on the csv file in memory */ 89/* *csv points on the csv file in memory */
90/* count defines the country, because csv depends on the country */ 90/* count defines the country, because csv depends on the country */
91/*****************************************************************************/ 91/*****************************************************************************/
92stock *parse_csv_file(char *csv) 92stock *parse_csv_file(char *csv)
93{ 93{
94 char *line; 94 char *line;
95 char *end_line; 95 char *end_line;
96 96
97 char *ptr; 97 char *ptr;
98 98
99 char *date; 99 char *date;
100 char *time; 100 char *time;
101 char *name; 101 char *name;
102 char *symbol; 102 char *symbol;
103 103
104 stock *StockPtr=NULL; 104 stock *StockPtr=NULL;
105 stock *LastStockPtr=NULL; 105 stock *LastStockPtr=NULL;
106 106
107 /* Used to return the pointer to the list */ 107 /* Used to return the pointer to the list */
108 stock *FirstStockPtr=NULL; 108 stock *FirstStockPtr=NULL;
109 109
110 /* used to see if symbol is valid */ 110 /* used to see if symbol is valid */
111 int valid; 111 int valid;
112 char *test; 112 char *test;
113 113
114 line = csv; 114 line = csv;
115 end_line = csv; 115 end_line = csv;
116 116
117 while ((end_line = strstr(line, "\n"))) 117 while ((end_line = strstr(line, "\n")))
118 { 118 {
119 *end_line = 0; 119 *end_line = 0;
120 120
121 /* Check if symbol valid */ 121 /* Check if symbol valid */
122 /* if 1 "N/A" then ok because Indices have N/A for volume */ 122 /* if 1 "N/A" then ok because Indices have N/A for volume */
123 /* if 4 "N/A" then ok because Mutual funds have */ 123 /* if 4 "N/A" then ok because Mutual funds have */
124 /* if 5 "N/A" then ok because currencies have */ 124 /* if 5 "N/A" then ok because currencies have */
125 /* So if >5 then stock not valid */ 125 /* So if >5 then stock not valid */
126 126
127 test = line; 127 test = line;
128 valid = 0; 128 valid = 0;
129 while ( (test = strstr(test, "N/A")) ) 129 while ( (test = strstr(test, "N/A")) )
130 { 130 {
131 valid ++; 131 valid ++;
132 test = test +3; 132 test = test +3;
133 } 133 }
134 134
135 if (valid < 6) 135 if (valid < 6)
136 { 136 {
137 /* This Symbol is valid */ 137 /* This Symbol is valid */
138 138
139 StockPtr = malloc_stock(); 139 StockPtr = malloc_stock();
140 140
141 ptr = csv_strtok(line, ","); 141 ptr = csv_strtok(line, ",");
142 if (!ptr) return 0; 142 if (!ptr) return 0;
143 143
144 symbol = (char *)malloc(strlen(ptr)+1); 144 symbol = (char *)malloc(strlen(ptr)+1);
145 if (symbol==NULL) 145 if (symbol==NULL)
146 { 146 {
147 fprintf(stderr,"Memory allocating error (%s line %d)\n" 147 fprintf(stderr,"Memory allocating error (%s line %d)\n"
148 ,__FILE__, __LINE__); 148 ,__FILE__, __LINE__);
149 exit(1); 149 exit(1);
150 } 150 }
151 strcpy((char *)(symbol), ptr); 151 strcpy((char *)(symbol), ptr);
152 StockPtr->Symbol = symbol; 152 StockPtr->Symbol = symbol;
153 153
154 ptr = csv_strtok(NULL, ","); 154 ptr = csv_strtok(NULL, ",");
155 if (!ptr) return 0; 155 if (!ptr) return 0;
156 156
157 name = (char *)malloc(strlen(ptr)+1); 157 name = (char *)malloc(strlen(ptr)+1);
158 if (name==NULL) 158 if (name==NULL)
159 { 159 {
160 fprintf(stderr,"Memory allocating error (%s line %d)\n" 160 fprintf(stderr,"Memory allocating error (%s line %d)\n"
161 ,__FILE__, __LINE__); 161 ,__FILE__, __LINE__);
162 exit(1); 162 exit(1);
163 } 163 }
164 strcpy((char *)(name), ptr); 164 strcpy((char *)(name), ptr);
165 StockPtr->Name = name; 165 StockPtr->Name = name;
166 166
167 ptr = csv_strtok(NULL, ","); 167 ptr = csv_strtok(NULL, ",");
168 if (!ptr) return 0; 168 if (!ptr) return 0;
169 sscanf(ptr,"%f",&(StockPtr->CurrentPrice)); 169 sscanf(ptr,"%f",&(StockPtr->CurrentPrice));
170 170
171 ptr = csv_strtok(NULL, ","); 171 ptr = csv_strtok(NULL, ",");
172 if (!ptr) return 0; 172 if (!ptr) return 0;
173 173
174 date = (char *)malloc(strlen(ptr)+1); 174 date = (char *)malloc(strlen(ptr)+1);
175 if (date==NULL) 175 if (date==NULL)
176 { 176 {
177 fprintf(stderr,"Memory allocating error (%s line %d)\n" 177 fprintf(stderr,"Memory allocating error (%s line %d)\n"
178 ,__FILE__, __LINE__); 178 ,__FILE__, __LINE__);
179 exit(1); 179 exit(1);
180 } 180 }
181 strcpy((char *)(date), ptr); 181 strcpy((char *)(date), ptr);
182 StockPtr->Date = date; 182 StockPtr->Date = date;
183 183
184 ptr = csv_strtok(NULL, ","); 184 ptr = csv_strtok(NULL, ",");
185 if (!ptr) return 0; 185 if (!ptr) return 0;
186 186
187 time = (char *)malloc(strlen(ptr)+1); 187 time = (char *)malloc(strlen(ptr)+1);
188 if (time==NULL) 188 if (time==NULL)
189 { 189 {
190 fprintf(stderr,"Memory allocating error (%s line %d)\n" 190 fprintf(stderr,"Memory allocating error (%s line %d)\n"
191 ,__FILE__, __LINE__); 191 ,__FILE__, __LINE__);
192 exit(1); 192 exit(1);
193 } 193 }
194 strcpy((char *)(time), ptr); 194 strcpy((char *)(time), ptr);
195 StockPtr->Time = time; 195 StockPtr->Time = time;
196 196
197 ptr = csv_strtok(NULL, ","); 197 ptr = csv_strtok(NULL, ",");
198 if (!ptr) return 0; 198 if (!ptr) return 0;
199 sscanf(ptr,"%f",&(StockPtr->Variation)); 199 sscanf(ptr,"%f",&(StockPtr->Variation));
200 200
201 StockPtr->Pourcentage = 100 * StockPtr->Variation / 201 StockPtr->Pourcentage = 100 * StockPtr->Variation /
202 (StockPtr->CurrentPrice - StockPtr->Variation); 202 (StockPtr->CurrentPrice - StockPtr->Variation);
203 203
204 StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation; 204 StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation;
205 205
206 ptr = csv_strtok(NULL, ","); 206 ptr = csv_strtok(NULL, ",");
207 if (!ptr) return 0; 207 if (!ptr) return 0;
208 sscanf(ptr,"%f",&(StockPtr->OpenPrice)); 208 sscanf(ptr,"%f",&(StockPtr->OpenPrice));
209 209
210 ptr = csv_strtok(NULL, ","); 210 ptr = csv_strtok(NULL, ",");
211 if (!ptr) return 0; 211 if (!ptr) return 0;
212 sscanf(ptr,"%f",&(StockPtr->MaxPrice)); 212 sscanf(ptr,"%f",&(StockPtr->MaxPrice));
213 213
214 ptr = csv_strtok(NULL, ","); 214 ptr = csv_strtok(NULL, ",");
215 if (!ptr) return 0; 215 if (!ptr) return 0;
216 sscanf(ptr,"%f",&(StockPtr->MinPrice)); 216 sscanf(ptr,"%f",&(StockPtr->MinPrice));
217 217
218 ptr = csv_strtok(NULL, ","); 218 ptr = csv_strtok(NULL, ",");
219 if (!ptr) return 0; 219 if (!ptr) return 0;
220 StockPtr->Volume = atoi(ptr); 220 StockPtr->Volume = atoi(ptr);
221 221
222 if( !FirstStockPtr ) 222 if( !FirstStockPtr )
223 { 223 {
224 FirstStockPtr = StockPtr; 224 FirstStockPtr = StockPtr;
225 StockPtr->PreviousStock = 0; 225 StockPtr->PreviousStock = 0;
226 } 226 }
227 227
228 StockPtr->NextStock = 0; 228 StockPtr->NextStock = 0;
229 229
230 if (LastStockPtr) 230 if (LastStockPtr)
231 { 231 {
232 LastStockPtr->NextStock = StockPtr; 232 LastStockPtr->NextStock = StockPtr;
233 StockPtr->PreviousStock = LastStockPtr; 233 StockPtr->PreviousStock = LastStockPtr;
234 } 234 }
235 235
236 LastStockPtr = StockPtr; 236 LastStockPtr = StockPtr;
237 237
238 } 238 }
239 else 239 else
240 { 240 {
241 /* this symbol is not valid */ 241 /* this symbol is not valid */
242 /* Set the stock struct just with Symbol, all other are NULL */ 242 /* Set the stock struct just with Symbol, all other are NULL */
243 /* This can be used to see if the symbol has been reached are not */ 243 /* This can be used to see if the symbol has been reached are not */
244 244
245 StockPtr = malloc_stock(); 245 StockPtr = malloc_stock();
246 246
247 ptr = csv_strtok(line, ","); 247 ptr = csv_strtok(line, ",");
248 if (!ptr) return 0; 248 if (!ptr) return 0;
249 249
250 symbol = (char *)malloc(strlen(ptr)+1); 250 symbol = (char *)malloc(strlen(ptr)+1);
251 if (symbol==NULL) 251 if (symbol==NULL)
252 { 252 {
253 fprintf(stderr,"Memory allocating error (%s line %d)\n" 253 fprintf(stderr,"Memory allocating error (%s line %d)\n"
254 ,__FILE__, __LINE__); 254 ,__FILE__, __LINE__);
255 exit(1); 255 exit(1);
256 } 256 }
257 strcpy((char *)(symbol), ptr); 257 strcpy((char *)(symbol), ptr);
258 StockPtr->Symbol = symbol; 258 StockPtr->Symbol = symbol;
259 259
260 if( !FirstStockPtr ) 260 if( !FirstStockPtr )
261 { 261 {
262 FirstStockPtr = StockPtr; 262 FirstStockPtr = StockPtr;
263 StockPtr->PreviousStock = 0; 263 StockPtr->PreviousStock = 0;
264 } 264 }
265 265
266 StockPtr->NextStock = 0; 266 StockPtr->NextStock = 0;
267 267
268 if (LastStockPtr) 268 if (LastStockPtr)
269 { 269 {
270 LastStockPtr->NextStock = StockPtr; 270 LastStockPtr->NextStock = StockPtr;
271 StockPtr->PreviousStock = LastStockPtr; 271 StockPtr->PreviousStock = LastStockPtr;
272 } 272 }
273 273
274 LastStockPtr = StockPtr; 274 LastStockPtr = StockPtr;
275 } 275 }
276 276
277 end_line++; 277 end_line++;
278 line = end_line; 278 line = end_line;
279 279
280 } 280 }
281 281
282 return (FirstStockPtr); 282 return (FirstStockPtr);
283} 283}
284 284
285/*****************************************************************************/ 285/*****************************************************************************/
286/* Parses the history quotes file and return a stock structure list. */ 286/* Parses the history quotes file and return a stock structure list. */
287/*****************************************************************************/ 287/*****************************************************************************/
288stock *parse_csv_history_file(char *csv_file) 288stock *parse_csv_history_file(char *csv_file)
289{ 289{
290 290
291 char *line; 291 char *line;
292 char *end_line; 292 char *end_line;
293 char *ptr; 293 char *ptr;
294 294
295 int day; 295 int day;
296 char smonth[10]; 296 char smonth[10];
297 int month; 297 int month;
298 int year; 298 int year;
299 299
300 char *date; 300 char *date;
301 301
302 int i; 302 int i;
303 int test; 303 int test;
304 304
305 stock *StockPtr=NULL; 305 stock *StockPtr=NULL;
306 stock *LastStockPtr=NULL; 306 stock *LastStockPtr=NULL;
307 307
308 /* Used to return the pointer to the list */ 308 /* Used to return the pointer to the list */
309 stock *FirstStockPtr=NULL; 309 stock *FirstStockPtr=NULL;
310 310
311 line = csv_file; 311 line = csv_file;
312 end_line = csv_file; 312 end_line = csv_file;
313 313
314 /* do not use the first line */ 314 /* do not use the first line */
315 end_line = strstr(line, "\n"); 315 end_line = strstr(line, "\n");
316 *end_line = 0; 316 *end_line = 0;
317 end_line++; 317 end_line++;
318 line = end_line; 318 line = end_line;
319 319
320 while ((end_line = strstr(line, "\n"))) 320 while ((end_line = strstr(line, "\n")))
321 { 321 {
322 *end_line = 0; 322 *end_line = 0;
323 323
324 StockPtr = malloc_stock(); 324 StockPtr = malloc_stock();
325 325
326 /* Date */ 326 /* Date */
327 ptr = strtok(line, ","); 327 ptr = strtok(line, ",");
328 if (!ptr) return 0; 328 if (!ptr) return 0;
329 329
330 sscanf(ptr,"%d-%3s-%d",&day,smonth,&year); 330 sscanf(ptr,"%d-%3s-%d",&day,smonth,&year);
331 331
332 i=0; 332 i=0;
333 333
334#ifdef __UNIX__ 334#ifdef __UNIX__
335 while((test=strcasecmp(months[i], smonth))) i++; 335 while((test=strcasecmp(months[i], smonth))) i++;
336#elif __WINDOWS__ 336#elif __WINDOWS__
337 while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++; 337 while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++;
338#endif 338#endif
339 339
340 month = i+1; 340 month = i+1;
341 341
342 date = (char *)malloc(DATE_LENGTH); 342 date = (char *)malloc(DATE_LENGTH);
343 if (date==NULL) 343 if (date==NULL)
344 { 344 {
345 fprintf(stderr,"Memory allocating error (%s line %d)\n" 345 fprintf(stderr,"Memory allocating error (%s line %d)\n"
346 ,__FILE__, __LINE__); 346 ,__FILE__, __LINE__);
347 exit(1); 347 exit(1);
348 } 348 }
349 sprintf(date,"%.2d%.2d%.2d", year, month, day); 349 sprintf(date,"%.2d%.2d%.2d", year, month, day);
350 StockPtr->Date = date; 350 StockPtr->Date = date;
351 351
352 /* Open */ 352 /* Open */
353 ptr = strtok(NULL, ","); 353 ptr = strtok(NULL, ",");
354 if (!ptr) return 0; 354 if (!ptr) return 0;
355 sscanf(ptr,"%f",&(StockPtr->OpenPrice)); 355 sscanf(ptr,"%f",&(StockPtr->OpenPrice));
356 356
357 /* High */ 357 /* High */
358 ptr = strtok(NULL, ","); 358 ptr = strtok(NULL, ",");
359 if (!ptr) return 0; 359 if (!ptr) return 0;
360 sscanf(ptr,"%f",&(StockPtr->MaxPrice)); 360 sscanf(ptr,"%f",&(StockPtr->MaxPrice));
361 361
362 /* Low */ 362 /* Low */
363 ptr = strtok(NULL, ","); 363 ptr = strtok(NULL, ",");
364 if (!ptr) return 0; 364 if (!ptr) return 0;
365 sscanf(ptr,"%f",&(StockPtr->MinPrice)); 365 sscanf(ptr,"%f",&(StockPtr->MinPrice));
366 366
367 /* Close */ 367 /* Close */
368 ptr = strtok(NULL, ","); 368 ptr = strtok(NULL, ",");
369 if (!ptr) return 0; 369 if (!ptr) return 0;
370 sscanf(ptr,"%f",&(StockPtr->LastPrice)); 370 sscanf(ptr,"%f",&(StockPtr->LastPrice));
371 371
372 /* Volume */ 372 /* Volume */
373 373
374 ptr = strtok(NULL, ","); 374 ptr = strtok(NULL, ",");
375 if (!ptr) 375 if (!ptr)
376 /* It seems to be an indice */ 376 /* It seems to be an indice */
377 /* No volume for indices */ 377 /* No volume for indices */
378 StockPtr->Volume = 0; 378 StockPtr->Volume = 0;
379 else 379 else
380 StockPtr->Volume = atoi(ptr); 380 StockPtr->Volume = atoi(ptr);
381 381
382 if( !FirstStockPtr ) 382 if( !FirstStockPtr )
383 { 383 {
384 FirstStockPtr = StockPtr; 384 FirstStockPtr = StockPtr;
385 StockPtr->PreviousStock = 0; 385 StockPtr->PreviousStock = 0;
386 } 386 }
387 387
388 StockPtr->NextStock = 0; 388 StockPtr->NextStock = 0;
389 389
390 if (LastStockPtr) 390 if (LastStockPtr)
391 { 391 {
392 LastStockPtr->NextStock = StockPtr; 392 LastStockPtr->NextStock = StockPtr;
393 StockPtr->PreviousStock = LastStockPtr; 393 StockPtr->PreviousStock = LastStockPtr;
394 } 394 }
395 395
396 LastStockPtr = StockPtr; 396 LastStockPtr = StockPtr;
397 397
398 end_line++; 398 end_line++;
399 line = end_line; 399 line = end_line;
400 } 400 }
401 401
402 return (FirstStockPtr); 402 return (FirstStockPtr);
403} 403}