All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.pawelkrol.CommTest.LabelLog.scala Maven / Gradle / Ivy

There is a newer version: 0.05
Show newest version
package com.github.pawelkrol.CommTest

import java.io.File

import scala.io.Codec.ISO8859
import scala.io.Source.fromFile

import MiscUtils._

class LabelLog(labels: Map[String, Short] = Map()) {

  def apply(key: String) = labels(key)

  def contains(key: String) = labels.contains(key)

  def label(address: Short) =
    labels.find(_._2 == address) match {
      case Some((key, _)) => Some(key)
      case None => None
    }
}

object LabelLog {

  private def split(log: List[Char]): List[List[Char]] =
    log match {
      case Nil => List(List())
      case x :: xs => {
        val result = split(xs)
        x.toInt match {
          case 0x0a => Nil +: result
          case _ => x +: result.head :: result.tail
        }
      }
    }

  private def extract(line: List[Char]) = {
    val (label, after, dollar, value) =
      line.foldLeft[Tuple4[String, Boolean, Boolean, String]](("", false, false, ""))((result, char) => {
        val (label, after, dollar, value) = result
        char.toInt match {
          case 0x3d => // "="
            (label, true, dollar, value)
          case _ => {
            after match {
              case false =>
                (label + char, after, dollar, value)
              case true =>
                char.toInt match {
                  case 0x24 => // "$"
                    (label, after, true, value)
                  case _ =>
                    dollar match {
                      case false =>
                        (label, after, dollar, value)
                      case true =>
                        if (isHexDigit(char))
                          (label, after, dollar, value + char)
                        else
                          (label, after, false, value)
                    }
                }
            }
          }
        }
      })

    if (label.nonEmpty && value.nonEmpty)
      Some(label.trim, hex2int(value).toShort)
    else
      None
  }

  private def parse(log: List[Char]) = split(log).filter(_.nonEmpty).map(extract(_)).filter(
    _ match {
      case Some(_) => true
      case None => false
    }
  ).map(_.get).toMap

  def apply() = new LabelLog()

  def apply(log: List[Char]) = new LabelLog(parse(log))

  def apply(file: File): LabelLog = LabelLog(fromFile(file)(ISO8859).toList)

  def apply(fileName: String): LabelLog = LabelLog(new File(fileName))
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy