]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | (* M2FileName.mod construct file names. |
2 | ||
83ffe9cd | 3 | Copyright (C) 2001-2023 Free Software Foundation, Inc. |
1eee94d3 GM |
4 | Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>. |
5 | ||
6 | This file is part of GNU Modula-2. | |
7 | ||
8 | GNU Modula-2 is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU Modula-2 is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU Modula-2; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. *) | |
21 | ||
22 | IMPLEMENTATION MODULE M2FileName ; | |
23 | ||
24 | ||
25 | FROM ASCII IMPORT nul ; | |
26 | FROM DynamicStrings IMPORT InitString, Mark, Slice, Dup, ConCatChar, ConCat, Length, Equal, Index ; | |
27 | ||
28 | ||
29 | CONST | |
30 | MaxFileName = 0 ; (* zero means no limits *) | |
31 | MaxStemName = 0 ; | |
32 | Directory = '/' ; | |
33 | ||
34 | ||
35 | (* | |
36 | currently there are no limits on filename length, this may | |
37 | be incorrect on some systems. | |
38 | *) | |
39 | ||
40 | ||
41 | (* | |
42 | CalculateFileName - calculates and returns a new string filename given a module | |
43 | and an extension. String, Extension, is concatenated onto | |
44 | Module and thus it is safe to `Mark' the extension for garbage | |
45 | collection. | |
46 | *) | |
47 | ||
48 | PROCEDURE CalculateFileName (Module, Extension: String) : String ; | |
49 | BEGIN | |
50 | IF MaxFileName=0 | |
51 | THEN | |
52 | RETURN( ConCat(ConCatChar(Slice(Module, 0, MaxFileName), '.'), Extension) ) | |
53 | ELSE | |
54 | RETURN( ConCat(ConCatChar(Slice(Module, 0, MaxFileName-Length(Extension)-1), '.'), Extension) ) | |
55 | END | |
56 | END CalculateFileName ; | |
57 | ||
58 | ||
59 | (* | |
60 | CalculateStemName - calculates the stem name for given a module. | |
61 | This name length will be operating system and | |
62 | compiler specific. | |
63 | *) | |
64 | ||
65 | PROCEDURE CalculateStemName (Module: String) : String ; | |
66 | BEGIN | |
67 | RETURN( Slice(Module, 0, MaxStemName) ) | |
68 | END CalculateStemName ; | |
69 | ||
70 | ||
71 | (* | |
72 | ExtractExtension - given a, filename, return the filename without | |
73 | the extension, Ext. | |
74 | *) | |
75 | ||
76 | PROCEDURE ExtractExtension (filename, ext: String) : String ; | |
77 | BEGIN | |
78 | IF Equal(ext, Mark(Slice(filename, -Length(ext), 0))) | |
79 | THEN | |
80 | RETURN( Slice(filename, 0, -Length(ext)) ) | |
81 | ELSE | |
82 | RETURN( filename ) | |
83 | END | |
84 | END ExtractExtension ; | |
85 | ||
86 | ||
87 | (* | |
88 | ExtractModule - given a, filename, return the module name including any | |
89 | extension. A new string is returned. | |
90 | *) | |
91 | ||
92 | PROCEDURE ExtractModule (filename: String) : String ; | |
93 | VAR | |
94 | i: INTEGER ; | |
95 | BEGIN | |
96 | i := Index(filename, Directory, 0) ; | |
97 | IF i=-1 | |
98 | THEN | |
99 | RETURN( Dup(filename) ) | |
100 | ELSE | |
101 | RETURN( Slice(filename, i+1, 0) ) | |
102 | END | |
103 | END ExtractModule ; | |
104 | ||
105 | ||
106 | END M2FileName. |