Skip to content

Commit 9066c67

Browse files
committed
Add -t output text option. Change default to binary.
1 parent 33f30cd commit 9066c67

11 files changed

+66
-1
lines changed

.gitattributes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.crlf.cppo.bin text eol=crlf
2+
*.crlf.ref.bin text eol=crlf
3+
*.lf.cppo.bin text eol=lf
4+
*.lf.ref.bin text eol=lf
5+
6+
*.sh text eol=lf

Changes.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Pending
2+
3+
- [+ui] Added the `-t` output text option so that Windows does
4+
not add CRLF endings. Default is now binary output.
5+
16
## v1.8.0 (2024-12-03)
27
- [+ui] A scope, delimited by `#scope ... #endscope`,
38
limits the effect of `#define`, `#def ... #enddef`, and `#undef`.

src/cppo_main.ml

+11-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ let main () =
7878
let preserve_quotations = ref false in
7979
let show_exact_locations = ref false in
8080
let show_no_locations = ref false in
81+
let output_text = ref false in
8182
let options = [
8283
"-D", Arg.String (fun s -> header := ("#define " ^ s ^ "\n") :: !header),
8384
"DEF
@@ -133,6 +134,11 @@ let main () =
133134
Do not output any line directive other than those found in the
134135
input (overrides -s).";
135136

137+
"-t", Arg.Set output_text,
138+
"
139+
Write output with LF and CRLF normalization on Unix and
140+
Windows, respectively.";
141+
136142
"-version", Arg.Unit (fun () ->
137143
print_endline Cppo_version.cppo_version;
138144
exit 0),
@@ -212,10 +218,14 @@ Options:" Sys.argv.(0) in
212218
in
213219
match !out_file with
214220
None ->
221+
set_binary_mode_out stdout (not !output_text);
215222
print_string (Buffer.contents buf);
216223
flush stdout
217224
| Some file ->
218-
let oc = open_out file in
225+
let oc =
226+
if !output_text then open_out file
227+
else open_out_bin file
228+
in
219229
output_string oc (Buffer.contents buf);
220230
close_out oc
221231

test/dune

+26
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@
111111
(deps (:< def.cppo))
112112
(action (with-stdout-to %{targets} (run %{bin:cppo} %{<}))))
113113

114+
(rule
115+
(target text.crlf.out.bin)
116+
(deps (:< text.crlf.cppo.bin))
117+
(action (with-stdout-to %{target} (run %{bin:cppo} %{<}))))
118+
119+
(rule
120+
(target text.lf.out.bin)
121+
(deps (:< text.lf.cppo.bin))
122+
(action (with-stdout-to %{target} (run %{bin:cppo} %{<}))))
123+
124+
(rule
125+
(target text_in_text_mode.lf.out.bin)
126+
(deps (:< text_in_text_mode.lf.cppo.bin))
127+
(action (with-stdout-to %{target} (run %{bin:cppo} -t %{<}))))
128+
114129
(rule (alias runtest) (package cppo)
115130
(action (diff ext.ref ext.out)))
116131

@@ -153,6 +168,17 @@
153168
(rule (alias runtest) (package cppo)
154169
(action (diff def.ref def.out)))
155170

171+
(rule (alias runtest) (package cppo)
172+
(action (diff text.crlf.ref.bin text.crlf.out.bin)))
173+
174+
(rule (alias runtest) (package cppo)
175+
(action (diff text.lf.ref.bin text.lf.out.bin)))
176+
177+
(rule (alias runtest) (package cppo) (enabled_if (= %{os_type} Win32))
178+
(action (diff text_in_text_mode.crlf.ref.bin text_in_text_mode.lf.out.bin)))
179+
(rule (alias runtest) (package cppo) (enabled_if (<> %{os_type} Win32))
180+
(action (diff text_in_text_mode.lf.ref.bin text_in_text_mode.lf.out.bin)))
181+
156182
;; ---------------------------------------------------------------------------
157183
;; Negative tests.
158184

test/text.crlf.cppo.bin

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1: CRLF
2+
2: CRLF

test/text.crlf.ref.bin

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1 "text.crlf.cppo.bin"
2+
1: CRLF
3+
2: CRLF

test/text.lf.cppo.bin

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1: LF
2+
2: LF

test/text.lf.ref.bin

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1 "text.lf.cppo.bin"
2+
1: LF
3+
2: LF

test/text_in_text_mode.crlf.ref.bin

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1 "text_in_text_mode.lf.cppo.bin"
2+
1: LF
3+
2: LF

test/text_in_text_mode.lf.cppo.bin

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1: LF
2+
2: LF

test/text_in_text_mode.lf.ref.bin

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1 "text_in_text_mode.lf.cppo.bin"
2+
1: LF
3+
2: LF

0 commit comments

Comments
 (0)