@@ -1006,11 +1006,6 @@ $opt_fmt = 0 unless defined $opt_fmt;
1006
1006
if ($opt_fmt) {
1007
1007
$opt_by_file = 1;
1008
1008
$opt_json = 1;
1009
- if (!defined $opt_report_file) {
1010
- my $fh;
1011
- ($fh, $opt_report_file) = tempfile(UNLINK => 0, DIR => ".", SUFFIX => ".json" );
1012
- $fh->close; # will be opened later by the JSON writer
1013
- }
1014
1009
}
1015
1010
my $CLOC_XSL = "cloc.xsl"; # created with --xsl
1016
1011
$CLOC_XSL = "cloc-diff.xsl" if $opt_diff;
@@ -2208,16 +2203,21 @@ if ($opt_by_file_by_lang) {
2208
2203
}
2209
2204
# 1}}}
2210
2205
}
2206
+ if ($opt_fmt) {
2207
+ my $json_string = "";
2208
+ write_file(\$json_string, {}, @Lines_Out);
2209
+ my ($file_len, $lang_len, $header, %contents) = load_json($json_string);
2210
+ @Lines_Out = print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents);
2211
+ }
2211
2212
if ($opt_report_file) {
2212
2213
write_file($opt_report_file, {}, @Lines_Out);
2214
+ } else {
2213
2215
if ($opt_fmt) {
2214
- my ($file_len, $lang_len, $header, %contents) = load_json($opt_report_file);
2215
- unlink $opt_report_file unless $opt_fmt < 0;
2216
- print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents);
2216
+ print "@Lines_Out";
2217
+ } else {
2218
+ print "\n" unless $opt_quiet;
2219
+ print join("\n", @Lines_Out), "\n";
2217
2220
}
2218
- } else {
2219
- print "\n" unless $opt_quiet;
2220
- print join("\n", @Lines_Out), "\n";
2221
2221
}
2222
2222
if ($opt_count_diff) {
2223
2223
++$opt_count_diff;
@@ -6713,60 +6713,54 @@ sub write_file { # {{{1
6713
6713
$rh_options , # in
6714
6714
@lines , # in
6715
6715
) = @_;
6716
+ # If $file is a conventional scalar, it is the name of the file to write to.
6717
+ # if $file is a reference to a scalar, rather than writing @lines to a file,
6718
+ # write @lines to this scalar as a single string.
6716
6719
6717
6720
my $local_formatting = 0;
6718
6721
foreach my $opt (sort keys %{$rh_options}) {
6719
6722
# print "write_file option $opt = $rh_options->{$opt}\n";
6720
6723
$local_formatting = 1;
6721
6724
}
6725
+ my $write_to_file = ref($file) eq "" ? 1 : 0;
6726
+
6722
6727
#print "write_file 1 [$file]\n";
6723
6728
# Do ~ expansion (by Tim LaBerge, fixes bug 2787984)
6724
- my $preglob_filename = $file;
6725
- #print "write_file 2 [$preglob_filename]\n";
6726
- if ($ON_WINDOWS) {
6727
- $file = (windows_glob($file))[0];
6728
- } else {
6729
- $file = File::Glob::bsd_glob($file);
6729
+ if ($write_to_file) {
6730
+ my $preglob_filename = $file;
6731
+ #print "write_file 2 [$preglob_filename]\n";
6732
+ if ($ON_WINDOWS) {
6733
+ $file = (windows_glob($file))[0];
6734
+ } else {
6735
+ $file = File::Glob::bsd_glob($file);
6736
+ }
6737
+ #print "write_file 3 [$file]\n";
6738
+ $file = $preglob_filename unless $file;
6739
+ #print "write_file 4 [$file]\n";
6730
6740
}
6731
- #print "write_file 3 [$file]\n";
6732
- $file = $preglob_filename unless $file;
6733
- #print "write_file 4 [$file]\n";
6734
6741
6735
- print "-> write_file($file)\n" if $opt_v > 2;
6736
-
6737
- # Create the destination directory if it doesn't already exist.
6738
- my $abs_file_path = File::Spec->rel2abs( $file );
6739
- my ($volume, $directories, $filename) = File::Spec->splitpath( $abs_file_path );
6740
- mkpath($volume . $directories, 1, 0777);
6741
-
6742
- my $OUT = undef;
6743
- unlink_file($file);
6744
- if ($opt_file_encoding) {
6745
- # $OUT = IO::File->new($file, ">:$opt_file_encoding"); # doesn't work?
6746
- $OUT = open_file(">:encoding($opt_file_encoding)", $file, 0);
6742
+ if ($write_to_file) {
6743
+ print "-> write_file($file)\n" if $opt_v > 2;
6747
6744
} else {
6748
- $OUT = open_file('>', $file, 1) ;
6745
+ print "-> write_file() -- writing to string variable\n" if $opt_v > 2 ;
6749
6746
}
6750
6747
6748
+ my @prt_lines = ();
6749
+
6751
6750
my $n_col = undef;
6752
6751
if ($local_formatting) {
6753
6752
$n_col = scalar @{$rh_options->{'columns'}};
6754
6753
if ($opt_xml) {
6755
- print $OUT '<?xml version="1.0" encoding="UTF-8"?>', "\n";
6756
- print $OUT "<all_$rh_options->{'file_type'}>\n";
6754
+ push @prt_lines, '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
6755
+ push @prt_lines, "<all_$rh_options->{'file_type'}>\n";
6757
6756
} elsif ($opt_yaml) {
6758
- print $OUT "---\n";
6757
+ push @prt_lines, "---\n";
6759
6758
} elsif ($opt_md) {
6760
- print $OUT join("|", @{$rh_options->{'columns'}}) , "\n";
6761
- print $OUT join("|", map( ":------", 1 .. $n_col)), "\n";
6759
+ push @prt_lines, join("|", @{$rh_options->{'columns'}}) . "\n";
6760
+ push @prt_lines, join("|", map( ":------", 1 .. $n_col)) . "\n";
6762
6761
}
6763
6762
}
6764
6763
6765
- if (!defined $OUT) {
6766
- warn "Unable to write to $file\n";
6767
- print "<- write_file\n" if $opt_v > 2;
6768
- return;
6769
- }
6770
6764
chomp(@lines);
6771
6765
6772
6766
if ($local_formatting) {
@@ -6779,11 +6773,11 @@ sub write_file { # {{{1
6779
6773
@entries = ( $L );
6780
6774
}
6781
6775
if ($opt_xml) {
6782
- print $OUT " <$rh_options->{'file_type'} ";
6776
+ push @prt_lines, " <$rh_options->{'file_type'} ";
6783
6777
for (my $i = 0; $i < $n_col; $i++) {
6784
- printf $OUT "%s=\"%s\" ", $rh_options->{'columns'}[$i], $entries[$i];
6778
+ push @prt_lines, sprintf( "%s=\"%s\" ", $rh_options->{'columns'}[$i], $entries[$i]) ;
6785
6779
}
6786
- print $OUT "/>\n";
6780
+ push @prt_lines, "/>\n";
6787
6781
} elsif ($opt_yaml or $opt_json) {
6788
6782
my @pairs = ();
6789
6783
for (my $i = 0; $i < $n_col; $i++) {
@@ -6795,27 +6789,46 @@ sub write_file { # {{{1
6795
6789
$pairs[0] =~ s/\\x//g;
6796
6790
push @json_lines, join(", ", @pairs );
6797
6791
} else {
6798
- print $OUT "- {", join(", ", @pairs), "}\n";
6792
+ push @prt_lines, "- {", join(", ", @pairs) . "}\n";
6799
6793
}
6800
6794
} elsif ($opt_csv) {
6801
- print $OUT join(",", @entries), "\n";
6795
+ push @prt_lines, join(",", @entries) . "\n";
6802
6796
} elsif ($opt_md) {
6803
- print $OUT join("|", @entries), "\n";
6797
+ push @prt_lines, join("|", @entries) . "\n";
6804
6798
}
6805
6799
}
6806
6800
if ($opt_json) {
6807
- print $OUT "[{", join("},\n {", @json_lines), "}]\n";
6801
+ push @prt_lines, "[{" . join("},\n {", @json_lines) . "}]\n";
6808
6802
}
6809
6803
if (!$opt_json and !$opt_yaml and !$opt_xml and !$opt_csv) {
6810
- print $OUT join("\n", @lines), "\n";
6804
+ push @prt_lines, join("\n", @lines) . "\n";
6811
6805
}
6812
6806
} else {
6813
- print $OUT join("\n", @lines), "\n";
6807
+ push @prt_lines, join("\n", @lines) . "\n";
6814
6808
}
6815
6809
6816
6810
if ($local_formatting and $opt_xml) {
6817
- print $OUT "</all_$rh_options->{'file_type'}>\n";
6811
+ push @prt_lines, "</all_$rh_options->{'file_type'}>\n";
6812
+ }
6813
+
6814
+ # Create the destination directory if it doesn't already exist.
6815
+ my $abs_file_path = File::Spec->rel2abs( $file );
6816
+ my ($volume, $directories, $filename) = File::Spec->splitpath( $abs_file_path );
6817
+ mkpath($volume . $directories, 1, 0777);
6818
+
6819
+ my $OUT = undef;
6820
+ unlink_file($file);
6821
+ if ($opt_file_encoding) {
6822
+ $OUT = open_file(">:encoding($opt_file_encoding)", $file, 0);
6823
+ } else {
6824
+ $OUT = open_file('>', $file, 1);
6818
6825
}
6826
+ if (!defined $OUT) {
6827
+ warn "Unable to write to $file\n";
6828
+ print "<- write_file\n" if $opt_v > 2;
6829
+ return;
6830
+ }
6831
+ print $OUT @prt_lines;
6819
6832
$OUT->close;
6820
6833
6821
6834
if (can_read($file)) {
@@ -14453,16 +14466,16 @@ sub glob2regex { # {{{
14453
14466
} # }}}
14454
14467
sub load_json { # {{{1
14455
14468
#
14456
- # Load a cloc-generated JSON file into %contents
14469
+ # Load a cloc-generated JSON string into %contents
14457
14470
# $contents{filename}{blank|comment|code|language} = value
14458
14471
# then print in a variety of formats.
14459
14472
#
14460
- my ($file, ) = @_;
14473
+ my ($json_string, ) = @_;
14474
+ print "-> load_json()\n" if $opt_v > 2;
14461
14475
14462
14476
my %contents = ();
14463
14477
my $heading = undef;
14464
- open IN, $file or die "failed load_json($file)";
14465
- while (<IN>) {
14478
+ foreach (split /\n/, $json_string) {
14466
14479
if (/^{?"(.*?)"/) {
14467
14480
$heading = $1;
14468
14481
} else {
@@ -14474,7 +14487,6 @@ sub load_json { # {{{1
14474
14487
}
14475
14488
}
14476
14489
}
14477
- close IN;
14478
14490
my $url = $contents{'header'}{'cloc_url'};
14479
14491
my $ver = $contents{'header'}{'cloc_version'};
14480
14492
my $sec = $contents{'header'}{'elapsed_seconds'};
@@ -14498,8 +14510,10 @@ sub load_json { # {{{1
14498
14510
$file_len = $file_len > $flen ? $file_len : $flen;
14499
14511
$lang_len = $lang_len > $llen ? $lang_len : $llen;
14500
14512
}
14513
+ print "<- load_json()\n" if $opt_v > 2;
14501
14514
return $file_len, $lang_len, $header, %contents;
14502
- } # 1}}}
14515
+ }
14516
+ # 1}}}
14503
14517
sub print_format_n { # {{{1
14504
14518
# by file with
14505
14519
# format 1 : Language | files | blank | comment | code
@@ -14508,7 +14522,11 @@ sub print_format_n { # {{{1
14508
14522
# format 4 : File | blank | comment | code | total
14509
14523
# format 5 : File | Language | blank | comment | code | total
14510
14524
my ($format, $file_len, $lang_len, $header, %contents) = @_;
14525
+ print "-> print_format_n($format)\n" if $opt_v > 2;
14526
+ my @prt_lines = ();
14511
14527
14528
+ # 8 = characters in "Language"
14529
+ $lang_len = max(8, $lang_len);
14512
14530
my %str_fmt = (
14513
14531
1 => sprintf("%%-%ds %%7s %%7s %%7s %%7s\n", $lang_len),
14514
14532
2 => sprintf("%%-%ds %%7s %%7s %%7s %%7s %%7s\n", $lang_len),
@@ -14552,10 +14570,10 @@ sub print_format_n { # {{{1
14552
14570
5 => ["File", "Language", "blank", "comment", "code", "Total"],
14553
14571
);
14554
14572
14555
- print "$header\n";
14556
- print "$hyphens{$format}\n";
14557
- printf $str_fmt{$format}, @{$col_headings{$format}};
14558
- print "$hyphens{$format}\n";
14573
+ push @prt_lines, "$header\n";
14574
+ push @prt_lines, "$hyphens{$format}\n";
14575
+ push @prt_lines, sprintf $str_fmt{$format}, @{$col_headings{$format}};
14576
+ push @prt_lines, "$hyphens{$format}\n";
14559
14577
my ($n_files, $n_blank, $n_comment, $n_code, $n_total) = (0, 0, 0, 0, 0);
14560
14578
my @out;
14561
14579
if ($format < 3) {
@@ -14570,7 +14588,7 @@ sub print_format_n { # {{{1
14570
14588
} else {
14571
14589
@out = ($lang, $nF, $nB, $nCm, $nCo, $nB + $nCm + $nCo);
14572
14590
}
14573
- printf $val_fmt{$format}, @out;
14591
+ push @prt_lines, sprintf $val_fmt{$format}, @out;
14574
14592
$n_files += $nF;
14575
14593
$n_blank += $nB;
14576
14594
$n_comment += $nCm;
@@ -14592,14 +14610,14 @@ sub print_format_n { # {{{1
14592
14610
} else {
14593
14611
@out = ($file, $lang, $nB, $nCm, $nCo, $nB + $nCm + $nCo);
14594
14612
}
14595
- printf $val_fmt{$format}, @out;
14613
+ push @prt_lines, sprintf $val_fmt{$format}, @out;
14596
14614
$n_blank += $nB;
14597
14615
$n_comment += $nCm;
14598
14616
$n_code += $nCo;
14599
14617
$n_total += $nB + $nCm + $nCo;
14600
14618
}
14601
14619
}
14602
- print "$hyphens{$format}\n";
14620
+ push @prt_lines, "$hyphens{$format}\n";
14603
14621
if (scalar @file_list > 1) {
14604
14622
if ($format == 1) {
14605
14623
@out = ( "SUM", $n_files, $n_blank, $n_comment, $n_code );
@@ -14612,9 +14630,11 @@ sub print_format_n { # {{{1
14612
14630
} else {
14613
14631
@out = ( "SUM", " ", $n_blank, $n_comment, $n_code, $n_total );
14614
14632
}
14615
- printf $val_fmt{$format}, @out;
14616
- print "$hyphens{$format}\n";
14633
+ push @prt_lines, sprintf $val_fmt{$format}, @out;
14634
+ push @prt_lines, "$hyphens{$format}\n";
14617
14635
}
14636
+ return @prt_lines;
14637
+ print "<- print_format_n()\n" if $opt_v > 2;
14618
14638
} # 1}}}
14619
14639
# really_is_pascal, really_is_incpascal, really_is_php from SLOCCount
14620
14640
my %php_files = (); # really_is_php()
0 commit comments