xmlstream.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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 https://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  ***************************************************************************/
22 /* <DESC>
23  * Stream-parse a document using the streaming Expat parser.
24  * </DESC>
25  */
26 /* Written by David Strauss
27  *
28  * Expat => http://www.libexpat.org/
29  *
30  * gcc -Wall -I/usr/local/include xmlstream.c -lcurl -lexpat -o xmlstream
31  *
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38 
39 #include <expat.h>
40 #include <curl/curl.h>
41 
42 struct MemoryStruct {
43  char *memory;
44  size_t size;
45 };
46 
47 struct ParserStruct {
48  int ok;
49  size_t tags;
50  size_t depth;
51  struct MemoryStruct characters;
52 };
53 
54 static void startElement(void *userData, const XML_Char *name,
55  const XML_Char **atts)
56 {
57  struct ParserStruct *state = (struct ParserStruct *) userData;
58  state->tags++;
59  state->depth++;
60 
61  /* Get a clean slate for reading in character data. */
62  free(state->characters.memory);
63  state->characters.memory = NULL;
64  state->characters.size = 0;
65 }
66 
67 static void characterDataHandler(void *userData, const XML_Char *s, int len)
68 {
69  struct ParserStruct *state = (struct ParserStruct *) userData;
70  struct MemoryStruct *mem = &state->characters;
71 
72  mem->memory = realloc(mem->memory, mem->size + len + 1);
73  if(mem->memory == NULL) {
74  /* Out of memory. */
75  fprintf(stderr, "Not enough memory (realloc returned NULL).\n");
76  state->ok = 0;
77  return;
78  }
79 
80  memcpy(&(mem->memory[mem->size]), s, len);
81  mem->size += len;
82  mem->memory[mem->size] = 0;
83 }
84 
85 static void endElement(void *userData, const XML_Char *name)
86 {
87  struct ParserStruct *state = (struct ParserStruct *) userData;
88  state->depth--;
89 
90  printf("%5lu %10lu %s\n", state->depth, state->characters.size, name);
91 }
92 
93 static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb,
94  void *userp)
95 {
96  XML_Parser parser = (XML_Parser) userp;
97  size_t real_size = length * nmemb;
98  struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser);
99 
100  /* Only parse if we're not already in a failure state. */
101  if(state->ok && XML_Parse(parser, contents, real_size, 0) == 0) {
102  int error_code = XML_GetErrorCode(parser);
103  fprintf(stderr, "Parsing response buffer of length %lu failed"
104  " with error code %d (%s).\n",
105  real_size, error_code, XML_ErrorString(error_code));
106  state->ok = 0;
107  }
108 
109  return real_size;
110 }
111 
112 int main(void)
113 {
114  CURL *curl_handle;
115  CURLcode res;
116  XML_Parser parser;
117  struct ParserStruct state;
118 
119  /* Initialize the state structure for parsing. */
120  memset(&state, 0, sizeof(struct ParserStruct));
121  state.ok = 1;
122 
123  /* Initialize a namespace-aware parser. */
124  parser = XML_ParserCreateNS(NULL, '\0');
125  XML_SetUserData(parser, &state);
126  XML_SetElementHandler(parser, startElement, endElement);
127  XML_SetCharacterDataHandler(parser, characterDataHandler);
128 
129  /* Initialize a libcurl handle. */
131  curl_handle = curl_easy_init();
132  curl_easy_setopt(curl_handle, CURLOPT_URL,
133  "http://www.w3schools.com/xml/simple.xml");
134  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
135  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
136 
137  printf("Depth Characters Closing Tag\n");
138 
139  /* Perform the request and any follow-up parsing. */
140  res = curl_easy_perform(curl_handle);
141  if(res != CURLE_OK) {
142  fprintf(stderr, "curl_easy_perform() failed: %s\n",
143  curl_easy_strerror(res));
144  }
145  else if(state.ok) {
146  /* Expat requires one final call to finalize parsing. */
147  if(XML_Parse(parser, NULL, 0, 1) == 0) {
148  int error_code = XML_GetErrorCode(parser);
149  fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
150  error_code, XML_ErrorString(error_code));
151  }
152  else {
153  printf(" --------------\n");
154  printf(" %lu tags total\n", state.tags);
155  }
156  }
157 
158  /* Clean up. */
159  free(state.characters.memory);
160  XML_ParserFree(parser);
161  curl_easy_cleanup(curl_handle);
163 
164  return 0;
165 }
#define free(ptr)
Definition: curl_memory.h:130
#define state(x, y)
Definition: ftp.c:100
size_t tags
Definition: xmlstream.c:49
static void startElement(void *userData, const XML_Char *name, const XML_Char **atts)
Definition: xmlstream.c:54
CURLcode
Definition: curl.h:454
static int res
#define realloc(ptr, size)
Definition: curl_memory.h:128
int main(void)
Definition: xmlstream.c:112
char * memory
Definition: getinmemory.c:35
static void characterDataHandler(void *userData, const XML_Char *s, int len)
Definition: xmlstream.c:67
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
size_t len
Definition: curl_sasl.c:55
memcpy(filename, filename1, strlen(filename1))
static void endElement(void *userData, const XML_Char *name)
Definition: xmlstream.c:85
size_t depth
Definition: xmlstream.c:50
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
size_t size
Definition: getinmemory.c:36
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb, void *userp)
Definition: xmlstream.c:93
Definition: curl.h:455
struct MemoryStruct characters
Definition: xmlstream.c:51
CURLM * curl_handle
Definition: multi-uv.c:41
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
parser
void CURL
Definition: curl.h:102
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
#define CURL_GLOBAL_SSL
Definition: curl.h:2517
const char * name
Definition: curl_sasl.c:54
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:17