enhanced record handling for CRLF

This commit is contained in:
hyung-hwan 2011-05-02 08:37:17 +00:00
parent 9514c71a03
commit 12adaea5a3
2 changed files with 69 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: rio.c 447 2011-05-01 13:28:51Z hyunghwan.chung $ * $Id: rio.c 449 2011-05-01 14:37:17Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -361,11 +361,20 @@ int qse_awk_rtx_readio (
if (QSE_STR_LEN(buf) == 0) ret = 0; if (QSE_STR_LEN(buf) == 0) ret = 0;
else if (rrs.ptr != QSE_NULL && rrs.len == 0) else if (rrs.ptr != QSE_NULL && rrs.len == 0)
{ {
/* TODO: handle different line terminator */ /* TODO: handle different line terminator */
/* drop the line terminator from the record /* drop the line terminator from the record
* if RS is a blank line and EOF is reached. */ * if RS is a blank line and EOF is reached. */
if (QSE_STR_LASTCHAR(buf) == QSE_T('\n')) if (QSE_STR_LASTCHAR(buf) == QSE_T('\n'))
{
QSE_STR_LEN(buf) -= 1; QSE_STR_LEN(buf) -= 1;
if (run->awk->option & QSE_AWK_CRLF)
{
/* drop preceding CR */
if (QSE_STR_LEN(buf) > 0 &&
QSE_STR_LASTCHAR(buf) == QSE_T('\r'))
QSE_STR_LEN(buf) -= 1;
}
}
} }
else if (rrs.len >= 2) else if (rrs.len >= 2)
{ {
@ -403,7 +412,7 @@ int qse_awk_rtx_readio (
c = p->in.buf[p->in.pos++]; c = p->in.buf[p->in.pos++];
end_pos = p->in.pos; end_pos = p->in.pos;
/* TODO: handle different line terminator */ /* TODO: handle different line terminator */
/* separate by a new line */ /* separate by a new line */
if (c == QSE_T('\n')) if (c == QSE_T('\n'))
{ {
@ -412,16 +421,16 @@ int qse_awk_rtx_readio (
{ {
if (end_pos > start_pos) if (end_pos > start_pos)
{ {
/* '\r' is the part of the read buffer. /* CR is the part of the read buffer.
* decrementing the end_pos variable can * decrementing the end_pos variable can
* simply drop it */ * simply drop it */
end_pos--; end_pos--;
} }
else else
{ {
/* '\r' must have come from the previous /* CR must have come from the previous
* read. the record buffer must contain * read. drop CR that must be found at
* it at the end. */ * the end of the record buffer. */
QSE_ASSERT (end_pos == start_pos); QSE_ASSERT (end_pos == start_pos);
QSE_ASSERT (QSE_STR_LEN(buf) > 0); QSE_ASSERT (QSE_STR_LEN(buf) > 0);
QSE_ASSERT (QSE_STR_LASTCHAR(buf) == QSE_T('\r')); QSE_ASSERT (QSE_STR_LASTCHAR(buf) == QSE_T('\r'));
@ -456,7 +465,7 @@ int qse_awk_rtx_readio (
pc = c; pc = c;
c = p->in.buf[p->in.pos++]; c = p->in.buf[p->in.pos++];
/* TODO: handle different line terminator */ /* TODO: handle different line terminator */
/* separate by a blank line */ /* separate by a blank line */
if (c == QSE_T('\n')) if (c == QSE_T('\n'))
{ {
@ -467,25 +476,58 @@ int qse_awk_rtx_readio (
* by dropping of CR before NL */ * by dropping of CR before NL */
QSE_ASSERT (line_len > 0); QSE_ASSERT (line_len > 0);
line_len--; line_len--;
QSE_STR_LEN(buf) -= 1;
/* we don't drop CR from the record buffer
* if we're in CRLF mode. POINT-X */
if (!(run->awk->option & QSE_AWK_CRLF))
QSE_STR_LEN(buf) -= 1;
} }
if (line_len == 0) if (line_len == 0)
{ {
/* we got a blank line */ /* we got a blank line */
if (QSE_STR_LEN(buf) <= 0)
if (run->awk->option & QSE_AWK_CRLF)
{ {
/* if the record is empty when a blank if (QSE_STR_LEN(buf) > 0 &&
* line is encountered, the line QSE_STR_LASTCHAR(buf) == QSE_T('\r'))
* terminator should not be added to {
* the record */ /* drop CR not dropped in POINT-X above */
continue; QSE_STR_LEN(buf) -= 1;
}
if (QSE_STR_LEN(buf) <= 0)
{
/* if the record is empty when a blank
* line is encountered, the line
* terminator should not be added to
* the record */
continue;
}
/* drop NL */
QSE_STR_LEN(buf) -= 1;
/* drop preceding CR */
if (QSE_STR_LEN(buf) > 0 &&
QSE_STR_LASTCHAR(buf) == QSE_T('\r'))
QSE_STR_LEN(buf) -= 1;
}
else
{
if (QSE_STR_LEN(buf) <= 0)
{
/* if the record is empty when a blank
* line is encountered, the line
* terminator should not be added to
* the record */
continue;
}
/* drop NL of the previous line */
QSE_STR_LEN(buf) -= 1; /* simply drop NL */
} }
/* when a blank line is encountered,
* it needs to snip off the line
* terminator of the previous line */
QSE_STR_LEN(buf) -= 1;
done = 1; done = 1;
break; break;
} }

View File

@ -84,14 +84,14 @@ WRect
1880 1880
5700 5700
4240 4240
0 1
0 0
22 22
WFileName WFileName
30 30
release/os2/lib/cmn/qsecmn.tgt release/os2/lib/cmn/qsecmn.tgt
28 28
30 29
23 23
VComponent VComponent
24 24
@ -145,10 +145,10 @@ VComponent
33 33
WRect WRect
380 380
-160 0
5700 5700
4240 4240
0 1
0 0
34 34
WFileName WFileName
@ -260,14 +260,14 @@ WRect
80 80
5700 5700
4240 4240
1 0
0 0
55 55
WFileName WFileName
28 28
debug/os2/lib/awk/qseawk.tgt debug/os2/lib/awk/qseawk.tgt
8 8
10 12
56 56
VComponent VComponent
57 57
@ -289,7 +289,7 @@ VComponent
60 60
WRect WRect
2920 2920
893 880
5700 5700
4240 4240
0 0
@ -300,4 +300,4 @@ WFileName
debug/os2/cmd/awk/qseawk.tgt debug/os2/cmd/awk/qseawk.tgt
0 0
1 1
32 59