1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package eu.simuline.octave.io.impl;
17
18 import java.io.BufferedReader;
19
20 import eu.simuline.octave.exception.OctaveParseException;
21 import eu.simuline.octave.io.OctaveIO;
22 import eu.simuline.octave.io.spi.OctaveDataReader;
23 import eu.simuline.octave.type.OctaveCell;
24 import eu.simuline.octave.type.OctaveObject;
25
26
27 import static eu.simuline.octave.io.impl.CellWriter.NROWS;
28 import static eu.simuline.octave.io.impl.CellWriter.NCOLUMNS;
29
30
31
32
33
34 public final class CellReader extends OctaveDataReader {
35
36 @Override
37 public String octaveType() {
38 return "cell";
39 }
40
41 @Override
42 public OctaveCell read(final BufferedReader reader) {
43 String line = OctaveIO.readerReadLine(reader);
44 String token = NROWS;
45
46 if (!line.startsWith(token)) {
47 throw new OctaveParseException
48 ("Expected <" + token + ">, but got <" + line + ">");
49 }
50 final int nrows = Integer.parseInt(line.substring(token.length()));
51
52 line = OctaveIO.readerReadLine(reader);
53 token = NCOLUMNS;
54 if (!line.startsWith(token)) {
55 throw new OctaveParseException
56 ("Expected <" + token + ">, but got <" + line + ">");
57 }
58 final int ncols = Integer.parseInt(line.substring(token.length()));
59
60 final OctaveCelll.html#OctaveCell">OctaveCell octaveCell = new OctaveCell(nrows, ncols);
61
62 for (int col = 1; col <= ncols; col++) {
63 for (int row = 1; row <= nrows; row++) {
64
65
66
67 do {
68 line = OctaveIO.readerReadLine(reader);
69 } while ("".equals(line));
70 token = "# name: <cell-element>";
71 if (!line.equals(token)) {
72 throw new OctaveParseException
73 ("Expected <" + token + ">, but got <" + line + ">");
74 }
75 final OctaveObject octaveType = OctaveIO.read(reader);
76 octaveCell.set(octaveType, row, col);
77 }
78 line = OctaveIO.readerReadLine(reader);
79 token = "";
80 if (line == null || !line.equals(token)) {
81 throw new OctaveParseException
82 ("Expected <" + token + ">, but got <" + line + ">");
83 }
84 }
85
86 return octaveCell;
87 }
88
89 }