doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
writeout.c
Go to the documentation of this file.
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 */
59  VAR_NUM_OF_VARS /* must be the last */
60 } replaceid;
61 
62 struct variable {
63  const char *name;
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 ==
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;
151  if(CURLE_OK ==
152  curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &doubleinfo))
153  fprintf(stream, "%.3f", doubleinfo);
154  break;
156  if(CURLE_OK ==
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 }
replaceid
Definition: writeout.c:43
CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info,...)
Definition: easy.c:299
const char * name
Definition: writeout.c:63
int i
Definition: process.py:33
replaceid id
Definition: writeout.c:64
void ourWriteOut(CURL *curl, char *writeinfo)
Definition: writeout.c:86
GLuint GLuint end
Definition: glext.h:2845
#define NULL
Definition: Lib.h:88
int fputc(int, FILE *)
Definition: curl.h:210
const GLcharARB * name
Definition: glext.h:3629
void CURL
Definition: types.h:25
int() curl_strequal(const char *s1, const char *s2)
Definition: strequal.c:37