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