01: package kawa.standard;
02:
03: import gnu.lists.FString;
04: import gnu.text.LineBufferedReader;
05: import gnu.mapping.*;
06:
07: public class read_line {
08: public static Object apply(LineBufferedReader in, String handling)
09: throws java.io.IOException {
10: int ch = in.read();
11: if (ch < 0)
12: return gnu.expr.Special.eof;
13: int start = in.pos - 1;
14: int pos = start;
15: int limit = in.limit;
16: char[] buffer = in.buffer;
17: int delim = -1; // Length of delimiter.
18:
19: // First do a quick scan of what is in in's input buffer.
20: while (pos < limit) {
21: ch = buffer[pos++];
22: if (ch == '\r' || ch == '\n') {
23: pos--;
24: if (handling == "trim" || handling == "peek") {
25: if (handling == "peek")
26: delim = 0;
27: if (ch == '\n')
28: delim = 1;
29: else if (pos + 1 < limit)
30: delim = buffer[pos + 1] == '\n' ? 2 : 1;
31: else
32: break;
33: in.pos = pos + delim;
34: } else if (handling == "concat" && ch == '\n') {
35: in.pos = ++pos;
36: } else
37: break;
38: return new FString(buffer, start, pos - start);
39: }
40: }
41:
42: // Ok, we haven't found the end-of-line yet, so use the general
43: // readLine method in LineBufferedReader.
44: StringBuffer sbuf = new StringBuffer(100);
45: if (pos > start)
46: sbuf.append(buffer, start, pos - start);
47: in.pos = pos;
48: char mode = handling == "peek" ? 'P' : handling == "concat"
49: || handling == "split" ? 'A' : 'I';
50: in.readLine(sbuf, mode);
51: int length = sbuf.length();
52: if (handling == "split") {
53: if (length == 0)
54: delim = 0;
55: else {
56: char last = sbuf.charAt(length - 1);
57: if (last == '\r')
58: delim = 1;
59: else if (last != '\n')
60: delim = 0;
61: else if (last > 2 && sbuf.charAt(length - 2) == '\r')
62: delim = 2;
63: else
64: delim = 1;
65: length -= delim;
66: }
67: }
68: FString dataStr = new FString(sbuf, 0, length);
69: if (handling == "split") {
70: FString delimStr = new FString(sbuf, length - delim, delim);
71: Object[] values = { dataStr, delimStr };
72: return new Values(values);
73: } else
74: return dataStr;
75: }
76: }
|