1 % Copyright (C) 2002-2003 David Roundy
3 % This program is free software; you can redistribute it and/or modify
4 % it under the terms of the GNU General Public License as published by
5 % the Free Software Foundation; either version 2, or (at your option)
8 % This program is distributed in the hope that it will be useful,
9 % but WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 % GNU General Public License for more details.
13 % You should have received a copy of the GNU General Public License
14 % along with this program; see the file COPYING. If not, write to
15 % the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 % Boston, MA 02110-1301, USA.
19 FileName is an abstract type intended to facilitate the input and output of
23 module Darcs.Patch.FileName ( FileName( ),
27 break_on_dir, norm_path, own_name, super_name,
29 encode_white, decode_white,
34 import Data.Char ( isSpace, chr, ord )
35 import qualified UTF8 ( encode )
36 import Data.Word ( Word8( ) )
37 import ByteStringUtils ( unpackPSfromUTF8 )
38 import qualified Data.ByteString.Char8 as BC (unpack, pack)
39 import qualified Data.ByteString as B (ByteString, pack)
43 newtype FileName = FN FilePath deriving ( Eq, Ord )
44 encode :: [Char] -> [Word8]
47 instance Show FileName where
48 showsPrec d (FN fp) = showParen (d > app_prec) $ showString "fp2fn " . showsPrec (app_prec + 1) fp
52 fp2fn :: FilePath -> FileName
56 fn2fp :: FileName -> FilePath
59 {-# INLINE niceps2fn #-}
60 niceps2fn :: B.ByteString -> FileName
61 niceps2fn = FN . decode_white . BC.unpack
63 {-# INLINE fn2niceps #-}
64 fn2niceps :: FileName -> B.ByteString
65 fn2niceps (FN fp) = BC.pack $ encode_white fp
68 fn2ps :: FileName -> B.ByteString
69 fn2ps (FN fp) = B.pack $ encode $ encode_white fp
72 ps2fn :: B.ByteString -> FileName
73 ps2fn ps = FN $ decode_white $ unpackPSfromUTF8 ps
75 encode_white :: FilePath -> String
76 encode_white (c:cs) | isSpace c || c == '\\' =
77 '\\' : (show $ ord c) ++ "\\" ++ encode_white cs
78 encode_white (c:cs) = c : encode_white cs
81 decode_white :: String -> FilePath
82 decode_white ('\\':cs) =
83 case break (=='\\') cs of
84 (theord, '\\':rest) ->
85 chr (read theord) : decode_white rest
86 _ -> error "malformed filename"
87 decode_white (c:cs) = c: decode_white cs
92 own_name :: FileName -> FileName
93 own_name (FN f) = case breakLast '/' f of Nothing -> FN f
95 super_name :: FileName -> FileName
96 super_name fn = case norm_path fn of
97 FN f -> case breakLast '/' f of
100 break_on_dir :: FileName -> Maybe (FileName,FileName)
101 break_on_dir (FN p) = case breakFirst '/' p of
103 Just (d,f) | d == "." -> break_on_dir $ FN f
104 | otherwise -> Just (FN d, FN f)
105 norm_path :: FileName -> FileName -- remove "./"
106 norm_path (FN p) = FN $ repath $ drop_dotdot $ breakup p
108 repath :: [String] -> String
111 repath (d:p) = d ++ "/" ++ repath p
113 drop_dotdot :: [String] -> [String]
114 drop_dotdot ("":p) = drop_dotdot p
115 drop_dotdot (".":p) = drop_dotdot p
116 drop_dotdot ("..":p) = ".." : (drop_dotdot p)
117 drop_dotdot (_:"..":p) = drop_dotdot p
118 drop_dotdot (d:p) = case drop_dotdot p of
123 breakup :: String -> [String]
124 breakup p = case break (=='/') p of
126 (d,p') -> d : breakup (tail p')
128 breakFirst :: Char -> String -> Maybe (String,String)
129 breakFirst c l = bf [] l
130 where bf a (r:rs) | r == c = Just (reverse a,rs)
131 | otherwise = bf (r:a) rs
133 breakLast :: Char -> String -> Maybe (String,String)
134 breakLast c l = case breakFirst c (reverse l) of
136 Just (a,b) -> Just (reverse b, reverse a)
138 (///) :: FileName -> FileName -> FileName
139 (FN "")///b = norm_path b
140 a///b = norm_path $ fp2fn $ fn2fp a ++ "/" ++ fn2fp b
142 movedirfilename :: FileName -> FileName -> FileName -> FileName
143 movedirfilename old new name =
144 if name' == old' then new
145 else if length name' > length old' &&
146 take (length old'+1) name' == old'++"/"
147 then fp2fn ("./"++new'++drop (length old') name')
149 where old' = fn2fp $ norm_path old
150 new' = fn2fp $ norm_path new
151 name' = fn2fp $ norm_path name