Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
Created July 23, 2015 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlohamalainen/83f5c8d67a81f4f580c2 to your computer and use it in GitHub Desktop.
Save carlohamalainen/83f5c8d67a81f4f580c2 to your computer and use it in GitHub Desktop.
Add CAI project ID to Bruker file (demo)
module AddProjToBruker where
-- ##$SUBJECT_comment=( 2048 )
-- <CAI:10001>
import Control.Monad (when)
import Data.Either
import Data.List
import Data.Maybe
import System.Environment
import System.FilePath
is5digits :: String -> Bool
is5digits s = (length s == 5) && (isJust $ (readMaybe s :: Maybe Integer))
where
readMaybe :: (Read a) => String -> Maybe a
readMaybe s =
case reads s of
[(a, "")] -> Just a
_ -> Nothing
addProjectIdLine :: Integer -> [String] -> Either [String] [String]
addProjectIdLine projID [] = Left []
addProjectIdLine projID (x:xs) = if "##$SUBJECT_comment=" `isPrefixOf` x
then Right (x:newProjectLine:xs)
else fmap (x:) (addProjectIdLine projID xs)
where newProjectLine = "<CAI:" ++ show projID ++ ">"
go :: String -> FilePath -> IO ()
go projID dir = do
let f = dir </> "before"
x <- readFile f
if is5digits projID
then case addProjectIdLine (read projID) (lines x) of
Left _ -> putStrLn $ "Error: did not find a SUBJECT line in " ++ f
Right x' -> putStrLn $ unlines x'
else
putStrLn $ "Error: did not get a 5 digit project ID: " ++ projID
main :: IO ()
main = do
args <- getArgs
case args of
["--id", projID, dir] -> go projID dir
_ -> putStrLn $ "Bad arguments? " ++ show args
@andrewjanke
Copy link

That's not even code, it reads like a 1980's choose your own adventure book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment