example18.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 using streams.
  26. *
  27. */
  28. #include <sstream>
  29. #include <cstdlib>
  30. #include <curlpp/cURLpp.hpp>
  31. #include <curlpp/Easy.hpp>
  32. #include <curlpp/Options.hpp>
  33. #include <curlpp/Exception.hpp>
  34. #include <curlpp/Infos.hpp>
  35. struct MethodClass
  36. {
  37. private:
  38. MethodClass();
  39. public:
  40. MethodClass(std::ostream * stream)
  41. : mStream(stream)
  42. , writeRound(0)
  43. {}
  44. // Helper Class for reading result from remote host
  45. size_t write(curlpp::Easy *handle, char* ptr, size_t size, size_t nmemb)
  46. {
  47. ++writeRound;
  48. curlpp::options::Url url;
  49. handle->getOpt(url);
  50. // Calculate the real size of the incoming buffer
  51. size_t realsize = size * nmemb;
  52. std::cerr << "write round: " << writeRound << ", url: " << url.getValue() << std::endl;
  53. mStream->write(ptr, realsize);
  54. // return the real size of the buffer...
  55. return realsize;
  56. };
  57. // Public member vars
  58. std::ostream * mStream;
  59. unsigned writeRound;
  60. };
  61. int main(int argc, char *argv[])
  62. {
  63. if(argc != 2) {
  64. std::cerr << argv[0] << ": Wrong number of arguments" << std::endl
  65. << argv[0] << ": Usage: " << " url "
  66. << std::endl;
  67. return EXIT_FAILURE;
  68. }
  69. char *url = argv[1];
  70. try {
  71. curlpp::Cleanup cleaner;
  72. curlpp::Easy request;
  73. std::ostringstream myStream;
  74. MethodClass mObject(&myStream);
  75. // Set the writer callback to enable cURL
  76. // to write result in a memory area
  77. #ifdef HAVE_BOOST
  78. curlpp::options::BoostWriteFunction *test = new curlpp::options::BoostWriteFunction(boost::bind(&MethodClass::write, &mObject, &request, _1, _2, _3));
  79. request.setOpt(test);
  80. #endif /* HAVE_BOOST */
  81. // Setting the URL to retrive.
  82. request.setOpt(new curlpp::options::Url(url));
  83. request.perform();
  84. return EXIT_SUCCESS;
  85. }
  86. catch ( curlpp::LogicError & e ) {
  87. std::cout << e.what() << std::endl;
  88. }
  89. catch ( curlpp::RuntimeError & e ) {
  90. std::cout << e.what() << std::endl;
  91. }
  92. return EXIT_FAILURE;
  93. }