Skip to content

Commit

Permalink
Add -t output text option. Change default to binary.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahbeckford committed Dec 5, 2024
1 parent 33f30cd commit 9066c67
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.crlf.cppo.bin text eol=crlf
*.crlf.ref.bin text eol=crlf
*.lf.cppo.bin text eol=lf
*.lf.ref.bin text eol=lf

*.sh text eol=lf
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Pending

- [+ui] Added the `-t` output text option so that Windows does
not add CRLF endings. Default is now binary output.

## v1.8.0 (2024-12-03)
- [+ui] A scope, delimited by `#scope ... #endscope`,
limits the effect of `#define`, `#def ... #enddef`, and `#undef`.
Expand Down
12 changes: 11 additions & 1 deletion src/cppo_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ let main () =
let preserve_quotations = ref false in
let show_exact_locations = ref false in
let show_no_locations = ref false in
let output_text = ref false in
let options = [
"-D", Arg.String (fun s -> header := ("#define " ^ s ^ "\n") :: !header),
"DEF
Expand Down Expand Up @@ -133,6 +134,11 @@ let main () =
Do not output any line directive other than those found in the
input (overrides -s).";

"-t", Arg.Set output_text,
"
Write output with LF and CRLF normalization on Unix and
Windows, respectively.";

"-version", Arg.Unit (fun () ->
print_endline Cppo_version.cppo_version;
exit 0),
Expand Down Expand Up @@ -212,10 +218,14 @@ Options:" Sys.argv.(0) in
in
match !out_file with
None ->
set_binary_mode_out stdout (not !output_text);
print_string (Buffer.contents buf);
flush stdout
| Some file ->
let oc = open_out file in
let oc =
if !output_text then open_out file
else open_out_bin file
in
output_string oc (Buffer.contents buf);
close_out oc

Expand Down
26 changes: 26 additions & 0 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@
(deps (:< def.cppo))
(action (with-stdout-to %{targets} (run %{bin:cppo} %{<}))))

(rule
(target text.crlf.out.bin)
(deps (:< text.crlf.cppo.bin))
(action (with-stdout-to %{target} (run %{bin:cppo} %{<}))))

(rule
(target text.lf.out.bin)
(deps (:< text.lf.cppo.bin))
(action (with-stdout-to %{target} (run %{bin:cppo} %{<}))))

(rule
(target text_in_text_mode.lf.out.bin)
(deps (:< text_in_text_mode.lf.cppo.bin))
(action (with-stdout-to %{target} (run %{bin:cppo} -t %{<}))))

(rule (alias runtest) (package cppo)
(action (diff ext.ref ext.out)))

Expand Down Expand Up @@ -153,6 +168,17 @@
(rule (alias runtest) (package cppo)
(action (diff def.ref def.out)))

(rule (alias runtest) (package cppo)
(action (diff text.crlf.ref.bin text.crlf.out.bin)))

(rule (alias runtest) (package cppo)
(action (diff text.lf.ref.bin text.lf.out.bin)))

(rule (alias runtest) (package cppo) (enabled_if (= %{os_type} Win32))
(action (diff text_in_text_mode.crlf.ref.bin text_in_text_mode.lf.out.bin)))
(rule (alias runtest) (package cppo) (enabled_if (<> %{os_type} Win32))
(action (diff text_in_text_mode.lf.ref.bin text_in_text_mode.lf.out.bin)))

;; ---------------------------------------------------------------------------
;; Negative tests.

Expand Down
2 changes: 2 additions & 0 deletions test/text.crlf.cppo.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1: CRLF
2: CRLF
3 changes: 3 additions & 0 deletions test/text.crlf.ref.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1 "text.crlf.cppo.bin"
1: CRLF
2: CRLF
2 changes: 2 additions & 0 deletions test/text.lf.cppo.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1: LF
2: LF
3 changes: 3 additions & 0 deletions test/text.lf.ref.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1 "text.lf.cppo.bin"
1: LF
2: LF
3 changes: 3 additions & 0 deletions test/text_in_text_mode.crlf.ref.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1 "text_in_text_mode.lf.cppo.bin"
1: LF
2: LF
2 changes: 2 additions & 0 deletions test/text_in_text_mode.lf.cppo.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1: LF
2: LF
3 changes: 3 additions & 0 deletions test/text_in_text_mode.lf.ref.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1 "text_in_text_mode.lf.cppo.bin"
1: LF
2: LF

0 comments on commit 9066c67

Please sign in to comment.