]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/dtc/string.cc
MFV r323678: file 5.32
[FreeBSD/FreeBSD.git] / usr.bin / dtc / string.cc
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  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
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>
35 #include <cstdio>
36 #include <cstdlib>
37 #include <ctype.h>
38 #include <libgen.h>
39
40 #include "util.hh"
41
42 using std::string;
43
44 namespace dtc
45 {
46
47 void
48 push_string(byte_buffer &buffer, const string &s, bool escapes)
49 {
50         size_t length = s.size();
51         for (size_t i=0 ; i<length ; ++i)
52         {
53                 uint8_t c = s[i];
54                 if (escapes && c == '\\' && i+1 < length)
55                 {
56                         c = s[++i];
57                         switch (c)
58                         {
59                                 // For now, we just ignore invalid escape sequences.
60                                 default:
61                                 case '"':
62                                 case '\'':
63                                 case '\\':
64                                         break;
65                                 case 'a':
66                                         c = '\a';
67                                         break;
68                                 case 'b':
69                                         c = '\b';
70                                         break;
71                                 case 't':
72                                         c = '\t';
73                                         break;
74                                 case 'n':
75                                         c = '\n';
76                                         break;
77                                 case 'v':
78                                         c = '\v';
79                                         break;
80                                 case 'f':
81                                         c = '\f';
82                                         break;
83                                 case 'r':
84                                         c = '\r';
85                                         break;
86                                 case '0'...'7':
87                                 {
88                                         int v = digittoint(c);
89                                         if (i+1 < length && s[i+1] <= '7' && s[i+1] >= '0')
90                                         {
91                                                 v <<= 3;
92                                                 v |= digittoint(s[i+1]);
93                                                 i++;
94                                                 if (i+1 < length && s[i+1] <= '7' && s[i+1] >= '0')
95                                                 {
96                                                         v <<= 3;
97                                                         v |= digittoint(s[i+1]);
98                                                 }
99                                         }
100                                         c = (uint8_t)v;
101                                         break;
102                                 }
103                                 case 'x':
104                                 {
105                                         ++i;
106                                         if (i >= length)
107                                         {
108                                                 break;
109                                         }
110                                         int v = digittoint(s[i]);
111                                         if (i+1 < length && ishexdigit(s[i+1]))
112                                         {
113                                                 v <<= 4;
114                                                 v |= digittoint(s[++i]);
115                                         }
116                                         c = (uint8_t)v;
117                                         break;
118                                 }
119                         }
120                 }
121                 buffer.push_back(c);
122         }
123 }
124
125 namespace {
126 string
127 dirbasename(std::function<char*(char*)> fn, const string &s)
128 {
129         if (s == string())
130         {
131                 return string();
132         }
133         std::unique_ptr<char, decltype(free)*> str = {strdup(s.c_str()), free};
134         string dn(fn(str.get()));
135         return dn;
136 }
137 }
138
139 string dirname(const string &s)
140 {
141         return dirbasename(::dirname, s);
142 }
143
144 string basename(const string &s)
145 {
146         return dirbasename(::basename, s);
147 }
148 } // namespace dtc
149