Kea 2.5.7
filename.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <iostream>
10#include <algorithm>
11#include <string>
12
13#include <ctype.h>
14
15#include <util/filename.h>
16
17using namespace std;
18
19
20namespace isc {
21namespace util {
22
23// Split string into components. Any backslashes are assumed to have
24// been replaced by forward slashes.
25
26void
27Filename::split(const string& full_name, string& directory, string& name,
28 string& extension) const {
29 directory = name = extension = "";
30 if (!full_name.empty()) {
31
32 bool dir_present = false;
33 // Find the directory.
34 size_t last_slash = full_name.find_last_of('/');
35 if (last_slash != string::npos) {
36
37 // Found the last slash, so extract directory component and
38 // set where the scan for the last_dot should terminate.
39 directory = full_name.substr(0, last_slash + 1);
40 if (last_slash == full_name.size()) {
41
42 // The entire string was a directory, so exit not and don't
43 // do any more searching.
44 return;
45 }
46
47 // Found a directory so note the fact.
48 dir_present = true;
49 }
50
51 // Now search backwards for the last ".".
52 size_t last_dot = full_name.find_last_of('.');
53 if ((last_dot == string::npos) ||
54 (dir_present && (last_dot < last_slash))) {
55
56 // Last "." either not found or it occurs to the left of the last
57 // slash if a directory was present (so it is part of a directory
58 // name). In this case, the remainder of the string after the slash
59 // is the name part.
60 name = full_name.substr(last_slash + 1);
61 return;
62 }
63
64 // Did find a valid dot, so it and everything to the right is the
65 // extension...
66 extension = full_name.substr(last_dot);
67
68 // ... and the name of the file is everything in between.
69 if ((last_dot - last_slash) > 1) {
70 name = full_name.substr(last_slash + 1, last_dot - last_slash - 1);
71 }
72 }
73
74}
75
76// Expand the stored filename with the default.
77
78string
79Filename::expandWithDefault(const string& defname) const {
80
81 string def_directory("");
82 string def_name("");
83 string def_extension("");
84
85 // Normalize the input string.
86 string copy_defname = isc::util::str::trim(defname);
87#ifdef WIN32
89#endif
90
91 // Split into the components
92 split(copy_defname, def_directory, def_name, def_extension);
93
94 // Now construct the result.
95 string retstring =
96 (directory_.empty() ? def_directory : directory_) +
97 (name_.empty() ? def_name : name_) +
98 (extension_.empty() ? def_extension : extension_);
99 return (retstring);
100}
101
102// Use the stored name as default for a given name
103
104string
105Filename::useAsDefault(const string& name) const {
106
107 string name_directory("");
108 string name_name("");
109 string name_extension("");
110
111 // Normalize the input string.
112 string copy_name = isc::util::str::trim(name);
113#ifdef WIN32
115#endif
116
117 // Split into the components
118 split(copy_name, name_directory, name_name, name_extension);
119
120 // Now construct the result.
121 string retstring =
122 (name_directory.empty() ? directory_ : name_directory) +
123 (name_name.empty() ? name_ : name_name) +
124 (name_extension.empty() ? extension_ : name_extension);
125 return (retstring);
126}
127
128void
129Filename::setDirectory(const std::string& new_directory) {
130 std::string directory(new_directory);
131
132 if (directory.length() > 0) {
133 // append '/' if necessary
134 size_t sep = directory.rfind('/');
135 if (sep == std::string::npos || sep < directory.size() - 1) {
136 directory += "/";
137 }
138 }
139 // and regenerate the full name
140 std::string full_name = directory + name_ + extension_;
141
142 directory_.swap(directory);
143 full_name_.swap(full_name);
144}
145
146
147} // namespace log
148} // namespace isc
std::string extension() const
Definition: filename.h:93
std::string name() const
Definition: filename.h:88
std::string expandWithDefault(const std::string &defname) const
Expand Name with Default.
Definition: filename.cc:79
std::string directory() const
Definition: filename.h:76
void setDirectory(const std::string &new_directory)
Set directory for the file.
Definition: filename.cc:129
std::string useAsDefault(const std::string &name) const
Use as Default and Substitute into String.
Definition: filename.cc:105
void normalizeSlash(std::string &name)
Normalize Backslash.
Definition: strutil.cc:41
string trim(const string &instring)
Trim Leading and Trailing Spaces.
Definition: strutil.cc:53
Defines the logger used by the top-level component of kea-lfc.