summaryrefslogtreecommitdiff
authorllornkcor <llornkcor>2002-10-31 20:39:13 (UTC)
committer llornkcor <llornkcor>2002-10-31 20:39:13 (UTC)
commit00a211621ea11ed73d803a3e52c09ce1e76864eb (patch) (unidiff)
tree01b54261103785b7eaa46471809494807291494b
parent88310c7207b84c79dbb11ab3c3afeded1874c10b (diff)
downloadopie-00a211621ea11ed73d803a3e52c09ce1e76864eb.zip
opie-00a211621ea11ed73d803a3e52c09ce1e76864eb.tar.gz
opie-00a211621ea11ed73d803a3e52c09ce1e76864eb.tar.bz2
better fix
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/todayplugins/stockticker/libstocks/csv.c2
-rw-r--r--noncore/todayplugins/stockticker/libstocks/http.c2
2 files changed, 4 insertions, 0 deletions
diff --git a/noncore/todayplugins/stockticker/libstocks/csv.c b/noncore/todayplugins/stockticker/libstocks/csv.c
index f45af52..6170bed 100644
--- a/noncore/todayplugins/stockticker/libstocks/csv.c
+++ b/noncore/todayplugins/stockticker/libstocks/csv.c
@@ -1,403 +1,405 @@
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#ifndef __UNIX__
22#define __UNIX__ 23#define __UNIX__
24#endif
23 25
24#include <string.h> 26#include <string.h>
25#include <stdlib.h> 27#include <stdlib.h>
26#include <stdio.h> 28#include <stdio.h>
27 29
28#ifdef __WINDOWS__ 30#ifdef __WINDOWS__
29#include <mbstring.h> 31#include <mbstring.h>
30#endif 32#endif
31 33
32#include "csv.h" 34#include "csv.h"
33#include "stocks.h" 35#include "stocks.h"
34#include "lists.h" 36#include "lists.h"
35 37
36#define DATE_LENGTH 7 /*YYMMDD*/ 38#define DATE_LENGTH 7 /*YYMMDD*/
37 39
38const char *months[12]= 40const char *months[12]=
39{ 41{
40 "Jan", 42 "Jan",
41 "Feb", 43 "Feb",
42 "Mar", 44 "Mar",
43 "Apr", 45 "Apr",
44 "May", 46 "May",
45 "Jun", 47 "Jun",
46 "Jul", 48 "Jul",
47 "Aug", 49 "Aug",
48 "Sep", 50 "Sep",
49 "Oct", 51 "Oct",
50 "Nov", 52 "Nov",
51 "Dec" 53 "Dec"
52}; 54};
53 55
54/*****************************************************************************/ 56/*****************************************************************************/
55/* Replacement of the strtok function. This one forgets "delim" when it is */ 57/* Replacement of the strtok function. This one forgets "delim" when it is */
56/* between two commas. */ 58/* between two commas. */
57/* Thanks to Julio Lucas who has told me the bug and has proposed me a patch */ 59/* Thanks to Julio Lucas who has told me the bug and has proposed me a patch */
58/*****************************************************************************/ 60/*****************************************************************************/
59char *csv_strtok(char *s, char *delim) 61char *csv_strtok(char *s, char *delim)
60{ 62{
61 static char *next=NULL; 63 static char *next=NULL;
62 char *temp, *first; 64 char *temp, *first;
63 int comma=0; 65 int comma=0;
64 66
65 if (s!=NULL) first=s; 67 if (s!=NULL) first=s;
66 else first=next; 68 else first=next;
67 69
68 temp=first; 70 temp=first;
69 if (*temp=='\0') return NULL; 71 if (*temp=='\0') return NULL;
70 72
71 while (*temp!='\0' && ((*temp!=*delim) || comma)) 73 while (*temp!='\0' && ((*temp!=*delim) || comma))
72 { 74 {
73 if (*temp=='"') comma ^= 1; 75 if (*temp=='"') comma ^= 1;
74 temp++; 76 temp++;
75 } 77 }
76 78
77 if (*temp=='\0') next=temp; 79 if (*temp=='\0') next=temp;
78 else 80 else
79 { 81 {
80 *temp='\0'; 82 *temp='\0';
81 next=temp+1; 83 next=temp+1;
82 } 84 }
83 85
84 return first; 86 return first;
85} 87}
86 88
87/*****************************************************************************/ 89/*****************************************************************************/
88/* Parses the csv file and return a list of stocks structure. */ 90/* Parses the csv file and return a list of stocks structure. */
89/* *csv points on the csv file in memory */ 91/* *csv points on the csv file in memory */
90/* count defines the country, because csv depends on the country */ 92/* count defines the country, because csv depends on the country */
91/*****************************************************************************/ 93/*****************************************************************************/
92stock *parse_csv_file(char *csv) 94stock *parse_csv_file(char *csv)
93{ 95{
94 char *line; 96 char *line;
95 char *end_line; 97 char *end_line;
96 98
97 char *ptr; 99 char *ptr;
98 100
99 char *date; 101 char *date;
100 char *time; 102 char *time;
101 char *name; 103 char *name;
102 char *symbol; 104 char *symbol;
103 105
104 stock *StockPtr=NULL; 106 stock *StockPtr=NULL;
105 stock *LastStockPtr=NULL; 107 stock *LastStockPtr=NULL;
106 108
107 /* Used to return the pointer to the list */ 109 /* Used to return the pointer to the list */
108 stock *FirstStockPtr=NULL; 110 stock *FirstStockPtr=NULL;
109 111
110 /* used to see if symbol is valid */ 112 /* used to see if symbol is valid */
111 int valid; 113 int valid;
112 char *test; 114 char *test;
113 115
114 line = csv; 116 line = csv;
115 end_line = csv; 117 end_line = csv;
116 118
117 while ((end_line = strstr(line, "\n"))) 119 while ((end_line = strstr(line, "\n")))
118 { 120 {
119 *end_line = 0; 121 *end_line = 0;
120 122
121 /* Check if symbol valid */ 123 /* Check if symbol valid */
122 /* 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 */
123 /* if 4 "N/A" then ok because Mutual funds have */ 125 /* if 4 "N/A" then ok because Mutual funds have */
124 /* if 5 "N/A" then ok because currencies have */ 126 /* if 5 "N/A" then ok because currencies have */
125 /* So if >5 then stock not valid */ 127 /* So if >5 then stock not valid */
126 128
127 test = line; 129 test = line;
128 valid = 0; 130 valid = 0;
129 while ( (test = strstr(test, "N/A")) ) 131 while ( (test = strstr(test, "N/A")) )
130 { 132 {
131 valid ++; 133 valid ++;
132 test = test +3; 134 test = test +3;
133 } 135 }
134 136
135 if (valid < 6) 137 if (valid < 6)
136 { 138 {
137 /* This Symbol is valid */ 139 /* This Symbol is valid */
138 140
139 StockPtr = malloc_stock(); 141 StockPtr = malloc_stock();
140 142
141 ptr = csv_strtok(line, ","); 143 ptr = csv_strtok(line, ",");
142 if (!ptr) return 0; 144 if (!ptr) return 0;
143 145
144 symbol = (char *)malloc(strlen(ptr)+1); 146 symbol = (char *)malloc(strlen(ptr)+1);
145 if (symbol==NULL) 147 if (symbol==NULL)
146 { 148 {
147 fprintf(stderr,"Memory allocating error (%s line %d)\n" 149 fprintf(stderr,"Memory allocating error (%s line %d)\n"
148 ,__FILE__, __LINE__); 150 ,__FILE__, __LINE__);
149 exit(1); 151 exit(1);
150 } 152 }
151 strcpy((char *)(symbol), ptr); 153 strcpy((char *)(symbol), ptr);
152 StockPtr->Symbol = symbol; 154 StockPtr->Symbol = symbol;
153 155
154 ptr = csv_strtok(NULL, ","); 156 ptr = csv_strtok(NULL, ",");
155 if (!ptr) return 0; 157 if (!ptr) return 0;
156 158
157 name = (char *)malloc(strlen(ptr)+1); 159 name = (char *)malloc(strlen(ptr)+1);
158 if (name==NULL) 160 if (name==NULL)
159 { 161 {
160 fprintf(stderr,"Memory allocating error (%s line %d)\n" 162 fprintf(stderr,"Memory allocating error (%s line %d)\n"
161 ,__FILE__, __LINE__); 163 ,__FILE__, __LINE__);
162 exit(1); 164 exit(1);
163 } 165 }
164 strcpy((char *)(name), ptr); 166 strcpy((char *)(name), ptr);
165 StockPtr->Name = name; 167 StockPtr->Name = name;
166 168
167 ptr = csv_strtok(NULL, ","); 169 ptr = csv_strtok(NULL, ",");
168 if (!ptr) return 0; 170 if (!ptr) return 0;
169 sscanf(ptr,"%f",&(StockPtr->CurrentPrice)); 171 sscanf(ptr,"%f",&(StockPtr->CurrentPrice));
170 172
171 ptr = csv_strtok(NULL, ","); 173 ptr = csv_strtok(NULL, ",");
172 if (!ptr) return 0; 174 if (!ptr) return 0;
173 175
174 date = (char *)malloc(strlen(ptr)+1); 176 date = (char *)malloc(strlen(ptr)+1);
175 if (date==NULL) 177 if (date==NULL)
176 { 178 {
177 fprintf(stderr,"Memory allocating error (%s line %d)\n" 179 fprintf(stderr,"Memory allocating error (%s line %d)\n"
178 ,__FILE__, __LINE__); 180 ,__FILE__, __LINE__);
179 exit(1); 181 exit(1);
180 } 182 }
181 strcpy((char *)(date), ptr); 183 strcpy((char *)(date), ptr);
182 StockPtr->Date = date; 184 StockPtr->Date = date;
183 185
184 ptr = csv_strtok(NULL, ","); 186 ptr = csv_strtok(NULL, ",");
185 if (!ptr) return 0; 187 if (!ptr) return 0;
186 188
187 time = (char *)malloc(strlen(ptr)+1); 189 time = (char *)malloc(strlen(ptr)+1);
188 if (time==NULL) 190 if (time==NULL)
189 { 191 {
190 fprintf(stderr,"Memory allocating error (%s line %d)\n" 192 fprintf(stderr,"Memory allocating error (%s line %d)\n"
191 ,__FILE__, __LINE__); 193 ,__FILE__, __LINE__);
192 exit(1); 194 exit(1);
193 } 195 }
194 strcpy((char *)(time), ptr); 196 strcpy((char *)(time), ptr);
195 StockPtr->Time = time; 197 StockPtr->Time = time;
196 198
197 ptr = csv_strtok(NULL, ","); 199 ptr = csv_strtok(NULL, ",");
198 if (!ptr) return 0; 200 if (!ptr) return 0;
199 sscanf(ptr,"%f",&(StockPtr->Variation)); 201 sscanf(ptr,"%f",&(StockPtr->Variation));
200 202
201 StockPtr->Pourcentage = 100 * StockPtr->Variation / 203 StockPtr->Pourcentage = 100 * StockPtr->Variation /
202 (StockPtr->CurrentPrice - StockPtr->Variation); 204 (StockPtr->CurrentPrice - StockPtr->Variation);
203 205
204 StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation; 206 StockPtr->LastPrice = StockPtr->CurrentPrice - StockPtr->Variation;
205 207
206 ptr = csv_strtok(NULL, ","); 208 ptr = csv_strtok(NULL, ",");
207 if (!ptr) return 0; 209 if (!ptr) return 0;
208 sscanf(ptr,"%f",&(StockPtr->OpenPrice)); 210 sscanf(ptr,"%f",&(StockPtr->OpenPrice));
209 211
210 ptr = csv_strtok(NULL, ","); 212 ptr = csv_strtok(NULL, ",");
211 if (!ptr) return 0; 213 if (!ptr) return 0;
212 sscanf(ptr,"%f",&(StockPtr->MaxPrice)); 214 sscanf(ptr,"%f",&(StockPtr->MaxPrice));
213 215
214 ptr = csv_strtok(NULL, ","); 216 ptr = csv_strtok(NULL, ",");
215 if (!ptr) return 0; 217 if (!ptr) return 0;
216 sscanf(ptr,"%f",&(StockPtr->MinPrice)); 218 sscanf(ptr,"%f",&(StockPtr->MinPrice));
217 219
218 ptr = csv_strtok(NULL, ","); 220 ptr = csv_strtok(NULL, ",");
219 if (!ptr) return 0; 221 if (!ptr) return 0;
220 StockPtr->Volume = atoi(ptr); 222 StockPtr->Volume = atoi(ptr);
221 223
222 if( !FirstStockPtr ) 224 if( !FirstStockPtr )
223 { 225 {
224 FirstStockPtr = StockPtr; 226 FirstStockPtr = StockPtr;
225 StockPtr->PreviousStock = 0; 227 StockPtr->PreviousStock = 0;
226 } 228 }
227 229
228 StockPtr->NextStock = 0; 230 StockPtr->NextStock = 0;
229 231
230 if (LastStockPtr) 232 if (LastStockPtr)
231 { 233 {
232 LastStockPtr->NextStock = StockPtr; 234 LastStockPtr->NextStock = StockPtr;
233 StockPtr->PreviousStock = LastStockPtr; 235 StockPtr->PreviousStock = LastStockPtr;
234 } 236 }
235 237
236 LastStockPtr = StockPtr; 238 LastStockPtr = StockPtr;
237 239
238 } 240 }
239 else 241 else
240 { 242 {
241 /* this symbol is not valid */ 243 /* this symbol is not valid */
242 /* Set the stock struct just with Symbol, all other are NULL */ 244 /* 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 */ 245 /* This can be used to see if the symbol has been reached are not */
244 246
245 StockPtr = malloc_stock(); 247 StockPtr = malloc_stock();
246 248
247 ptr = csv_strtok(line, ","); 249 ptr = csv_strtok(line, ",");
248 if (!ptr) return 0; 250 if (!ptr) return 0;
249 251
250 symbol = (char *)malloc(strlen(ptr)+1); 252 symbol = (char *)malloc(strlen(ptr)+1);
251 if (symbol==NULL) 253 if (symbol==NULL)
252 { 254 {
253 fprintf(stderr,"Memory allocating error (%s line %d)\n" 255 fprintf(stderr,"Memory allocating error (%s line %d)\n"
254 ,__FILE__, __LINE__); 256 ,__FILE__, __LINE__);
255 exit(1); 257 exit(1);
256 } 258 }
257 strcpy((char *)(symbol), ptr); 259 strcpy((char *)(symbol), ptr);
258 StockPtr->Symbol = symbol; 260 StockPtr->Symbol = symbol;
259 261
260 if( !FirstStockPtr ) 262 if( !FirstStockPtr )
261 { 263 {
262 FirstStockPtr = StockPtr; 264 FirstStockPtr = StockPtr;
263 StockPtr->PreviousStock = 0; 265 StockPtr->PreviousStock = 0;
264 } 266 }
265 267
266 StockPtr->NextStock = 0; 268 StockPtr->NextStock = 0;
267 269
268 if (LastStockPtr) 270 if (LastStockPtr)
269 { 271 {
270 LastStockPtr->NextStock = StockPtr; 272 LastStockPtr->NextStock = StockPtr;
271 StockPtr->PreviousStock = LastStockPtr; 273 StockPtr->PreviousStock = LastStockPtr;
272 } 274 }
273 275
274 LastStockPtr = StockPtr; 276 LastStockPtr = StockPtr;
275 } 277 }
276 278
277 end_line++; 279 end_line++;
278 line = end_line; 280 line = end_line;
279 281
280 } 282 }
281 283
282 return (FirstStockPtr); 284 return (FirstStockPtr);
283} 285}
284 286
285/*****************************************************************************/ 287/*****************************************************************************/
286/* Parses the history quotes file and return a stock structure list. */ 288/* Parses the history quotes file and return a stock structure list. */
287/*****************************************************************************/ 289/*****************************************************************************/
288stock *parse_csv_history_file(char *csv_file) 290stock *parse_csv_history_file(char *csv_file)
289{ 291{
290 292
291 char *line; 293 char *line;
292 char *end_line; 294 char *end_line;
293 char *ptr; 295 char *ptr;
294 296
295 int day; 297 int day;
296 char smonth[10]; 298 char smonth[10];
297 int month; 299 int month;
298 int year; 300 int year;
299 301
300 char *date; 302 char *date;
301 303
302 int i; 304 int i;
303 int test; 305 int test;
304 306
305 stock *StockPtr=NULL; 307 stock *StockPtr=NULL;
306 stock *LastStockPtr=NULL; 308 stock *LastStockPtr=NULL;
307 309
308 /* Used to return the pointer to the list */ 310 /* Used to return the pointer to the list */
309 stock *FirstStockPtr=NULL; 311 stock *FirstStockPtr=NULL;
310 312
311 line = csv_file; 313 line = csv_file;
312 end_line = csv_file; 314 end_line = csv_file;
313 315
314 /* do not use the first line */ 316 /* do not use the first line */
315 end_line = strstr(line, "\n"); 317 end_line = strstr(line, "\n");
316 *end_line = 0; 318 *end_line = 0;
317 end_line++; 319 end_line++;
318 line = end_line; 320 line = end_line;
319 321
320 while ((end_line = strstr(line, "\n"))) 322 while ((end_line = strstr(line, "\n")))
321 { 323 {
322 *end_line = 0; 324 *end_line = 0;
323 325
324 StockPtr = malloc_stock(); 326 StockPtr = malloc_stock();
325 327
326 /* Date */ 328 /* Date */
327 ptr = strtok(line, ","); 329 ptr = strtok(line, ",");
328 if (!ptr) return 0; 330 if (!ptr) return 0;
329 331
330 sscanf(ptr,"%d-%3s-%d",&day,smonth,&year); 332 sscanf(ptr,"%d-%3s-%d",&day,smonth,&year);
331 333
332 i=0; 334 i=0;
333 335
334#ifdef __UNIX__ 336#ifdef __UNIX__
335 while((test=strcasecmp(months[i], smonth))) i++; 337 while((test=strcasecmp(months[i], smonth))) i++;
336#elif __WINDOWS__ 338#elif __WINDOWS__
337 while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++; 339 while(test=_mbsnbicmp(months[i], smonth, strlen(months[i]))) i++;
338#endif 340#endif
339 341
340 month = i+1; 342 month = i+1;
341 343
342 date = (char *)malloc(DATE_LENGTH); 344 date = (char *)malloc(DATE_LENGTH);
343 if (date==NULL) 345 if (date==NULL)
344 { 346 {
345 fprintf(stderr,"Memory allocating error (%s line %d)\n" 347 fprintf(stderr,"Memory allocating error (%s line %d)\n"
346 ,__FILE__, __LINE__); 348 ,__FILE__, __LINE__);
347 exit(1); 349 exit(1);
348 } 350 }
349 sprintf(date,"%.2d%.2d%.2d", year, month, day); 351 sprintf(date,"%.2d%.2d%.2d", year, month, day);
350 StockPtr->Date = date; 352 StockPtr->Date = date;
351 353
352 /* Open */ 354 /* Open */
353 ptr = strtok(NULL, ","); 355 ptr = strtok(NULL, ",");
354 if (!ptr) return 0; 356 if (!ptr) return 0;
355 sscanf(ptr,"%f",&(StockPtr->OpenPrice)); 357 sscanf(ptr,"%f",&(StockPtr->OpenPrice));
356 358
357 /* High */ 359 /* High */
358 ptr = strtok(NULL, ","); 360 ptr = strtok(NULL, ",");
359 if (!ptr) return 0; 361 if (!ptr) return 0;
360 sscanf(ptr,"%f",&(StockPtr->MaxPrice)); 362 sscanf(ptr,"%f",&(StockPtr->MaxPrice));
361 363
362 /* Low */ 364 /* Low */
363 ptr = strtok(NULL, ","); 365 ptr = strtok(NULL, ",");
364 if (!ptr) return 0; 366 if (!ptr) return 0;
365 sscanf(ptr,"%f",&(StockPtr->MinPrice)); 367 sscanf(ptr,"%f",&(StockPtr->MinPrice));
366 368
367 /* Close */ 369 /* Close */
368 ptr = strtok(NULL, ","); 370 ptr = strtok(NULL, ",");
369 if (!ptr) return 0; 371 if (!ptr) return 0;
370 sscanf(ptr,"%f",&(StockPtr->LastPrice)); 372 sscanf(ptr,"%f",&(StockPtr->LastPrice));
371 373
372 /* Volume */ 374 /* Volume */
373 375
374 ptr = strtok(NULL, ","); 376 ptr = strtok(NULL, ",");
375 if (!ptr) 377 if (!ptr)
376 /* It seems to be an indice */ 378 /* It seems to be an indice */
377 /* No volume for indices */ 379 /* No volume for indices */
378 StockPtr->Volume = 0; 380 StockPtr->Volume = 0;
379 else 381 else
380 StockPtr->Volume = atoi(ptr); 382 StockPtr->Volume = atoi(ptr);
381 383
382 if( !FirstStockPtr ) 384 if( !FirstStockPtr )
383 { 385 {
384 FirstStockPtr = StockPtr; 386 FirstStockPtr = StockPtr;
385 StockPtr->PreviousStock = 0; 387 StockPtr->PreviousStock = 0;
386 } 388 }
387 389
388 StockPtr->NextStock = 0; 390 StockPtr->NextStock = 0;
389 391
390 if (LastStockPtr) 392 if (LastStockPtr)
391 { 393 {
392 LastStockPtr->NextStock = StockPtr; 394 LastStockPtr->NextStock = StockPtr;
393 StockPtr->PreviousStock = LastStockPtr; 395 StockPtr->PreviousStock = LastStockPtr;
394 } 396 }
395 397
396 LastStockPtr = StockPtr; 398 LastStockPtr = StockPtr;
397 399
398 end_line++; 400 end_line++;
399 line = end_line; 401 line = end_line;
400 } 402 }
401 403
402 return (FirstStockPtr); 404 return (FirstStockPtr);
403} 405}
diff --git a/noncore/todayplugins/stockticker/libstocks/http.c b/noncore/todayplugins/stockticker/libstocks/http.c
index 3078746..2f38f8a 100644
--- a/noncore/todayplugins/stockticker/libstocks/http.c
+++ b/noncore/todayplugins/stockticker/libstocks/http.c
@@ -1,301 +1,303 @@
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 __HTTP_C__ 21#define __HTTP_C__
22#ifndef __UNIX__
22#define __UNIX__ 23#define __UNIX__
24#endif
23 25
24#ifdef __UNIX__ 26#ifdef __UNIX__
25#include <sys/socket.h> 27#include <sys/socket.h>
26#include <netinet/in.h> 28#include <netinet/in.h>
27#include <arpa/inet.h> 29#include <arpa/inet.h>
28#include <netdb.h> 30#include <netdb.h>
29#include <unistd.h> 31#include <unistd.h>
30#elif __WINDOWS__ 32#elif __WINDOWS__
31#include <winsock.h> 33#include <winsock.h>
32#include <mbstring.h> 34#include <mbstring.h>
33#endif 35#endif
34 36
35#include <sys/types.h> 37#include <sys/types.h>
36#include <ctype.h> 38#include <ctype.h>
37#include <string.h> 39#include <string.h>
38#include <stdlib.h> 40#include <stdlib.h>
39#include <stdio.h> 41#include <stdio.h>
40 42
41#include "http.h" 43#include "http.h"
42#include "stocks.h" 44#include "stocks.h"
43 45
44#define BUF_SIZE 1024 46#define BUF_SIZE 1024
45#define HEADER_MAXBUF 512 47#define HEADER_MAXBUF 512
46 48
47/* pointer to proxy server name or NULL */ 49/* pointer to proxy server name or NULL */
48char *http_proxy_server=NULL; 50char *http_proxy_server=NULL;
49/* proxy server port number or 0 */ 51/* proxy server port number or 0 */
50int http_proxy_port=0; 52int http_proxy_port=0;
51 53
52/*****************************************************************************/ 54/*****************************************************************************/
53/* Gets the file from http://http_server/http_file */ 55/* Gets the file from http://http_server/http_file */
54/* This function is used to get quotes csv file from yahoo. */ 56/* This function is used to get quotes csv file from yahoo. */
55/* It allocates memory for the file and defines *pdata (the pointer of datas)*/ 57/* It allocates memory for the file and defines *pdata (the pointer of datas)*/
56/*****************************************************************************/ 58/*****************************************************************************/
57libstocks_return_code http_get(char *http_file, char *http_server, char **pdata) 59libstocks_return_code http_get(char *http_file, char *http_server, char **pdata)
58{ 60{
59 struct hostent *host; /* structs needed by socket */ 61 struct hostent *host; /* structs needed by socket */
60 struct sockaddr_in server; 62 struct sockaddr_in server;
61 int s; /* socket descriptor */ 63 int s; /* socket descriptor */
62 char header[HEADER_MAXBUF]; /* request header */ 64 char header[HEADER_MAXBUF]; /* request header */
63 int hlg; /* header length */ 65 int hlg; /* header length */
64 char buf[BUF_SIZE+1]; /* tempory buffer from socket read */ 66 char buf[BUF_SIZE+1]; /* tempory buffer from socket read */
65 int r; /* number of bytes read by read function */ 67 int r; /* number of bytes read by read function */
66 char *data=NULL; /* http server response */ 68 char *data=NULL; /* http server response */
67 int data_lgr; /* http server response length */ 69 int data_lgr; /* http server response length */
68 char *temp; /* pointer used to split header and csv */ 70 char *temp; /* pointer used to split header and csv */
69 int error_code; /* error code returned by http server */ 71 int error_code; /* error code returned by http server */
70 char *csv_ptr; /* pointer to the csv content */ 72 char *csv_ptr; /* pointer to the csv content */
71 int header_founded; /* test if header is founded */ 73 int header_founded; /* test if header is founded */
72 74
73#ifdef DEBUG 75#ifdef DEBUG
74 printf("*http_get\n"); 76 printf("*http_get\n");
75#endif 77#endif
76 78
77 /* get host info by name :*/ 79 /* get host info by name :*/
78 if ((host = gethostbyname( http_proxy_server ? http_proxy_server : http_server))) 80 if ((host = gethostbyname( http_proxy_server ? http_proxy_server : http_server)))
79 { 81 {
80 memset((char *) &server,0, sizeof(server)); 82 memset((char *) &server,0, sizeof(server));
81 memmove((char *) &server.sin_addr, host->h_addr, host->h_length); 83 memmove((char *) &server.sin_addr, host->h_addr, host->h_length);
82 server.sin_family = host->h_addrtype; 84 server.sin_family = host->h_addrtype;
83 server.sin_port = (unsigned short) htons( http_proxy_server ? http_proxy_port : 80 ); 85 server.sin_port = (unsigned short) htons( http_proxy_server ? http_proxy_port : 80 );
84 86
85 } else 87 } else
86 { 88 {
87 89
88#ifdef DEBUG 90#ifdef DEBUG
89 printf(" gethostbyname : NOK\n"); 91 printf(" gethostbyname : NOK\n");
90#endif 92#endif
91 return ERRHOST; 93 return ERRHOST;
92 } 94 }
93 95
94 /* create socket */ 96 /* create socket */
95 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) 97 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
96 { 98 {
97 99
98#ifdef DEBUG 100#ifdef DEBUG
99 printf(" create socket : NOK\n"); 101 printf(" create socket : NOK\n");
100#endif 102#endif
101 return ERRSOCK; 103 return ERRSOCK;
102 } 104 }
103 105
104 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0); 106 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
105 107
106 /* connect to server */ 108 /* connect to server */
107 if (connect(s, &server, sizeof(server)) < 0) 109 if (connect(s, &server, sizeof(server)) < 0)
108 { 110 {
109#ifdef DEBUG 111#ifdef DEBUG
110 printf(" connect to server : NOK\n"); 112 printf(" connect to server : NOK\n");
111#endif 113#endif
112 114
113#ifdef __UNIX__ 115#ifdef __UNIX__
114 close(s); 116 close(s);
115#elif __WINDOWS__ 117#elif __WINDOWS__
116 closesocket(s); 118 closesocket(s);
117#endif 119#endif
118 return ERRCONN; 120 return ERRCONN;
119 } 121 }
120 122
121 /* create header */ 123 /* create header */
122 if (http_proxy_server) 124 if (http_proxy_server)
123 { 125 {
124 sprintf(header,"GET http://%.128s:80%.256s HTTP/1.0\015\012\015\012", 126 sprintf(header,"GET http://%.128s:80%.256s HTTP/1.0\015\012\015\012",
125 http_server, http_file); 127 http_server, http_file);
126 } 128 }
127 else 129 else
128 { 130 {
129 sprintf(header,"GET %s HTTP/1.0\015\012\015\012",http_file); 131 sprintf(header,"GET %s HTTP/1.0\015\012\015\012",http_file);
130 } 132 }
131 133
132 hlg=strlen(header); 134 hlg=strlen(header);
133 135
134 /* send header */ 136 /* send header */
135#ifdef __UNIX__ 137#ifdef __UNIX__
136 if (write(s,header,hlg)!=hlg) 138 if (write(s,header,hlg)!=hlg)
137#elif __WINDOWS__ 139#elif __WINDOWS__
138 if (send(s,header,hlg, 0)!=hlg) 140 if (send(s,header,hlg, 0)!=hlg)
139#endif 141#endif
140 { 142 {
141#ifdef DEBUG 143#ifdef DEBUG
142 printf(" send header : NOK\n"); 144 printf(" send header : NOK\n");
143#endif 145#endif
144 return ERRWHEA; 146 return ERRWHEA;
145 } 147 }
146 148
147 data_lgr = 0; 149 data_lgr = 0;
148 r=1; 150 r=1;
149 while(r) 151 while(r)
150 { 152 {
151 /* Clear Buffer */ 153 /* Clear Buffer */
152 memset(buf,0,BUF_SIZE+1); 154 memset(buf,0,BUF_SIZE+1);
153 155
154#ifdef __UNIX__ 156#ifdef __UNIX__
155 r=read(s,buf,BUF_SIZE); 157 r=read(s,buf,BUF_SIZE);
156#elif __WINDOWS__ 158#elif __WINDOWS__
157 r=recv(s,buf,BUF_SIZE,0); 159 r=recv(s,buf,BUF_SIZE,0);
158#endif 160#endif
159 161
160 if (r) 162 if (r)
161 { 163 {
162 if(!data_lgr) 164 if(!data_lgr)
163 { 165 {
164 if((data = malloc(r+1))==NULL) 166 if((data = malloc(r+1))==NULL)
165 { 167 {
166 fprintf(stderr,"Memory allocating error (%s line %d)\n" 168 fprintf(stderr,"Memory allocating error (%s line %d)\n"
167 ,__FILE__, __LINE__); 169 ,__FILE__, __LINE__);
168 exit(1); 170 exit(1);
169 } 171 }
170 172
171 memcpy(data,buf,r); 173 memcpy(data,buf,r);
172 data_lgr = r; 174 data_lgr = r;
173 data[r]=0; 175 data[r]=0;
174 } 176 }
175 else 177 else
176 { 178 {
177 if((temp = malloc(r+data_lgr+1))==NULL) 179 if((temp = malloc(r+data_lgr+1))==NULL)
178 { 180 {
179 fprintf(stderr,"Memory allocating error (%s line %d)\n" 181 fprintf(stderr,"Memory allocating error (%s line %d)\n"
180 ,__FILE__, __LINE__); 182 ,__FILE__, __LINE__);
181 exit(1); 183 exit(1);
182 } 184 }
183 memcpy(temp, data, data_lgr); 185 memcpy(temp, data, data_lgr);
184 memcpy(temp+data_lgr, buf, r); 186 memcpy(temp+data_lgr, buf, r);
185 temp[r+data_lgr]=0; 187 temp[r+data_lgr]=0;
186 data_lgr += r; 188 data_lgr += r;
187 free(data); 189 free(data);
188 data = temp; 190 data = temp;
189 } 191 }
190 } 192 }
191 } 193 }
192 194
193 /* close socket */ 195 /* close socket */
194#ifdef __UNIX__ 196#ifdef __UNIX__
195 close(s); 197 close(s);
196#elif __WINDOWS__ 198#elif __WINDOWS__
197 closesocket(s); 199 closesocket(s);
198#endif 200#endif
199 201
200#ifdef DEBUG 202#ifdef DEBUG
201 printf("%s\n", data); 203 printf("%s\n", data);
202#endif 204#endif
203 205
204 /* get headers to test status line */ 206 /* get headers to test status line */
205 /* and to split headers and content */ 207 /* and to split headers and content */
206 208
207 temp = data; 209 temp = data;
208 header_founded = 0; 210 header_founded = 0;
209 while( !header_founded ) 211 while( !header_founded )
210 { 212 {
211 if (*temp==0) return ERRRHEA; 213 if (*temp==0) return ERRRHEA;
212 214
213 if( *temp==0x0A ) 215 if( *temp==0x0A )
214 { 216 {
215 /* test if it is the header end */ 217 /* test if it is the header end */
216 temp ++; 218 temp ++;
217 if (*temp == 0x0D) temp++; 219 if (*temp == 0x0D) temp++;
218 if (*temp == 0x0A) header_founded = 1; 220 if (*temp == 0x0A) header_founded = 1;
219 } 221 }
220 else 222 else
221 temp++; 223 temp++;
222 } 224 }
223 225
224 *temp = 0; 226 *temp = 0;
225 temp++; 227 temp++;
226 228
227 sscanf(data,"HTTP/1.%*d %03d",&error_code); 229 sscanf(data,"HTTP/1.%*d %03d",&error_code);
228 230
229 if (error_code != 200) 231 if (error_code != 200)
230 { 232 {
231#ifdef DEBUG 233#ifdef DEBUG
232 printf(" HTTP error code : %d\n", error_code); 234 printf(" HTTP error code : %d\n", error_code);
233#endif 235#endif
234 free(data); 236 free(data);
235 return ERRPAHD; 237 return ERRPAHD;
236 } 238 }
237 239
238 if ((csv_ptr = malloc(strlen(temp)+1))==NULL) 240 if ((csv_ptr = malloc(strlen(temp)+1))==NULL)
239 { 241 {
240 free(data); 242 free(data);
241 fprintf(stderr,"Memory allocating error (%s line %d)\n" 243 fprintf(stderr,"Memory allocating error (%s line %d)\n"
242 ,__FILE__, __LINE__); 244 ,__FILE__, __LINE__);
243 exit(1); 245 exit(1);
244 } 246 }
245 247
246 memcpy(csv_ptr, temp, strlen(temp)+1); 248 memcpy(csv_ptr, temp, strlen(temp)+1);
247 free(data); 249 free(data);
248 250
249#ifdef DEBUG 251#ifdef DEBUG
250 printf(" CSV\n"); 252 printf(" CSV\n");
251 printf("%s,\n", csv_ptr); 253 printf("%s,\n", csv_ptr);
252#endif 254#endif
253 255
254 *pdata = csv_ptr; 256 *pdata = csv_ptr;
255 257
256 return 0; 258 return 0;
257} 259}
258 260
259/******************************************************************************/ 261/******************************************************************************/
260/* Set the proxy server to use */ 262/* Set the proxy server to use */
261/******************************************************************************/ 263/******************************************************************************/
262libstocks_return_code set_proxy(char *proxy) 264libstocks_return_code set_proxy(char *proxy)
263{ 265{
264 char *ptr; 266 char *ptr;
265 char c; 267 char c;
266 268
267#ifdef DEBUG 269#ifdef DEBUG
268 printf("*set_proxy\n"); 270 printf("*set_proxy\n");
269#endif 271#endif
270 272
271 /* Parse the proxy URL - It must start with http:// */ 273 /* Parse the proxy URL - It must start with http:// */
272#ifdef __UNIX__ 274#ifdef __UNIX__
273 if (strncasecmp("http://",proxy,7)) return ERRPROX; 275 if (strncasecmp("http://",proxy,7)) return ERRPROX;
274#elif __WINDOWS__ 276#elif __WINDOWS__
275 if (_mbsnbicmp("http://",proxy,7)) return ERRPROX; 277 if (_mbsnbicmp("http://",proxy,7)) return ERRPROX;
276#endif 278#endif
277 279
278 proxy+=7; 280 proxy+=7;
279 281
280 /* find ":" in the proxy url */ 282 /* find ":" in the proxy url */
281 ptr = proxy; 283 ptr = proxy;
282 for (c=*ptr; (c && c!=':');) c=*ptr++; 284 for (c=*ptr; (c && c!=':');) c=*ptr++;
283 285
284 /* ptr points just after the ":" or at the end of proxy if : not founded */ 286 /* ptr points just after the ":" or at the end of proxy if : not founded */
285 *(ptr-1)=0; /* clear the ":" */ 287 *(ptr-1)=0; /* clear the ":" */
286 288
287 http_proxy_server=strdup(proxy); 289 http_proxy_server=strdup(proxy);
288 290
289#ifdef DEBUG 291#ifdef DEBUG
290 printf("http_proxy_server : %s\n", http_proxy_server); 292 printf("http_proxy_server : %s\n", http_proxy_server);
291#endif 293#endif
292 294
293 /* get the port number of the url */ 295 /* get the port number of the url */
294 if (sscanf(ptr,"%d",&http_proxy_port)!=1) return ERRPROX; 296 if (sscanf(ptr,"%d",&http_proxy_port)!=1) return ERRPROX;
295 297
296#ifdef DEBUG 298#ifdef DEBUG
297 printf("http_proxy_port : %d\n", http_proxy_port); 299 printf("http_proxy_port : %d\n", http_proxy_port);
298#endif 300#endif
299 301
300 return 0; 302 return 0;
301} 303}