string.cc (242b24828472137ec4411826b86e753d49bd2c39) | string.cc (21d5d37ba4c0131d6c141695366e266e32cc3bc1) |
---|---|
1/*- 2 * Copyright (c) 2013 David Chisnall 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * --- 17 unchanged lines hidden (view full) --- 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33#include <string> | 1/*- 2 * Copyright (c) 2013 David Chisnall 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * --- 17 unchanged lines hidden (view full) --- 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 33#include <string> |
34#include <functional> |
|
34#include <cstdio> 35#include <cstdlib> 36#include <ctype.h> 37#include <libgen.h> 38 39#include "util.hh" 40 41using std::string; --- 74 unchanged lines hidden (view full) --- 116 break; 117 } 118 } 119 } 120 buffer.push_back(c); 121 } 122} 123 | 35#include <cstdio> 36#include <cstdlib> 37#include <ctype.h> 38#include <libgen.h> 39 40#include "util.hh" 41 42using std::string; --- 74 unchanged lines hidden (view full) --- 117 break; 118 } 119 } 120 } 121 buffer.push_back(c); 122 } 123} 124 |
124std::string dirname(const string &s) | 125namespace { 126string 127dirbasename(std::function<char*(char*)> fn, const string &s) |
125{ 126 if (s == string()) 127 { 128 return string(); 129 } | 128{ 129 if (s == string()) 130 { 131 return string(); 132 } |
130 char *str = strdup(s.c_str()); 131 string dn(::dirname(str)); 132 free(str); | 133 std::unique_ptr<char, decltype(free)*> str = {strdup(s.c_str()), free}; 134 string dn(fn(str.get())); |
133 return dn; 134} | 135 return dn; 136} |
137} |
|
135 | 138 |
136std::string basename(const string &s) | 139string dirname(const string &s) |
137{ | 140{ |
138 if (s == string()) 139 { 140 return string(); 141 } 142 char *str = strdup(s.c_str()); 143 string bn(::basename(str)); 144 free(str); 145 return bn; | 141 return dirbasename(::dirname, s); |
146} | 142} |
143 144string basename(const string &s) 145{ 146 return dirbasename(::basename, s); 147} |
|
147} // namespace dtc 148 | 148} // namespace dtc 149 |