| 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 | 6 | 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 | 2 | return "scalar struct"; |
| 43 | |
} |
| 44 | |
|
| 45 | |
@Override |
| 46 | |
public OctaveStruct read(final BufferedReader reader) { |
| 47 | |
|
| 48 | |
|
| 49 | 4 | String line = OctaveIO.readerReadLine(reader); |
| 50 | 4 | if (!N_DIMS2.equals(line)) { |
| 51 | 0 | throw new OctaveParseException |
| 52 | |
("JavaOctave does not support matrix structs, read=" + line); |
| 53 | |
} |
| 54 | |
|
| 55 | |
|
| 56 | 4 | line = OctaveIO.readerReadLine(reader); |
| 57 | 4 | if (!V_DIMS2.equals(line)) { |
| 58 | 0 | throw new OctaveParseException |
| 59 | |
("JavaOctave does not support matrix structs, read=" + line); |
| 60 | |
} |
| 61 | |
|
| 62 | |
|
| 63 | 4 | line = OctaveIO.readerReadLine(reader); |
| 64 | 4 | if (line == null || !line.startsWith(LENGTH)) { |
| 65 | 0 | throw new OctaveParseException |
| 66 | |
("Expected <" + LENGTH + "> got <" + line + ">. "); |
| 67 | |
} |
| 68 | 4 | final int length = Integer.parseInt(line.substring(LENGTH.length())); |
| 69 | |
|
| 70 | |
|
| 71 | 4 | final Map<String, OctaveObject> data = |
| 72 | |
new HashMap<String, OctaveObject>(); |
| 73 | |
|
| 74 | 246 | for (int i = 0; i < length; i++) { |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
do { |
| 80 | 718 | line = OctaveIO.readerReadLine(reader); |
| 81 | 718 | } while ("".equals(line)); |
| 82 | 242 | if (!line.startsWith(NAME)) { |
| 83 | 0 | throw new OctaveParseException |
| 84 | |
("Expected <" + NAME + "> got <" + line + ">. "); |
| 85 | |
} |
| 86 | 242 | final String subname = line.substring(NAME.length()); |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | 242 | final OctaveObject value = OctaveIO.read(reader); |
| 93 | 242 | data.put(subname, value); |
| 94 | |
} |
| 95 | |
|
| 96 | 4 | return new OctaveStruct(data); |
| 97 | |
} |
| 98 | |
|
| 99 | |
} |