File: quote_c.cc

package info (click to toggle)
aegis 4.24.3-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 34,056 kB
  • ctags: 12,500
  • sloc: cpp: 178,528; sh: 79,948; makefile: 34,813; yacc: 4,610; perl: 1,499; ansic: 492; awk: 325
file content (110 lines) | stat: -rw-r--r-- 2,648 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
//	aegis - project change supervisor
//	Copyright (C) 2006, 2008 Peter Miller
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program. If not, see
//	<http://www.gnu.org/licenses/>.
//

#include <common/ac/ctype.h>

#include <common/nstring.h>
#include <common/nstring/accumulator.h>


nstring
nstring::quote_c()
    const
{
    static nstring_accumulator ac;
    ac.clear();
    ac.push_back('"');
    const char *cp = c_str();
    for (;;)
    {
	unsigned char c = *cp++;
	switch (c)
	{
	case '\0':
	    ac.push_back('"');
	    return ac.mkstr();

	case '\\':
	    ac.push_back("\\\\");
	    break;

	case '"':
	    ac.push_back("\\\"");
	    break;

	case '\a':
	    ac.push_back("\\a");
	    break;

	case '\b':
	    ac.push_back("\\b");
	    break;

	case '\f':
	    ac.push_back("\\f");
	    break;

	case '\n':
	    ac.push_back("\\n");
	    break;

	case '\r':
	    ac.push_back("\\r");
	    break;

	case '\v':
	    ac.push_back("\\v");
	    break;

	case ' ': case '!':           case '#':
	case '$': case '%': case '&': case '\'':
	case '(': case ')': case '*': case '+':
	case ',': case '-': case '.': case '/':
	case '0': case '1': case '2': case '3':
	case '4': case '5': case '6': case '7':
	case '8': case '9': case ':': case ';':
	case '<': case '=': case '>': case '?':
	case '@': case 'A': case 'B': case 'C':
	case 'D': case 'E': case 'F': case 'G':
	case 'H': case 'I': case 'J': case 'K':
	case 'L': case 'M': case 'N': case 'O':
	case 'P': case 'Q': case 'R': case 'S':
	case 'T': case 'U': case 'V': case 'W':
	case 'X': case 'Y': case 'Z': case '[':
	          case ']': case '^': case '_':
	case '`': case 'a': case 'b': case 'c':
	case 'd': case 'e': case 'f': case 'g':
	case 'h': case 'i': case 'j': case 'k':
	case 'l': case 'm': case 'n': case 'o':
	case 'p': case 'q': case 'r': case 's':
	case 't': case 'u': case 'v': case 'w':
	case 'x': case 'y': case 'z': case '{':
	case '|': case '}': case '~':
	    ac.push_back(c);
	    break;

	default:
	    if (isprint(c))
		ac.push_back(c);
	    else
		ac.push_back(nstring::format("\\%03o", c));
	    break;
	}
    }
}