Blame src/bs_size.h

Packit Service af52df
#ifndef _BS_SIZE_H
Packit Service af52df
#define _BS_SIZE_H
Packit Service af52df
Packit Service af52df
#include <stdint.h>
Packit Service af52df
#include <stdbool.h>
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSSize:
Packit Service af52df
 * An opaque type representing a size in bytes.
Packit Service af52df
 */
Packit Service af52df
typedef struct _BSSize * BSSize;
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSErrorCode:
Packit Service af52df
 *
Packit Service af52df
 * @BS_ERROR_INVALID_SPEC: invalid size or unit spec provided
Packit Service af52df
 * @BS_ERROR_OVER: a value is over the limits imposed by a type
Packit Service af52df
 * @BS_ERROR_ZERO_DIV: an attempt to do division by zero
Packit Service af52df
 *
Packit Service af52df
 * Error codes that identify various errors that can occur while working with
Packit Service af52df
 * #BSSize instances.
Packit Service af52df
 */
Packit Service af52df
typedef enum {
Packit Service af52df
    BS_ERROR_INVALID_SPEC,
Packit Service af52df
    BS_ERROR_OVER,
Packit Service af52df
    BS_ERROR_ZERO_DIV,
Packit Service af52df
    BS_ERROR_FAIL
Packit Service af52df
} BSErrorCode;
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSError:
Packit Service af52df
 *
Packit Service af52df
 * @code: error code
Packit Service af52df
 * @msg: optional error message
Packit Service af52df
 */
Packit Service af52df
typedef struct _BSError {
Packit Service af52df
    BSErrorCode code;
Packit Service af52df
    char *msg;
Packit Service af52df
} BSError;
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSBunit:
Packit Service af52df
 *
Packit Service af52df
 * Binary units (multiples of 1024) of size in bytes.
Packit Service af52df
 */
Packit Service af52df
typedef enum {
Packit Service af52df
    BS_BUNIT_B = 0, BS_BUNIT_KiB, BS_BUNIT_MiB, BS_BUNIT_GiB, BS_BUNIT_TiB,
Packit Service af52df
    BS_BUNIT_PiB, BS_BUNIT_EiB, BS_BUNIT_ZiB, BS_BUNIT_YiB, BS_BUNIT_UNDEF,
Packit Service af52df
} BSBunit;
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSDunit:
Packit Service af52df
 *
Packit Service af52df
 * Decimal units (multiples of 1000) of size in bytes.
Packit Service af52df
 */
Packit Service af52df
typedef enum {
Packit Service af52df
    BS_DUNIT_B = 20, BS_DUNIT_KB, BS_DUNIT_MB, BS_DUNIT_GB, BS_DUNIT_TB,
Packit Service af52df
    BS_DUNIT_PB, BS_DUNIT_EB, BS_DUNIT_ZB, BS_DUNIT_YB, BS_DUNIT_UNDEF,
Packit Service af52df
} BSDunit;
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSRoundDir:
Packit Service af52df
 *
Packit Service af52df
 * Rounding direction for rounding operations.
Packit Service af52df
 */
Packit Service af52df
typedef enum {
Packit Service af52df
    BS_ROUND_DIR_UP = 0,
Packit Service af52df
    BS_ROUND_DIR_DOWN = 1,
Packit Service af52df
    BS_ROUND_DIR_HALF_UP = 2
Packit Service af52df
} BSRoundDir;
Packit Service af52df
Packit Service af52df
/**
Packit Service af52df
 * BSUnit:
Packit Service af52df
 * @bunit: a binary unit
Packit Service af52df
 * @dunit: a decimal unit
Packit Service af52df
 *
Packit Service af52df
 * Generic unit fo size in bytes.
Packit Service af52df
 */
Packit Service af52df
typedef union {
Packit Service af52df
    BSBunit bunit;
Packit Service af52df
    BSDunit dunit;
Packit Service af52df
} BSUnit;
Packit Service af52df
Packit Service af52df
/* use 256 bits of precision for floating point numbets, that should be more
Packit Service af52df
   than enough */
Packit Service af52df
/**
Packit Service af52df
 * BS_FLOAT_PREC_BITS:
Packit Service af52df
 *
Packit Service af52df
 * Precision (in bits) of floating-point numbers used internally.
Packit Service af52df
 */
Packit Service af52df
#define BS_FLOAT_PREC_BITS 256
Packit Service af52df
Packit Service af52df
/* Constructors */
Packit Service af52df
BSSize bs_size_new (void);
Packit Service af52df
BSSize bs_size_new_from_bytes (uint64_t bytes, int sgn);
Packit Service af52df
BSSize bs_size_new_from_str (const char *size_str, BSError **error);
Packit Service af52df
BSSize bs_size_new_from_size (const BSSize size);
Packit Service af52df
Packit Service af52df
/* Destructors */
Packit Service af52df
void bs_size_free (BSSize size);
Packit Service af52df
void bs_clear_error (BSError **error);
Packit Service af52df
Packit Service af52df
/* Query functions */
Packit Service af52df
uint64_t bs_size_get_bytes (const BSSize size, int *sgn, BSError **error);
Packit Service af52df
int bs_size_sgn (const BSSize size);
Packit Service af52df
char* bs_size_get_bytes_str (const BSSize size);
Packit Service af52df
char* bs_size_convert_to (const BSSize size, BSUnit unit, BSError **error);
Packit Service af52df
char* bs_size_human_readable (const BSSize size, BSBunit min_unit, int max_places, bool xlate);
Packit Service af52df
Packit Service af52df
/* Arithmetic */
Packit Service af52df
BSSize bs_size_add (const BSSize size1, const BSSize size2);
Packit Service af52df
BSSize bs_size_grow (BSSize size1, const BSSize size2);
Packit Service af52df
BSSize bs_size_add_bytes (const BSSize size, uint64_t bytes);
Packit Service af52df
BSSize bs_size_grow_bytes (BSSize size, uint64_t bytes);
Packit Service af52df
BSSize bs_size_sub (const BSSize size1, const BSSize size2);
Packit Service af52df
BSSize bs_size_shrink (BSSize size1, const BSSize size2);
Packit Service af52df
BSSize bs_size_sub_bytes (const BSSize size, uint64_t bytes);
Packit Service af52df
BSSize bs_size_shrink_bytes (BSSize size, uint64_t bytes);
Packit Service af52df
BSSize bs_size_mul_int (const BSSize size, uint64_t times);
Packit Service af52df
BSSize bs_size_grow_mul_int (BSSize size, uint64_t times);
Packit Service af52df
BSSize bs_size_mul_float_str (const BSSize size, const char *float_str, BSError **error);
Packit Service af52df
BSSize bs_size_grow_mul_float_str (BSSize size, const char *float_str, BSError **error);
Packit Service af52df
uint64_t bs_size_div (const BSSize size1, const BSSize size2, int *sgn, BSError **error);
Packit Service af52df
BSSize bs_size_div_int (const BSSize size, uint64_t divisor, BSError **error);
Packit Service af52df
BSSize bs_size_shrink_div_int (BSSize size, uint64_t shrink_divisor, BSError **error);
Packit Service af52df
char* bs_size_true_div (const BSSize size1, const BSSize size2, BSError **error);
Packit Service af52df
char* bs_size_true_div_int (const BSSize size, uint64_t divisor, BSError **error);
Packit Service af52df
BSSize bs_size_mod (const BSSize size1, const BSSize size2, BSError **error);
Packit Service af52df
BSSize bs_size_round_to_nearest (const BSSize size, const BSSize round_to, BSRoundDir dir, BSError **error);
Packit Service af52df
Packit Service af52df
/* Comparisons */
Packit Service af52df
int bs_size_cmp (const BSSize size1, const BSSize size2, bool abs);
Packit Service af52df
int bs_size_cmp_bytes (const BSSize size1, uint64_t bytes, bool abs);
Packit Service af52df
Packit Service af52df
#endif  /* _BS_SIZE_H */