Extract file extension: Difference between revisions
Content added Content deleted
(C solution addition.) |
(Added Algol 68 and Algol W) |
||
Line 12: | Line 12: | ||
# file.odd_one returns an empty string as an extension (by this definition), cannot contain an underscore. |
# file.odd_one returns an empty string as an extension (by this definition), cannot contain an underscore. |
||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}} |
|||
<lang algol68># extracts a file-extension from the end of a pathname. The file extension is # |
|||
# defined as a dot followed by one or more letters or digits # |
|||
OP EXTENSION = ( STRING pathname )STRING: |
|||
IF LWB pathname >= UPB pathname THEN |
|||
# the pathname has 0 or 1 characters and so has no extension # |
|||
"" |
|||
ELIF NOT isalnum( pathname[ UPB pathname ] ) THEN |
|||
# the final character is not a letter or digit - no extension # |
|||
"" |
|||
ELSE |
|||
# could have an extension # |
|||
INT pos := UPB pathname; |
|||
WHILE pos > LWB pathname AND isalnum( pathname[ pos ] ) DO |
|||
pos -:= 1 |
|||
OD; |
|||
IF pathname[ pos ] = "." THEN |
|||
# the character before the letters and digits was a "." # |
|||
pathname[ pos : ] |
|||
ELSE |
|||
# no "." before the letters and digits - no extension # |
|||
"" |
|||
FI |
|||
FI ; # EXTENSION # |
|||
# test the EXTENSION operator # |
|||
PROC test extension = ( STRING pathname, STRING expected extension )VOID: |
|||
BEGIN |
|||
STRING extension = EXTENSION pathname; |
|||
write( ( ( pathname |
|||
+ " got extension: (" |
|||
+ extension |
|||
+ ") " |
|||
+ IF extension = expected extension THEN "" ELSE "NOT" FI |
|||
+ " as expected" |
|||
) |
|||
, newline |
|||
) |
|||
) |
|||
END ; # text extension # |
|||
main: |
|||
( test extension( "picture.jpg", ".jpg" ) |
|||
; test extension( "http://mywebsite.com/picture/image.png", ".png" ) |
|||
; test extension( "myuniquefile.longextension", ".longextension" ) |
|||
; test extension( "IAmAFileWithoutExtension", "" ) |
|||
; test extension( "/path/to.my/file", "" ) |
|||
; test extension( "file.odd_one", "" ) |
|||
)</lang> |
|||
{{out}} |
|||
<pre> |
|||
picture.jpg got extension: (.jpg) as expected |
|||
http://mywebsite.com/picture/image.png got extension: (.png) as expected |
|||
myuniquefile.longextension got extension: (.longextension) as expected |
|||
IAmAFileWithoutExtension got extension: () as expected |
|||
/path/to.my/file got extension: () as expected |
|||
file.odd_one got extension: () as expected |
|||
</pre> |
|||
=={{header|ALGOL W}}== |
|||
<lang algolw>begin |
|||
% extracts a file-extension from the end of a pathname. % |
|||
% The file extension is defined as a dot followed by one or more letters % |
|||
% or digits. As Algol W only has fixed length strings we limit the % |
|||
% extension to 32 characters and the pathname to 256 (the longest string % |
|||
% allowed by Algol W) % |
|||
string(32) procedure extension( string(256) value pathname ) ; |
|||
begin |
|||
integer pathPos; |
|||
% position to the previous character in the pathname % |
|||
procedure prev ; pathPos := pathPos - 1; |
|||
% get the character as pathPos from pathname % |
|||
string(1) procedure ch ; pathname( pathPos // 1 ); |
|||
% checks for a letter or digit - assumes the letters are contiguous % |
|||
% in the character set - not true for EBCDIC % |
|||
logical procedure isLetterOrDigit( string(1) value c ) ; |
|||
( c <= "z" and c >= "a" ) or ( c <= "Z" and c >= "A" ) |
|||
or ( c <= "9" and c >= "0" ) ; |
|||
% find the length of the pathname with trailing blanks removed % |
|||
pathPos := 255; |
|||
while pathPos >= 0 and ch = " " do prev; |
|||
% extract the extension if possible % |
|||
if pathPos <= 0 |
|||
then "" % no extension: 0 or 1 character pathname % |
|||
else if not isLetterOrDigit( ch ) |
|||
then "" % no extension: last character not a letter/digit % |
|||
else begin |
|||
while pathPos > 0 and isLetterOrDigit( ch ) do prev; |
|||
if ch not = "." |
|||
then "" % no extension: letters/digits not preceeded by "." % |
|||
else begin |
|||
% have an extension % |
|||
string(32) ext; |
|||
ext := " "; |
|||
% algol W substring lengths must be compile-time constants % |
|||
% hence the loop to copy the extension characters % |
|||
for charPos := 0 until 31 do begin |
|||
if pathPos <= 255 then begin |
|||
ext( charPos // 1 ) := pathname( pathPos // 1 ); |
|||
pathPos := pathPos + 1 |
|||
end |
|||
end for_charPos ; |
|||
ext |
|||
end |
|||
end |
|||
end extension ; |
|||
% test the extension procedure % |
|||
procedure testExtension( string(256) value pathname |
|||
; string(32) value expectedExtension |
|||
) ; |
|||
begin |
|||
string(32) ext; |
|||
ext := extension( pathname ); |
|||
write( pathname( 0 // 40 ) |
|||
, " -> (" |
|||
, ext( 0 // 16 ) |
|||
, ") " |
|||
, if ext = expectedExtension then "" else "NOT" |
|||
, " as expected" |
|||
) |
|||
end ; % text extension % |
|||
testExtension( "picture.jpg", ".jpg" ); |
|||
testExtension( "http://mywebsite.com/picture/image.png", ".png" ); |
|||
testExtension( "myuniquefile.longextension", ".longextension" ); |
|||
testExtension( "IAmAFileWithoutExtension", "" ); |
|||
testExtension( "/path/to.my/file", "" ); |
|||
testExtension( "file.odd_one", "" ); |
|||
end.</lang> |
|||
{{out}} |
|||
<pre> |
|||
picture.jpg -> (.jpg ) as expected |
|||
http://mywebsite.com/picture/image.png -> (.png ) as expected |
|||
myuniquefile.longextension -> (.longextension ) as expected |
|||
IAmAFileWithoutExtension -> ( ) as expected |
|||
/path/to.my/file -> ( ) as expected |
|||
file.odd_one -> ( ) as expected |
|||
</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |