topspeed 0.3.0
topspeed: ^0.3.0 copied to clipboard
Pure Dart reader and writer for Clarion TopSpeed (.tps) database files: tables, keys, MEMO/BLOB, code pages and encrypted files. No native code.
example/topspeed_example.dart
// Lists the tables, fields and first rows of a .tps file, then adds a row
// to a copy of it.
//
// Usage: dart run example/topspeed_example.dart <file.tps> [password]
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:topspeed/topspeed.dart';
void main(List<String> args) {
if (args.isEmpty) {
print(
'Usage: dart run example/topspeed_example.dart <file.tps> [password]');
exit(64);
}
final file = TpsFile.parse(
File(args[0]).readAsBytesSync(),
password: args.length > 1 ? args[1] : null,
);
for (final table in file.tables) {
final def = table.definition;
print('Table ${table.name}: ${table.rows.length} rows');
for (final f in def.valueFields) {
print(' ${f.label.padRight(16)} ${f.type.name}');
}
for (final m in def.memos) {
print(' ${m.label.padRight(16)} ${m.isBlob ? 'BLOB' : 'MEMO'}');
}
for (final k in def.indexes) {
final parts = k.parts.map((p) => def.fields[p.field].label).join(', ');
print(' key ${k.label}($parts)${k.allowsDuplicates ? ' DUP' : ''}');
}
// Read rows: values come back as int, double, String, DateTime, ...
for (final row in table.rows.take(3)) {
print(' #${row.recordNumber} ${row.values}');
}
// Read in key order, like SET(key) and NEXT in Clarion.
if (def.indexes.isNotEmpty) {
final key = def.indexes.first;
final first = table.byKey(key.label).take(3).map((r) => r.recordNumber);
print(' first rows by ${key.label}: $first');
}
}
// Write only when the library reproduces the file's keys exactly.
final table = file.tables.first;
if (!table.indexesVerified) {
print('Keys of ${table.name} could not be verified; not writing.');
return;
}
final firstText = table.definition.valueFields
.where((f) => f.type.isText && f.elements <= 1)
.firstOrNull;
if (firstText == null) return;
try {
final n = table.insert({firstText.label: 'Added by topspeed'});
final copy = File('${args[0]}.copy.tps')..writeAsBytesSync(file.toBytes());
print('Inserted row #$n into ${copy.path}');
} on TpsException catch (e) {
print('Insert rejected: ${e.message}');
}
}