module Music.Theory.Byte where
import qualified Data.ByteString as B
import Data.Char
import Data.List.Split
import Data.Maybe
import Numeric
import qualified Music.Theory.Read as T
byte_hex_pp :: (Integral i, Show i) => i -> Maybe String
byte_hex_pp n =
case showHex n "" of
[c] -> Just ['0',toUpper c]
[c,d] -> Just (map toUpper [c,d])
_ -> Nothing
byte_hex_pp_err :: (Integral i, Show i) => i -> String
byte_hex_pp_err = fromMaybe (error "byte_hex_pp") . byte_hex_pp
byte_seq_hex_pp :: (Integral i, Show i) => [i] -> String
byte_seq_hex_pp = unwords . map byte_hex_pp_err
read_hex_byte :: (Eq t,Num t) => String -> t
read_hex_byte s =
case s of
[_,_] -> T.reads_to_read_precise_err "readHex" readHex s
_ -> error "read_hex_byte"
read_hex_byte_seq :: (Eq t,Num t) => String -> [t]
read_hex_byte_seq = map read_hex_byte . words
load_byte_seq :: Integral i => FilePath -> IO [i]
load_byte_seq = fmap (map fromIntegral . B.unpack) . B.readFile
store_byte_seq :: Integral i => FilePath -> [i] -> IO ()
store_byte_seq fn = B.writeFile fn . B.pack . map fromIntegral
load_hex_byte_seq :: Integral i => FilePath -> IO [i]
load_hex_byte_seq = fmap (map read_hex_byte . words) . readFile
store_hex_byte_seq :: (Integral i,Show i) => FilePath -> [i] -> IO ()
store_hex_byte_seq fn = writeFile fn . unlines . map unwords . chunksOf 16 . map byte_hex_pp_err