findStartorCodeCharacter method

List findStartorCodeCharacter(
  1. String s,
  2. int col
)

Implementation

List findStartorCodeCharacter(String s, int col) {
  var rows = <_CodeData>[];

  //if two chars are numbers (or FNC1) then START_C or CODE_C
  if (s.length > 1 &&
      (s[0].isNumber() || s[0] == String.fromCharCode(200)) &&
      (s[1].isNumber() || s[1] == String.fromCharCode(200))) {
    if (startCharacter == null) {
      startCharacter = _codes.firstWhere((item) => item!.a == 'START_C');
      rows.add(startCharacter!);
    } else {
      rows.add(_codes.firstWhere((item) => item!.a == 'CODE_C')!);
    }

    col = 1;
  } else {
    var aFound = false;
    var bFound = false;
    for (_CodeData? row in _codes) {
      if (!aFound && s == row!.a) {
        aFound = true;
        col = 2;

        if (startCharacter == null) {
          startCharacter = _codes.firstWhere((item) => item!.a == 'START_A');
          rows.add(startCharacter!);
        } else {
          rows.add(_codes.firstWhere(
              (item) => item!.b == 'CODE_A')!); //first column is FNC4 so use B
        }
      } else if (!bFound && s == row!.b) {
        bFound = true;
        col = 1;

        if (startCharacter == null) {
          startCharacter = _codes.firstWhere((item) => item!.a == 'START_B');
          rows.add(startCharacter!);
        } else
          rows.add(_codes.firstWhere((item) => item!.a == 'CODE_B')!);
      } else if (aFound && bFound) break;
    }

    if (rows.length <= 0)
      throw new Exception("EC128-2: Could not determine start character.");
  }

  return [rows, col];
}