sampleconv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. The IBM-1047 EBCDIC codeset is used for this example but the code
  27. would be similar for other non-ASCII codesets.
  28. Three callback functions are created below:
  29. my_conv_from_ascii_to_ebcdic,
  30. my_conv_from_ebcdic_to_ascii, and
  31. my_conv_from_utf8_to_ebcdic
  32. The "platform_xxx" calls represent platform-specific conversion routines.
  33. */
  34. #include <stdio.h>
  35. #include <curl/curl.h>
  36. CURLcode my_conv_from_ascii_to_ebcdic(char *buffer, size_t length)
  37. {
  38. char *tempptrin, *tempptrout;
  39. size_t bytes = length;
  40. int rc;
  41. tempptrin = tempptrout = buffer;
  42. rc = platform_a2e(&tempptrin, &bytes, &tempptrout, &bytes);
  43. if (rc == PLATFORM_CONV_OK) {
  44. return(CURLE_OK);
  45. } else {
  46. return(CURLE_CONV_FAILED);
  47. }
  48. }
  49. CURLcode my_conv_from_ebcdic_to_ascii(char *buffer, size_t length)
  50. {
  51. char *tempptrin, *tempptrout;
  52. size_t bytes = length;
  53. int rc;
  54. tempptrin = tempptrout = buffer;
  55. rc = platform_e2a(&tempptrin, &bytes, &tempptrout, &bytes);
  56. if (rc == PLATFORM_CONV_OK) {
  57. return(CURLE_OK);
  58. } else {
  59. return(CURLE_CONV_FAILED);
  60. }
  61. }
  62. CURLcode my_conv_from_utf8_to_ebcdic(char *buffer, size_t length)
  63. {
  64. char *tempptrin, *tempptrout;
  65. size_t bytes = length;
  66. int rc;
  67. tempptrin = tempptrout = buffer;
  68. rc = platform_u2e(&tempptrin, &bytes, &tempptrout, &bytes);
  69. if (rc == PLATFORM_CONV_OK) {
  70. return(CURLE_OK);
  71. } else {
  72. return(CURLE_CONV_FAILED);
  73. }
  74. }
  75. int main(void)
  76. {
  77. CURL *curl;
  78. CURLcode res;
  79. curl = curl_easy_init();
  80. if(curl) {
  81. curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
  82. /* use platform-specific functions for codeset conversions */
  83. curl_easy_setopt(curl, CURLOPT_CONV_FROM_NETWORK_FUNCTION,
  84. my_conv_from_ascii_to_ebcdic);
  85. curl_easy_setopt(curl, CURLOPT_CONV_TO_NETWORK_FUNCTION,
  86. my_conv_from_ebcdic_to_ascii);
  87. curl_easy_setopt(curl, CURLOPT_CONV_FROM_UTF8_FUNCTION,
  88. my_conv_from_utf8_to_ebcdic);
  89. res = curl_easy_perform(curl);
  90. /* always cleanup */
  91. curl_easy_cleanup(curl);
  92. }
  93. return 0;
  94. }