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