mirror of
https://github.com/curl/curl.git
synced 2026-07-16 07:37:15 +03:00
examples: check more errors, fix cleanups, scope variables
Inspired by Joshua's report on examples. Closes #19055
This commit is contained in:
parent
61dcb56743
commit
64ed2ea196
42 changed files with 874 additions and 785 deletions
|
|
@ -117,56 +117,60 @@ static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb,
|
|||
int main(void)
|
||||
{
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
XML_Parser parser;
|
||||
struct ParserStruct state;
|
||||
|
||||
/* Initialize the state structure for parsing. */
|
||||
memset(&state, 0, sizeof(state));
|
||||
state.ok = 1;
|
||||
|
||||
/* Initialize a namespace-aware parser. */
|
||||
parser = XML_ParserCreateNS(NULL, '\0');
|
||||
XML_SetUserData(parser, &state);
|
||||
XML_SetElementHandler(parser, startElement, endElement);
|
||||
XML_SetCharacterDataHandler(parser, characterDataHandler);
|
||||
|
||||
res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if(res)
|
||||
return (int)res;
|
||||
|
||||
/* Initialize a libcurl handle. */
|
||||
curl_handle = curl_easy_init();
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL,
|
||||
"https://www.w3schools.com/xml/simple.xml");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
|
||||
if(curl) {
|
||||
XML_Parser parser;
|
||||
struct ParserStruct state;
|
||||
|
||||
printf("Depth Characters Closing Tag\n");
|
||||
/* Initialize the state structure for parsing. */
|
||||
memset(&state, 0, sizeof(state));
|
||||
state.ok = 1;
|
||||
|
||||
/* Perform the request and any follow-up parsing. */
|
||||
res = curl_easy_perform(curl_handle);
|
||||
if(res != CURLE_OK) {
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
||||
curl_easy_strerror(res));
|
||||
}
|
||||
else if(state.ok) {
|
||||
/* Expat requires one final call to finalize parsing. */
|
||||
if(XML_Parse(parser, NULL, 0, 1) == 0) {
|
||||
enum XML_Error error_code = XML_GetErrorCode(parser);
|
||||
fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
|
||||
error_code, XML_ErrorString(error_code));
|
||||
/* Initialize a namespace-aware parser. */
|
||||
parser = XML_ParserCreateNS(NULL, '\0');
|
||||
XML_SetUserData(parser, &state);
|
||||
XML_SetElementHandler(parser, startElement, endElement);
|
||||
XML_SetCharacterDataHandler(parser, characterDataHandler);
|
||||
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL,
|
||||
"https://www.w3schools.com/xml/simple.xml");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser);
|
||||
|
||||
printf("Depth Characters Closing Tag\n");
|
||||
|
||||
/* Perform the request and any follow-up parsing. */
|
||||
res = curl_easy_perform(curl_handle);
|
||||
if(res != CURLE_OK) {
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n",
|
||||
curl_easy_strerror(res));
|
||||
}
|
||||
else {
|
||||
printf(" --------------\n");
|
||||
printf(" %lu tags total\n", state.tags);
|
||||
else if(state.ok) {
|
||||
/* Expat requires one final call to finalize parsing. */
|
||||
if(XML_Parse(parser, NULL, 0, 1) == 0) {
|
||||
enum XML_Error error_code = XML_GetErrorCode(parser);
|
||||
fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n",
|
||||
error_code, XML_ErrorString(error_code));
|
||||
}
|
||||
else {
|
||||
printf(" --------------\n");
|
||||
printf(" %lu tags total\n", state.tags);
|
||||
}
|
||||
}
|
||||
|
||||
/* Clean up. */
|
||||
free(state.characters.memory);
|
||||
XML_ParserFree(parser);
|
||||
|
||||
curl_easy_cleanup(curl_handle);
|
||||
}
|
||||
|
||||
/* Clean up. */
|
||||
free(state.characters.memory);
|
||||
XML_ParserFree(parser);
|
||||
curl_easy_cleanup(curl_handle);
|
||||
curl_global_cleanup();
|
||||
|
||||
return (int)res;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue