Git init
[external/curl.git] / docs / examples / sampleconv.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9 /*
10    This is a simple example showing how a program on a non-ASCII platform
11    would invoke callbacks to do its own codeset conversions instead of
12    using the built-in iconv functions in libcurl.
13
14    The IBM-1047 EBCDIC codeset is used for this example but the code
15    would be similar for other non-ASCII codesets.
16
17    Three callback functions are created below:
18         my_conv_from_ascii_to_ebcdic,
19         my_conv_from_ebcdic_to_ascii, and
20         my_conv_from_utf8_to_ebcdic
21
22    The "platform_xxx" calls represent platform-specific conversion routines.
23
24  */
25
26 #include <stdio.h>
27 #include <curl/curl.h>
28
29 CURLcode my_conv_from_ascii_to_ebcdic(char *buffer, size_t length)
30 {
31     char *tempptrin, *tempptrout;
32     size_t bytes = length;
33     int rc;
34     tempptrin = tempptrout = buffer;
35     rc = platform_a2e(&tempptrin, &bytes, &tempptrout, &bytes);
36     if (rc == PLATFORM_CONV_OK) {
37       return(CURLE_OK);
38     } else {
39       return(CURLE_CONV_FAILED);
40     }
41 }
42
43 CURLcode my_conv_from_ebcdic_to_ascii(char *buffer, size_t length)
44 {
45     char *tempptrin, *tempptrout;
46     size_t bytes = length;
47     int rc;
48     tempptrin = tempptrout = buffer;
49     rc = platform_e2a(&tempptrin, &bytes, &tempptrout, &bytes);
50     if (rc == PLATFORM_CONV_OK) {
51       return(CURLE_OK);
52     } else {
53       return(CURLE_CONV_FAILED);
54     }
55 }
56
57 CURLcode my_conv_from_utf8_to_ebcdic(char *buffer, size_t length)
58 {
59     char *tempptrin, *tempptrout;
60     size_t bytes = length;
61     int rc;
62     tempptrin = tempptrout = buffer;
63     rc = platform_u2e(&tempptrin, &bytes, &tempptrout, &bytes);
64     if (rc == PLATFORM_CONV_OK) {
65       return(CURLE_OK);
66     } else {
67       return(CURLE_CONV_FAILED);
68     }
69 }
70
71 int main(void)
72 {
73   CURL *curl;
74   CURLcode res;
75
76   curl = curl_easy_init();
77   if(curl) {
78     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
79
80     /* use platform-specific functions for codeset conversions */
81     curl_easy_setopt(curl, CURLOPT_CONV_FROM_NETWORK_FUNCTION,
82                      my_conv_from_ascii_to_ebcdic);
83     curl_easy_setopt(curl, CURLOPT_CONV_TO_NETWORK_FUNCTION,
84                      my_conv_from_ebcdic_to_ascii);
85     curl_easy_setopt(curl, CURLOPT_CONV_FROM_UTF8_FUNCTION,
86                      my_conv_from_utf8_to_ebcdic);
87
88     res = curl_easy_perform(curl);
89
90     /* always cleanup */
91     curl_easy_cleanup(curl);
92   }
93   return 0;
94 }