stevedlawrence commented on a change in pull request #16: Implemented packed 
binary formats
URL: https://github.com/apache/incubator-daffodil/pull/16#discussion_r155274322
 
 

 ##########
 File path: 
daffodil-runtime1/src/main/scala/edu/illinois/ncsa/daffodil/processors/parsers/IBM4690PackedDecimalParsers.scala
 ##########
 @@ -0,0 +1,108 @@
+/* Copyright (c) 2012-2014 Tresys Technology, LLC. All rights reserved.
+ *
+ * Developed by: Tresys Technology, LLC
+ *               http://www.tresys.com
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
copy of
+ * this software and associated documentation files (the "Software"), to deal 
with
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies
+ * of the Software, and to permit persons to whom the Software is furnished to 
do
+ * so, subject to the following conditions:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimers.
+ *
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimers in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ *  3. Neither the names of Tresys Technology, nor the names of its 
contributors
+ *     may be used to endorse or promote products derived from this Software
+ *     without specific prior written permission.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH 
THE
+ * SOFTWARE.
+ */
+
+package edu.illinois.ncsa.daffodil.processors.parsers
+
+import edu.illinois.ncsa.daffodil.processors.Evaluatable
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.LengthUnits
+import edu.illinois.ncsa.daffodil.schema.annotation.props.gen.YesNo
+import edu.illinois.ncsa.daffodil.processors.ElementRuntimeData
+import edu.illinois.ncsa.daffodil.util.DecimalUtils._
+import java.lang.{ Long => JLong, Number => JNumber }
+import edu.illinois.ncsa.daffodil.processors.ParseOrUnparseState
+
+class IBM4690PackedDecimalKnownLengthParser(e: ElementRuntimeData, signed: 
YesNo, binaryDecimalVirtualPoint: Int, val lengthInBits: Int)
+  extends IBM4690PackedDecimalParserBase(e, signed, binaryDecimalVirtualPoint)
+  with HasKnownLengthInBits {
+}
+
+class IBM4690PackedDecimalRuntimeLengthParser(val e: ElementRuntimeData, 
signed: YesNo, binaryDecimalVirtualPoint: Int, val lengthEv: 
Evaluatable[JLong], val lUnits: LengthUnits)
+  extends IBM4690PackedDecimalParserBase(e, signed, binaryDecimalVirtualPoint)
+  with HasRuntimeExplicitLength {
+}
+
+abstract class IBM4690PackedDecimalParserBase(e: ElementRuntimeData, signed: 
YesNo, binaryDecimalVirtualPoint: Int)
+  extends PrimParserObject(e) {
+
+  protected def getBitLength(s: ParseOrUnparseState): Int
+
+  def parse(start: PState): Unit = {
+    val nBits = getBitLength(start)
+    val dis = start.dataInputStream
+
+    if (!dis.isDefinedForLength(nBits)) {
+      PE(start, "Insufficient bits in data. Needed %d bit(s) but found only %d 
available.", nBits, dis.remainingBits.get)
+      return
+    }
+
+    val bigDec = IBM4690ToBigDecimal(dis.getByteArray(nBits), 
binaryDecimalVirtualPoint)
+    start.simpleElement.overwriteDataValue(bigDec)
+  }
+}
+
+class IBM4690PackedIntegerRuntimeLengthParser(val e: ElementRuntimeData, 
signed: Boolean, val lengthEv: Evaluatable[JLong], val lUnits: LengthUnits)
+  extends IBM4690PackedIntegerBaseParser(e, signed)
+  with HasRuntimeExplicitLength {
+}
+
+class IBM4690PackedIntegerKnownLengthParser(e: ElementRuntimeData, signed: 
Boolean, val lengthInBits: Int)
+  extends IBM4690PackedIntegerBaseParser(e, signed)
+  with HasKnownLengthInBits {
+}
+
+abstract class IBM4690PackedIntegerBaseParser(e: ElementRuntimeData, signed: 
Boolean)
+  extends PrimParserObject(e) {
+
+  protected def getBitLength(s: ParseOrUnparseState): Int
+
+  def parse(start: PState): Unit = {
+    val nBits = getBitLength(start)
+    if (nBits == 0) return // zero length is used for outputValueCalc often.
+    val dis = start.dataInputStream
+
+    if (!dis.isDefinedForLength(nBits)) {
+      PE(start, "Insufficient bits in data. Needed %d bit(s) but found only %d 
available.", nBits, dis.remainingBits.get)
+      return
+    }
+
+    val int: JNumber =
+      if (signed) {
+        if (nBits > 64) { IBM4690ToBigInteger(dis.getByteArray(nBits)) }
+        else { IBM4690ToBigInteger(dis.getByteArray(nBits)).longValue() }
+      } else {
+        if (nBits >= 64) { IBM4690ToBigInteger(dis.getByteArray(nBits)) }
+        else { IBM4690ToBigInteger(dis.getByteArray(nBits)).longValue() }
+      }
 
 Review comment:
   I don't think if (signed) is being used here correctly. If if/else branches 
are doing basically the same thing. How does signedness apply to these 
zoned/packed types?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to