buffutil.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /// Get file size
  50. /*!
  51. checks the size against an expected maximum size.
  52. \param[in] filename
  53. The file path.
  54. \param[in] max_size
  55. the maximum expected size of the file.
  56. \returns size of the file in bytes
  57. */
  58. size_t GetFileSize_S(char const* filename, size_t max_size);
  59. /// Allocate a buffer of a fixed size
  60. /*!
  61. Logs an error message on failure.
  62. \param[out] buffer
  63. A pointer to the buffer to allocate.
  64. \param[in] size
  65. the requested size of the buffer in bytes.
  66. \returns
  67. A pointer to the allocated buffer or NULL if the allocation failed.
  68. */
  69. void* AllocBuffer(size_t size);
  70. /// Allocate a buffer to hold the content of a file and load
  71. /*!
  72. Logs an error message on failure.
  73. \param[in] filename
  74. The file path.
  75. \param[out] size
  76. The allocated size of the buffer in bytes (same as file size).
  77. \returns
  78. A pointer to the allocated buffer or NULL if the allocation failed.
  79. \see ToggleVerbosity()
  80. */
  81. void* NewBufferFromFile(const char* filename, size_t* size);
  82. /// Read a buffer from a file with logging
  83. /*!
  84. Verbosity of logging controlled by verbosity state
  85. \param[in] filename
  86. The file path.
  87. \param[in,out] buf
  88. The buffer.
  89. \param[in] size
  90. The size of the buffer in bytes.
  91. \returns 0 on success, non-zero failure
  92. \see ToggleVerbosity()
  93. */
  94. int ReadLoud(char const* filename, void* buf, size_t size);
  95. /// write a buffer from a file with logging
  96. /*!
  97. Verbosity of logging controlled by verbosity state
  98. \param[in] buf
  99. The buffer.
  100. \param[in] size
  101. The size of the buffer in bytes.
  102. \param[in] filename
  103. The file path.
  104. \returns 0 on success, non-zero failure
  105. \see ToggleVerbosity()
  106. */
  107. int WriteLoud(void* buf, size_t size, char const* filename);
  108. /// print a buffer to standard out using user provided options
  109. /*!
  110. \param[in] buf
  111. The buffer.
  112. \param[in] size
  113. The size of the buffer in bytes.
  114. \param[in] opts
  115. The formatting options.
  116. */
  117. void PrintBufferOpt(const void* buffer, size_t size, BufferPrintOptions opts);
  118. /// print a buffer to standard out using default options
  119. /*!
  120. \param[in] buf
  121. The buffer.
  122. \param[in] size
  123. The size of the buffer in bytes.
  124. */
  125. void PrintBuffer(const void* buffer, size_t size);
  126. #endif // EXAMPLE_UTIL_BUFFUTIL_H_