Skip to content

Commit 64005f3

Browse files
committed
--fmt: remove need to write temp file
also fix column misalignments for formats 1 through 5
1 parent ba6c1aa commit 64005f3

File tree

2 files changed

+176
-136
lines changed

2 files changed

+176
-136
lines changed

Unix/cloc

Lines changed: 88 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,11 +1006,6 @@ $opt_fmt = 0 unless defined $opt_fmt;
10061006
if ($opt_fmt) {
10071007
$opt_by_file = 1;
10081008
$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-
}
10141009
}
10151010
my $CLOC_XSL = "cloc.xsl"; # created with --xsl
10161011
$CLOC_XSL = "cloc-diff.xsl" if $opt_diff;
@@ -2208,16 +2203,21 @@ if ($opt_by_file_by_lang) {
22082203
}
22092204
# 1}}}
22102205
}
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+
}
22112212
if ($opt_report_file) {
22122213
write_file($opt_report_file, {}, @Lines_Out);
2214+
} else {
22132215
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";
22172220
}
2218-
} else {
2219-
print "\n" unless $opt_quiet;
2220-
print join("\n", @Lines_Out), "\n";
22212221
}
22222222
if ($opt_count_diff) {
22232223
++$opt_count_diff;
@@ -6713,60 +6713,54 @@ sub write_file { # {{{1
67136713
$rh_options , # in
67146714
@lines , # in
67156715
) = @_;
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.
67166719

67176720
my $local_formatting = 0;
67186721
foreach my $opt (sort keys %{$rh_options}) {
67196722
# print "write_file option $opt = $rh_options->{$opt}\n";
67206723
$local_formatting = 1;
67216724
}
6725+
my $write_to_file = ref($file) eq "" ? 1 : 0;
6726+
67226727
#print "write_file 1 [$file]\n";
67236728
# 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";
67306740
}
6731-
#print "write_file 3 [$file]\n";
6732-
$file = $preglob_filename unless $file;
6733-
#print "write_file 4 [$file]\n";
67346741

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;
67476744
} else {
6748-
$OUT = open_file('>', $file, 1);
6745+
print "-> write_file() -- writing to string variable\n" if $opt_v > 2;
67496746
}
67506747

6748+
my @prt_lines = ();
6749+
67516750
my $n_col = undef;
67526751
if ($local_formatting) {
67536752
$n_col = scalar @{$rh_options->{'columns'}};
67546753
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";
67576756
} elsif ($opt_yaml) {
6758-
print $OUT "---\n";
6757+
push @prt_lines, "---\n";
67596758
} 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";
67626761
}
67636762
}
67646763

6765-
if (!defined $OUT) {
6766-
warn "Unable to write to $file\n";
6767-
print "<- write_file\n" if $opt_v > 2;
6768-
return;
6769-
}
67706764
chomp(@lines);
67716765

67726766
if ($local_formatting) {
@@ -6779,11 +6773,11 @@ sub write_file { # {{{1
67796773
@entries = ( $L );
67806774
}
67816775
if ($opt_xml) {
6782-
print $OUT " <$rh_options->{'file_type'} ";
6776+
push @prt_lines, " <$rh_options->{'file_type'} ";
67836777
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]);
67856779
}
6786-
print $OUT "/>\n";
6780+
push @prt_lines, "/>\n";
67876781
} elsif ($opt_yaml or $opt_json) {
67886782
my @pairs = ();
67896783
for (my $i = 0; $i < $n_col; $i++) {
@@ -6795,27 +6789,46 @@ sub write_file { # {{{1
67956789
$pairs[0] =~ s/\\x//g;
67966790
push @json_lines, join(", ", @pairs );
67976791
} else {
6798-
print $OUT "- {", join(", ", @pairs), "}\n";
6792+
push @prt_lines, "- {", join(", ", @pairs) . "}\n";
67996793
}
68006794
} elsif ($opt_csv) {
6801-
print $OUT join(",", @entries), "\n";
6795+
push @prt_lines, join(",", @entries) . "\n";
68026796
} elsif ($opt_md) {
6803-
print $OUT join("|", @entries), "\n";
6797+
push @prt_lines, join("|", @entries) . "\n";
68046798
}
68056799
}
68066800
if ($opt_json) {
6807-
print $OUT "[{", join("},\n {", @json_lines), "}]\n";
6801+
push @prt_lines, "[{" . join("},\n {", @json_lines) . "}]\n";
68086802
}
68096803
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";
68116805
}
68126806
} else {
6813-
print $OUT join("\n", @lines), "\n";
6807+
push @prt_lines, join("\n", @lines) . "\n";
68146808
}
68156809

68166810
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);
68186825
}
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;
68196832
$OUT->close;
68206833

68216834
if (can_read($file)) {
@@ -14453,16 +14466,16 @@ sub glob2regex { # {{{
1445314466
} # }}}
1445414467
sub load_json { # {{{1
1445514468
#
14456-
# Load a cloc-generated JSON file into %contents
14469+
# Load a cloc-generated JSON string into %contents
1445714470
# $contents{filename}{blank|comment|code|language} = value
1445814471
# then print in a variety of formats.
1445914472
#
14460-
my ($file, ) = @_;
14473+
my ($json_string, ) = @_;
14474+
print "-> load_json()\n" if $opt_v > 2;
1446114475

1446214476
my %contents = ();
1446314477
my $heading = undef;
14464-
open IN, $file or die "failed load_json($file)";
14465-
while (<IN>) {
14478+
foreach (split /\n/, $json_string) {
1446614479
if (/^{?"(.*?)"/) {
1446714480
$heading = $1;
1446814481
} else {
@@ -14474,7 +14487,6 @@ sub load_json { # {{{1
1447414487
}
1447514488
}
1447614489
}
14477-
close IN;
1447814490
my $url = $contents{'header'}{'cloc_url'};
1447914491
my $ver = $contents{'header'}{'cloc_version'};
1448014492
my $sec = $contents{'header'}{'elapsed_seconds'};
@@ -14498,8 +14510,10 @@ sub load_json { # {{{1
1449814510
$file_len = $file_len > $flen ? $file_len : $flen;
1449914511
$lang_len = $lang_len > $llen ? $lang_len : $llen;
1450014512
}
14513+
print "<- load_json()\n" if $opt_v > 2;
1450114514
return $file_len, $lang_len, $header, %contents;
14502-
} # 1}}}
14515+
}
14516+
# 1}}}
1450314517
sub print_format_n { # {{{1
1450414518
# by file with
1450514519
# format 1 : Language | files | blank | comment | code
@@ -14508,7 +14522,11 @@ sub print_format_n { # {{{1
1450814522
# format 4 : File | blank | comment | code | total
1450914523
# format 5 : File | Language | blank | comment | code | total
1451014524
my ($format, $file_len, $lang_len, $header, %contents) = @_;
14525+
print "-> print_format_n($format)\n" if $opt_v > 2;
14526+
my @prt_lines = ();
1451114527

14528+
# 8 = characters in "Language"
14529+
$lang_len = max(8, $lang_len);
1451214530
my %str_fmt = (
1451314531
1 => sprintf("%%-%ds %%7s %%7s %%7s %%7s\n", $lang_len),
1451414532
2 => sprintf("%%-%ds %%7s %%7s %%7s %%7s %%7s\n", $lang_len),
@@ -14552,10 +14570,10 @@ sub print_format_n { # {{{1
1455214570
5 => ["File", "Language", "blank", "comment", "code", "Total"],
1455314571
);
1455414572

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";
1455914577
my ($n_files, $n_blank, $n_comment, $n_code, $n_total) = (0, 0, 0, 0, 0);
1456014578
my @out;
1456114579
if ($format < 3) {
@@ -14570,7 +14588,7 @@ sub print_format_n { # {{{1
1457014588
} else {
1457114589
@out = ($lang, $nF, $nB, $nCm, $nCo, $nB + $nCm + $nCo);
1457214590
}
14573-
printf $val_fmt{$format}, @out;
14591+
push @prt_lines, sprintf $val_fmt{$format}, @out;
1457414592
$n_files += $nF;
1457514593
$n_blank += $nB;
1457614594
$n_comment += $nCm;
@@ -14592,14 +14610,14 @@ sub print_format_n { # {{{1
1459214610
} else {
1459314611
@out = ($file, $lang, $nB, $nCm, $nCo, $nB + $nCm + $nCo);
1459414612
}
14595-
printf $val_fmt{$format}, @out;
14613+
push @prt_lines, sprintf $val_fmt{$format}, @out;
1459614614
$n_blank += $nB;
1459714615
$n_comment += $nCm;
1459814616
$n_code += $nCo;
1459914617
$n_total += $nB + $nCm + $nCo;
1460014618
}
1460114619
}
14602-
print "$hyphens{$format}\n";
14620+
push @prt_lines, "$hyphens{$format}\n";
1460314621
if (scalar @file_list > 1) {
1460414622
if ($format == 1) {
1460514623
@out = ( "SUM", $n_files, $n_blank, $n_comment, $n_code );
@@ -14612,9 +14630,11 @@ sub print_format_n { # {{{1
1461214630
} else {
1461314631
@out = ( "SUM", " ", $n_blank, $n_comment, $n_code, $n_total );
1461414632
}
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";
1461714635
}
14636+
return @prt_lines;
14637+
print "<- print_format_n()\n" if $opt_v > 2;
1461814638
} # 1}}}
1461914639
# really_is_pascal, really_is_incpascal, really_is_php from SLOCCount
1462014640
my %php_files = (); # really_is_php()

0 commit comments

Comments
 (0)