Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -t output text option #97

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading