forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuncrustify.cpp
More file actions
2036 lines (1778 loc) · 52 KB
/
uncrustify.cpp
File metadata and controls
2036 lines (1778 loc) · 52 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file uncrustify.cpp
* This file takes an input C/C++/D/Java file and reformats it.
*
* @author Ben Gardner
* @license GPL v2+
*/
#define DEFINE_PCF_NAMES
#define DEFINE_CHAR_TABLE
#include "uncrustify_version.h"
#include "uncrustify_types.h"
#include "char_table.h"
#include "chunk_list.h"
#include "prototypes.h"
#include "token_names.h"
#include "args.h"
#include "logger.h"
#include "log_levels.h"
#include "md5.h"
#include "backup.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "unc_ctype.h"
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h> /* strcasecmp() */
#endif
#include <vector>
#include <deque>
/* Global data */
struct cp_data cpd;
static int language_flags_from_name(const char *tag);
static int language_flags_from_filename(const char *filename);
static const char *language_name_from_flags(int lang);
static bool read_stdin(file_mem& fm);
static void uncrustify_start(const deque<int>& data);
static void uncrustify_end();
static void uncrustify_file(const file_mem& fm, FILE *pfout,
const char *parsed_file);
static void do_source_file(const char *filename_in,
const char *filename_out,
const char *parsed_file,
bool no_backup, bool keep_mtime);
static void process_source_list(const char *source_list, const char *prefix,
const char *suffix, bool no_backup, bool keep_mtime);
static int load_header_files();
static const char *make_output_filename(char *buf, int buf_size,
const char *filename,
const char *prefix,
const char *suffix);
static int load_mem_file(const char *filename, file_mem& fm);
/**
* Replace the brain-dead and non-portable basename().
* Returns a pointer to the character after the last '/'.
* The returned value always points into path, unless path is NULL.
*
* Input Returns
* NULL => ""
* "/some/path/" => ""
* "/some/path" => "path"
* "afile" => "afile"
*
* @param path The path to look at
* @return Pointer to the character after the last path seperator
*/
const char *path_basename(const char *path)
{
if (path == NULL)
{
return("");
}
const char *last_path = path;
char ch;
while ((ch = *path) != 0)
{
path++;
/* Check both slash types to support windows */
if ((ch == '/') || (ch == '\\'))
{
last_path = path;
}
}
return(last_path);
}
/**
* Returns the length of the directory part of the filename.
*/
int path_dirname_len(const char *filename)
{
if (filename == NULL)
{
return(0);
}
return((int)(path_basename(filename) - filename));
}
static void usage_exit(const char *msg, const char *argv0, int code)
{
if (msg != NULL)
{
fprintf(stderr, "%s\n", msg);
}
if ((code != EXIT_SUCCESS) || (argv0 == NULL))
{
fprintf(stderr, "Try running with -h for usage information\n");
exit(code);
}
fprintf(stdout,
"Usage:\n"
"%s [options] [files ...]\n"
"\n"
"If no input files are specified, the input is read from stdin\n"
"If reading from stdin, you should specify the language using -l\n"
"\n"
"If -F is used or files are specified on the command line, the output filename is\n"
"'prefix/filename' + suffix\n"
"\n"
"When reading from stdin or doing a single file via the '-f' option,\n"
"the output is dumped to stdout, unless redirected with -o FILE.\n"
"\n"
"Errors are always dumped to stderr\n"
"\n"
"The '-f' and '-o' options may not be used with '-F', '--replace' or '--no-backup'.\n"
"The '--prefix' and '--suffix' options may not be used with '--replace' or '--no-backup'.\n"
"\n"
"Basic Options:\n"
" -c CFG : use the config file CFG\n"
" -f FILE : process the single file FILE (output to stdout, use with -o)\n"
" -o FILE : Redirect stdout to FILE\n"
" -F FILE : read files to process from FILE, one filename per line (- is stdin)\n"
" --check : Do not output the new text, instead verify that nothing changes when\n"
" the file(s) are processed.\n"
" The status of every file is printed to stderr.\n"
" The exit code is EXIT_SUCCESS if there were no changes, EXIT_FAILURE otherwise.\n"
" files : files to process (can be combined with -F)\n"
" --suffix SFX : Append SFX to the output filename. The default is '.uncrustify'\n"
" --prefix PFX : Prepend PFX to the output filename path.\n"
" --replace : replace source files (creates a backup)\n"
" --no-backup : replace files, no backup. Useful if files are under source control\n"
#ifdef HAVE_UTIME_H
" --mtime : preserve mtime on replaced files\n"
#endif
" -l : language override: C, CPP, D, CS, JAVA, PAWN, OC, OC+, VALA\n"
" -t : load a file with types (usually not needed)\n"
" -q : quiet mode - no output on stderr (-L will override)\n"
" --frag : code fragment, assume the first line is indented correctly\n"
"\n"
"Config/Help Options:\n"
" -h -? --help --usage : print this message and exit\n"
" --version : print the version and exit\n"
" --show-config : print out option documentation and exit\n"
" --update-config : Output a new config file. Use with -o FILE\n"
" --update-config-with-doc : Output a new config file. Use with -o FILE\n"
" --universalindent : Output a config file for Universal Indent GUI\n"
" --detect : detects the config from a source file. Use with '-f FILE'\n"
" Detection is fairly limited.\n"
"\n"
"Debug Options:\n"
" -p FILE : dump debug info to a file\n"
" -L SEV : Set the log severity (see log_levels.h)\n"
" -s : Show the log severity in the logs\n"
" --decode : decode remaining args (chunk flags) and exit\n"
"\n"
"Usage Examples\n"
"cat foo.d | uncrustify -q -c my.cfg -l d\n"
"uncrustify -c my.cfg -f foo.d\n"
"uncrustify -c my.cfg -f foo.d -L0-2,20-23,51\n"
"uncrustify -c my.cfg -f foo.d -o foo.d\n"
"uncrustify -c my.cfg foo.d\n"
"uncrustify -c my.cfg --replace foo.d\n"
"uncrustify -c my.cfg --no-backup foo.d\n"
"uncrustify -c my.cfg --prefix=out -F files.txt\n"
"\n"
"Note: Use comments containing ' *INDENT-OFF*' and ' *INDENT-ON*' to disable\n"
" processing of parts of the source file.\n"
"\n"
"There are currently %d options and minimal documentation.\n"
"Try UniversalIndentGUI and good luck.\n"
"\n"
,
path_basename(argv0), UO_option_count);
exit(code);
}
static void version_exit(void)
{
printf("uncrustify %s\n", UNCRUSTIFY_VERSION);
exit(0);
}
static void redir_stdout(const char *output_file)
{
/* Reopen stdout */
FILE *my_stdout = stdout;
if (output_file != NULL)
{
my_stdout = freopen(output_file, "wb", stdout);
if (my_stdout == NULL)
{
LOG_FMT(LERR, "Unable to open %s for write: %s (%d)\n",
output_file, strerror(errno), errno);
usage_exit(NULL, NULL, 56);
}
LOG_FMT(LNOTE, "Redirecting output to %s\n", output_file);
}
}
int main(int argc, char *argv[])
{
string cfg_file;
const char *parsed_file = NULL;
const char *source_file = NULL;
const char *output_file = NULL;
const char *source_list = NULL;
log_mask_t mask;
int idx;
const char *p_arg;
/* If ran without options... check keyword sort and show the usage info */
if (argc == 1)
{
keywords_are_sorted();
usage_exit(NULL, argv[0], EXIT_SUCCESS);
}
/* Build options map */
register_options();
Args arg(argc, argv);
if (arg.Present("--version") || arg.Present("-v"))
{
version_exit();
}
if (arg.Present("--help") || arg.Present("-h") ||
arg.Present("--usage") || arg.Present("-?"))
{
usage_exit(NULL, argv[0], EXIT_SUCCESS);
}
if (arg.Present("--show-config"))
{
print_options(stdout);
return EXIT_SUCCESS;
}
cpd.do_check = arg.Present("--check");
#ifdef WIN32
/* tell windoze not to change what I write to stdout */
(void)_setmode(_fileno(stdout), _O_BINARY);
#endif
/* Init logging */
log_init(cpd.do_check ? stdout : stderr);
if (arg.Present("-q"))
{
logmask_from_string("", mask);
log_set_mask(mask);
}
if (((p_arg = arg.Param("-L")) != NULL) ||
((p_arg = arg.Param("--log")) != NULL))
{
logmask_from_string(p_arg, mask);
log_set_mask(mask);
}
cpd.frag = arg.Present("--frag");
if (arg.Present("--decode"))
{
idx = 1;
while ((p_arg = arg.Unused(idx)) != NULL)
{
log_pcf_flags(LSYS, strtoul(p_arg, NULL, 16));
}
return EXIT_SUCCESS;
}
/* Get the config file name */
if (((p_arg = arg.Param("--config")) != NULL) ||
((p_arg = arg.Param("-c")) != NULL))
{
cfg_file = p_arg;
}
/* Try to find a config file at an alternate location */
if (cfg_file.empty())
{
if (!unc_getenv("UNCRUSTIFY_CONFIG", cfg_file))
{
string home;
if (unc_homedir(home))
{
struct stat tmp_stat;
string path;
path = home + "/uncrustify.cfg";
if (stat(path.c_str(), &tmp_stat) == 0)
{
cfg_file = path;
}
else
{
path = home + "/.uncrustify.cfg";
if (stat(path.c_str(), &tmp_stat) == 0)
{
cfg_file = path;
}
}
}
}
}
/* Get the parsed file name */
if (((parsed_file = arg.Param("--parsed")) != NULL) ||
((parsed_file = arg.Param("-p")) != NULL))
{
LOG_FMT(LNOTE, "Will export parsed data to: %s\n", parsed_file);
}
/* Enable log sevs? */
if (arg.Present("-s") || arg.Present("--show"))
{
log_show_sev(true);
}
/* Load the config file */
set_option_defaults();
/* Load type files */
idx = 0;
while ((p_arg = arg.Params("-t", idx)) != NULL)
{
load_keyword_file(p_arg);
}
/* add types */
idx = 0;
while ((p_arg = arg.Params("--type", idx)) != NULL)
{
add_keyword(p_arg, CT_TYPE);
}
/* Load define files */
idx = 0;
while ((p_arg = arg.Params("-d", idx)) != NULL)
{
load_define_file(p_arg);
}
/* add defines */
idx = 0;
while ((p_arg = arg.Params("--define", idx)) != NULL)
{
add_define(p_arg, NULL);
}
/* Check for a language override */
if ((p_arg = arg.Param("-l")) != NULL)
{
cpd.lang_flags = language_flags_from_name(p_arg);
if (cpd.lang_flags == 0)
{
LOG_FMT(LWARN, "Ignoring unknown language: %s\n", p_arg);
}
else
{
cpd.lang_forced = true;
}
}
/* Get the source file name */
if (((source_file = arg.Param("--file")) == NULL) &&
((source_file = arg.Param("-f")) == NULL))
{
// not using a single file, source_file is NULL
}
if (((source_list = arg.Param("--files")) == NULL) &&
((source_list = arg.Param("-F")) == NULL))
{
// not using a file list, source_list is NULL
}
const char *prefix = arg.Param("--prefix");
const char *suffix = arg.Param("--suffix");
bool no_backup = arg.Present("--no-backup");
bool replace = arg.Present("--replace");
bool keep_mtime = arg.Present("--mtime");
bool update_config = arg.Present("--update-config");
bool update_config_wd = arg.Present("--update-config-with-doc");
bool detect = arg.Present("--detect");
/* Grab the output override */
output_file = arg.Param("-o");
LOG_FMT(LDATA, "config_file = %s\n", cfg_file.c_str());
LOG_FMT(LDATA, "output_file = %s\n", (output_file != NULL) ? output_file : "null");
LOG_FMT(LDATA, "source_file = %s\n", (source_file != NULL) ? source_file : "null");
LOG_FMT(LDATA, "source_list = %s\n", (source_list != NULL) ? source_list : "null");
LOG_FMT(LDATA, "prefix = %s\n", (prefix != NULL) ? prefix : "null");
LOG_FMT(LDATA, "suffix = %s\n", (suffix != NULL) ? suffix : "null");
LOG_FMT(LDATA, "replace = %d\n", replace);
LOG_FMT(LDATA, "no_backup = %d\n", no_backup);
LOG_FMT(LDATA, "detect = %d\n", detect);
LOG_FMT(LDATA, "check = %d\n", cpd.do_check);
if (cpd.do_check &&
(output_file || replace || no_backup || keep_mtime || update_config ||
update_config_wd || detect || prefix || suffix))
{
usage_exit("Cannot use --check with output options.", argv[0], 67);
}
if (!cpd.do_check)
{
if (replace || no_backup)
{
if ((prefix != NULL) || (suffix != NULL))
{
usage_exit("Cannot use --replace with --prefix or --suffix", argv[0], 66);
}
if ((source_file != NULL) || (output_file != NULL))
{
usage_exit("Cannot use --replace or --no-backup with -f or -o", argv[0], 66);
}
}
else
{
if ((prefix == NULL) && (suffix == NULL))
{
suffix = ".uncrustify";
}
}
}
/* Try to load the config file, if available.
* It is optional for "--universalindent" and "--detect", but required for
* everything else.
*/
if (!cfg_file.empty())
{
cpd.filename = cfg_file.c_str();
if (load_option_file(cpd.filename) < 0)
{
usage_exit("Unable to load the config file", argv[0], 56);
}
}
if (arg.Present("--universalindent"))
{
FILE *pfile = stdout;
if (output_file != NULL)
{
pfile = fopen(output_file, "w");
if (pfile == NULL)
{
fprintf(stderr, "Unable to open %s for write: %s (%d)\n",
output_file, strerror(errno), errno);
return EXIT_FAILURE;
}
}
print_universal_indent_cfg(pfile);
fclose(pfile);
return EXIT_SUCCESS;
}
if (detect)
{
file_mem fm;
if ((source_file == NULL) || (source_list != NULL))
{
fprintf(stderr, "The --detect option requires a single input file\n");
return EXIT_FAILURE;
}
/* Do some simple language detection based on the filename extension */
if (!cpd.lang_forced || (cpd.lang_flags == 0))
{
cpd.lang_flags = language_flags_from_filename(source_file);
}
/* Try to read in the source file */
if (load_mem_file(source_file, fm) < 0)
{
LOG_FMT(LERR, "Failed to load (%s)\n", source_file);
cpd.error_count++;
return EXIT_FAILURE;
}
uncrustify_start(fm.data);
detect_options();
uncrustify_end();
redir_stdout(output_file);
save_option_file(stdout, update_config_wd);
return EXIT_SUCCESS;
}
if (update_config || update_config_wd)
{
/* TODO: complain if file-processing related options are present */
redir_stdout(output_file);
save_option_file(stdout, update_config_wd);
return EXIT_SUCCESS;
}
/* Everything beyond this point requires a config file, so complain and
* bail if we don't have one.
*/
if (cfg_file.empty())
{
usage_exit("Specify the config file with '-c file' or set UNCRUSTIFY_CONFIG",
argv[0], 58);
}
/*
* Done parsing args
*/
/* Check for unused args (ignore them) */
idx = 1;
p_arg = arg.Unused(idx);
/* Check args - for multifile options */
if ((source_list != NULL) || (p_arg != NULL))
{
if (source_file != NULL)
{
usage_exit("Cannot specify both the single file option and a multi-file option.",
argv[0], 67);
}
if (output_file != NULL)
{
usage_exit("Cannot specify -o with a multi-file option.",
argv[0], 68);
}
}
/* This relies on cpd.filename being the config file name */
load_header_files();
if (cpd.do_check)
{
cpd.bout = new deque<UINT8>();
}
if ((source_file == NULL) && (source_list == NULL) && (p_arg == NULL))
{
/* no input specified, so use stdin */
if (cpd.lang_flags == 0)
{
cpd.lang_flags = LANG_C;
}
if (!cpd.do_check)
{
redir_stdout(output_file);
}
file_mem fm;
if (!read_stdin(fm))
{
LOG_FMT(LERR, "Failed to read stdin\n");
return(100);
}
cpd.filename = "stdin";
/* Done reading from stdin */
LOG_FMT(LSYS, "Parsing: %d bytes (%d chars) from stdin as language %s\n",
(int)fm.raw.size(), (int)fm.data.size(),
language_name_from_flags(cpd.lang_flags));
uncrustify_file(fm, stdout, parsed_file);
}
else if (source_file != NULL)
{
/* Doing a single file */
do_source_file(source_file, output_file, parsed_file, no_backup, keep_mtime);
}
else
{
/* Doing multiple files */
if (prefix != NULL)
{
LOG_FMT(LSYS, "Output prefix: %s/\n", prefix);
}
if (suffix != NULL)
{
LOG_FMT(LSYS, "Output suffix: %s\n", suffix);
}
/* Do the files on the command line first */
idx = 1;
while ((p_arg = arg.Unused(idx)) != NULL)
{
char outbuf[1024];
do_source_file(p_arg,
make_output_filename(outbuf, sizeof(outbuf), p_arg, prefix, suffix),
NULL, no_backup, keep_mtime);
}
if (source_list != NULL)
{
process_source_list(source_list, prefix, suffix, no_backup, keep_mtime);
}
}
clear_keyword_file();
clear_defines();
if (cpd.do_check)
{
return cpd.check_fail_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
}
return((cpd.error_count != 0) ? EXIT_FAILURE : EXIT_SUCCESS);
}
static void process_source_list(const char *source_list,
const char *prefix, const char *suffix,
bool no_backup, bool keep_mtime)
{
int from_stdin = strcmp(source_list, "-") == 0;
FILE *p_file = from_stdin ? stdin : fopen(source_list, "r");
if (p_file == NULL)
{
LOG_FMT(LERR, "%s: fopen(%s) failed: %s (%d)\n",
__func__, source_list, strerror(errno), errno);
cpd.error_count++;
return;
}
char linebuf[256];
char *fname;
int line = 0;
int len;
while (fgets(linebuf, sizeof(linebuf), p_file) != NULL)
{
line++;
fname = linebuf;
len = strlen(fname);
while ((len > 0) && unc_isspace(*fname))
{
fname++;
len--;
}
while ((len > 0) && unc_isspace(fname[len - 1]))
{
len--;
}
fname[len] = 0;
while (len-- > 0)
{
if (fname[len] == '\\')
{
fname[len] = '/';
}
}
LOG_FMT(LFILELIST, "%3d] %s\n", line, fname);
if (fname[0] != '#')
{
char outbuf[1024];
do_source_file(fname,
make_output_filename(outbuf, sizeof(outbuf), fname, prefix, suffix),
NULL, no_backup, keep_mtime);
}
}
if (!from_stdin)
{
fclose(p_file);
}
}
static bool read_stdin(file_mem& fm)
{
deque<UINT8> dq;
char buf[4096];
int len;
int idx;
fm.raw.clear();
fm.data.clear();
fm.enc = ENC_ASCII;
while (!feof(stdin))
{
len = fread(buf, 1, sizeof(buf), stdin);
for (idx = 0; idx < len; idx++)
{
dq.push_back(buf[idx]);
}
}
/* Copy the raw data from the deque to the vector */
fm.raw.insert(fm.raw.end(), dq.begin(), dq.end());
return(decode_unicode(fm.raw, fm.data, fm.enc, fm.bom));
}
static void make_folders(const string& filename)
{
int idx;
int last_idx = 0;
char outname[4096];
snprintf(outname, sizeof(outname), "%s", filename.c_str());
for (idx = 0; outname[idx] != 0; idx++)
{
if ((outname[idx] == '/') || (outname[idx] == '\\'))
{
outname[idx] = PATH_SEP;
}
if ((idx > last_idx) && (outname[idx] == PATH_SEP))
{
outname[idx] = 0;
if ((strcmp(&outname[last_idx], ".") != 0) &&
(strcmp(&outname[last_idx], "..") != 0))
{
//fprintf(stderr, "%s: %s\n", __func__, outname);
mkdir(outname, 0750);
}
outname[idx] = PATH_SEP;
}
if (outname[idx] == PATH_SEP)
{
last_idx = idx + 1;
}
}
}
/**
* Loads a file into memory
*/
static int load_mem_file(const char *filename, file_mem& fm)
{
int retval = -1;
struct stat my_stat;
FILE *p_file;
fm.raw.clear();
fm.data.clear();
fm.enc = ENC_ASCII;
/* Grab the stat info for the file */
if (stat(filename, &my_stat) < 0)
{
return(-1);
}
#ifdef HAVE_UTIME_H
/* Save off mtime */
fm.utb.modtime = my_stat.st_mtime;
#endif
/* Try to read in the file */
p_file = fopen(filename, "rb");
if (p_file == NULL)
{
return(-1);
}
fm.raw.resize(my_stat.st_size);
if (my_stat.st_size == 0)
{
/* Empty file */
retval = 0;
fm.bom = false;
fm.enc = ENC_ASCII;
fm.data.clear();
}
else
{
/* read the raw data */
if (fread(&fm.raw[0], fm.raw.size(), 1, p_file) != 1)
{
LOG_FMT(LERR, "%s: fread(%s) failed: %s (%d)\n",
__func__, filename, strerror(errno), errno);
cpd.error_count++;
}
else if (!decode_unicode(fm.raw, fm.data, fm.enc, fm.bom))
{
LOG_FMT(LERR, "%s: failed to decode the file '%s'\n", __func__, filename);
}
else
{
LOG_FMT(LNOTE, "%s: '%s' encoding looks like %s (%d)\n", __func__, filename,
fm.enc == ENC_ASCII ? "ASCII" :
fm.enc == ENC_BYTE ? "BYTES" :
fm.enc == ENC_UTF16_LE ? "UTF-16-LE" :
fm.enc == ENC_UTF16_BE ? "UTF-16-BE" : "Error",
fm.enc);
retval = 0;
}
}
fclose(p_file);
return(retval);
}
/**
* Try to load the file from the config folder first and then by name
*/
static int load_mem_file_config(const char *filename, file_mem& fm)
{
int retval;
char buf[1024];
snprintf(buf, sizeof(buf), "%.*s%s",
path_dirname_len(cpd.filename), cpd.filename, filename);
retval = load_mem_file(buf, fm);
if (retval < 0)
{
retval = load_mem_file(filename, fm);
if (retval < 0)
{
LOG_FMT(LERR, "Failed to load (%s) or (%s)\n", buf, filename);
cpd.error_count++;
}
}
return(retval);
}
static int load_header_files()
{
int retval = 0;
if ((cpd.settings[UO_cmt_insert_file_header].str != NULL) &&
(cpd.settings[UO_cmt_insert_file_header].str[0] != 0))
{
retval |= load_mem_file_config(cpd.settings[UO_cmt_insert_file_header].str,
cpd.file_hdr);
}
if ((cpd.settings[UO_cmt_insert_file_footer].str != NULL) &&
(cpd.settings[UO_cmt_insert_file_footer].str[0] != 0))
{
retval |= load_mem_file_config(cpd.settings[UO_cmt_insert_file_footer].str,
cpd.file_ftr);
}
if ((cpd.settings[UO_cmt_insert_func_header].str != NULL) &&
(cpd.settings[UO_cmt_insert_func_header].str[0] != 0))
{
retval |= load_mem_file_config(cpd.settings[UO_cmt_insert_func_header].str,
cpd.func_hdr);
}
if ((cpd.settings[UO_cmt_insert_class_header].str != NULL) &&
(cpd.settings[UO_cmt_insert_class_header].str[0] != 0))
{
retval |= load_mem_file_config(cpd.settings[UO_cmt_insert_class_header].str,
cpd.class_hdr);
}
if ((cpd.settings[UO_cmt_insert_oc_msg_header].str != NULL) &&
(cpd.settings[UO_cmt_insert_oc_msg_header].str[0] != 0))
{
retval |= load_mem_file_config(cpd.settings[UO_cmt_insert_oc_msg_header].str,
cpd.oc_msg_hdr);
}
return(retval);
}
static const char *make_output_filename(char *buf, int buf_size,
const char *filename,
const char *prefix,
const char *suffix)
{
int len = 0;
if (prefix != NULL)
{
len = snprintf(buf, buf_size, "%s/", prefix);
}
snprintf(&buf[len], buf_size - len, "%s%s", filename,
(suffix != NULL) ? suffix : "");
return(buf);
}
/**
* Reinvent the wheel with a file comparision function...
*/
static bool file_content_matches(const string& filename1, const string& filename2)
{
struct stat st1, st2;
int fd1, fd2;
UINT8 buf1[1024], buf2[1024];
int len1 = 0, len2 = 0;
int minlen;
/* Check the sizes first */
if ((stat(filename1.c_str(), &st1) != 0) ||
(stat(filename2.c_str(), &st2) != 0) ||
(st1.st_size != st2.st_size))
{
return(false);
}
if ((fd1 = open(filename1.c_str(), O_RDONLY)) < 0)
{
return(false);
}
if ((fd2 = open(filename2.c_str(), O_RDONLY)) < 0)
{
close(fd1);
return(false);
}
while ((len1 >= 0) && (len2 >= 0))
{
if (len1 == 0)
{
len1 = read(fd1, buf1, sizeof(buf1));
}
if (len2 == 0)
{
len2 = read(fd2, buf2, sizeof(buf2));
}
if ((len1 <= 0) || (len2 <= 0))
{
break;
}
minlen = (len1 < len2) ? len1 : len2;
if (memcmp(buf1, buf2, minlen) != 0)
{
break;
}
len1 -= minlen;
len2 -= minlen;
}
close(fd1);
close(fd2);
return((len1 == 0) && (len2 == 0));
}
const char *fix_filename(const char *filename)
{
char *tmp_file;
/* Create 'outfile.uncrustify' */
tmp_file = new char[strlen(filename) + 16 + 1]; /* + 1 for '\0' */
if (tmp_file != NULL)
{
sprintf(tmp_file, "%s.uncrustify", filename);
}
return(tmp_file);
}