StdPathname.icl 3.24 KB
Newer Older
Diederik van Arkel's avatar
Diederik van Arkel committed
1 2
implementation module StdPathname

Diederik van Arkel's avatar
Diederik van Arkel committed
3
import StdArray, StdBool, StdChar, StdClass, StdInt, StdString, StdList
Diederik van Arkel's avatar
Diederik van Arkel committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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);
Diederik van Arkel's avatar
Diederik van Arkel committed
44 45
//		suffix				= suf == ".dcl" || suf == ".icl" || suf == ".abc" || suf == ".o" || suf == ".obj" || suf == ".prj";
		suffix				= isMember suf [".",".dcl",".icl",".abc",".o",".obj",".obj0",".obj1",".obj2",".xo",".cxo",".prj"];
Diederik van Arkel's avatar
Diederik van Arkel committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		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));
	
68
replace_prefix_path	:: !Pathname !Pathname !Pathname -> Pathname;
Diederik van Arkel's avatar
Diederik van Arkel committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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);

109
LastChar :: !Char !String !Int -> Int;
Diederik van Arkel's avatar
Diederik van Arkel committed
110 111 112 113 114
LastChar c s i
	| i <= 0		= -1;
	| c ==  s.[i]	= i;
					= LastChar c s (dec i);