insertStartandCodeCharacters method

dynamic insertStartandCodeCharacters()

Implementation

insertStartandCodeCharacters() {
  _CodeData currentCodeSet;
  var currentCodeString = "";

  if (_code128Type != Code128Type.Auto) {
    switch (_code128Type) {
      case Code128Type.A:
        _formattedData.insert(0, "START_A");
        break;
      case Code128Type.B:
        _formattedData.insert(0, "START_B");
        break;
      case Code128Type.C:
        _formattedData.insert(0, "START_C");
        break;
      default:
        throw new Exception(
            "EC128-4: Unknown start type in fixed type encoding.");
    }
  } else {
    try {
      for (var i = 0; i < (_formattedData.length); i++) {
        var col = 0;
        var result = findStartorCodeCharacter(_formattedData[i], col);
        List<_CodeData> tempStartChars = result[0];
        col = result[1];
        //check all the start characters and see if we need to stay with the same codeset or if a change of sets is required
        var sameCodeSet = false;
        for (var row in tempStartChars) {
          if (row.a.endsWith(currentCodeString) ||
              row.b.endsWith(currentCodeString) ||
              row.c.endsWith(currentCodeString)) {
            sameCodeSet = true;
            break;
          }
        }

        if (currentCodeString == "" || !sameCodeSet) {
          currentCodeSet = tempStartChars[0];
          var error = true;

          while (error) {
            try {
              currentCodeString = currentCodeSet.getValue(col).split('_')[1];
              error = false;
            } catch (_) {
              error = true;

              if (col++ > 5)
                throw new Exception(
                    "No start character found in CurrentCodeSet.");
            }
          }

          _formattedData.insert(i++, currentCodeSet.getValue(col));
        }
      }
    } catch (ex) {
      throw Exception("EC128-3: Could not insert start and code characters.");
    }
  }
}