Debian Codegraph @

src/endline.c

raw symbols SCIP
1 /* Conversion of files between different charsets and surfaces.
2 Copyright © 1990-2018 Free Software Foundation, Inc.
3 Contributed by François Pinard <[email protected]>, 1988.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 3 of the
8 License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the Recode Library; see the file `COPYING.LIB'.
17 If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "common.h"
22 #include "decsteps.h"
23
24 #define CR 13 /* carriage return */
25 #define LF 10 /* line feed */
26 #define OLD_EOF 26 /* oldish end of file */
27
28 static bool
29 transform_data_cr (RECODE_SUBTASK subtask)
30 {
31 bool strict = subtask->step->fallback_routine != recode_reversibility;
32 int character;
33
34 while (character = recode_get_byte (subtask), character != EOF)
35 switch (character)
36 {
37 case '\n':
38 recode_put_byte (CR, subtask);
39 break;
40
41 case CR:
42 if (!strict)
43 {
44 recode_put_byte ('\n', subtask);
45 break;
46 }
47 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
48 FALLTHROUGH;
49
50 default:
51 recode_put_byte (character, subtask);
52 }
53
54 SUBTASK_RETURN (subtask);
55 }
56
57 static bool
58 transform_cr_data (RECODE_SUBTASK subtask)
59 {
60 bool strict = subtask->step->fallback_routine != recode_reversibility;
61 int character;
62
63 while (character = recode_get_byte (subtask), character != EOF)
64 switch (character)
65 {
66 case CR:
67 recode_put_byte ('\n', subtask);
68 break;
69
70 case '\n':
71 if (!strict)
72 {
73 recode_put_byte (CR, subtask);
74 break;
75 }
76 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
77 FALLTHROUGH;
78
79 default:
80 recode_put_byte (character, subtask);
81 }
82
83 SUBTASK_RETURN (subtask);
84 }
85
86 static bool
87 transform_data_crlf (RECODE_SUBTASK subtask)
88 {
89 int character = recode_get_byte (subtask);
90
91 while (character != EOF)
92 switch (character)
93 {
94 case '\n':
95 recode_put_byte (CR, subtask);
96 recode_put_byte (LF, subtask);
97 character = recode_get_byte (subtask);
98 break;
99
100 case CR:
101 character = recode_get_byte (subtask);
102 if (character == LF)
103 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
104 recode_put_byte (CR, subtask);
105 break;
106
107 case OLD_EOF:
108 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
109 FALLTHROUGH;
110
111 default:
112 recode_put_byte (character, subtask);
113 character = recode_get_byte (subtask);
114 }
115
116 SUBTASK_RETURN (subtask);
117 }
118
119 static bool
120 transform_crlf_data (RECODE_SUBTASK subtask)
121 {
122 int character = recode_get_byte (subtask);
123
124 while (character != EOF)
125 switch (character)
126 {
127 case OLD_EOF:
128 RETURN_IF_NOGO (RECODE_NOT_CANONICAL, subtask);
129 SUBTASK_RETURN (subtask);
130
131 case CR:
132 character = recode_get_byte (subtask);
133 if (character == LF)
134 {
135 recode_put_byte ('\n', subtask);
136 character = recode_get_byte (subtask);
137 }
138 else
139 recode_put_byte (CR, subtask);
140 break;
141
142 case LF:
143 RETURN_IF_NOGO (RECODE_AMBIGUOUS_OUTPUT, subtask);
144 FALLTHROUGH;
145
146 default:
147 recode_put_byte (character, subtask);
148 character = recode_get_byte (subtask);
149 }
150
151 SUBTASK_RETURN (subtask);
152 }
153
154 bool
155 module_endline (RECODE_OUTER outer)
156 {
157 return
158 recode_declare_single (outer, "data", "CR",
159 outer->quality_byte_to_byte,
160 NULL, transform_data_cr)
161 && recode_declare_single (outer, "CR", "data",
162 outer->quality_byte_to_byte,
163 NULL, transform_cr_data)
164 && recode_declare_single (outer, "data", "CR-LF",
165 outer->quality_byte_to_variable,
166 NULL, transform_data_crlf)
167 && recode_declare_single (outer, "CR-LF", "data",
168 outer->quality_variable_to_byte,
169 NULL, transform_crlf_data)
170
171 && recode_declare_alias (outer, "cl", "CR-LF");
172 }
173
174 void
175 delmodule_endline (_GL_UNUSED RECODE_OUTER outer)
176 {
177 }