example06.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * WriteFunction option using functor 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. class WriterMemoryClass
  36. {
  37. public:
  38. // Helper Class for reading result from remote host
  39. WriterMemoryClass()
  40. {
  41. this->m_pBuffer = NULL;
  42. this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char));
  43. this->m_Size = 0;
  44. };
  45. ~WriterMemoryClass()
  46. {
  47. if (this->m_pBuffer)
  48. free(this->m_pBuffer);
  49. };
  50. void* Realloc(void* ptr, size_t size)
  51. {
  52. if(ptr)
  53. return realloc(ptr, size);
  54. else
  55. return malloc(size);
  56. };
  57. // Callback must be declared static, otherwise it won't link...
  58. size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb)
  59. {
  60. // Calculate the real size of the incoming buffer
  61. size_t realsize = size * nmemb;
  62. // (Re)Allocate memory for the buffer
  63. m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize);
  64. // Test if Buffer is initialized correctly & copy memory
  65. if (m_pBuffer == NULL) {
  66. realsize = 0;
  67. }
  68. memcpy(&(m_pBuffer[m_Size]), ptr, realsize);
  69. m_Size += realsize;
  70. // return the real size of the buffer...
  71. return realsize;
  72. };
  73. void print()
  74. {
  75. std::cout << "Size: " << m_Size << std::endl;
  76. std::cout << "Content: " << std::endl << m_pBuffer << std::endl;
  77. }
  78. // Public member vars
  79. char* m_pBuffer;
  80. size_t m_Size;
  81. };
  82. int main(int argc, char *argv[])
  83. {
  84. if(argc != 2)
  85. {
  86. std::cerr << "Example 06: Wrong number of arguments" << std::endl
  87. << "Example 06: Usage: example06 url"
  88. << std::endl;
  89. return EXIT_FAILURE;
  90. }
  91. char *url = argv[1];
  92. try
  93. {
  94. curlpp::Cleanup cleaner;
  95. curlpp::Easy request;
  96. WriterMemoryClass mWriterChunk;
  97. // Set the writer callback to enable cURL
  98. // to write result in a memory area
  99. using namespace std::placeholders;
  100. curlpp::types::WriteFunctionFunctor functor = std::bind(&WriterMemoryClass::WriteMemoryCallback, &mWriterChunk, _1, _2, _3);
  101. curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(functor);
  102. request.setOpt(test);
  103. // Setting the URL to retrive.
  104. request.setOpt(new curlpp::options::Url(url));
  105. request.setOpt(new curlpp::options::Verbose(true));
  106. request.perform();
  107. mWriterChunk.print();
  108. }
  109. catch ( curlpp::LogicError & e )
  110. {
  111. std::cout << e.what() << std::endl;
  112. }
  113. catch ( curlpp::RuntimeError & e )
  114. {
  115. std::cout << e.what() << std::endl;
  116. }
  117. }