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