example05.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (curlpp), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /**
  24. * \file
  25. * Write function using free function as a callback.
  26. *
  27. */
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <curlpp/cURLpp.hpp>
  31. #include <curlpp/Easy.hpp>
  32. #include <curlpp/Options.hpp>
  33. #include <curlpp/Exception.hpp>
  34. #define MAX_FILE_LENGTH 20000
  35. char *m_pBuffer = NULL;
  36. size_t m_Size = 0;
  37. void* Realloc(void* ptr, size_t size)
  38. {
  39. if(ptr)
  40. return realloc(ptr, size);
  41. else
  42. return malloc(size);
  43. };
  44. // Callback must be declared static, otherwise it won't link...
  45. size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb)
  46. {
  47. // Calculate the real size of the incoming buffer
  48. size_t realsize = size * nmemb;
  49. // (Re)Allocate memory for the buffer
  50. m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize);
  51. // Test if Buffer is initialized correctly & copy memory
  52. if (m_pBuffer == NULL) {
  53. realsize = 0;
  54. }
  55. memcpy(&(m_pBuffer[m_Size]), ptr, realsize);
  56. m_Size += realsize;
  57. // return the real size of the buffer...
  58. return realsize;
  59. };
  60. void print()
  61. {
  62. std::cout << "Size: " << m_Size << std::endl;
  63. std::cout << "Content: " << std::endl << m_pBuffer << std::endl;
  64. }
  65. int main(int argc, char *argv[])
  66. {
  67. m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char));
  68. if(argc != 2)
  69. {
  70. std::cerr << "Example 05: Wrong number of arguments" << std::endl
  71. << "Example 05: Usage: example05 url"
  72. << std::endl;
  73. return EXIT_FAILURE;
  74. }
  75. char *url = argv[1];
  76. try
  77. {
  78. curlpp::Cleanup cleaner;
  79. curlpp::Easy request;
  80. // Set the writer callback to enable cURL
  81. // to write result in a memory area
  82. curlpp::types::WriteFunctionFunctor functor(WriteMemoryCallback);
  83. curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(functor);
  84. request.setOpt(test);
  85. // Setting the URL to retrive.
  86. request.setOpt(new curlpp::options::Url(url));
  87. request.setOpt(new curlpp::options::Verbose(true));
  88. request.perform();
  89. print();
  90. }
  91. catch ( curlpp::LogicError & e )
  92. {
  93. std::cout << e.what() << std::endl;
  94. }
  95. catch ( curlpp::RuntimeError & e )
  96. {
  97. std::cout << e.what() << std::endl;
  98. }
  99. }