private void encode(ScilabString var, VectorOfDouble vec) { // header encodeHeader(var, vec, ScilabTypeEnum.sci_strings); // add the offset table which contains the offset of each UTF-8 encoded strings int offsetTableStart = vec.size(); vec.resize(offsetTableStart + var.getHeight() * var.getWidth()); int offsetTableAccumulated = 0; // encode the strings as UTF-8 and store the associated offset Charset utf8 = Charset.forName("UTF-8"); for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { String str = var.getData()[i][j]; byte[] bytes = str.getBytes(utf8); // append the terminal '\0' final int requiredBytes = bytes.length + 1; final int doubleLen = (requiredBytes + Double.BYTES - 1) / Double.BYTES; // set the offset offsetTableAccumulated += doubleLen; vec.set(offsetTableStart++, offsetTableAccumulated); // push the data through a temporary byte buffer int index = vec.size(); vec.resize(index + doubleLen); vec.asByteBuffer(index, doubleLen).put(bytes).put((byte) 0); } } }
@Test public void readJavaSerializedStringTest() throws IOException { String[][] a = {{"This", "is", "my", "string"}, {"and", "I want to", "compare", " them"}}; ScilabString aMatrix = new ScilabString(a); assertTrue(aMatrix.equals(readFromFile(scilabStringFile))); }
/** * Allocate a port according to the explicit/implicit values. * * @return a new typed port */ private InputPort allocatePort() { InputPort ret; /* * backward compatibility, use explicit as default. */ if (graphics.size() <= GRAPHICS_INIMPL_INDEX) { return new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } ScilabType inImpl = graphics.get(GRAPHICS_INIMPL_INDEX); /* * backward compatibility, use explicit as default. */ if (isEmptyField(inImpl)) { return new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } final ScilabString inImplicit = (ScilabString) inImpl; /* * backward compatibility, use explicit as default. */ if (isEmptyField(inImplicit)) { return new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } final boolean isColumnDominant = inImplicit.getHeight() >= inImplicit.getWidth(); final int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant); final String[][] inimpl = inImplicit.getData(); // can we safely access the indexed data ? final boolean isSet = canGet(inImplicit, indexes); /* * when the type is set, create a new port instance; create an explicit typed port otherwise. */ if (isSet && inimpl[indexes[0]][indexes[1]].equals(EXPLICIT)) { ret = new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } else if (isSet && inimpl[indexes[0]][indexes[1]].equals(IMPLICIT)) { ret = new ImplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } else { // when not specified, use explicit ret = new ExplicitInputPort( controller, controller.createObject(Kind.PORT), Kind.PORT, null, null, null); } return ret; }
/** @return the fontSize */ private int getFontSize() { final ScilabString exprs = getLocalExprs(); int number; final boolean isColumnDominant = exprs.getHeight() >= exprs.getWidth(); final int[] indexes = getIndexes(2, isColumnDominant); if (canGet(exprs, indexes)) { number = Integer.parseInt(exprs.getData()[indexes[0]][indexes[1]]); } else { number = 0; } return Font.getSize(number); }
@Test() public void putAndGetStringTest() throws NullPointerException, JavasciException { String[][] a = { {"String1", "String2", "String3", "String4"}, {"String5", "String6", "My String 7", "String8"} }; ScilabString aOriginal = new ScilabString(a); sci.put("a", aOriginal); assertTrue(sci.exec("checksize = and(size(a)==[2,4]);")); ScilabBoolean checksize = (ScilabBoolean) sci.get("checksize"); assertTrue(checksize.getData()[0][0]); ScilabString aFromScilab = (ScilabString) sci.get("a"); assertTrue(aFromScilab.equals(aOriginal)); }
@Test() public void ReadStructTest() throws NullPointerException, JavasciException { assertTrue(sci.exec("myDate=struct('day',25,'month' ,'DEC','year',2006)")); assertEquals(sci.getVariableType("myDate"), ScilabTypeEnum.sci_mlist); ScilabMList myDate = (ScilabMList) sci.get("myDate"); assertTrue( myDate .toString() .equals( "mlist([\"st\", \"dims\", \"day\", \"month\", \"year\"], int32([1, 1]), [25.0], [\"DEC\"], [2006.0])")); assertEquals(myDate.getHeight(), 1); assertEquals(myDate.getWidth(), 5); assertTrue(myDate.getVarName().equals("myDate")); assertTrue(myDate.getMListType().equals("st")); Map<String, ScilabType> listFields = myDate.getMListFields(); ScilabString month = (ScilabString) listFields.get("month"); assertTrue(month.getData()[0][0].equals("DEC")); ScilabDouble year = (ScilabDouble) listFields.get("year"); assertEquals(year.getRealPart()[0][0], 2006.0, 1e-8); ScilabDouble day = (ScilabDouble) listFields.get("day"); assertEquals(day.getRealPart()[0][0], 25.0, 1e-8); }
/** * Fill the port with the parameters from the graphics structure. * * @param port the target instance */ private void decodeGraphics(InputPort port) { // protection against previously stored blocks if (graphics.size() > GRAPHICS_INSTYLE_INDEX && !isEmptyField(graphics.get(GRAPHICS_INSTYLE_INDEX))) { final ScilabString styles = (ScilabString) graphics.get(GRAPHICS_INSTYLE_INDEX); boolean isColumnDominant = styles.getHeight() >= styles.getWidth(); int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant); if (canGet(styles, indexes)) { final String style; style = styles.getData()[indexes[0]][indexes[1]]; port.setStyle(new StyleMap(port.getStyle()).putAll(style).toString()); } } // protection against previously stored blocks if (graphics.size() > GRAPHICS_INLABEL_INDEX && !isEmptyField(graphics.get(GRAPHICS_INLABEL_INDEX))) { final ScilabString labels = (ScilabString) graphics.get(GRAPHICS_INLABEL_INDEX); boolean isColumnDominant = labels.getHeight() >= labels.getWidth(); int[] indexes = getIndexes(alreadyDecodedCount, isColumnDominant); if (canGet(labels, indexes)) { final String label = labels.getData()[indexes[0]][indexes[1]]; if (label != null) { port.setValue(label); } else { port.setValue(""); } } } }
public static void readData(int dataSetId, ScilabString data) throws NullPointerException, HDF5Exception { data.setData(getStringMatrix(dataSetId)); }
private ScilabType decode(VectorOfDouble vec, ScilabString var) { final String[][] data = var.getData(); decodeString(var.getHeight(), var.getWidth(), vec, (i, j, str) -> data[i][j] = str); return var; }