summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.78.0/src/universalindentgui.cpp
blob: 23b2912a7134b8c3eeb5acfa293803cd1dcc4175 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * @file universalindentgui.cpp
 * Exports the config file for UniversalIndentGUI
 *
 * @author  Ben Gardner
 * @author  Guy Maurel since version 0.62 for uncrustify4Qt
 *          October 2015, 2016
 * @license GPL v2+
 */
#include "universalindentgui.h"

#include "error_types.h"
#include "log_rules.h"
#include "prototypes.h"
#include "unc_ctype.h"
#include "uncrustify.h"
#include "uncrustify_version.h"

#include <cstdio>
#include <vector>


constexpr static auto LCURRENT = LOTHER;

using namespace std;


std::vector<uncrustify::OptionGroup *> get_option_groups()
{
   std::vector<uncrustify::OptionGroup *> groups;
   size_t                                 i = 0;

   while (auto *const g = uncrustify::get_option_group(i))
   {
      groups.push_back(g);
      ++i;
   }
   return(groups);
}


void print_option_choices(FILE *pfile, uncrustify::GenericOption *option,
                          char const *key = "Choices")
{
   fprintf(pfile, "%s=", key);

   for (auto c = option->possibleValues(); *c; ++c)
   {
      fprintf(pfile, "%s\\s*=\\s*%s%c", option->name(), *c, c[1] ? '|' : '\n');
   }
}


void print_universal_indent_cfg(FILE *pfile)
{
   const char *p_name;
   char       ch      = '=';
   const auto &groups = get_option_groups();
   size_t     idx;

#if defined (DEBUG) && !defined (WIN32)
   vector<size_t> allGroups;
   allGroups.reserve(16);
   // first run to get the first option number of each group/category
   size_t optionNumber         = 0;
   bool   firstOptionNumberSet = false;

   for (idx = 0; idx < groups.size(); ++idx)
   {
      const auto *p_grp = groups[idx];

      for (auto *const option : p_grp->options)
      {
         UNUSED(option);

         if (!firstOptionNumberSet)
         {
            allGroups[idx]       = optionNumber;
            firstOptionNumberSet = true;
         }
         optionNumber++;
      } // for (auto *const option : p_grp->options)

      firstOptionNumberSet = false;
   } // end of first run

//#else
//   UNUSED(allGroups);
#endif // DEBUG

   // second run
   // Dump the header and the categories
   fprintf(pfile, "[header]\n");

   // Add all the categories
   //const auto &groups = get_option_groups();
   ch = '=';

   fprintf(pfile, "categories");
   idx = 0;
#if defined (DEBUG) && !defined (WIN32)
   optionNumber = 0;
#endif // DEBUG

   for (auto *const g : groups)
   {
      fputc(ch, pfile);
      ch = '|';

#if defined (DEBUG) && !defined (WIN32)
      fprintf(pfile, "(%zu)", allGroups[idx]);
#endif // DEBUG

      // Write description, stripping leading and trailing newlines
      for (auto dc = g->description + 1; *(dc + 1); ++dc)
      {
         fputc(*dc, pfile);
      }

      idx++;
   }

   fprintf(pfile, "\n");

   fprintf(pfile,
           "cfgFileParameterEnding=cr\n"
           "configFilename=uncrustify.cfg\n");


   // Add all the recognized file extensions
   ch = '=';
   int fileIdx = 0;
   fprintf(pfile, "fileTypes");

   while ((p_name = get_file_extension(fileIdx)) != nullptr)
   {
      fprintf(pfile, "%c*%s", ch, p_name);
      ch = '|';
   }
   fprintf(pfile, "\n");

   // Add the rest of the constant file header
   fprintf(pfile,
           "indenterFileName=uncrustify\n"
           "indenterName=Uncrustify (C, C++, C#, ObjectiveC, D, Java, Pawn, VALA)\n"
           "inputFileName=indentinput\n"
           "inputFileParameter=\"-f \"\n"
           "manual=http://uncrustify.sourceforge.net/config.txt\n"
           "outputFileName=indentoutput\n"
           "outputFileParameter=\"-o \"\n"
           "stringparaminquotes=false\n"
           "parameterOrder=ipo\n"
           "showHelpParameter=-h\n"
           "stringparaminquotes=false\n"
           "useCfgFileParameter=\"-c \"\n"
           "useRegex=true\n");

   fprintf(pfile, "version=%s\n", UNCRUSTIFY_VERSION);

   ch = '=';

   // Now add each option
   for (idx = 0; idx < groups.size(); ++idx)
   {
      const auto *p_grp = groups[idx];

      for (auto *const option : p_grp->options)
      {
         /*
          * Create a better readable name from the options name
          * by replacing '_' by a space and use some upper case characters.
          */
         char *optionNameReadable = new char[strlen(option->name()) + 1];
         strcpy(optionNameReadable, option->name());

         bool was_space = true;

         for (char *character = optionNameReadable; *character != 0; character++)
         {
            if (*character == '_')
            {
               *character = ' ';
               was_space  = true;
            }
            else if (was_space)
            {
               *character = unc_toupper(*character);
               was_space  = false;
            }
         }

#if defined (DEBUG) && !defined (WIN32)
         fprintf(pfile, "\n[(%zu)%s]\n", optionNumber, optionNameReadable);
#else // DEBUG
         fprintf(pfile, "\n[%s]\n", optionNameReadable);
#endif // DEBUG
         fprintf(pfile, "Category=%zu\n", idx);
#if defined (DEBUG) && !defined (WIN32)
         fprintf(pfile, "Description=\"<html>(%zu)", optionNumber);
#else // DEBUG
         fprintf(pfile, "Description=\"<html>");
#endif // DEBUG

         // Skip first character, which is always a newline
         const char *tmp = option->description() + 1;
         ch = 0;

         // Output the description which may contain forbidden chars, skipping
         // the last character which is always an extra newline
         while (  *tmp != 0
               && *(tmp + 1) != 0)
         {
            switch (*tmp)
            {
            case '<':
               fputs("&lt;", pfile);
               break;

            case '>':
               fputs("&gt;", pfile);
               break;

            case '&':
               fputs("&amp;", pfile);
               break;

            case '\n':
               fputs("<br/>", pfile);
               break;

            default:
               fputc(*tmp, pfile);
            }
            tmp++;
         }
         const auto ds = option->defaultStr();

         if (!ds.empty())
         {
            fprintf(pfile, "<br/><br/>Default: %s", ds.c_str());
         }
         fprintf(pfile, "</html>\"\n");

         // Handle some options independent of their type and most by their type.
         log_rule_B("indent_with_tabs");

         if (option == &uncrustify::options::indent_with_tabs)
         {
            // Indenting with tabs selector becomes a multiple selector and not
            // only a number. Also it is by default enabled.
            fprintf(pfile, "Enabled=true\n");
            fprintf(pfile, "EditorType=multiple\n");
            fprintf(pfile, "Choices=\"%s\\s*=\\s*0|%s\\s*=\\s*1|%s\\s*=\\s*2\"\n",
                    option->name(), option->name(), option->name());
#if defined (DEBUG) && !defined (WIN32)
            fprintf(pfile, "ChoicesReadable=\"(%zu)Spaces only|(%zu)Indent with tabs, align with spaces|(%zu)Indent and align with tabs\"\n",
                    optionNumber, optionNumber, optionNumber);
#else // DEBUG
            fprintf(pfile, "ChoicesReadable=\"Spaces only|Indent with tabs, align with spaces|Indent and align with tabs\"\n");
#endif // DEBUG
            fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
         }
         else
         {
            // All not specially handled options are created only dependent by
            // their type.
            fprintf(pfile, "Enabled=false\n");

            switch (option->type())
            {
            case uncrustify::OT_BOOL:
               fprintf(pfile, "EditorType=boolean\n");
               print_option_choices(pfile, option, "TrueFalse");
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;

            case uncrustify::OT_IARF:
               fprintf(pfile, "EditorType=multiple\n");
               print_option_choices(pfile, option);
#if defined (DEBUG) && !defined (WIN32)
               fprintf(pfile, "ChoicesReadable=\"(%zu)Ignore %s|(%zu)Add %s|(%zu)Remove %s|(%zu)Force %s\"\n",
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable);
#else // DEBUG
               //                                0         1      2         3
               fprintf(pfile, "ChoicesReadable=\"Ignore %s|Add %s|Remove %s|Force %s\"\n",
                       optionNameReadable, optionNameReadable, optionNameReadable, optionNameReadable);
#endif // DEBUG
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;

            case uncrustify::OT_NUM:
               fprintf(pfile, "EditorType=numeric\n");
               fprintf(pfile, "CallName=\"%s\\s*=\\s*\"\n", option->name());
               fprintf(pfile, "MinVal=%s\n", option->minStr().c_str());
               fprintf(pfile, "MaxVal=%s\n", option->maxStr().c_str());
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;

            case uncrustify::OT_UNUM:
               fprintf(pfile, "EditorType=numeric\n");
               fprintf(pfile, "CallName=\"%s\\s*=\\s*\"\n", option->name());
               fprintf(pfile, "MinVal=%s\n", option->minStr().c_str());
               fprintf(pfile, "MaxVal=%s\n", option->maxStr().c_str());
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;

            case uncrustify::OT_LINEEND:
               fprintf(pfile, "EditorType=multiple\n");
               print_option_choices(pfile, option);
#if defined (DEBUG) && !defined (WIN32)
               fprintf(pfile, "ChoicesReadable=\"(%zu)Newlines Unix|(%zu)Newlines Win|(%zu)Newlines Mac|(%zu)Newlines Auto\"\n",
                       optionNumber, optionNumber, optionNumber, optionNumber);
#else // DEBUG
               fprintf(pfile, "ChoicesReadable=\"Newlines Unix|Newlines Win|Newlines Mac|Newlines Auto\"\n");
#endif // DEBUG
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;

            case uncrustify::OT_TOKENPOS:
               fprintf(pfile, "EditorType=multiple\n");
               // Issue #2300-a
               print_option_choices(pfile, option);
#if defined (DEBUG) && !defined (WIN32)
               fprintf(pfile, "ChoicesReadable=\"(%zu)Ignore %s|(%zu)Break %s|(%zu)Force %s|(%zu)Lead %s|(%zu)Trail %s|",
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable);
#else // DEBUG
               //                                0         1        2        4       8
               fprintf(pfile, "ChoicesReadable=\"Ignore %s|Break %s|Force %s|Lead %s|Trail %s|",
                       optionNameReadable, optionNameReadable, optionNameReadable,
                       optionNameReadable, optionNameReadable);
#endif // DEBUG
               //                                16      5             6             9              10
#if defined (DEBUG) && !defined (WIN32)
               fprintf(pfile, "(%zu)Join %s|(%zu)Lead Break %s|(%zu)Lead Force %s|(%zu)Trail Break %s|(%zu)Trail Force %s\"\n",
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable,
                       optionNumber, optionNameReadable);
#else // DEBUG
               fprintf(pfile, "Join %s|Lead Break %s|Lead Force %s|Trail Break %s|Trail Force %s\"\n",
                       optionNameReadable, optionNameReadable, optionNameReadable,
                       optionNameReadable, optionNameReadable);
#endif // DEBUG
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;

            case uncrustify::OT_STRING:
            {
               fprintf(pfile, "CallName=%s\\s*=\\s*\n", option->name());
               fprintf(pfile, "EditorType=string\n");
               fprintf(pfile, "ValueDefault=%s\n", option->str().c_str());
               break;
            }

            default:
               fprintf(stderr, "FATAL: Illegal option type %d for '%s'\n",
                       static_cast<int>(option->type()), option->name());
               log_flush(true);
               exit(EX_SOFTWARE);
               break;
            } // switch
         }
#if defined (DEBUG) && !defined (WIN32)
         optionNumber++;
#endif // DEBUG
         delete[] optionNameReadable;
      } // for (auto *const option : p_grp->options)
   }
} // print_universal_indent_cfg