Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/liblcdf/filename.cc

raw
1 // -*- related-file-name: "../include/lcdf/filename.hh" -*-
2
3 /* filename.{cc,hh} -- filenames
4 *
5 * Copyright (c) 1999-2019 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <lcdf/filename.hh>
20 #include <lcdf/landmark.hh>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #ifndef PATHNAME_SEPARATOR
27 # define PATHNAME_SEPARATOR '/'
28 #endif
29 #ifndef CURRENT_DIRECTORY
30 # define CURRENT_DIRECTORY "./"
31 #endif
32
33
34 Filename::Filename(const String &fn)
35 : _path(fn), _actual(0)
36 {
37 if (!fn)
38 return;
39
40 int last_slash = fn.find_right(PATHNAME_SEPARATOR);
41 if (last_slash >= 0) {
42 _dir = _path.substring(0, last_slash + 1);
43 _name = _path.substring(last_slash + 1);
44 } else {
45 _dir = CURRENT_DIRECTORY;
46 _name = fn;
47 }
48
49 assert(_dir.back() == PATHNAME_SEPARATOR);
50 }
51
52 Filename::Filename(const String &dir, const String &name)
53 : _name(name), _actual(0)
54 {
55 if (!name)
56 return;
57
58 if (_name[0] == PATHNAME_SEPARATOR)
59 _dir = "";
60 else if (dir) {
61 _dir = dir;
62 if (dir.back() != PATHNAME_SEPARATOR)
63 _dir += PATHNAME_SEPARATOR;
64 } else
65 _dir = CURRENT_DIRECTORY;
66
67 int slash = name.find_right(PATHNAME_SEPARATOR);
68 if (slash >= 0) {
69 _dir += name.substring(0, slash + 1);
70 _name = name.substring(slash + 1);
71 }
72
73 _path = _dir + _name;
74 }
75
76 Filename::Filename(FILE *actual, const String &name)
77 : _name(name), _path(name), _actual(actual)
78 {
79 }
80
81
82 String
83 Filename::extension() const
84 {
85 int dot = _name.find_right('.');
86 while (dot > 0 && _name[dot - 1] == '.')
87 dot--;
88 if (dot > 0)
89 return _name.substring(dot + 1);
90 else
91 return String();
92 }
93
94 String
95 Filename::base() const
96 {
97 if (String ex = extension())
98 return _name.substring(0, _name.length() - ex.length() - 1);
99 else
100 return _name;
101 }
102
103
104 FILE *
105 Filename::open_read(bool binary) const
106 {
107 if (_actual || !_path)
108 return _actual;
109 else
110 return fopen(_path.c_str(), binary ? "rb" : "r");
111 }
112
113 bool
114 Filename::readable() const
115 {
116 struct stat s;
117 if (!_path)
118 return false;
119 return _actual || (_path && (stat(_path.c_str(), &s) >= 0));
120 }
121
122
123 FILE *
124 Filename::open_write(bool binary) const
125 {
126 if (_actual || !_path)
127 return _actual;
128 else
129 return fopen(_path.c_str(), "wb");
130 }