buffutil.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief Buffer handling utilities interface.
  19. */
  20. #ifndef EXAMPLE_UTIL_BUFFUTIL_H_
  21. #define EXAMPLE_UTIL_BUFFUTIL_H_
  22. #include <stddef.h>
  23. #include "util/stdtypes.h"
  24. /// Options controlling how a buffer should be printed.
  25. typedef struct BufferPrintOptions {
  26. bool show_header;
  27. bool show_offset;
  28. bool show_hex;
  29. bool show_ascii;
  30. size_t bytes_per_group;
  31. size_t groups_per_line;
  32. } BufferPrintOptions;
  33. /// Toggle verbose logging
  34. bool ToggleVerbosity();
  35. /// Test if file exists
  36. /*!
  37. \param[in] filename
  38. The file path.
  39. \returns bool
  40. */
  41. bool FileExists(char const* filename);
  42. /// Get file size
  43. /*!
  44. \param[in] filename
  45. The file path.
  46. \returns size of the file in bytes
  47. */
  48. size_t GetFileSize(char const* filename);
  49. /// Allocate a buffer of a fixed size
  50. /*!
  51. Logs an error message on failure.
  52. \param[out] buffer
  53. A pointer to the buffer to allocate.
  54. \param[in] size
  55. the requested size of the buffer in bytes.
  56. \returns
  57. A pointer to the allocated buffer or NULL if the allocation failed.
  58. */
  59. void* AllocBuffer(size_t size);
  60. /// Allocate a buffer to hold the content of a file and load
  61. /*!
  62. Logs an error message on failure.
  63. \param[in] filename
  64. The file path.
  65. \param[out] size
  66. The allocated size of the buffer in bytes (same as file size).
  67. \returns
  68. A pointer to the allocated buffer or NULL if the allocation failed.
  69. \see ToggleVerbosity()
  70. */
  71. void* NewBufferFromFile(const char* filename, size_t* size);
  72. /// Read a buffer from a file with logging
  73. /*!
  74. Verbosity of logging controlled by verbosity state
  75. \param[in] filename
  76. The file path.
  77. \param[in,out] buf
  78. The buffer.
  79. \param[in] size
  80. The size of the buffer in bytes.
  81. \returns 0 on success, non-zero failure
  82. \see ToggleVerbosity()
  83. */
  84. int ReadLoud(char const* filename, void* buf, size_t size);
  85. /// write a buffer from a file with logging
  86. /*!
  87. Verbosity of logging controlled by verbosity state
  88. \param[in] buf
  89. The buffer.
  90. \param[in] size
  91. The size of the buffer in bytes.
  92. \param[in] filename
  93. The file path.
  94. \returns 0 on success, non-zero failure
  95. \see ToggleVerbosity()
  96. */
  97. int WriteLoud(void* buf, size_t size, char const* filename);
  98. /// print a buffer to standard out using user provided options
  99. /*!
  100. \param[in] buf
  101. The buffer.
  102. \param[in] size
  103. The size of the buffer in bytes.
  104. \param[in] opts
  105. The formatting options.
  106. */
  107. void PrintBufferOpt(const void* buffer, size_t size, BufferPrintOptions opts);
  108. /// print a buffer to standard out using default options
  109. /*!
  110. \param[in] buf
  111. The buffer.
  112. \param[in] size
  113. The size of the buffer in bytes.
  114. */
  115. void PrintBuffer(const void* buffer, size_t size);
  116. #endif // EXAMPLE_UTIL_BUFFUTIL_H_