Home | History | Annotate | Download | only in util
      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 /*!
     18  * \file
     19  * \brief Buffer handling utilities interface.
     20  */
     21 #ifndef EXAMPLE_UTIL_BUFFUTIL_H_
     22 #define EXAMPLE_UTIL_BUFFUTIL_H_
     23 
     24 #include <stddef.h>
     25 #include "util/stdtypes.h"
     26 
     27 /// Options controlling how a buffer should be printed.
     28 typedef struct BufferPrintOptions {
     29   bool show_header;
     30   bool show_offset;
     31   bool show_hex;
     32   bool show_ascii;
     33   size_t bytes_per_group;
     34   size_t groups_per_line;
     35 } BufferPrintOptions;
     36 
     37 /// Toggle verbose logging
     38 bool ToggleVerbosity();
     39 
     40 /// Test if file exists
     41 /*!
     42   \param[in] filename
     43   The file path.
     44 
     45   \returns bool
     46 */
     47 bool FileExists(char const* filename);
     48 
     49 /// Get file size
     50 /*!
     51   \param[in] filename
     52 
     53   The file path.
     54   \returns size of the file in bytes
     55 */
     56 size_t GetFileSize(char const* filename);
     57 
     58 /// Get file size
     59 /*!
     60   checks the size against an expected maximum size.
     61   \param[in] filename
     62 
     63   The file path.
     64   \param[in] max_size
     65 
     66   the maximum expected size of the file.
     67   \returns size of the file in bytes
     68 */
     69 size_t GetFileSize_S(char const* filename, size_t max_size);
     70 
     71 /// Allocate a buffer of a fixed size
     72 /*!
     73   Logs an error message on failure.
     74 
     75   \param[out] buffer
     76   A pointer to the buffer to allocate.
     77   \param[in] size
     78   the requested size of the buffer in bytes.
     79 
     80   \returns
     81   A pointer to the allocated buffer or NULL if the allocation failed.
     82 
     83 */
     84 void* AllocBuffer(size_t size);
     85 
     86 /// Allocate a buffer to hold the content of a file and load
     87 /*!
     88   Logs an error message on failure.
     89 
     90   \param[in] filename
     91   The file path.
     92   \param[out] size
     93   The allocated size of the buffer in bytes (same as file size).
     94 
     95   \returns
     96   A pointer to the allocated buffer or NULL if the allocation failed.
     97 
     98   \see ToggleVerbosity()
     99 */
    100 void* NewBufferFromFile(const char* filename, size_t* size);
    101 
    102 /// Read a buffer from a file with logging
    103 /*!
    104 
    105   Verbosity of logging controlled by verbosity state
    106 
    107 
    108   \param[in] filename
    109   The file path.
    110   \param[in,out] buf
    111   The buffer.
    112   \param[in] size
    113   The size of the buffer in bytes.
    114 
    115   \returns 0 on success, non-zero failure
    116 
    117   \see ToggleVerbosity()
    118 */
    119 int ReadLoud(char const* filename, void* buf, size_t size);
    120 
    121 /// write a buffer from a file with logging
    122 /*!
    123 
    124   Verbosity of logging controlled by verbosity state
    125 
    126   \param[in] buf
    127   The buffer.
    128   \param[in] size
    129   The size of the buffer in bytes.
    130   \param[in] filename
    131   The file path.
    132 
    133   \returns 0 on success, non-zero failure
    134 
    135   \see ToggleVerbosity()
    136 */
    137 int WriteLoud(void* buf, size_t size, char const* filename);
    138 
    139 /// print a buffer to standard out using user provided options
    140 /*!
    141   \param[in] buf
    142   The buffer.
    143   \param[in] size
    144   The size of the buffer in bytes.
    145   \param[in] opts
    146   The formatting options.
    147 */
    148 void PrintBufferOpt(const void* buffer, size_t size, BufferPrintOptions opts);
    149 
    150 /// print a buffer to standard out using default options
    151 /*!
    152   \param[in] buf
    153   The buffer.
    154   \param[in] size
    155   The size of the buffer in bytes.
    156 */
    157 void PrintBuffer(const void* buffer, size_t size);
    158 
    159 #endif  // EXAMPLE_UTIL_BUFFUTIL_H_
    160