Compare commits

...
Sign in to create a new pull request.
Author SHA1 Message Date
f1f03616cb write utf8 result to file 2025-01-12 04:19:32 +00:00
a09df042a1 partially fix utf8 problem
still need to work on the data structure that holds all info
2025-01-12 03:03:54 +00:00
8010fa6776 Update src/FileUtils.hs 2025-01-08 01:48:54 +00:00
b4579242d4 Update src/FileUtils.hs 2025-01-08 01:47:11 +00:00
47ca93dc00 Update src/FileUtils.hs 2025-01-08 01:27:52 +00:00
6bc63a42e8 Update src/FileUtils.hs 2025-01-08 01:03:07 +00:00

View file

@ -1,28 +1,61 @@
import Z.Data.CBytes (CBytes, pack, unpack)
import Z.IO.FileSystem (scandirRecursively, splitExtension)
import Z.Data.CBytes (pack, unpack)
import Z.IO.FileSystem (scandirRecursively)
import System.FilePath (takeDirectory)
import Data.List (groupBy, isSuffixOf)
import System.IO
-- High-level function
findFilesWithExtension :: String -> String -> IO [String]
findFilesWithExtension path extension = do
-- Convert inputs to CBytes
let cPath = pack path
let cExtension = pack extension
-- Perform recursive search
result <- scandirRecursively cPath (\p _ -> (== cExtension) . snd <$> splitExtension p)
-- Convert result from CBytes to String
return $ map unpack result
-- Derived version with automatic string handling
scanDirectoryRecursivelyStr :: String -> IO [String]
scanDirectoryRecursivelyStr path = do
result <- scandirRecursively (pack path) (\p _ -> return True)
return (map unpack result) -- Ensures all results are decoded to Strings (UTF-8)
-- Function to filter files with a specific extension
filterMdx :: [String] -> String -> [String]
filterMdx files ext = filter (isSuffixOf ext) files
findMdict = flip findFilesWithExtension ".mdx"
-- Custom implementation of takeDirectory for UTF-8 strings
takeDirectory' :: String -> String
takeDirectory' path = reverse . dropWhile (/= '/') . reverse $ path
-- Example usage
data FileGroup = FileGroup
{ directory :: String -- The directory part of the file path (UTF-8 String)
, files :: [String] -- List of file paths in that directory (UTF-8 Strings)
} deriving (Show, Eq)
-- Group files by directory
groupFilesByDirectory :: [String] -> IO [FileGroup]
groupFilesByDirectory files = do
-- Grouping files by directory
let groupDir file = (takeDirectory' file, file) -- use custom takeDirectoryUtf8
grouped = groupBy (\(dir1, _) (dir2, _) -> dir1 == dir2) (map groupDir files)
-- Create FileGroup instances
return $ map (\group ->
let dir = fst (head group)
allFiles = map snd group
in FileGroup dir allFiles) grouped
-- Function to convert FileGroup to a string representation for file output
fileGroupToString :: FileGroup -> String
fileGroupToString (FileGroup dir files) =
"Directory: " ++ dir ++ "\n" ++
unlines (map (" " ++) files)
-- Main function
main :: IO ()
main = do
-- Use the high-level curried function
files <- findMdict "/mnt/Data/Japanese_Resources/"
allFiles <- scanDirectoryRecursivelyStr "/mnt/Data/Japanese_Resources/"
-- Print each file path
mapM_ putStrLn files
-- Filter files with '.mdx' extension
let mdxFiles = filterMdx allFiles ".mdx"
-- Print the count
putStrLn $ "Total .mdx files found: " ++ show (length files)
-- Group the files by their directories
groupedFiles <- groupFilesByDirectory mdxFiles
-- Open the output file and set its encoding to UTF-8
withFile "output.txt" WriteMode $ \handle -> do
hSetEncoding handle utf8 -- Ensure the file is written in UTF-8
-- Write the grouped files to the file
mapM_ (hPutStrLn handle . fileGroupToString) groupedFiles