filename.pl 1000 Bytes
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  filename,
  [
Thierry Martinez's avatar
Thierry Martinez committed
4 5
    input_file/1,
    output_file/1,
Thierry Martinez's avatar
Thierry Martinez committed
6 7
    filename/2,
    chop_suffix/2,
Thierry Martinez's avatar
NuSMV  
Thierry Martinez committed
8
    automatic_suffix/4
Thierry Martinez's avatar
Thierry Martinez committed
9 10 11 12
  ]
).


Thierry Martinez's avatar
Thierry Martinez committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
:- grammar(input_file).


input_file(Name) :-
  atom(Name).


:- grammar(output_file).


output_file(Name) :-
  atom(Name).


Thierry Martinez's avatar
Thierry Martinez committed
27
filename(Filename, ExpandedFilename) :-
Thierry Martinez's avatar
Thierry Martinez committed
28
  expand_file_name(Filename, List),
Thierry Martinez's avatar
Thierry Martinez committed
29 30 31
  member(ExpandedFilename, List).


Thierry Martinez's avatar
Thierry Martinez committed
32 33
path_delimiter('/').

Thierry Martinez's avatar
Thierry Martinez committed
34 35 36 37 38 39 40 41 42 43
chop_suffix(Filename, Suffix) :-
  atom_chars(Filename, Chars),
  (
    append(_, ['.' | SuffixChars], Chars),
    \+ member('.', SuffixChars),
    path_delimiter(PathDelimiter),
    \+ member(PathDelimiter, SuffixChars)
  ->
    atom_chars(Suffix, ['.' | SuffixChars])
  ;
Thierry Martinez's avatar
Thierry Martinez committed
44
    Suffix = ''
Thierry Martinez's avatar
Thierry Martinez committed
45 46 47
  ).


Thierry Martinez's avatar
NuSMV  
Thierry Martinez committed
48
automatic_suffix(Filename, DefaultSuffix, Mode, FullFilename) :-
Thierry Martinez's avatar
Thierry Martinez committed
49 50 51 52
  chop_suffix(Filename, Suffix),
  (
    Suffix = '',
    atom_concat(Filename, DefaultSuffix, FullFilename),
Thierry Martinez's avatar
NuSMV  
Thierry Martinez committed
53 54 55 56 57 58 59
    (
      Mode = read
    ->
      exists_file(FullFilename)
    ;
      true
    )
Thierry Martinez's avatar
Thierry Martinez committed
60 61 62 63 64
  ->
    true
  ;
    FullFilename = Filename
  ).