1 /*
2 * sccsname.cc: Part of GNU CSSC.
3 *
4 *
5 * Copyright (C) 1997, 2007, 2008, 2009, 2010, 2011, 2014, 2019 Free
6 * Software Foundation, Inc.
7 *
8 * This program 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 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * CSSC was originally Based on MySC, by Ross Ridge, which was
22 * placed in the Public Domain.
23 *
24 *
25 * Members of the class sccs_name.
26 *
27 */
28
29 #include <config.h>
30 #include <string>
31 #include "cssc.h"
32 #include "sccsname.h"
33 #include "cssc-assert.h"
34 #include "file.h"
35
36 #include <ctype.h>
37
38 #ifdef CONFIG_MSDOS_FILES
39 #error The CONFIG_MSDOS_FILES option is no longer supported. See sccsname.cc.
40 // Please feel free to add the support yourself. Don't forget
41 // to send the patches to <jay@gnu.org>.
42 #endif
43
44 std::string
base_part(const std::string & name)45 base_part(const std::string& name)
46 {
47 std::string dirname, basename;
48 split_filename(name, dirname, basename);
49 return basename;
50 }
51
52 int
valid_filename(const char * thename)53 sccs_name::valid_filename(const char *thename)
54 {
55 ASSERT(0 != thename);
56
57 if (thename && thename[0])
58 {
59 std::string base = base_part(std::string(thename));
60 const int valid = (base.at(0) == 's' && base.at(1) == '.');
61 return valid;
62 }
63 else
64 {
65 // empty filename; not valid.
66 return 0;
67 }
68 }
69
70 void
create()71 sccs_name::create()
72 {
73 // derive all other names from "name",
74 // when they're asked for.
75 std::string dirname, basename;
76 split_filename(sname, dirname, basename);
77
78 name_front = dirname;
79
80 if (basename.length() >= 2)
81 {
82 const char *s = basename.c_str();
83
84 // name_rear does not contain the "s"
85 // of the "s." but does contain the dot itself.
86 name_rear = std::string(s+1);
87
88 // The gname does not include the directory part,
89 // or the leading "s.".
90 gname = std::string(s+2);
91 }
92 else
93 {
94 name_rear = basename;
95 gname = std::string("");
96 }
97 }
98
99 // I don't think we EVER need this function.
100 // sccs_name::sccs_name(sccs_name const &from) : lock_cnt(from.lock_cnt)
101 // {
102 // sname = from.sfile();
103 // create();
104 // }
105
106 sccs_name &
operator =(const std::string & newname)107 sccs_name::operator =(const std::string& newname)
108 {
109 ASSERT(newname.length() != 0);
110 sname = newname;
111 create();
112 return *this;
113 }
114
115
116 std::string sccs_name::
sub_file(char insertme) const117 sub_file(char insertme) const
118 {
119 char prefix[2];
120 prefix[0] = insertme;
121 prefix[1] = 0;
122 std::string ret = name_front + std::string(prefix) + name_rear;
123 return ret;
124 }
125
126 std::string sccs_name::
lfile() const127 lfile() const
128 {
129 // Can't use sub_file since, like for the g-file,
130 // we need to omit the directory.
131 return std::string("l") + name_rear;
132 }
133
134
135 #ifdef CONFIG_FILE_NAME_GUESSING
136
137 void
make_valid()138 sccs_name::make_valid()
139 {
140 ASSERT(sname.length() != 0);
141 ASSERT(!valid_filename(sname.c_str()));
142
143 std::string dirpart, basepart;
144 split_filename(sname, dirpart, basepart);
145
146
147 // try dir/s.foo
148 const std::string s_dot_foo = std::string("s.") + basepart;
149 sname = dirpart + s_dot_foo;
150
151 if (!is_readable(sname.c_str()))
152 {
153 // try dir/SCCS/s.foo
154 const std::string tmp = dirpart + std::string("SCCS/") + s_dot_foo;
155
156 if (is_readable(tmp.c_str()))
157 sname = tmp;
158 }
159
160 create();
161 }
162
163 #endif /* CONFIG_FILE_NAME_GUESSING */
164
165 /* Local variables: */
166 /* mode: c++ */
167 /* End: */
168