host_tool_utils.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _HOST_TOOL_UTILS_H_
  6. #define _HOST_TOOL_UTILS_H_
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include "bi-inc/attr_container.h"
  11. #include "cJSON.h"
  12. /**
  13. * @brief Convert attribute container object to cJSON object.
  14. *
  15. * @param attr the attribute container object to be converted
  16. *
  17. * @return the created cJSON object if not NULL, NULL means fail
  18. *
  19. * @warning the return object should be deleted with cJSON_Delete by caller
  20. */
  21. cJSON *
  22. attr2json(const attr_container_t *attr);
  23. /**
  24. * @brief Convert cJSON object to attribute container object.
  25. *
  26. * @param json the cJSON object to be converted
  27. *
  28. * @return the created attribute container object if not NULL, NULL means fail
  29. *
  30. * @warning the return object should be deleted with attr_container_destroy
  31. */
  32. attr_container_t *
  33. json2attr(const cJSON *json);
  34. /**
  35. * @brief Generate a random 32 bit integer.
  36. *
  37. * @return the generated random integer
  38. */
  39. int
  40. gen_random_id();
  41. /**
  42. * @brief Read file content to buffer.
  43. *
  44. * @param filename the file name to read
  45. * @param ret_size pointer of integer to save file size once return success
  46. *
  47. * @return the created buffer which contains file content if not NULL, NULL
  48. * means fail
  49. *
  50. * @warning the return buffer should be deleted with free by caller
  51. */
  52. char *
  53. read_file_to_buffer(const char *filename, int *ret_size);
  54. /**
  55. * @brief Write buffer content to file.
  56. *
  57. * @param filename name the file name to be written
  58. * @param buffer the buffer
  59. * @param size size of the buffer to be written
  60. *
  61. * @return < 0 means fail, > 0 means the number of bytes actually written
  62. */
  63. int
  64. wirte_buffer_to_file(const char *filename, const char *buffer, int size);
  65. #ifdef __cplusplus
  66. } /* end of extern "C" */
  67. #endif
  68. #endif