Skip to content

Instantly share code, notes, and snippets.

@davidxifeng
Created July 13, 2012 05:06
Show Gist options
  • Save davidxifeng/3102838 to your computer and use it in GitHub Desktop.
Save davidxifeng/3102838 to your computer and use it in GitHub Desktop.
delete the first /**/ comment in .h and .c file in a directory
-- remove first long comment in a directory's all .h and .c file and it's all sub directory
import System.Environment
import System.Directory
import System.FilePath.Windows
import Control.Monad
import Data.List
-- 必须保证/*和*/是匹配的...这里满足这个条件
removeFirstComment :: String -> String
removeFirstComment [] = []
removeFirstComment ('/':'*':xs) = inComment xs
where inComment [] = []
inComment ('*':'/':xs) = xs
inComment (x:xs) = inComment xs
removeFirstComment (x:xs) = x: removeFirstComment xs
getSourceFile :: FilePath -> IO [FilePath]
getSourceFile dir = (sort . filter isSourceFile) `liftM` getDirectoryContents dir
where isSourceFile filepath = (snd $ splitExtension filepath) `elem` [".h", ".H", ".c", ".C"]
processOneFile :: FilePath -> FilePath-> IO ()
processOneFile file dir = do
let raw_file_name = dir ++ "\\" ++ file
let temp_file_name = dir ++ "\\temp_" ++ file
renameFile raw_file_name temp_file_name
file_string <- readFile temp_file_name
writeFile raw_file_name (removeFirstComment file_string)
putStrLn $ "write the processed file to " ++ raw_file_name
removeFile temp_file_name
processDirectoryFile :: [FilePath] -> FilePath -> IO ()
processDirectoryFile [] _ = return ()
processDirectoryFile (x:xs) path = do
processOneFile x path
processDirectoryFile xs path
isTrueSubDirectory :: FilePath -> Bool
isTrueSubDirectory x
| x == "." = False
| x == ".." = False
| otherwise = True
getAllSubDirectory :: FilePath -> IO [FilePath]
getAllSubDirectory dir = do
dc <- getDirectoryContents dir
let tdc = filter isTrueSubDirectory dc
let full_dc = map ((dir ++ "\\") ++) tdc
sdc <- filterM doesDirectoryExist full_dc
return sdc
processDirectoryList :: [FilePath] -> IO ()
processDirectoryList [] = return ()
processDirectoryList (d:ds) = do
is_valid_dir <- doesDirectoryExist d
if is_valid_dir
then do
--处理本目录下的文件
file_list <- getSourceFile d
processDirectoryFile file_list d
--处理所有子目录
subdir_list <- getAllSubDirectory d
--putStrLn $ "process files in sub dir of " ++ (dirs subdir_list)
--where
--dirs :: [FilePath] -> FilePath
--dirs [] = ""
--dirs (x:xs) = foldl (++) "" $ map (\x -> " " ++ x ++ " ") (x:xs)
processDirectoryList subdir_list
else return ()
processDirectoryList ds
main = do
cur_dir <- getCurrentDirectory
let config_file = cur_dir ++ "\\dir_list.txt"
dir_list_str <- readFile config_file
processDirectoryList $ lines dir_list_str
-- 2012-07-12 18:30 done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment