forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefines.cpp
More file actions
150 lines (130 loc) · 3.2 KB
/
defines.cpp
File metadata and controls
150 lines (130 loc) · 3.2 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* @file defines.cpp
* Manages the table of defines for some future time when these will be used to
* help decide whether a block of #if'd code should be formatted.
*
* !! This isn't used right now. !!
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "uncrustify_types.h"
#include "char_table.h"
#include "args.h"
#include <cstring>
#include <cerrno>
#include <cstdlib>
#include <map>
#include "unc_ctype.h"
#include "chunk_list.h"
#include "prototypes.h"
using namespace std;
typedef map<string, string> defmap;
defmap defines;
/**
* Adds an entry to the define list
*
* @param tag The tag (string) must be zero terminated
* @param value NULL or the value of the define
*/
void add_define(const char *tag, const char *value)
{
if ((tag == NULL) || (*tag == 0))
{
return;
}
value = value ? value : "";
/* Try to update an existing entry first */
defmap::iterator it = defines.find(tag);
if (it != defines.end())
{
(*it).second = value;
LOG_FMT(LDEFVAL, "%s: updated '%s' = '%s'\n", __func__, tag, value);
return;
}
/* Insert a new entry */
defines.insert(defmap::value_type(tag, value));
LOG_FMT(LDEFVAL, "%s: added '%s' = '%s'\n", __func__, tag, value);
}
/**
* Loads the defines from a file
*
* @param filename The path to the file to load
* @return SUCCESS or FAILURE
*/
int load_define_file(const char *filename)
{
FILE *pf;
char buf[160];
char *ptr;
char *args[3];
int argc;
int line_no = 0;
pf = fopen(filename, "r");
if (pf == NULL)
{
LOG_FMT(LERR, "%s: fopen(%s) failed: %s (%d)\n",
__func__, filename, strerror(errno), errno);
cpd.error_count++;
return(FAILURE);
}
while (fgets(buf, sizeof(buf), pf) != NULL)
{
line_no++;
/* remove comments */
if ((ptr = strchr(buf, '#')) != NULL)
{
*ptr = 0;
}
argc = Args::SplitLine(buf, args, ARRAY_SIZE(args) - 1);
args[argc] = 0;
if (argc > 0)
{
if ((argc <= 2) && CharTable::IsKw1(*args[0]))
{
LOG_FMT(LDEFVAL, "%s: line %d - %s\n", filename, line_no, args[0]);
add_define(args[0], args[1]);
}
else
{
LOG_FMT(LWARN, "%s: line %d invalid (starts with '%s')\n",
filename, line_no, args[0]);
cpd.error_count++;
}
}
}
fclose(pf);
return(SUCCESS);
}
void output_defines(FILE *pfile)
{
if (defines.size() > 0)
{
fprintf(pfile, "-== User Defines ==-\n");
defmap::iterator it;
for (it = defines.begin(); it != defines.end(); ++it)
{
if ((*it).second.size() > 0)
{
fprintf(pfile, "%s = %s\n", (*it).first.c_str(), (*it).second.c_str());
}
else
{
fprintf(pfile, "%s\n", (*it).first.c_str());
}
}
}
}
void print_defines(FILE *pfile)
{
defmap::iterator it;
for (it = defines.begin(); it != defines.end(); ++it)
{
fprintf(pfile, "define %*.s%s \"%s\"\n",
cpd.max_option_name_len - 6, " ", (*it).first.c_str(), (*it).second.c_str());
}
}
void clear_defines(void)
{
defines.clear();
}