From fc3bd53838cdb09c6115a2d89052f684c93787a1 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 29 Aug 2011 14:37:47 +1000 Subject: [PATCH] Add convenience si byte conversions --- Makefile.am | 2 ++ si.cpp | 29 +++++++++++++++++++++++++++++ si.hpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 si.cpp create mode 100644 si.hpp diff --git a/Makefile.am b/Makefile.am index f30140eb..9e4b650e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -29,6 +29,7 @@ UTIL_INCLUDE = \ signal.hpp \ stream.hpp \ string.hpp \ + si.hpp \ types.hpp \ vector.hpp \ version.hpp @@ -52,6 +53,7 @@ UTIL_FILES = \ signal.cpp \ stream.cpp \ string.cpp \ + si.cpp \ types.cpp \ vector.cpp \ version.cpp diff --git a/si.cpp b/si.cpp new file mode 100644 index 00000000..12888db2 --- /dev/null +++ b/si.cpp @@ -0,0 +1,29 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2011 Danny Robson + */ + +#include "si.hpp" + +uintmax_t to_KiB (uintmax_t bytes) { return bytes >> 10; } +uintmax_t to_MiB (uintmax_t bytes) { return bytes >> 20; } +uintmax_t to_GiB (uintmax_t bytes) { return bytes >> 30; } + +uintmax_t from_KiB (uintmax_t bytes) { return bytes << 10; } +uintmax_t from_MiB (uintmax_t bytes) { return bytes << 20; } +uintmax_t from_GiB (uintmax_t bytes) { return bytes << 30; } + diff --git a/si.hpp b/si.hpp new file mode 100644 index 00000000..c214df30 --- /dev/null +++ b/si.hpp @@ -0,0 +1,33 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2011 Danny Robson + */ + +#ifndef __UTIL_SI_HPP +#define __UTIL_SI_HPP + +#include + +uintmax_t to_KiB (uintmax_t bytes); +uintmax_t to_MiB (uintmax_t bytes); +uintmax_t to_GiB (uintmax_t bytes); + +uintmax_t from_KiB (uintmax_t bytes); +uintmax_t from_MiB (uintmax_t bytes); +uintmax_t from_GiB (uintmax_t bytes); + +#endif