sampleconv.c 2.6 KB

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