2008-08-13 01:30:42 -04:00
|
|
|
#include "transcode_data.h"
|
|
|
|
|
|
|
|
<%
|
|
|
|
map_normalize = {}
|
|
|
|
map_normalize["{00-ff}"] = :func_so
|
2008-08-31 13:35:00 -04:00
|
|
|
|
|
|
|
transcode_generate_node(ActionMap.parse(map_normalize), "universal_newline")
|
|
|
|
|
|
|
|
map_crlf = {}
|
|
|
|
map_crlf["{00-09,0b-ff}"] = :nomap
|
|
|
|
map_crlf["0a"] = "0d0a"
|
|
|
|
|
|
|
|
transcode_generate_node(ActionMap.parse(map_crlf), "crlf_newline")
|
|
|
|
|
|
|
|
map_cr = {}
|
|
|
|
map_cr["{00-09,0b-ff}"] = :nomap
|
|
|
|
map_cr["0a"] = "0d"
|
|
|
|
|
|
|
|
transcode_generate_node(ActionMap.parse(map_cr), "cr_newline")
|
2008-08-13 01:30:42 -04:00
|
|
|
%>
|
|
|
|
|
2008-08-31 13:35:00 -04:00
|
|
|
<%= transcode_generated_code %>
|
2008-08-13 01:30:42 -04:00
|
|
|
|
2008-09-04 10:20:14 -04:00
|
|
|
#define STATE (sp[0])
|
2008-09-03 10:12:06 -04:00
|
|
|
#define NORMAL 0
|
|
|
|
#define JUST_AFTER_CR 1
|
|
|
|
|
2008-09-04 10:20:14 -04:00
|
|
|
/* no way to access this information, yet. */
|
|
|
|
#define NEWLINES_MET (sp[1])
|
|
|
|
#define MET_LF 0x01
|
|
|
|
#define MET_CRLF 0x02
|
|
|
|
#define MET_CR 0x04
|
|
|
|
|
2008-09-03 10:12:06 -04:00
|
|
|
static int
|
|
|
|
universal_newline_init(void *statep)
|
|
|
|
{
|
|
|
|
unsigned char *sp = statep;
|
2008-09-04 10:20:14 -04:00
|
|
|
STATE = NORMAL;
|
|
|
|
NEWLINES_MET = 0;
|
2008-09-03 10:12:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-14 14:35:17 -04:00
|
|
|
static ssize_t
|
2008-09-14 14:06:20 -04:00
|
|
|
fun_so_universal_newline(void *statep, const unsigned char *s, size_t l, unsigned char *o, size_t osize)
|
2008-08-13 01:30:42 -04:00
|
|
|
{
|
2008-09-03 10:12:06 -04:00
|
|
|
unsigned char *sp = statep;
|
2008-08-13 01:30:42 -04:00
|
|
|
int len;
|
|
|
|
if (s[0] == '\n') {
|
2008-09-04 10:20:14 -04:00
|
|
|
if (STATE == NORMAL) {
|
|
|
|
NEWLINES_MET |= MET_LF;
|
2008-08-13 01:30:42 -04:00
|
|
|
}
|
2008-09-03 10:12:06 -04:00
|
|
|
else { /* JUST_AFTER_CR */
|
2008-09-04 10:20:14 -04:00
|
|
|
NEWLINES_MET |= MET_CRLF;
|
2008-08-13 01:30:42 -04:00
|
|
|
}
|
2009-11-22 14:15:55 -05:00
|
|
|
o[0] = '\n';
|
|
|
|
len = 1;
|
2008-09-04 10:20:14 -04:00
|
|
|
STATE = NORMAL;
|
2008-08-13 01:30:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2009-11-22 14:15:55 -05:00
|
|
|
len = 0;
|
|
|
|
if (STATE == JUST_AFTER_CR) {
|
2008-09-04 10:20:14 -04:00
|
|
|
o[0] = '\n';
|
|
|
|
len = 1;
|
2009-11-22 14:15:55 -05:00
|
|
|
NEWLINES_MET |= MET_CR;
|
|
|
|
}
|
|
|
|
if (s[0] == '\r') {
|
2008-09-04 10:20:14 -04:00
|
|
|
STATE = JUST_AFTER_CR;
|
|
|
|
}
|
|
|
|
else {
|
2009-11-22 14:15:55 -05:00
|
|
|
o[len++] = s[0];
|
2008-09-04 10:20:14 -04:00
|
|
|
STATE = NORMAL;
|
|
|
|
}
|
2008-08-13 01:30:42 -04:00
|
|
|
}
|
2008-09-04 10:20:14 -04:00
|
|
|
|
2008-08-13 01:30:42 -04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-09-14 14:35:17 -04:00
|
|
|
static ssize_t
|
2008-09-14 14:06:20 -04:00
|
|
|
universal_newline_finish(void *statep, unsigned char *o, size_t osize)
|
2008-09-05 07:16:28 -04:00
|
|
|
{
|
|
|
|
unsigned char *sp = statep;
|
2009-11-22 14:15:55 -05:00
|
|
|
int len = 0;
|
|
|
|
if (STATE == JUST_AFTER_CR) {
|
|
|
|
o[0] = '\n';
|
|
|
|
len = 1;
|
2008-09-05 07:16:28 -04:00
|
|
|
NEWLINES_MET |= MET_CR;
|
2009-11-22 14:15:55 -05:00
|
|
|
}
|
2008-09-05 07:16:28 -04:00
|
|
|
STATE = NORMAL;
|
2009-11-22 14:15:55 -05:00
|
|
|
return len;
|
2008-09-05 07:16:28 -04:00
|
|
|
}
|
|
|
|
|
2008-08-13 01:30:42 -04:00
|
|
|
static const rb_transcoder
|
|
|
|
rb_universal_newline = {
|
2008-09-09 12:06:54 -04:00
|
|
|
"", "universal_newline", universal_newline,
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_TABLE_INFO,
|
2008-08-13 01:30:42 -04:00
|
|
|
1, /* input_unit_length */
|
|
|
|
1, /* max_input */
|
2009-11-22 14:15:55 -05:00
|
|
|
2, /* max_output */
|
2008-09-08 10:33:17 -04:00
|
|
|
asciicompat_converter, /* asciicompat_type */
|
2008-09-04 10:20:14 -04:00
|
|
|
2, universal_newline_init, universal_newline_init, /* state_size, state_init, state_fini */
|
2008-09-05 07:16:28 -04:00
|
|
|
NULL, NULL, NULL, fun_so_universal_newline,
|
|
|
|
universal_newline_finish
|
2008-08-13 01:30:42 -04:00
|
|
|
};
|
|
|
|
|
2008-08-13 01:48:57 -04:00
|
|
|
static const rb_transcoder
|
|
|
|
rb_crlf_newline = {
|
2008-09-01 12:22:49 -04:00
|
|
|
"", "crlf_newline", crlf_newline,
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_TABLE_INFO,
|
2008-08-13 01:48:57 -04:00
|
|
|
1, /* input_unit_length */
|
|
|
|
1, /* max_input */
|
|
|
|
2, /* max_output */
|
2008-09-08 10:33:17 -04:00
|
|
|
asciicompat_converter, /* asciicompat_type */
|
2008-09-03 10:12:06 -04:00
|
|
|
0, NULL, NULL, /* state_size, state_init, state_fini */
|
2008-08-13 01:48:57 -04:00
|
|
|
NULL, NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const rb_transcoder
|
|
|
|
rb_cr_newline = {
|
2008-09-01 12:22:49 -04:00
|
|
|
"", "cr_newline", cr_newline,
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_TABLE_INFO,
|
2008-08-13 01:48:57 -04:00
|
|
|
1, /* input_unit_length */
|
|
|
|
1, /* max_input */
|
|
|
|
1, /* max_output */
|
2008-09-08 10:33:17 -04:00
|
|
|
asciicompat_converter, /* asciicompat_type */
|
2008-09-03 10:12:06 -04:00
|
|
|
0, NULL, NULL, /* state_size, state_init, state_fini */
|
2008-08-13 01:48:57 -04:00
|
|
|
NULL, NULL, NULL, NULL
|
|
|
|
};
|
2008-08-13 01:30:42 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
Init_newline(void)
|
|
|
|
{
|
|
|
|
rb_register_transcoder(&rb_universal_newline);
|
2008-08-13 01:48:57 -04:00
|
|
|
rb_register_transcoder(&rb_crlf_newline);
|
|
|
|
rb_register_transcoder(&rb_cr_newline);
|
2008-08-13 01:30:42 -04:00
|
|
|
}
|