2011-03-26 18:24:29 +00:00
|
|
|
{- data size display and parsing
|
2011-03-23 05:06:14 +00:00
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-07-06 00:36:43 +00:00
|
|
|
module Utility.DataUnits (
|
2011-03-26 18:24:29 +00:00
|
|
|
dataUnits,
|
|
|
|
storageUnits,
|
|
|
|
memoryUnits,
|
2011-03-26 18:49:23 +00:00
|
|
|
bandwidthUnits,
|
2011-03-26 18:24:29 +00:00
|
|
|
oldSchoolUnits,
|
2011-03-26 19:10:12 +00:00
|
|
|
|
2011-03-26 18:24:29 +00:00
|
|
|
roughSize,
|
2011-03-26 18:37:39 +00:00
|
|
|
compareSizes,
|
|
|
|
readSize
|
2011-03-26 18:24:29 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.List
|
|
|
|
import Data.Char
|
|
|
|
|
|
|
|
type ByteSize = Integer
|
|
|
|
type Name = String
|
|
|
|
type Abbrev = String
|
|
|
|
data Unit = Unit ByteSize Abbrev Name
|
|
|
|
deriving (Ord, Show, Eq)
|
2011-03-23 05:06:14 +00:00
|
|
|
|
|
|
|
{- And now a rant:
|
|
|
|
-
|
|
|
|
- In the beginning, we had powers of two, and they were good.
|
|
|
|
-
|
|
|
|
- Disk drive manufacturers noticed that some powers of two were
|
|
|
|
- sorta close to some powers of ten, and that rounding down to the nearest
|
|
|
|
- power of ten allowed them to advertise their drives were bigger. This
|
|
|
|
- was sorta annoying.
|
|
|
|
-
|
|
|
|
- Then drives got big. Really, really big. This was good.
|
|
|
|
-
|
|
|
|
- Except that the small rounding error perpretrated by the drive
|
|
|
|
- manufacturers suffered the fate of a small error, and became a large
|
|
|
|
- error. This was bad.
|
|
|
|
-
|
|
|
|
- So, a committee was formed. And it arrived at a committee-like decision,
|
|
|
|
- which satisfied noone, confused everyone, and made the world an uglier
|
|
|
|
- place. As with all committees, this was meh.
|
|
|
|
-
|
|
|
|
- And the drive manufacturers happily continued selling drives that are
|
|
|
|
- increasingly smaller than you'd expect, if you don't count on your
|
2011-03-26 18:24:29 +00:00
|
|
|
- fingers. But that are increasingly too big for anyone to much notice.
|
|
|
|
- This caused me to need git-annex.
|
2011-03-23 05:06:14 +00:00
|
|
|
-
|
|
|
|
- Thus, I use units here that I loathe. Because if I didn't, people would
|
|
|
|
- be confused that their drives seem the wrong size, and other people would
|
|
|
|
- complain at me for not being standards compliant. And we call this
|
|
|
|
- progress?
|
|
|
|
-}
|
|
|
|
|
2011-03-26 18:37:39 +00:00
|
|
|
dataUnits :: [Unit]
|
2011-03-26 18:24:29 +00:00
|
|
|
dataUnits = storageUnits ++ memoryUnits
|
|
|
|
|
|
|
|
{- Storage units are (stupidly) powers of ten. -}
|
|
|
|
storageUnits :: [Unit]
|
|
|
|
storageUnits =
|
|
|
|
[ Unit (p 8) "YB" "yottabyte"
|
|
|
|
, Unit (p 7) "ZB" "zettabyte"
|
|
|
|
, Unit (p 6) "EB" "exabyte"
|
|
|
|
, Unit (p 5) "PB" "petabyte"
|
|
|
|
, Unit (p 4) "TB" "terabyte"
|
|
|
|
, Unit (p 3) "GB" "gigabyte"
|
|
|
|
, Unit (p 2) "MB" "megabyte"
|
|
|
|
, Unit (p 1) "kB" "kilobyte" -- weird capitalization thanks to committe
|
|
|
|
, Unit (p 0) "B" "byte"
|
|
|
|
]
|
|
|
|
where
|
2011-03-26 18:37:39 +00:00
|
|
|
p :: Integer -> Integer
|
2011-03-26 18:24:29 +00:00
|
|
|
p n = 1000^n
|
|
|
|
|
|
|
|
{- Memory units are (stupidly named) powers of 2. -}
|
|
|
|
memoryUnits :: [Unit]
|
|
|
|
memoryUnits =
|
|
|
|
[ Unit (p 8) "YiB" "yobibyte"
|
|
|
|
, Unit (p 7) "ZiB" "zebibyte"
|
|
|
|
, Unit (p 6) "EiB" "exbibyte"
|
|
|
|
, Unit (p 5) "PiB" "pebibyte"
|
|
|
|
, Unit (p 4) "TiB" "tebibyte"
|
|
|
|
, Unit (p 3) "GiB" "gigabyte"
|
|
|
|
, Unit (p 2) "MiB" "mebibyte"
|
2011-03-26 18:54:11 +00:00
|
|
|
, Unit (p 1) "KiB" "kibibyte"
|
2011-03-26 18:24:29 +00:00
|
|
|
, Unit (p 0) "B" "byte"
|
|
|
|
]
|
|
|
|
where
|
2011-03-26 18:37:39 +00:00
|
|
|
p :: Integer -> Integer
|
2011-03-26 18:24:29 +00:00
|
|
|
p n = 2^(n*10)
|
|
|
|
|
2011-03-26 18:47:55 +00:00
|
|
|
{- Bandwidth units are only measured in bits if you're some crazy telco. -}
|
|
|
|
bandwidthUnits :: [Unit]
|
|
|
|
bandwidthUnits = error "stop trying to rip people off"
|
|
|
|
|
2011-03-26 18:24:29 +00:00
|
|
|
{- Do you yearn for the days when men were men and megabytes were megabytes? -}
|
2011-03-26 18:37:39 +00:00
|
|
|
oldSchoolUnits :: [Unit]
|
2011-03-26 18:24:29 +00:00
|
|
|
oldSchoolUnits = map mingle $ zip storageUnits memoryUnits
|
|
|
|
where
|
2011-03-26 18:37:39 +00:00
|
|
|
mingle (Unit _ a n, Unit s' _ _) = Unit s' a n
|
2011-03-26 18:24:29 +00:00
|
|
|
|
2011-03-23 05:06:14 +00:00
|
|
|
{- approximate display of a particular number of bytes -}
|
2011-03-26 18:24:29 +00:00
|
|
|
roughSize :: [Unit] -> Bool -> ByteSize -> String
|
|
|
|
roughSize units abbrev i
|
|
|
|
| i < 0 = "-" ++ findUnit units' (negate i)
|
|
|
|
| otherwise = findUnit units' i
|
2011-03-23 05:06:14 +00:00
|
|
|
where
|
2011-03-26 18:24:29 +00:00
|
|
|
units' = reverse $ sort units -- largest first
|
2011-03-23 05:06:14 +00:00
|
|
|
|
2011-03-26 18:24:29 +00:00
|
|
|
findUnit (u@(Unit s _ _):us) i'
|
|
|
|
| i' >= s = showUnit i' u
|
|
|
|
| otherwise = findUnit us i'
|
|
|
|
findUnit [] i' = showUnit i' (last units') -- bytes
|
2011-03-23 05:06:14 +00:00
|
|
|
|
2011-03-26 18:24:29 +00:00
|
|
|
showUnit i' (Unit s a n) = let num = chop i' s in
|
2011-03-23 05:06:14 +00:00
|
|
|
show num ++ " " ++
|
2011-03-26 18:24:29 +00:00
|
|
|
(if abbrev then a else plural num n)
|
|
|
|
|
|
|
|
chop :: Integer -> Integer -> Integer
|
|
|
|
chop i' d = round $ (fromInteger i' :: Double) / fromInteger d
|
2011-03-23 05:06:14 +00:00
|
|
|
|
|
|
|
plural n u
|
|
|
|
| n == 1 = u
|
|
|
|
| otherwise = u ++ "s"
|
2011-03-23 06:42:14 +00:00
|
|
|
|
2011-03-26 18:24:29 +00:00
|
|
|
{- displays comparison of two sizes -}
|
|
|
|
compareSizes :: [Unit] -> Bool -> ByteSize -> ByteSize -> String
|
|
|
|
compareSizes units abbrev old new
|
|
|
|
| old > new = roughSize units abbrev (old - new) ++ " smaller"
|
|
|
|
| old < new = roughSize units abbrev (new - old) ++ " larger"
|
2011-03-23 06:42:14 +00:00
|
|
|
| otherwise = "same"
|
2011-03-26 18:24:29 +00:00
|
|
|
|
|
|
|
{- Parses strings like "10 kilobytes" or "0.5tb". -}
|
2011-03-26 18:37:39 +00:00
|
|
|
readSize :: [Unit] -> String -> Maybe ByteSize
|
|
|
|
readSize units input
|
2011-03-26 18:24:29 +00:00
|
|
|
| null parsednum = Nothing
|
|
|
|
| null parsedunit = Nothing
|
|
|
|
| otherwise = Just $ round $ number * (fromIntegral multiplier)
|
|
|
|
where
|
|
|
|
(number, rest) = head parsednum
|
|
|
|
multiplier = head $ parsedunit
|
2011-03-26 19:10:12 +00:00
|
|
|
unitname = takeWhile isAlpha $ dropWhile isSpace rest
|
2011-03-26 18:24:29 +00:00
|
|
|
|
2011-03-26 18:37:39 +00:00
|
|
|
parsednum = reads input :: [(Double, String)]
|
2011-03-26 19:10:12 +00:00
|
|
|
parsedunit = lookupUnit units unitname
|
2011-03-26 18:24:29 +00:00
|
|
|
|
|
|
|
lookupUnit _ [] = [1] -- no unit given, assume bytes
|
|
|
|
lookupUnit [] _ = []
|
2011-03-26 18:37:39 +00:00
|
|
|
lookupUnit (Unit s a n:us) v
|
2011-03-26 18:24:29 +00:00
|
|
|
| a ~~ v || n ~~ v = [s]
|
|
|
|
| plural n ~~ v || a ~~ byteabbrev v = [s]
|
|
|
|
| otherwise = lookupUnit us v
|
|
|
|
|
|
|
|
a ~~ b = map toLower a == map toLower b
|
|
|
|
|
|
|
|
plural n = n ++ "s"
|
|
|
|
byteabbrev a = a ++ "b"
|