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 import java.util.HashMap;
20 import java.util.Map;
21
22 import eu.simuline.octave.exception.OctaveParseException;
23 import eu.simuline.octave.io.OctaveIO;
24 import eu.simuline.octave.io.spi.OctaveDataReader;
25 import eu.simuline.octave.type.OctaveObject;
26 import eu.simuline.octave.type.OctaveStruct;
27
28
29
30
31
32
33
34 public final class ScalarStructReader extends OctaveDataReader {
35 private static final String NAME = "# name: ";
36 private static final String LENGTH = "# length: ";
37 private static final String N_DIMS2 = "# ndims: 2";
38 private static final String V_DIMS2 = " 1 1";
39
40 @Override
41 public String octaveType() {
42 return "scalar struct";
43 }
44
45 @Override
46 public OctaveStruct read(final BufferedReader reader) {
47
48
49 String line = OctaveIO.readerReadLine(reader);
50 if (!N_DIMS2.equals(line)) {
51 throw new OctaveParseException
52 ("JavaOctave does not support matrix structs, read=" + line);
53 }
54
55
56 line = OctaveIO.readerReadLine(reader);
57 if (!V_DIMS2.equals(line)) {
58 throw new OctaveParseException
59 ("JavaOctave does not support matrix structs, read=" + line);
60 }
61
62
63 line = OctaveIO.readerReadLine(reader);
64 if (line == null || !line.startsWith(LENGTH)) {
65 throw new OctaveParseException
66 ("Expected <" + LENGTH + "> got <" + line + ">. ");
67 }
68 final int length = Integer.parseInt(line.substring(LENGTH.length()));
69
70
71 final Map<String, OctaveObject> data =
72 new HashMap<String, OctaveObject>();
73
74 for (int i = 0; i < length; i++) {
75
76
77
78
79 do {
80 line = OctaveIO.readerReadLine(reader);
81 } while ("".equals(line));
82 if (!line.startsWith(NAME)) {
83 throw new OctaveParseException
84 ("Expected <" + NAME + "> got <" + line + ">. ");
85 }
86 final String subname = line.substring(NAME.length());
87
88
89
90
91
92 final OctaveObject value = OctaveIO.read(reader);
93 data.put(subname, value);
94 }
95
96 return new OctaveStruct(data);
97 }
98
99 }