Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/string/escape.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
#include "../mednafen.h"
19
#include "escape.h"
20
21
static unsigned int hex_nibble_to_val(char nibble)
22
{
23
unsigned int ret = 0;
24
nibble = tolower(nibble);
25
26
if(nibble >= '0' && nibble <= '9')
27
ret = nibble - '0';
28
else
29
ret = nibble - 'a';
30
31
return(ret);
32
}
33
34
void unescape_string(char *string)
35
{
36
char *src = string;
37
bool inescape = 0;
38
uint8 hoval = 0;
39
int inhex = 0;
40
int inoctal = 0;
41
42
while(*src)
43
{
44
if(*src == '\\')
45
{
46
inescape = TRUE;
47
inhex = 0;
48
inoctal = 0;
49
}
50
else if(inhex)
51
{
52
if(inhex == 1)
53
{
54
hoval = hex_nibble_to_val(*src) << 4;
55
inhex++;
56
}
57
else if(inhex == 2)
58
{
59
hoval |= hex_nibble_to_val(*src);
60
*string = hoval;
61
string++;
62
hoval = 0;
63
inhex = 0;
64
}
65
}
66
else if(inoctal)
67
{
68
if(inoctal == 1)
69
{
70
hoval = (*src - '0') * 8 * 8;
71
}
72
else if(inoctal == 2)
73
{
74
hoval += (*src - '0') * 8;
75
}
76
else
77
{
78
hoval += *src - '0';
79
*string = hoval;
80
string++;
81
hoval = 0;
82
inoctal = 0;
83
}
84
}
85
else if(inescape)
86
{
87
switch(*src)
88
{
89
case 'a': *string = 7; string++; break;
90
case 'b': *string = 8; string++; break;
91
case 'f': *string = 12; string++; break;
92
case 'n': *string = 10; string++; break;
93
case 'r': *string = 13; string++; break;
94
case 't': *string = 9; string++; break;
95
case 'v': *string = 11; string++; break;
96
97
case '\\': *string = '\\'; string++; break;
98
case '?': *string = '?'; string++; break;
99
case '\'': *string = '\''; string++; break;
100
case '"': *string = '"'; string++; break;
101
102
case 'o': inoctal = 1; break;
103
case 'x': inhex = 1; break;
104
105
106
default: *string = *src; string++; break;
107
}
108
inescape = 0;
109
}
110
else
111
{
112
*string = *src;
113
string++;
114
}
115
src++;
116
}
117
*string = 0;
118
}
119
120
char *escape_string(const char *text)
121
{
122
uint32 slen = strlen(text);
123
char *ret = (char*)malloc(slen * 4 + 1); // \xFF
124
char *outoo = ret;
125
126
for(uint32 x = 0; x < slen; x++)
127
{
128
int c = (uint8)text[x];
129
130
if(c < 0x20 || c == 0x7F || c == '\\' || c == '\'' || c == '"')
131
{
132
*outoo++ = '\\';
133
134
switch(c)
135
{
136
case '\\': *outoo++ = '\\'; break;
137
case '\'': *outoo++ = '\''; break;
138
case '"': *outoo++ = '"'; break;
139
case 7: *outoo++ = 'a'; break;
140
case 8: *outoo++ = 'b'; break;
141
case 12: *outoo++ = 'f'; break;
142
case 10: *outoo++ = 'n'; break;
143
case 13: *outoo++ = 'r'; break;
144
case 9: *outoo++ = 't'; break;
145
case 11: *outoo++ = 'v'; break;
146
147
default: outoo += sprintf(outoo, "x%02x", c); break;
148
}
149
}
150
else
151
*outoo++ = c;
152
}
153
154
*outoo = 0;
155
156
return(ret);
157
}
158
159