Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New example project in examples/bt/. Moved main project to 'core'.

  • Loading branch information...
commit bb11109f2a85d68f5b508b6bf71f9c50c4bdd517 1 parent 70d43e1
@harrah harrah authored
View
0  src/defaultprotocol.scala → core/src/defaultprotocol.scala
File renamed without changes
View
0  src/generic.scala → core/src/generic.scala
File renamed without changes
View
0  src/io.scala → core/src/io.scala
File renamed without changes
View
0  src/javaprotocol.scala → core/src/javaprotocol.scala
File renamed without changes
View
0  src/operations.scala → core/src/operations.scala
File renamed without changes
View
0  src/protocol.scala → core/src/protocol.scala
File renamed without changes
View
0  src/standardtypes.scala → core/src/standardtypes.scala
File renamed without changes
View
0  test-src/binaryTests.scala → core/test-src/binaryTests.scala
File renamed without changes
View
61 examples/bt/BT.scala
@@ -0,0 +1,61 @@
+package sbintest
+
+import sbinary.DefaultProtocol._
+import sbinary.Operations._
+import sbinary._
+
+ // Binary tree data types that will be
+ // created and converted to/from byte arrays
+
+sealed abstract class BT;
+case class Bin(left : BT, right : BT) extends BT;
+case class Leaf(label : String) extends BT;
+
+ // Custom protocol for (de)serializing binary trees
+
+object MyProtocol extends DefaultProtocol {
+ implicit object BTFormat extends Format[BT]{
+ def reads(in : Input) = read[Byte](in) match {
+ case 0 => Bin(reads(in), reads(in));
+ case _ => Leaf(read[String](in))
+ }
+
+ def writes(out : Output, value : BT) = value match {
+ case Bin(left, right) => write[Byte](out, 0); writes(out, left); writes(out, right);
+ case Leaf(label) => write[Byte](out, 1); write(out, label);
+ }
+ }
+}
+
+ // test runner
+
+object main {
+ def main(args : Array[String]) : Unit = {
+ val input = List("foo", "bar", "baz")
+ println(input)
+
+ val binary = toByteArray(input)
+ println(showArray(binary))
+
+ val normalAgain = fromByteArray[List[String]](binary)
+ println(normalAgain)
+
+ toFile(input)(new java.io.File("foo.bin"))
+ println(fromFile[List[String]](new java.io.File("foo.bin")))
+
+ import MyProtocol._
+
+ val someTree = Bin(Leaf("foo"), Bin(Leaf("bar"), Leaf("baz")))
+ println(someTree)
+
+ val someTreeInAByteArray = toByteArray[BT](someTree)
+ println(showArray(someTreeInAByteArray))
+
+ val theSameTree = fromByteArray[BT](someTreeInAByteArray)
+ println(theSameTree)
+ }
+ // utility methods for printing a byte array
+ def showArray(b: Array[Byte]) = b.map(showByte).mkString(" ")
+ def showByte(b: Byte) = pad( ((b+256) % 256).toHexString )
+ def pad(s: String) = if(s.length == 1) "0" + s else s
+}
View
4 project/build.properties
@@ -1,8 +1,8 @@
#Project properties
#Sun Jan 24 13:59:22 EST 2010
project.organization=org.scala-tools.sbinary
-project.name=SBinary
-sbt.version=0.7.1
+project.name=SBinary Parent
+sbt.version=0.7.3
project.version=0.3.1-SNAPSHOT
def.scala.version=2.7.7
build.scala.versions=2.7.7 2.8.0.Beta1
View
29 project/build/SBinaryProject.scala
@@ -1,15 +1,30 @@
import sbt._
-class SBinaryProject(info: ProjectInfo) extends DefaultProject(info) with TemplateProject
+class SBinaryProject(info: ProjectInfo) extends ParentProject(info) with NoPublish
{
- val sc =
- if(buildScalaVersion.startsWith("2.7")) "org.scala-tools.testing" %% "scalacheck" % "1.6" % "test"
- else "org.scala-tools.testing" % "scalacheck" % "1.7-SNAPSHOT" % "test" from("http://scalacheck.googlecode.com/files/scalacheck_2.8.0.Beta1-1.7-SNAPSHOT.jar")
-
- override def mainResources = super.mainResources +++ "LICENSE"
-
// publishing
override def managedStyle = ManagedStyle.Maven
val publishTo = "Scala Tools Nexus" at "http://nexus.scala-tools.org/content/repositories/releases/"
Credentials(Path.userHome / ".ivy2" / ".credentials", log)
+
+ lazy val core = project("core", "SBinary", new CoreProject(_))
+ lazy val treeExample = project("examples" / "bt", "Binary Tree Example", new ExampleProject(_), core)
+
+ class ExampleProject(info: ProjectInfo) extends DefaultProject(info) with NoPublish
+ {
+ override def scratch = true
+ }
+ class CoreProject(info: ProjectInfo) extends DefaultProject(info) with TemplateProject
+ {
+ val sc =
+ if(buildScalaVersion.startsWith("2.7")) "org.scala-tools.testing" %% "scalacheck" % "1.6" % "test"
+ else "org.scala-tools.testing" % "scalacheck" % "1.7-SNAPSHOT" % "test" from("http://scalacheck.googlecode.com/files/scalacheck_2.8.0.Beta1-1.7-SNAPSHOT.jar")
+
+ override def mainResources = super.mainResources +++ "LICENSE"
+ }
}
+trait NoPublish extends BasicManagedProject
+{
+ override def deliverAction = publishAction
+ override def publishAction = task { None }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.