loadFromCsvString static method

CodecRegistry loadFromCsvString(
  1. String csvString
)

Implementation

static CodecRegistry loadFromCsvString(String csvString) {
  final List<CodecInfo> loadedCodecs = [];
  final lines = csvString.trim().split('\n');

  if (lines.length < 2) { // Header + at least one data line
    throw FormatException('Codec CSV string is too short or empty.');
  }

  // Skip header line
  for (int i = 1; i < lines.length; i++) {
    final line = lines[i].trim();
    if (line.isEmpty) continue;

    final parts = line.split(',');
    if (parts.length < 3) { // name, tag, code are mandatory
      // print('Skipping malformed line: $line'); // Optional: log malformed lines
      continue;
    }

    try {
      final name = parts[0].trim();
      final tag = parts[1].trim();
      final codeHex = parts[2].trim();
      final status = parts.length > 3 ? parts[3].trim() : '';
      final description = parts.length > 4 ? parts.sublist(4).join(',').trim() : ''; // Handle commas in description

      if (name.isEmpty || codeHex.isEmpty) {
          // print('Skipping line with empty name or code: $line');
          continue;
      }

      final int code = int.parse(codeHex.startsWith('0x') ? codeHex.substring(2) : codeHex, radix: 16);
      loadedCodecs.add(CodecInfo(name, tag, code, status, description));
    } catch (e) {
      // print('Error parsing line "$line": $e'); // Optional: log errors
      // Decide whether to skip or rethrow. Skipping for robustness.
    }
  }
  if (loadedCodecs.isEmpty) {
      throw FormatException('No valid codecs found in CSV string.');
  }
  return CodecRegistry._(loadedCodecs);
}