example10.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.
  26. * Writing to FILE*
  27. *
  28. */
  29. #include <cstdlib>
  30. #include <cstdio>
  31. #include <cstring>
  32. #include <curlpp/cURLpp.hpp>
  33. #include <curlpp/Easy.hpp>
  34. #include <curlpp/Options.hpp>
  35. #include <curlpp/Exception.hpp>
  36. #define MAX_FILE_LENGTH 20000
  37. size_t
  38. FileCallback(FILE *f, char* ptr, size_t size, size_t nmemb)
  39. {
  40. return fwrite(ptr, size, nmemb, f);
  41. };
  42. int main(int argc, char *argv[])
  43. {
  44. if(argc != 3)
  45. {
  46. std::cerr << argv[0] << ": Wrong number of arguments" << std::endl
  47. << argv[0] << ": Usage: " << " url file"
  48. << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. char *url = argv[1];
  52. char *filename = argv[2];
  53. FILE * file = fopen(filename, "w");
  54. if (!file)
  55. {
  56. std::cerr << "Error opening " << filename << std::endl;
  57. return EXIT_FAILURE;
  58. }
  59. try
  60. {
  61. curlpp::Cleanup cleaner;
  62. curlpp::Easy request;
  63. // Set the writer callback to enable cURL to write result in a memory area
  64. using namespace std::placeholders;
  65. curlpp::options::WriteFunction *test = new curlpp::options::WriteFunction(std::bind(&FileCallback, file, _1, _2, _3));
  66. request.setOpt(test);
  67. // Setting the URL to retrive.
  68. request.setOpt(new curlpp::options::Url(url));
  69. request.setOpt(new curlpp::options::Verbose(true));
  70. request.perform();
  71. return EXIT_SUCCESS;
  72. }
  73. catch ( curlpp::LogicError & e )
  74. {
  75. std::cout << e.what() << std::endl;
  76. }
  77. catch ( curlpp::RuntimeError & e )
  78. {
  79. std::cout << e.what() << std::endl;
  80. }
  81. return EXIT_FAILURE;
  82. }