implementation module StdPathname

import StdArray, StdBool, StdChar, StdClass, StdInt, StdString, StdList
from StdSystem import dirseparator
import Platform

::	Pathname			:== String

EmptyPathname	:== ""

quoted_string :: !String -> String
quoted_string s = PlatformDependant (win_quoted_string s) (mac_quoted_string s)

win_quoted_string :: !String -> String
win_quoted_string string = "\"" +++ string +++ "\""
   
mac_quoted_string :: !String -> String
mac_quoted_string string = "\'" +++ double_quotes 0 string +++ "\'"
where
	double_quotes i string
		| i>=size string
			= string
		| string.[i]=='\''
			= double_quotes (i+2) (string % (0,i)+++"\'"+++string % (i+1,dec (size string)))
			= double_quotes (inc i) string

RemovePath	:: !Pathname -> String;
RemovePath path
	| found	= path % (inc position, last);
			= path;
	where 
		(found,position)= FindLastChar dirseparator path last;
		last			= dec (size path);
	

RemoveSuffix :: !Pathname -> String;
RemoveSuffix path
	| not found		= path;
	| not suffix	= path;
					= path % (0, dec position);
	where 
		(found, position)	= FindLastChar '.' path last;
		suf					= path % (position, last);
//		suffix				= suf == ".dcl" || suf == ".icl" || suf == ".abc" || suf == ".o" || suf == ".obj" || suf == ".prj";
		suffix				= isMember suf [".",".dcl",".icl",".hs",".lhs",".abc",".o",".obj",".obj0",".obj1",".obj2",".xo",".cxo",".prj"];
		last				= dec (size path);
	
RemoveSuffix` :: !Pathname -> String;
RemoveSuffix` path
	| not found		= path;
//	| not suffix	= path;
					= path % (0, dec position);
	where 
		(found, position)	= FindLastChar '.' path last;
//		suf					= path % (position, last);
//		suffix				= suf == ".dcl" || suf == ".icl" || suf == ".abc" || suf == ".o" || suf == ".prj";
		last				= dec (size path);
	
		
RemoveFilename :: !Pathname -> Pathname;
RemoveFilename path 
	| found
		= path % (0, dec position);
		= path;
	where 
		(found,position)	= FindLastChar dirseparator path (dec (size path));
	
replace_prefix_path	:: !Pathname !Pathname !Pathname -> Pathname;
replace_prefix_path old_prefix_path new_prefix_path path
	| old_prefix_path==path
		= new_prefix_path;
	| equal_path_prefix old_prefix_path path
		= new_prefix_path +++ {dirseparator} +++ path % (inc (size old_prefix_path), dec (size path));
		= path;
	
IsFullPathname :: !Pathname -> Bool;
IsFullPathname name = LastChar dirseparator name (dec (size name)) >= 0;

MakeFullPathname :: !Pathname !String -> Pathname;
MakeFullPathname path name =  path +++ toString dirseparator +++ name;
	
/* Auxilary functions */
	
equal_suffix	:: !String !String -> Bool;
equal_suffix suffix string
	| lengths < lengthsuf
		= False;
		= string % (lengths - lengthsuf,lengths) == suffix;
	where 
		lengths		= dec (size string);
		lengthsuf	= dec (size suffix);
	
	
equal_path_prefix	:: !String !String -> Bool;
equal_path_prefix prefix string
	| lengths<=lengthpre
		= False;
		= string.[inc lengthpre]==dirseparator && string % (0,lengthpre)==prefix;
	where 
		lengths		= dec (size string);
		lengthpre	= dec (size prefix);
	
FindLastChar :: !Char !String !Int -> (!Bool, !Int);
FindLastChar c s i
	| i <= 0			= (False,0);
	| c ==  s.[i]		= (True, i);
						= FindLastChar c s (dec i);

LastChar :: !Char !String !Int -> Int;
LastChar c s i
	| i <= 0		= -1;
	| c ==  s.[i]	= i;
					= LastChar c s (dec i);

