tizen 2.3.1 release
[external/curl.git] / docs / examples / sampleconv.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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  ***************************************************************************/
22 /*
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
27    The IBM-1047 EBCDIC codeset is used for this example but the code
28    would be similar for other non-ASCII codesets.
29
30    Three callback functions are created below:
31         my_conv_from_ascii_to_ebcdic,
32         my_conv_from_ebcdic_to_ascii, and
33         my_conv_from_utf8_to_ebcdic
34
35    The "platform_xxx" calls represent platform-specific conversion routines.
36
37  */
38
39 #include <stdio.h>
40 #include <curl/curl.h>
41
42 CURLcode my_conv_from_ascii_to_ebcdic(char *buffer, size_t length)
43 {
44     char *tempptrin, *tempptrout;
45     size_t bytes = length;
46     int rc;
47     tempptrin = tempptrout = buffer;
48     rc = platform_a2e(&tempptrin, &bytes, &tempptrout, &bytes);
49     if (rc == PLATFORM_CONV_OK) {
50       return(CURLE_OK);
51     } else {
52       return(CURLE_CONV_FAILED);
53     }
54 }
55
56 CURLcode my_conv_from_ebcdic_to_ascii(char *buffer, size_t length)
57 {
58     char *tempptrin, *tempptrout;
59     size_t bytes = length;
60     int rc;
61     tempptrin = tempptrout = buffer;
62     rc = platform_e2a(&tempptrin, &bytes, &tempptrout, &bytes);
63     if (rc == PLATFORM_CONV_OK) {
64       return(CURLE_OK);
65     } else {
66       return(CURLE_CONV_FAILED);
67     }
68 }
69
70 CURLcode my_conv_from_utf8_to_ebcdic(char *buffer, size_t length)
71 {
72     char *tempptrin, *tempptrout;
73     size_t bytes = length;
74     int rc;
75     tempptrin = tempptrout = buffer;
76     rc = platform_u2e(&tempptrin, &bytes, &tempptrout, &bytes);
77     if (rc == PLATFORM_CONV_OK) {
78       return(CURLE_OK);
79     } else {
80       return(CURLE_CONV_FAILED);
81     }
82 }
83
84 int main(void)
85 {
86   CURL *curl;
87   CURLcode res;
88
89   curl = curl_easy_init();
90   if(curl) {
91     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
92
93     /* use platform-specific functions for codeset conversions */
94     curl_easy_setopt(curl, CURLOPT_CONV_FROM_NETWORK_FUNCTION,
95                      my_conv_from_ascii_to_ebcdic);
96     curl_easy_setopt(curl, CURLOPT_CONV_TO_NETWORK_FUNCTION,
97                      my_conv_from_ebcdic_to_ascii);
98     curl_easy_setopt(curl, CURLOPT_CONV_FROM_UTF8_FUNCTION,
99                      my_conv_from_utf8_to_ebcdic);
100
101     res = curl_easy_perform(curl);
102
103     /* always cleanup */
104     curl_easy_cleanup(curl);
105   }
106   return 0;
107 }