]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/src/writeout.c
hello world
[icculus/iodoom3.git] / neo / curl / src / writeout.c
1 /***************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  * 
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: writeout.c,v 1.21 2004/02/25 15:41:36 bagder Exp $
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
34 #endif
35
36 #include <curl/curl.h>
37
38 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
39 #include <curl/mprintf.h>
40
41 #include "writeout.h"
42
43 typedef enum {
44   VAR_NONE,       /* must be the first */
45   VAR_TOTAL_TIME,
46   VAR_NAMELOOKUP_TIME,
47   VAR_CONNECT_TIME,
48   VAR_PRETRANSFER_TIME,
49   VAR_STARTTRANSFER_TIME,
50   VAR_SIZE_DOWNLOAD,
51   VAR_SIZE_UPLOAD,
52   VAR_SPEED_DOWNLOAD,
53   VAR_SPEED_UPLOAD,
54   VAR_HTTP_CODE,
55   VAR_HEADER_SIZE,
56   VAR_REQUEST_SIZE,
57   VAR_EFFECTIVE_URL,
58   VAR_CONTENT_TYPE,
59   VAR_NUM_OF_VARS /* must be the last */
60 } replaceid;
61
62 struct variable {
63   const char *name;
64   replaceid id;
65 };
66
67
68 static struct variable replacements[]={
69   {"url_effective", VAR_EFFECTIVE_URL},
70   {"http_code", VAR_HTTP_CODE},
71   {"time_total", VAR_TOTAL_TIME},
72   {"time_namelookup", VAR_NAMELOOKUP_TIME},
73   {"time_connect", VAR_CONNECT_TIME},
74   {"time_pretransfer", VAR_PRETRANSFER_TIME},
75   {"time_starttransfer", VAR_STARTTRANSFER_TIME},
76   {"size_header", VAR_HEADER_SIZE},
77   {"size_request", VAR_REQUEST_SIZE},
78   {"size_download", VAR_SIZE_DOWNLOAD},
79   {"size_upload", VAR_SIZE_UPLOAD},
80   {"speed_download", VAR_SPEED_DOWNLOAD},
81   {"speed_upload", VAR_SPEED_UPLOAD},
82   {"content_type", VAR_CONTENT_TYPE},
83   {NULL, VAR_NONE}
84 };
85
86 void ourWriteOut(CURL *curl, char *writeinfo)
87 {
88   FILE *stream = stdout;
89   char *ptr=writeinfo;
90   char *stringp;
91   long longinfo;
92   double doubleinfo;
93
94   while(*ptr) {
95     if('%' == *ptr) {
96       if('%' == ptr[1]) {
97         /* an escaped %-letter */
98         fputc('%', stream);
99         ptr+=2;
100       }
101       else {
102         /* this is meant as a variable to output */
103         char *end;
104         char keepit;
105         int i;
106         if(('{' == ptr[1]) && (end=strchr(ptr, '}'))) {
107           ptr+=2; /* pass the % and the { */
108           keepit=*end;
109           *end=0; /* zero terminate */
110           for(i=0; replacements[i].name; i++) {
111             if(curl_strequal(ptr, replacements[i].name)) {
112               switch(replacements[i].id) {
113               case VAR_EFFECTIVE_URL:
114                 if((CURLE_OK ==
115                     curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &stringp))
116                    && stringp)
117                   fputs(stringp, stream);
118                 break;
119               case VAR_HTTP_CODE:
120                 if(CURLE_OK ==
121                    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &longinfo))
122                   fprintf(stream, "%03ld", longinfo);
123                 break;
124               case VAR_HEADER_SIZE:
125                 if(CURLE_OK ==
126                    curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &longinfo))
127                   fprintf(stream, "%ld", longinfo);
128                 break;
129               case VAR_REQUEST_SIZE:
130                 if(CURLE_OK ==
131                    curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &longinfo))
132                   fprintf(stream, "%ld", longinfo);
133                 break;
134               case VAR_TOTAL_TIME:
135                 if(CURLE_OK ==
136                    curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &doubleinfo))
137                   fprintf(stream, "%.3f", doubleinfo);
138                 break;
139               case VAR_NAMELOOKUP_TIME:
140                 if(CURLE_OK ==
141                    curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME,
142                                      &doubleinfo))
143                   fprintf(stream, "%.3f", doubleinfo);
144                 break;
145               case VAR_CONNECT_TIME:
146                 if(CURLE_OK ==
147                    curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &doubleinfo))
148                   fprintf(stream, "%.3f", doubleinfo);
149                 break;
150               case VAR_PRETRANSFER_TIME:
151                 if(CURLE_OK ==
152                    curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &doubleinfo))
153                   fprintf(stream, "%.3f", doubleinfo);
154                 break;
155               case VAR_STARTTRANSFER_TIME:
156                 if(CURLE_OK ==
157                    curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &doubleinfo))
158                   fprintf(stream, "%.3f", doubleinfo);
159                 break;
160               case VAR_SIZE_UPLOAD:
161                 if(CURLE_OK ==
162                    curl_easy_getinfo(curl, CURLINFO_SIZE_UPLOAD, &doubleinfo))
163                   fprintf(stream, "%.0f", doubleinfo);
164                 break;
165               case VAR_SIZE_DOWNLOAD:
166                 if(CURLE_OK ==
167                    curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &doubleinfo))
168                   fprintf(stream, "%.0f", doubleinfo);
169                 break;
170               case VAR_SPEED_DOWNLOAD:
171                 if(CURLE_OK ==
172                    curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &doubleinfo))
173                   fprintf(stream, "%.3f", doubleinfo);
174                 break;
175               case VAR_SPEED_UPLOAD:
176                 if(CURLE_OK ==
177                    curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &doubleinfo))
178                   fprintf(stream, "%.3f", doubleinfo);
179                 break;
180               case VAR_CONTENT_TYPE:
181                 if((CURLE_OK ==
182                     curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &stringp))
183                    && stringp)
184                   fputs(stringp, stream);
185                 break;
186               default:
187                 /* -Wunreachable-code wrongly complains on this */
188                 break;
189               }
190               break;
191             }
192           }
193           ptr=end+1; /* pass the end */
194           *end = keepit;
195         }
196         else {
197           /* illegal syntax, then just output the characters that are used */
198           fputc('%', stream);
199           fputc(ptr[1], stream);
200           ptr+=2;
201         }
202       }
203     }
204     else if('\\' == *ptr) {
205       switch(ptr[1]) {
206       case 'r':
207         fputc('\r', stream);
208         break;
209       case 'n':
210         fputc('\n', stream);
211         break;
212       case 't':
213         fputc('\t', stream);
214         break;
215       default:
216         /* unknown, just output this */
217         fputc(*ptr, stream);
218         fputc(ptr[1], stream);
219         break;
220       }
221       ptr+=2;
222     }
223     else {
224       fputc(*ptr, stream);
225       ptr++;
226     }
227   }
228   
229 }