Imported Upstream version 7.48.0
[platform/upstream/curl.git] / docs / examples / sampleconv.c
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  * This is a simple example showing how a program on a non-ASCII platform
24  * would invoke callbacks to do its own codeset conversions instead of
25  * using the built-in iconv functions in libcurl.
26  * </DESC>
27  */
28 /*
29
30    The IBM-1047 EBCDIC codeset is used for this example but the code
31    would be similar for other non-ASCII codesets.
32
33    Three callback functions are created below:
34         my_conv_from_ascii_to_ebcdic,
35         my_conv_from_ebcdic_to_ascii, and
36         my_conv_from_utf8_to_ebcdic
37
38    The "platform_xxx" calls represent platform-specific conversion routines.
39
40  */
41
42 #include <stdio.h>
43 #include <curl/curl.h>
44
45 CURLcode my_conv_from_ascii_to_ebcdic(char *buffer, size_t length)
46 {
47     char *tempptrin, *tempptrout;
48     size_t bytes = length;
49     int rc;
50     tempptrin = tempptrout = buffer;
51     rc = platform_a2e(&tempptrin, &bytes, &tempptrout, &bytes);
52     if(rc == PLATFORM_CONV_OK) {
53       return CURLE_OK;
54     }
55     else {
56       return CURLE_CONV_FAILED;
57     }
58 }
59
60 CURLcode my_conv_from_ebcdic_to_ascii(char *buffer, size_t length)
61 {
62     char *tempptrin, *tempptrout;
63     size_t bytes = length;
64     int rc;
65     tempptrin = tempptrout = buffer;
66     rc = platform_e2a(&tempptrin, &bytes, &tempptrout, &bytes);
67     if(rc == PLATFORM_CONV_OK) {
68       return CURLE_OK;
69     }
70     else {
71       return CURLE_CONV_FAILED;
72     }
73 }
74
75 CURLcode my_conv_from_utf8_to_ebcdic(char *buffer, size_t length)
76 {
77     char *tempptrin, *tempptrout;
78     size_t bytes = length;
79     int rc;
80     tempptrin = tempptrout = buffer;
81     rc = platform_u2e(&tempptrin, &bytes, &tempptrout, &bytes);
82     if(rc == PLATFORM_CONV_OK) {
83       return CURLE_OK;
84     }
85     else {
86       return CURLE_CONV_FAILED;
87     }
88 }
89
90 int main(void)
91 {
92   CURL *curl;
93   CURLcode res;
94
95   curl = curl_easy_init();
96   if(curl) {
97     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
98
99     /* use platform-specific functions for codeset conversions */
100     curl_easy_setopt(curl, CURLOPT_CONV_FROM_NETWORK_FUNCTION,
101                      my_conv_from_ascii_to_ebcdic);
102     curl_easy_setopt(curl, CURLOPT_CONV_TO_NETWORK_FUNCTION,
103                      my_conv_from_ebcdic_to_ascii);
104     curl_easy_setopt(curl, CURLOPT_CONV_FROM_UTF8_FUNCTION,
105                      my_conv_from_utf8_to_ebcdic);
106
107     res = curl_easy_perform(curl);
108
109     /* always cleanup */
110     curl_easy_cleanup(curl);
111   }
112   return 0;
113 }