author | llornkcor <llornkcor> | 2002-10-31 00:09:31 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2002-10-31 00:09:31 (UTC) |
commit | 5a08fd92ac139820e1a1202d0b4b67190f24ccdb (patch) (unidiff) | |
tree | e9c0692cf445a886cd529b60f8e535922a7f5d4d /noncore/todayplugins/stockticker/libstocks | |
parent | ad396dd7b58fc772423f95be050f645fc7a6d9b9 (diff) | |
download | opie-5a08fd92ac139820e1a1202d0b4b67190f24ccdb.zip opie-5a08fd92ac139820e1a1202d0b4b67190f24ccdb.tar.gz opie-5a08fd92ac139820e1a1202d0b4b67190f24ccdb.tar.bz2 |
added - initial check in
Diffstat (limited to 'noncore/todayplugins/stockticker/libstocks') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/.cvsignore | 3 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/csv.c | 402 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/csv.h | 36 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/currency.c | 66 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/history.c | 149 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/http.c | 300 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/http.h | 35 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/lists.c | 123 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/lists.h | 35 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/stocks.c | 347 | ||||
-rw-r--r-- | noncore/todayplugins/stockticker/libstocks/stocks.h | 116 |
11 files changed, 1612 insertions, 0 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/.cvsignore b/noncore/todayplugins/stockticker/libstocks/.cvsignore new file mode 100644 index 0000000..c9bb88e --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/.cvsignore | |||
@@ -0,0 +1,3 @@ | |||
1 | Makefile* | ||
2 | moc_* | ||
3 | *.moc \ No newline at end of file | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/csv.c b/noncore/todayplugins/stockticker/libstocks/csv.c new file mode 100644 index 0000000..99a44e4 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/csv.c | |||
@@ -0,0 +1,402 @@ | |||
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 __CSV_C__ | ||
22 | |||
23 | #include <string.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <stdio.h> | ||
26 | |||
27 | #ifdef __WINDOWS__ | ||
28 | #include <mbstring.h> | ||
29 | #endif | ||
30 | |||
31 | #include "csv.h" | ||
32 | #include "stocks.h" | ||
33 | #include "lists.h" | ||
34 | |||
35 | #define DATE_LENGTH 7 /*YYMMDD*/ | ||
36 | |||
37 | const char *months[12]= | ||
38 | { | ||
39 | "Jan", | ||
40 | "Feb", | ||
41 | "Mar", | ||
42 | "Apr", | ||
43 | "May", | ||
44 | "Jun", | ||
45 | "Jul", | ||
46 | "Aug", | ||
47 | "Sep", | ||
48 | "Oct", | ||
49 | "Nov", | ||
50 | "Dec" | ||
51 | }; | ||
52 | |||
53 | /*****************************************************************************/ | ||
54 | /* Replacement of the strtok function. This one forgets "delim" when it is */ | ||
55 | /* between two commas. */ | ||
56 | /* Thanks to Julio Lucas who has told me the bug and has proposed me a patch */ | ||
57 | /*****************************************************************************/ | ||
58 | char *csv_strtok(char *s, char *delim) | ||
59 | { | ||
60 | static char *next=NULL; | ||
61 | char *temp, *first; | ||
62 | int comma=0; | ||
63 | |||
64 | if (s!=NULL) first=s; | ||
65 | else first=next; | ||
66 | |||
67 | temp=first; | ||
68 | if (*temp=='\0') return NULL; | ||
69 | |||
70 | while (*temp!='\0' && ((*temp!=*delim) || comma)) | ||
71 | { | ||
72 | if (*temp=='"') comma ^= 1; | ||
73 | temp++; | ||
74 | } | ||
75 | |||
76 | if (*temp=='\0') next=temp; | ||
77 | else | ||
78 | { | ||
79 | *temp='\0'; | ||
80 | next=temp+1; | ||
81 | } | ||
82 | |||
83 | return first; | ||
84 | } | ||
85 | |||
86 | /*****************************************************************************/ | ||
87 | /* Parses the csv file and return a list of stocks structure. */ | ||
88 | /* *csv points on the csv file in memory */ | ||
89 | /* count defines the country, because csv depends on the country */ | ||
90 | /*****************************************************************************/ | ||
91 | stock *parse_csv_file(char *csv) | ||
92 | { | ||
93 | char *line; | ||
94 | char *end_line; | ||
95 | |||
96 | char *ptr; | ||
97 | |||
98 | char *date; | ||
99 | char *time; | ||
100 | char *name; | ||
101 | char *symbol; | ||
102 | |||
103 | stock *StockPtr=NULL; | ||
104 | stock *LastStockPtr=NULL; | ||
105 | |||
106 | /* Used to return the pointer to the list */ | ||
107 | stock *FirstStockPtr=NULL; | ||
108 | |||
109 | /* used to see if symbol is valid */ | ||
110 | int valid; | ||
111 | char *test; | ||
112 | |||
113 | line = csv; | ||
114 | end_line = csv; | ||
115 | |||
116 | while ((end_line = strstr(line, "\n"))) | ||
117 | { | ||
118 | *end_line = 0; | ||
119 | |||
120 | /* Check if symbol valid */ | ||
121 | /* if 1 "N/A" then ok because Indices have N/A for volume */ | ||
122 | /* if 4 "N/A" then ok because Mutual funds have */ | ||
123 | /* if 5 "N/A" then ok because currencies have */ | ||
124 | /* So if >5 then stock not valid */ | ||
125 | |||
126 | test = line; | ||
127 | valid = 0; | ||
128 | while ( (test = strstr(test, "N/A")) ) | ||
129 | { | ||
130 | valid ++; | ||
131 | test = test +3; | ||
132 | } | ||
133 | |||
134 | if (valid < 6) | ||
135 | { | ||
136 | /* This Symbol is valid */ | ||
137 | |||
138 | StockPtr = malloc_stock(); | ||
139 | |||
140 | ptr = csv_strtok(line, ","); | ||
141 | if (!ptr) return 0; | ||
142 | |||
143 | symbol = (char *)malloc(strlen(ptr)+1); | ||
144 | if (symbol==NULL) | ||
145 | { | ||
146 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
147 | ,__FILE__, __LINE__); | ||
148 | exit(1); | ||
149 | } | ||
150 | strcpy((char *)(symbol), ptr); | ||
151 | StockPtr->Symbol = symbol; | ||
152 | |||
153 | ptr = csv_strtok(NULL, ","); | ||
154 | if (!ptr) return 0; | ||
155 | |||
156 | name = (char *)malloc(strlen(ptr)+1); | ||
157 | if (name==NULL) | ||
158 | { | ||
159 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
160 | ,__FILE__, __LINE__); | ||
161 | exit(1); | ||
162 | } | ||
163 | strcpy((char *)(name), ptr); | ||
164 | StockPtr->Name = name; | ||
165 | |||
166 | ptr = csv_strtok(NULL, ","); | ||
167 | if (!ptr) return 0; | ||
168 | sscanf(ptr,"%f",&(StockPtr->CurrentPrice)); | ||
169 | |||
170 | ptr = csv_strtok(NULL, ","); | ||
171 | if (!ptr) return 0; | ||
172 | |||
173 | date = (char *)malloc(strlen(ptr)+1); | ||
174 | if (date==NULL) | ||
175 | { | ||
176 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
177 | ,__FILE__, __LINE__); | ||
178 | exit(1); | ||
179 | } | ||
180 | strcpy((char *)(date), ptr); | ||
181 | StockPtr->Date = date; | ||
182 | |||
183 | ptr = csv_strtok(NULL, ","); | ||
184 | if (!ptr) return 0; | ||
185 | |||
186 | time = (char *)malloc(strlen(ptr)+1); | ||
187 | if (time==NULL) | ||
188 | { | ||
189 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
190 | ,__FILE__, __LINE__); | ||
191 | exit(1); | ||
192 | } | ||
193 | strcpy((char *)(time), ptr); | ||
194 | StockPtr->Time = time; | ||
195 | |||
196 | ptr = csv_strtok(NULL, ","); | ||
197 | if (!ptr) return 0; | ||
198 | sscanf(ptr,"%f",&(StockPtr->Variation)); | ||
199 | |||
200 | StockPtr->Pourcentage = 100 * StockPtr->Variation / | ||
201 | (StockPtr->CurrentPrice - StockPtr->Variation); | ||
202 | |||
203 | StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation; | ||
204 | |||
205 | ptr = csv_strtok(NULL, ","); | ||
206 | if (!ptr) return 0; | ||
207 | sscanf(ptr,"%f",&(StockPtr->OpenPrice)); | ||
208 | |||
209 | ptr = csv_strtok(NULL, ","); | ||
210 | if (!ptr) return 0; | ||
211 | sscanf(ptr,"%f",&(StockPtr->MaxPrice)); | ||
212 | |||
213 | ptr = csv_strtok(NULL, ","); | ||
214 | if (!ptr) return 0; | ||
215 | sscanf(ptr,"%f",&(StockPtr->MinPrice)); | ||
216 | |||
217 | ptr = csv_strtok(NULL, ","); | ||
218 | if (!ptr) return 0; | ||
219 | StockPtr->Volume = atoi(ptr); | ||
220 | |||
221 | if( !FirstStockPtr ) | ||
222 | { | ||
223 | FirstStockPtr = StockPtr; | ||
224 | StockPtr->PreviousStock = 0; | ||
225 | } | ||
226 | |||
227 | StockPtr->NextStock = 0; | ||
228 | |||
229 | if (LastStockPtr) | ||
230 | { | ||
231 | LastStockPtr->NextStock = StockPtr; | ||
232 | StockPtr->PreviousStock = LastStockPtr; | ||
233 | } | ||
234 | |||
235 | LastStockPtr = StockPtr; | ||
236 | |||
237 | } | ||
238 | else | ||
239 | { | ||
240 | /* this symbol is not valid */ | ||
241 | /* Set the stock struct just with Symbol, all other are NULL */ | ||
242 | /* This can be used to see if the symbol has been reached are not */ | ||
243 | |||
244 | StockPtr = malloc_stock(); | ||
245 | |||
246 | ptr = csv_strtok(line, ","); | ||
247 | if (!ptr) return 0; | ||
248 | |||
249 | symbol = (char *)malloc(strlen(ptr)+1); | ||
250 | if (symbol==NULL) | ||
251 | { | ||
252 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
253 | ,__FILE__, __LINE__); | ||
254 | exit(1); | ||
255 | } | ||
256 | strcpy((char *)(symbol), ptr); | ||
257 | StockPtr->Symbol = symbol; | ||
258 | |||
259 | if( !FirstStockPtr ) | ||
260 | { | ||
261 | FirstStockPtr = StockPtr; | ||
262 | StockPtr->PreviousStock = 0; | ||
263 | } | ||
264 | |||
265 | StockPtr->NextStock = 0; | ||
266 | |||
267 | if (LastStockPtr) | ||
268 | { | ||
269 | LastStockPtr->NextStock = StockPtr; | ||
270 | StockPtr->PreviousStock = LastStockPtr; | ||
271 | } | ||
272 | |||
273 | LastStockPtr = StockPtr; | ||
274 | } | ||
275 | |||
276 | end_line++; | ||
277 | line = end_line; | ||
278 | |||
279 | } | ||
280 | |||
281 | return (FirstStockPtr); | ||
282 | } | ||
283 | |||
284 | /*****************************************************************************/ | ||
285 | /* Parses the history quotes file and return a stock structure list. */ | ||
286 | /*****************************************************************************/ | ||
287 | stock *parse_csv_history_file(char *csv_file) | ||
288 | { | ||
289 | |||
290 | char *line; | ||
291 | char *end_line; | ||
292 | char *ptr; | ||
293 | |||
294 | int day; | ||
295 | char smonth[10]; | ||
296 | int month; | ||
297 | int year; | ||
298 | |||
299 | char *date; | ||
300 | |||
301 | int i; | ||
302 | int test; | ||
303 | |||
304 | stock *StockPtr=NULL; | ||
305 | stock *LastStockPtr=NULL; | ||
306 | |||
307 | /* Used to return the pointer to the list */ | ||
308 | stock *FirstStockPtr=NULL; | ||
309 | |||
310 | line = csv_file; | ||
311 | end_line = csv_file; | ||
312 | |||
313 | /* do not use the first line */ | ||
314 | end_line = strstr(line, "\n"); | ||
315 | *end_line = 0; | ||
316 | end_line++; | ||
317 | line = end_line; | ||
318 | |||
319 | while ((end_line = strstr(line, "\n"))) | ||
320 | { | ||
321 | *end_line = 0; | ||
322 | |||
323 | StockPtr = malloc_stock(); | ||
324 | |||
325 | /* Date */ | ||
326 | ptr = strtok(line, ","); | ||
327 | if (!ptr) return 0; | ||
328 | |||
329 | sscanf(ptr,"%d-%3s-%d",&day,smonth,&year); | ||
330 | |||
331 | i=0; | ||
332 | |||
333 | #ifdef __UNIX__ | ||
334 | while((test=strcasecmp(months[i], smonth))) i++; | ||
335 | #elif __WINDOWS__ | ||
336 | while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++; | ||
337 | #endif | ||
338 | |||
339 | month = i+1; | ||
340 | |||
341 | date = (char *)malloc(DATE_LENGTH); | ||
342 | if (date==NULL) | ||
343 | { | ||
344 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
345 | ,__FILE__, __LINE__); | ||
346 | exit(1); | ||
347 | } | ||
348 | sprintf(date,"%.2d%.2d%.2d", year, month, day); | ||
349 | StockPtr->Date = date; | ||
350 | |||
351 | /* Open */ | ||
352 | ptr = strtok(NULL, ","); | ||
353 | if (!ptr) return 0; | ||
354 | sscanf(ptr,"%f",&(StockPtr->OpenPrice)); | ||
355 | |||
356 | /* High */ | ||
357 | ptr = strtok(NULL, ","); | ||
358 | if (!ptr) return 0; | ||
359 | sscanf(ptr,"%f",&(StockPtr->MaxPrice)); | ||
360 | |||
361 | /* Low */ | ||
362 | ptr = strtok(NULL, ","); | ||
363 | if (!ptr) return 0; | ||
364 | sscanf(ptr,"%f",&(StockPtr->MinPrice)); | ||
365 | |||
366 | /* Close */ | ||
367 | ptr = strtok(NULL, ","); | ||
368 | if (!ptr) return 0; | ||
369 | sscanf(ptr,"%f",&(StockPtr->LastPrice)); | ||
370 | |||
371 | /* Volume */ | ||
372 | |||
373 | ptr = strtok(NULL, ","); | ||
374 | if (!ptr) | ||
375 | /* It seems to be an indice */ | ||
376 | /* No volume for indices */ | ||
377 | StockPtr->Volume = 0; | ||
378 | else | ||
379 | StockPtr->Volume = atoi(ptr); | ||
380 | |||
381 | if( !FirstStockPtr ) | ||
382 | { | ||
383 | FirstStockPtr = StockPtr; | ||
384 | StockPtr->PreviousStock = 0; | ||
385 | } | ||
386 | |||
387 | StockPtr->NextStock = 0; | ||
388 | |||
389 | if (LastStockPtr) | ||
390 | { | ||
391 | LastStockPtr->NextStock = StockPtr; | ||
392 | StockPtr->PreviousStock = LastStockPtr; | ||
393 | } | ||
394 | |||
395 | LastStockPtr = StockPtr; | ||
396 | |||
397 | end_line++; | ||
398 | line = end_line; | ||
399 | } | ||
400 | |||
401 | return (FirstStockPtr); | ||
402 | } | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/csv.h b/noncore/todayplugins/stockticker/libstocks/csv.h new file mode 100644 index 0000000..4205c61 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/csv.h | |||
@@ -0,0 +1,36 @@ | |||
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 | #ifndef __CSV_H__ | ||
22 | #define __CSV_H__ | ||
23 | |||
24 | #ifndef __CSV_C__ | ||
25 | #define PUBEXT_CSV extern | ||
26 | #else | ||
27 | #define PUBEXT_CSV | ||
28 | #endif | ||
29 | |||
30 | #include "stocks.h" | ||
31 | |||
32 | PUBEXT_CSV stock *parse_csv_file(char *); | ||
33 | PUBEXT_CSV char *csv_strtok(char *, char *); | ||
34 | PUBEXT_CSV stock *parse_csv_history_file(char *); | ||
35 | |||
36 | #endif /* __CSV_H__ */ | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/currency.c b/noncore/todayplugins/stockticker/libstocks/currency.c new file mode 100644 index 0000000..9a08a9d --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/currency.c | |||
@@ -0,0 +1,66 @@ | |||
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 __CURRENCY_C__ | ||
22 | |||
23 | |||
24 | #include <stdio.h> | ||
25 | #include <string.h> | ||
26 | #include <malloc.h> | ||
27 | #include <stdlib.h> | ||
28 | |||
29 | #include "stocks.h" | ||
30 | |||
31 | /*****************************************************************************/ | ||
32 | /* returns the currency exchange rate of "from" currency into */ | ||
33 | /* "into" currency. */ | ||
34 | /*****************************************************************************/ | ||
35 | libstocks_return_code get_currency_exchange(char *from, | ||
36 | char *into, | ||
37 | float *exchange) | ||
38 | { | ||
39 | char *symbol; | ||
40 | stock *data; | ||
41 | libstocks_return_code error; | ||
42 | |||
43 | if((symbol = (char *)malloc(strlen(from)+strlen(into)+3))==NULL) | ||
44 | { | ||
45 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
46 | ,__FILE__, __LINE__); | ||
47 | exit(1); | ||
48 | } | ||
49 | |||
50 | strcpy(symbol, from); | ||
51 | strcat(symbol, into); | ||
52 | strcat(symbol, "=X"); | ||
53 | |||
54 | error = get_stocks(symbol, &data); | ||
55 | if (error) | ||
56 | { | ||
57 | *exchange = 0; | ||
58 | return(error); | ||
59 | } | ||
60 | |||
61 | free_stocks(data); | ||
62 | |||
63 | *exchange = data->CurrentPrice; | ||
64 | return(error); | ||
65 | |||
66 | } | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/history.c b/noncore/todayplugins/stockticker/libstocks/history.c new file mode 100644 index 0000000..61f57d1 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/history.c | |||
@@ -0,0 +1,149 @@ | |||
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 __HISTORY_C__ | ||
22 | |||
23 | #include <string.h> | ||
24 | #include <stdlib.h> | ||
25 | |||
26 | #ifdef __WINDOWS__ | ||
27 | #include <malloc.h> | ||
28 | #endif | ||
29 | |||
30 | #include "stocks.h" | ||
31 | #include "http.h" | ||
32 | #include "csv.h" | ||
33 | |||
34 | const char history_stocks_server[]="chart.yahoo.com"; | ||
35 | const char url_history_beg[]="/table.csv?s="; | ||
36 | |||
37 | /*****************************************************************************/ | ||
38 | /* Gets the stock history csv file and return in csv_file the result. */ | ||
39 | /* The stock is defined by its symbol. */ | ||
40 | /* The history file starts at date1 and stops at date2. */ | ||
41 | /*****************************************************************************/ | ||
42 | libstocks_return_code get_history_csv(char *symbol, | ||
43 | char *date1, | ||
44 | char *date2, | ||
45 | char **csv_file) | ||
46 | { | ||
47 | char *data; | ||
48 | char url[80]; | ||
49 | |||
50 | char adate[12]; | ||
51 | |||
52 | char *y1, *y2; | ||
53 | char *m1, *m2; | ||
54 | char *d1, *d2; | ||
55 | |||
56 | libstocks_return_code error; | ||
57 | |||
58 | char *test; | ||
59 | |||
60 | strcpy(url, url_history_beg); | ||
61 | strcat(url, symbol); | ||
62 | |||
63 | strcpy(adate, date1); | ||
64 | |||
65 | y1=strtok(adate, "/"); | ||
66 | if(!y1) return ERRDATE; | ||
67 | m1 = strtok(NULL, "/"); | ||
68 | if(!m1) return ERRDATE; | ||
69 | d1 = strtok(NULL, "/"); | ||
70 | if(!m1) return ERRDATE; | ||
71 | |||
72 | strcat(url, "&a="); | ||
73 | strcat(url, m1); | ||
74 | strcat(url, "&b="); | ||
75 | strcat(url, d1); | ||
76 | strcat(url, "&c="); | ||
77 | strcat(url, y1); | ||
78 | |||
79 | strcpy(adate, date2); | ||
80 | |||
81 | y2=strtok(adate, "/"); | ||
82 | if(!y2) return ERRDATE; | ||
83 | m2 = strtok(NULL, "/"); | ||
84 | if(!m2) return ERRDATE; | ||
85 | d2 = strtok(NULL, "/"); | ||
86 | if(!d2) return ERRDATE; | ||
87 | |||
88 | strcat(url, "&d="); | ||
89 | strcat(url, m2); | ||
90 | strcat(url, "&e="); | ||
91 | strcat(url, d2); | ||
92 | strcat(url, "&f="); | ||
93 | strcat(url, y2); | ||
94 | strcat(url, "&g=d&q=q&y=0&x=.csv"); | ||
95 | |||
96 | error=http_get(url, (char *)history_stocks_server, &data); | ||
97 | if (error) return error; | ||
98 | |||
99 | /* tests if data is valid */ | ||
100 | |||
101 | /* The server returns "No data available for symbol" */ | ||
102 | /* when the stock is not available */ | ||
103 | test = strstr(data, "No data available"); | ||
104 | if (test) | ||
105 | { | ||
106 | free(data); | ||
107 | return ERRDATA; | ||
108 | } | ||
109 | |||
110 | /* The server return "No Prices in this date range" */ | ||
111 | /* when dates have not the good range */ | ||
112 | test = strstr(data, "No Prices in this date range"); | ||
113 | if (test) | ||
114 | { | ||
115 | free(data); | ||
116 | return ERRRANG; | ||
117 | } | ||
118 | |||
119 | *csv_file = data; | ||
120 | |||
121 | return(0); | ||
122 | } | ||
123 | |||
124 | /*****************************************************************************/ | ||
125 | /* Gets the stock history and return a stock structure list. */ | ||
126 | /* The stock is defined by its symbol. */ | ||
127 | /* The history file starts at date1 and stops at date2. */ | ||
128 | /*****************************************************************************/ | ||
129 | libstocks_return_code get_stock_history(char *symbol, | ||
130 | char *date1, | ||
131 | char *date2, | ||
132 | stock **stock_history) | ||
133 | { | ||
134 | libstocks_return_code error; | ||
135 | char *csv_file; | ||
136 | stock *stock_parsed; | ||
137 | |||
138 | error = get_history_csv(symbol, date1, date2, &csv_file); | ||
139 | if (error) return error; | ||
140 | |||
141 | stock_parsed = parse_csv_history_file(csv_file); | ||
142 | |||
143 | free(csv_file); | ||
144 | |||
145 | if (!stock_parsed) return ERRPCSV; | ||
146 | *stock_history = stock_parsed; | ||
147 | |||
148 | return(0); | ||
149 | } | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/http.c b/noncore/todayplugins/stockticker/libstocks/http.c new file mode 100644 index 0000000..462080d --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/http.c | |||
@@ -0,0 +1,300 @@ | |||
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 __HTTP_C__ | ||
22 | |||
23 | #ifdef __UNIX__ | ||
24 | #include <sys/socket.h> | ||
25 | #include <netinet/in.h> | ||
26 | #include <arpa/inet.h> | ||
27 | #include <netdb.h> | ||
28 | #include <unistd.h> | ||
29 | #elif __WINDOWS__ | ||
30 | #include <winsock.h> | ||
31 | #include <mbstring.h> | ||
32 | #endif | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <ctype.h> | ||
36 | #include <string.h> | ||
37 | #include <stdlib.h> | ||
38 | #include <stdio.h> | ||
39 | |||
40 | #include "http.h" | ||
41 | #include "stocks.h" | ||
42 | |||
43 | #define BUF_SIZE 1024 | ||
44 | #define HEADER_MAXBUF 512 | ||
45 | |||
46 | /* pointer to proxy server name or NULL */ | ||
47 | char *http_proxy_server=NULL; | ||
48 | /* proxy server port number or 0 */ | ||
49 | int http_proxy_port=0; | ||
50 | |||
51 | /*****************************************************************************/ | ||
52 | /* Gets the file from http://http_server/http_file */ | ||
53 | /* This function is used to get quotes csv file from yahoo. */ | ||
54 | /* It allocates memory for the file and defines *pdata (the pointer of datas)*/ | ||
55 | /*****************************************************************************/ | ||
56 | libstocks_return_code http_get(char *http_file, char *http_server, char **pdata) | ||
57 | { | ||
58 | struct hostent *host; /* structs needed by socket */ | ||
59 | struct sockaddr_in server; | ||
60 | int s; /* socket descriptor */ | ||
61 | char header[HEADER_MAXBUF]; /* request header */ | ||
62 | int hlg; /* header length */ | ||
63 | char buf[BUF_SIZE+1]; /* tempory buffer from socket read */ | ||
64 | int r; /* number of bytes read by read function */ | ||
65 | char *data=NULL; /* http server response */ | ||
66 | int data_lgr; /* http server response length */ | ||
67 | char *temp; /* pointer used to split header and csv */ | ||
68 | int error_code; /* error code returned by http server */ | ||
69 | char *csv_ptr; /* pointer to the csv content */ | ||
70 | int header_founded; /* test if header is founded */ | ||
71 | |||
72 | #ifdef DEBUG | ||
73 | printf("*http_get\n"); | ||
74 | #endif | ||
75 | |||
76 | /* get host info by name :*/ | ||
77 | if ((host = gethostbyname( http_proxy_server ? http_proxy_server : http_server))) | ||
78 | { | ||
79 | memset((char *) &server,0, sizeof(server)); | ||
80 | memmove((char *) &server.sin_addr, host->h_addr, host->h_length); | ||
81 | server.sin_family = host->h_addrtype; | ||
82 | server.sin_port = (unsigned short) htons( http_proxy_server ? http_proxy_port : 80 ); | ||
83 | |||
84 | } else | ||
85 | { | ||
86 | |||
87 | #ifdef DEBUG | ||
88 | printf(" gethostbyname : NOK\n"); | ||
89 | #endif | ||
90 | return ERRHOST; | ||
91 | } | ||
92 | |||
93 | /* create socket */ | ||
94 | if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) | ||
95 | { | ||
96 | |||
97 | #ifdef DEBUG | ||
98 | printf(" create socket : NOK\n"); | ||
99 | #endif | ||
100 | return ERRSOCK; | ||
101 | } | ||
102 | |||
103 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0); | ||
104 | |||
105 | /* connect to server */ | ||
106 | if (connect(s, &server, sizeof(server)) < 0) | ||
107 | { | ||
108 | #ifdef DEBUG | ||
109 | printf(" connect to server : NOK\n"); | ||
110 | #endif | ||
111 | |||
112 | #ifdef __UNIX__ | ||
113 | close(s); | ||
114 | #elif __WINDOWS__ | ||
115 | closesocket(s); | ||
116 | #endif | ||
117 | return ERRCONN; | ||
118 | } | ||
119 | |||
120 | /* create header */ | ||
121 | if (http_proxy_server) | ||
122 | { | ||
123 | sprintf(header,"GET http://%.128s:80%.256s HTTP/1.0\015\012\015\012", | ||
124 | http_server, http_file); | ||
125 | } | ||
126 | else | ||
127 | { | ||
128 | sprintf(header,"GET %s HTTP/1.0\015\012\015\012",http_file); | ||
129 | } | ||
130 | |||
131 | hlg=strlen(header); | ||
132 | |||
133 | /* send header */ | ||
134 | #ifdef __UNIX__ | ||
135 | if (write(s,header,hlg)!=hlg) | ||
136 | #elif __WINDOWS__ | ||
137 | if (send(s,header,hlg, 0)!=hlg) | ||
138 | #endif | ||
139 | { | ||
140 | #ifdef DEBUG | ||
141 | printf(" send header : NOK\n"); | ||
142 | #endif | ||
143 | return ERRWHEA; | ||
144 | } | ||
145 | |||
146 | data_lgr = 0; | ||
147 | r=1; | ||
148 | while(r) | ||
149 | { | ||
150 | /* Clear Buffer */ | ||
151 | memset(buf,0,BUF_SIZE+1); | ||
152 | |||
153 | #ifdef __UNIX__ | ||
154 | r=read(s,buf,BUF_SIZE); | ||
155 | #elif __WINDOWS__ | ||
156 | r=recv(s,buf,BUF_SIZE,0); | ||
157 | #endif | ||
158 | |||
159 | if (r) | ||
160 | { | ||
161 | if(!data_lgr) | ||
162 | { | ||
163 | if((data = malloc(r+1))==NULL) | ||
164 | { | ||
165 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
166 | ,__FILE__, __LINE__); | ||
167 | exit(1); | ||
168 | } | ||
169 | |||
170 | memcpy(data,buf,r); | ||
171 | data_lgr = r; | ||
172 | data[r]=0; | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | if((temp = malloc(r+data_lgr+1))==NULL) | ||
177 | { | ||
178 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
179 | ,__FILE__, __LINE__); | ||
180 | exit(1); | ||
181 | } | ||
182 | memcpy(temp, data, data_lgr); | ||
183 | memcpy(temp+data_lgr, buf, r); | ||
184 | temp[r+data_lgr]=0; | ||
185 | data_lgr += r; | ||
186 | free(data); | ||
187 | data = temp; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | /* close socket */ | ||
193 | #ifdef __UNIX__ | ||
194 | close(s); | ||
195 | #elif __WINDOWS__ | ||
196 | closesocket(s); | ||
197 | #endif | ||
198 | |||
199 | #ifdef DEBUG | ||
200 | printf("%s\n", data); | ||
201 | #endif | ||
202 | |||
203 | /* get headers to test status line */ | ||
204 | /* and to split headers and content */ | ||
205 | |||
206 | temp = data; | ||
207 | header_founded = 0; | ||
208 | while( !header_founded ) | ||
209 | { | ||
210 | if (*temp==0) return ERRRHEA; | ||
211 | |||
212 | if( *temp==0x0A ) | ||
213 | { | ||
214 | /* test if it is the header end */ | ||
215 | temp ++; | ||
216 | if (*temp == 0x0D) temp++; | ||
217 | if (*temp == 0x0A) header_founded = 1; | ||
218 | } | ||
219 | else | ||
220 | temp++; | ||
221 | } | ||
222 | |||
223 | *temp = 0; | ||
224 | temp++; | ||
225 | |||
226 | sscanf(data,"HTTP/1.%*d %03d",&error_code); | ||
227 | |||
228 | if (error_code != 200) | ||
229 | { | ||
230 | #ifdef DEBUG | ||
231 | printf(" HTTP error code : %d\n", error_code); | ||
232 | #endif | ||
233 | free(data); | ||
234 | return ERRPAHD; | ||
235 | } | ||
236 | |||
237 | if ((csv_ptr = malloc(strlen(temp)+1))==NULL) | ||
238 | { | ||
239 | free(data); | ||
240 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
241 | ,__FILE__, __LINE__); | ||
242 | exit(1); | ||
243 | } | ||
244 | |||
245 | memcpy(csv_ptr, temp, strlen(temp)+1); | ||
246 | free(data); | ||
247 | |||
248 | #ifdef DEBUG | ||
249 | printf(" CSV\n"); | ||
250 | printf("%s,\n", csv_ptr); | ||
251 | #endif | ||
252 | |||
253 | *pdata = csv_ptr; | ||
254 | |||
255 | return 0; | ||
256 | } | ||
257 | |||
258 | /******************************************************************************/ | ||
259 | /* Set the proxy server to use */ | ||
260 | /******************************************************************************/ | ||
261 | libstocks_return_code set_proxy(char *proxy) | ||
262 | { | ||
263 | char *ptr; | ||
264 | char c; | ||
265 | |||
266 | #ifdef DEBUG | ||
267 | printf("*set_proxy\n"); | ||
268 | #endif | ||
269 | |||
270 | /* Parse the proxy URL - It must start with http:// */ | ||
271 | #ifdef __UNIX__ | ||
272 | if (strncasecmp("http://",proxy,7)) return ERRPROX; | ||
273 | #elif __WINDOWS__ | ||
274 | if (_mbsnbicmp("http://",proxy,7)) return ERRPROX; | ||
275 | #endif | ||
276 | |||
277 | proxy+=7; | ||
278 | |||
279 | /* find ":" in the proxy url */ | ||
280 | ptr = proxy; | ||
281 | for (c=*ptr; (c && c!=':');) c=*ptr++; | ||
282 | |||
283 | /* ptr points just after the ":" or at the end of proxy if : not founded */ | ||
284 | *(ptr-1)=0; /* clear the ":" */ | ||
285 | |||
286 | http_proxy_server=strdup(proxy); | ||
287 | |||
288 | #ifdef DEBUG | ||
289 | printf("http_proxy_server : %s\n", http_proxy_server); | ||
290 | #endif | ||
291 | |||
292 | /* get the port number of the url */ | ||
293 | if (sscanf(ptr,"%d",&http_proxy_port)!=1) return ERRPROX; | ||
294 | |||
295 | #ifdef DEBUG | ||
296 | printf("http_proxy_port : %d\n", http_proxy_port); | ||
297 | #endif | ||
298 | |||
299 | return 0; | ||
300 | } | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/http.h b/noncore/todayplugins/stockticker/libstocks/http.h new file mode 100644 index 0000000..33346f3 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/http.h | |||
@@ -0,0 +1,35 @@ | |||
1 | /* libstocks - Library to get current stock quotes from Yahoo Finance | ||
2 | * | ||
3 | * Copyright (C) 2000 Eric Laeuffer | ||
4 | * | ||
5 | * | ||
6 | * This library is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Library General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * This library is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Library General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Library General Public | ||
17 | * License along with this library; if not, write to the | ||
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | * Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | #ifndef __HTTP_H__ | ||
23 | #define __HTTP_H__ | ||
24 | |||
25 | #ifndef __HTTP_C__ | ||
26 | #define PUBEXT_HTTP extern | ||
27 | #else | ||
28 | #define PUBEXT_HTTP | ||
29 | #endif | ||
30 | |||
31 | #include "stocks.h" | ||
32 | |||
33 | PUBEXT_HTTP libstocks_return_code http_get(char *, char *, char **); | ||
34 | |||
35 | #endif /* __HTTP_H__ */ | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/lists.c b/noncore/todayplugins/stockticker/libstocks/lists.c new file mode 100644 index 0000000..1ed66b3 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/lists.c | |||
@@ -0,0 +1,123 @@ | |||
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 __LISTS_C__ | ||
22 | |||
23 | #include <malloc.h> | ||
24 | #include <stdio.h> | ||
25 | #include <string.h> | ||
26 | |||
27 | #ifdef __WINDOWS__ | ||
28 | #include <stdlib.h> | ||
29 | #endif | ||
30 | |||
31 | #include "stocks.h" | ||
32 | #include "lists.h" | ||
33 | |||
34 | /******************************************************************************/ | ||
35 | /* Finds a stock in the stocks list and return the stock pointer. */ | ||
36 | /* *stocks points to the stocks list */ | ||
37 | /* *stock_to_find is the stock to find :)) */ | ||
38 | /******************************************************************************/ | ||
39 | stock *find_stock(stock *stocks, char *stock_to_find) | ||
40 | { | ||
41 | char *test=NULL; | ||
42 | |||
43 | while(stocks != NULL) | ||
44 | { | ||
45 | test = strstr(stocks->Symbol, stock_to_find); | ||
46 | if(test) return(stocks); | ||
47 | stocks = stocks->NextStock; | ||
48 | } | ||
49 | |||
50 | return(0); | ||
51 | } | ||
52 | |||
53 | /******************************************************************************/ | ||
54 | /* Allocate memory for a stock structure and return the pointer of this */ | ||
55 | /* structure. */ | ||
56 | /******************************************************************************/ | ||
57 | stock *malloc_stock(void) | ||
58 | { | ||
59 | stock *stockptr; | ||
60 | |||
61 | if((stockptr = (stock *)malloc(sizeof(stock)))==NULL) | ||
62 | { | ||
63 | fprintf(stderr,"Memory allocating error (%s line %d)\n" | ||
64 | ,__FILE__, __LINE__); | ||
65 | exit(1); | ||
66 | } | ||
67 | |||
68 | stockptr->Symbol=NULL; | ||
69 | stockptr->Name=NULL; | ||
70 | stockptr->Time=NULL; | ||
71 | stockptr->Date=NULL; | ||
72 | stockptr->CurrentPrice=0; | ||
73 | stockptr->LastPrice=0; | ||
74 | stockptr->OpenPrice=0; | ||
75 | stockptr->MinPrice=0; | ||
76 | stockptr->MaxPrice=0; | ||
77 | stockptr->Variation=0; | ||
78 | stockptr->Pourcentage=0; | ||
79 | stockptr->Volume=0; | ||
80 | stockptr->PreviousStock=0; | ||
81 | stockptr->NextStock=0; | ||
82 | |||
83 | return(stockptr); | ||
84 | } | ||
85 | |||
86 | /******************************************************************************/ | ||
87 | /* Deallocates a stock structure. */ | ||
88 | /* *stocks_to_free is the pointer of the structure to free */ | ||
89 | /******************************************************************************/ | ||
90 | void free_stocks(stock *stock_to_free) | ||
91 | { | ||
92 | stock *next_stock; | ||
93 | |||
94 | while(stock_to_free) | ||
95 | { | ||
96 | next_stock = stock_to_free->NextStock; | ||
97 | free(stock_to_free->Symbol); | ||
98 | free(stock_to_free->Name); | ||
99 | free(stock_to_free->Time); | ||
100 | free(stock_to_free->Date); | ||
101 | free(stock_to_free); | ||
102 | stock_to_free = next_stock; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | /******************************************************************************/ | ||
107 | /* Returns the next stock structure from the list. */ | ||
108 | /* *the_stocks points to the list. */ | ||
109 | /******************************************************************************/ | ||
110 | stock *next_stock(stock *the_stock) | ||
111 | { | ||
112 | return((stock *)(the_stock->NextStock)); | ||
113 | } | ||
114 | |||
115 | /******************************************************************************/ | ||
116 | /* Returns the previous stock structure from the list. */ | ||
117 | /* *the_stocks points to the list. */ | ||
118 | /******************************************************************************/ | ||
119 | stock *previous_stock(stock *the_stock) | ||
120 | { | ||
121 | return((stock *)(the_stock->PreviousStock)); | ||
122 | } | ||
123 | |||
diff --git a/noncore/todayplugins/stockticker/libstocks/lists.h b/noncore/todayplugins/stockticker/libstocks/lists.h new file mode 100644 index 0000000..0132317 --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/lists.h | |||
@@ -0,0 +1,35 @@ | |||
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 | #ifndef __LISTS_H__ | ||
22 | #define __LISTS_H__ | ||
23 | |||
24 | #ifndef __LISTS_C__ | ||
25 | #define PUBEXT_LISTS extern | ||
26 | #else | ||
27 | #define PUBEXT_LISTS | ||
28 | #endif | ||
29 | |||
30 | #include "stocks.h" | ||
31 | |||
32 | PUBEXT_LISTS stock *malloc_stock(void); | ||
33 | |||
34 | |||
35 | #endif /* __LISTS_H */ | ||
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 | /* | ||
38 | s = symbol | ||
39 | n = name | ||
40 | l1 = last trade | ||
41 | d1 = date | ||
42 | t1 = time | ||
43 | c1 = variation | ||
44 | o = open | ||
45 | h = higher price | ||
46 | g = lower price | ||
47 | v = volume | ||
48 | */ | ||
49 | |||
50 | const char yahoo_us_stocks_server[]="finance.yahoo.com"; | ||
51 | const char yahoo_eu_stocks_server[]="finance.yahoo.com"; | ||
52 | //const char yahoo_eu_stocks_server[]="fr.finance.yahoo.com"; | ||
53 | |||
54 | const char yahoo_url_beg[]="/d/quotes.csv?s="; | ||
55 | const char yahoo_url_end[]="&f=snl1d1t1c1ohgv&e=.csv"; | ||
56 | |||
57 | typedef enum { | ||
58 | YAHOO_EUROPE, | ||
59 | YAHOO_US | ||
60 | } yahoo_source; | ||
61 | |||
62 | #define YAHOO_US_EXT_NB 11 | ||
63 | const 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 | /*****************************************************************************/ | ||
83 | yahoo_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 | /*****************************************************************************/ | ||
119 | libstocks_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 | /*****************************************************************************/ | ||
191 | libstocks_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 | } | ||
diff --git a/noncore/todayplugins/stockticker/libstocks/stocks.h b/noncore/todayplugins/stockticker/libstocks/stocks.h new file mode 100644 index 0000000..13250ca --- a/dev/null +++ b/noncore/todayplugins/stockticker/libstocks/stocks.h | |||
@@ -0,0 +1,116 @@ | |||
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 | #ifndef __STOCKS_H__ | ||
22 | #define __STOCKS_H__ | ||
23 | |||
24 | /* Defines for prototypes */ | ||
25 | #ifndef __LISTS_C__ | ||
26 | #define _LISTS_C_EXT extern | ||
27 | #else | ||
28 | #define _LISTS_C_EXT | ||
29 | #endif /* __LISTS_C__ */ | ||
30 | |||
31 | #ifndef __STOCKS_C__ | ||
32 | #define _STOCKS_C_EXT extern | ||
33 | #else | ||
34 | #define _STOCKS_C_EXT | ||
35 | #endif /* __STOCKS_C__ */ | ||
36 | |||
37 | #ifndef __HTTP_C__ | ||
38 | #define _HTTP_C_EXT extern | ||
39 | #else | ||
40 | #define _HTTP_C_EXT | ||
41 | #endif /* __HTTP_C__ */ | ||
42 | |||
43 | #ifndef __CURRENCY_C__ | ||
44 | #define _CURRENCY_C_EXT extern | ||
45 | #else | ||
46 | #define _CURRENCY_C_EXT | ||
47 | #endif /* __CURRENCY_C__ */ | ||
48 | |||
49 | #ifndef __HISTORY_C__ | ||
50 | #define _HISTORY_C_EXT extern | ||
51 | #else | ||
52 | #define _HISTORY_C_EXT | ||
53 | #endif /* __HISTORY_C__ */ | ||
54 | |||
55 | typedef struct stockstruct stock; | ||
56 | |||
57 | struct stockstruct { | ||
58 | char *Symbol; | ||
59 | char *Name; | ||
60 | char *Time; | ||
61 | char *Date; | ||
62 | float CurrentPrice; | ||
63 | float LastPrice; | ||
64 | float OpenPrice; | ||
65 | float MinPrice; | ||
66 | float MaxPrice; | ||
67 | float Variation; | ||
68 | float Pourcentage; | ||
69 | int Volume; | ||
70 | stock *PreviousStock; | ||
71 | stock *NextStock; | ||
72 | }; | ||
73 | |||
74 | /* Errors generates by the library */ | ||
75 | typedef enum { | ||
76 | NOERR = 0, /* No error */ | ||
77 | ERRHOST = 1, /* No such host */ | ||
78 | ERRSOCK = 2, /* Can't create socket */ | ||
79 | ERRCONN = 3, /* Can't connect to host */ | ||
80 | ERRWHEA = 4, /* Write error on socket while writing */ | ||
81 | ERRRHEA = 5, /* Cannot find header in http response */ | ||
82 | ERRPAHD = 7, /* Invalid answer from data server */ | ||
83 | ERRPCSV = 8, /* Error in parsing csv file */ | ||
84 | ERRPROX = 20, /* Bad proxy url */ | ||
85 | ERRDATE = 30, /* Bad date format */ | ||
86 | ERRDATA = 40, /* No data available */ | ||
87 | ERRRANG = 50 /* No prices for this date range */ | ||
88 | |||
89 | } libstocks_return_code; | ||
90 | |||
91 | _LISTS_C_EXT stock *next_stock(stock *); | ||
92 | _LISTS_C_EXT stock *previous_stock(stock *); | ||
93 | _LISTS_C_EXT void free_stocks(stock *); | ||
94 | _LISTS_C_EXT stock *find_stock(stock *stocks, char *); | ||
95 | |||
96 | _STOCKS_C_EXT libstocks_return_code get_stocks(const char *, stock **); | ||
97 | |||
98 | _HTTP_C_EXT libstocks_return_code set_proxy(char *); | ||
99 | |||
100 | |||
101 | _CURRENCY_C_EXT libstocks_return_code get_currency_exchange(char *, | ||
102 | char *, | ||
103 | float *); | ||
104 | |||
105 | _HISTORY_C_EXT libstocks_return_code get_history_csv(char *, | ||
106 | char *, | ||
107 | char *, | ||
108 | char **); | ||
109 | |||
110 | _HISTORY_C_EXT libstocks_return_code get_stock_history(char *, | ||
111 | char *, | ||
112 | char *, | ||
113 | stock **); | ||
114 | |||
115 | |||
116 | #endif /* __STOCKS_H__ */ | ||