This repository was archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cc
More file actions
79 lines (66 loc) · 1.85 KB
/
util.cc
File metadata and controls
79 lines (66 loc) · 1.85 KB
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
// misc utilities
// Copyright (C) 2014-2015 Serguei Makarov
// Copyright (C) 2005-2013 Red Hat Inc.
// Copyright (C) 2005-2008 Intel Corporation.
// Copyright (C) 2010 Novell Corporation.
//
// This file is part of EBT, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include <iostream>
#include "util.h"
using namespace std;
translator_output::translator_output (ostream &file) : o(file), tablevel(0) {}
ostream&
translator_output::newline (int indent_amount)
{
indent(indent_amount);
o << "\n";
for (unsigned i = 0; i < tablevel; i++)
o << " ";
return o;
}
void
translator_output::indent (int amount)
{
tablevel += amount;
}
ostream&
translator_output::line ()
{
return o;
}
/* escape questionable stuff in a C string literal
quick and dirty (meant for printf statements that emit original script code) */
string
c_stringify(const std::string &unescaped)
{
string result("");
for (unsigned i = 0; i < unescaped.size(); i++)
{
char c = unescaped[i];
/* escape questionable stuff: */
if (c == '\n') { result.push_back('\\'); result.push_back('n'); continue; } // TODOXXX may want to omit newline altogether
if (c == '\"' || c == '\\') result.push_back('\\');
result.push_back(c);
}
return result;
}
/* output a string into a multiline C comment
(does not support // C++-style comments) */
string
c_comment(const std::string &unescaped)
{
string result("");
for (unsigned i = 0; i < unescaped.size(); i++)
{
char c = unescaped[i];
char c2 = i < unescaped.size() - 1 ? unescaped[i+1] : '\0';
result.push_back(c);
/* avoid inserting closing comment: */
if (c == '*' && c2 == '/')
result.push_back(' ');
}
return result;
}