helpers.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @file helpers.h
  3. * @brief header file for restclient-cpp helpers
  4. * @author Daniel Schauenberg <d@unwiredcouch.com>
  5. * @version
  6. * @date 2010-10-11
  7. */
  8. #ifndef INCLUDE_RESTCLIENT_CPP_HELPERS_H_
  9. #define INCLUDE_RESTCLIENT_CPP_HELPERS_H_
  10. #include <string>
  11. #include <cctype>
  12. #include <algorithm>
  13. #include <functional>
  14. #include "utils/Log.h"
  15. /**
  16. * @brief namespace for all RestClient definitions
  17. */
  18. namespace RestClient {
  19. /**
  20. * @brief: namespace for all helper functions
  21. */
  22. namespace Helpers {
  23. /** @struct UploadObject
  24. * @brief This structure represents the payload to upload on POST
  25. * requests
  26. * @var UploadObject::data
  27. * Member 'data' contains the data to upload
  28. * @var UploadObject::length
  29. * Member 'length' contains the length of the data to upload
  30. */
  31. typedef struct {
  32. const char* data;
  33. size_t length;
  34. } UploadObject;
  35. // writedata callback function
  36. size_t write_callback(void *ptr, size_t size, size_t nmemb,
  37. void *userdata);
  38. // header callback function
  39. size_t header_callback(void *ptr, size_t size, size_t nmemb,
  40. void *userdata);
  41. // read callback function
  42. size_t read_callback(void *ptr, size_t size, size_t nmemb,
  43. void *userdata);
  44. // download callback function
  45. size_t download_callback(void *ptr, size_t size, size_t nmemb,
  46. void *userdata);
  47. // trim from start
  48. static inline std::string &ltrim(std::string &s) { // NOLINT
  49. s.erase(s.begin(), std::find_if(s.begin(), s.end(),
  50. std::not1(std::ptr_fun<int, int>(std::isspace))));
  51. return s;
  52. }
  53. // trim from end
  54. static inline std::string &rtrim(std::string &s) { // NOLINT
  55. s.erase(std::find_if(s.rbegin(), s.rend(),
  56. std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
  57. return s;
  58. }
  59. // trim from both ends
  60. static inline std::string &trim(std::string &s) { // NOLINT
  61. return ltrim(rtrim(s));
  62. }
  63. }; // namespace Helpers
  64. }; // namespace RestClient
  65. #endif // INCLUDE_RESTCLIENT_CPP_HELPERS_H_