Add convenience si byte conversions

This commit is contained in:
Danny Robson 2011-08-29 14:37:47 +10:00
parent 29a8aa7ee8
commit fc3bd53838
3 changed files with 64 additions and 0 deletions

View File

@ -29,6 +29,7 @@ UTIL_INCLUDE = \
signal.hpp \ signal.hpp \
stream.hpp \ stream.hpp \
string.hpp \ string.hpp \
si.hpp \
types.hpp \ types.hpp \
vector.hpp \ vector.hpp \
version.hpp version.hpp
@ -52,6 +53,7 @@ UTIL_FILES = \
signal.cpp \ signal.cpp \
stream.cpp \ stream.cpp \
string.cpp \ string.cpp \
si.cpp \
types.cpp \ types.cpp \
vector.cpp \ vector.cpp \
version.cpp version.cpp

29
si.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright 2011 Danny Robson <danny@blubinc.net>
*/
#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; }

33
si.hpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* Copyright 2011 Danny Robson <danny@blubinc.net>
*/
#ifndef __UTIL_SI_HPP
#define __UTIL_SI_HPP
#include <cstdint>
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