diff --git a/.packages b/.packages new file mode 100644 index 0000000..3f46bed --- /dev/null +++ b/.packages @@ -0,0 +1,2 @@ +# Generated by pub on 2019-07-08 12:24:25.090026. +csv:lib/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6a7e0fc --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Dart", + "program": "example/simple_csv_file_write.dart", + "request": "launch", + "type": "dart" + } + ] +} \ No newline at end of file diff --git a/example/simple_csv_file_parse.dart b/example/simple_csv_file_parse.dart index 2038d03..73297f7 100644 --- a/example/simple_csv_file_parse.dart +++ b/example/simple_csv_file_parse.dart @@ -2,7 +2,7 @@ import '../lib/csv.dart'; import 'dart:io'; void main() { - File csvFile = new File('D:\\tools\\flights\\csv\\example\\sample.csv'); + File csvFile = new File('D:\\Github\\csv\\example\\sample.csv'); Future>> f = parseCsvFile(csvFile); f.then((List> csv) { diff --git a/example/simple_csv_file_write.dart b/example/simple_csv_file_write.dart index ff1af4b..1bb13b1 100644 --- a/example/simple_csv_file_write.dart +++ b/example/simple_csv_file_write.dart @@ -2,7 +2,7 @@ import '../lib/csv.dart'; import 'dart:io'; void main() { - File csvFile = new File('D:\\tools\\flights\\csv\\example\\sample_write.csv'); + File csvFile = new File('D:\\Github\\csv\\example\\sample_write.csv'); List> csvFileContent = new List>(); List row1 = new List(); diff --git a/lib/csv.dart b/lib/csv.dart index 0da8287..f566d72 100644 --- a/lib/csv.dart +++ b/lib/csv.dart @@ -1,27 +1,25 @@ library csv; import 'dart:io'; +import 'dart:async'; /** * Parses the given `csvFile` and return a `Future>>` that * can be use conveniently to read the contents of the file. */ Future>> parseCsvFile(File csvFile) { - Completer completer = new Completer(); - InputStream inputStream = csvFile.openInputStream(); + Completer>> completer = new Completer>>(); + Future inputStream = csvFile.open(); String csvContent = ''; - inputStream.onError = (e) => throw e; + inputStream.catchError((e) => throw e); - inputStream.onData = () { - List bytes = inputStream.read(); + inputStream.then((RandomAccessFile file) { + List bytes = file.readSync(file.lengthSync()); String dataString = new String.fromCharCodes(bytes); csvContent = '$csvContent$dataString'; - }; - - inputStream.onClosed = () { completer.complete(parseCsvContent(csvContent)); - }; + }); return completer.future; } @@ -37,7 +35,7 @@ List> parseCsvContent(String csvFileContent) { String column = ''; bool foundDoubleQuote = false; List columns = new List(); - List chars = contentSplitted.trim().splitChars(); + List chars = contentSplitted.trim().split(''); for (String c in chars) { if (c != ',' && c != '"') { column = '$column$c'; @@ -73,24 +71,24 @@ List> parseCsvContent(String csvFileContent) { * Writes the `csvFileContent` to `csvFile`. */ void writeCsvFile(File csvFile, List> csvFileContent, [bool overwrite = true]) { - FileMode fileMode = FileMode.WRITE; + FileMode fileMode = FileMode.write; if (!overwrite) { - fileMode = FileMode.APPEND; + fileMode = FileMode.append; } - OutputStream os = csvFile.openOutputStream(fileMode); + RandomAccessFile os = csvFile.openSync(mode: fileMode); for (List row in csvFileContent) { for (int i = 0; i < row.length; i++) { String column = row[i]; if (i > 0) { - os.writeString(','); + os.writeStringSync(','); } - os.writeString(column); + os.writeStringSync(column); } - os.writeString('\n'); + os.writeStringSync('\n'); } os.close(); diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..802445c --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,5 @@ +# Generated by pub +# See https://www.dartlang.org/tools/pub/glossary#lockfile +packages: {} +sdks: + dart: any diff --git a/pubspec.yaml b/pubspec.yaml index 7d29a05..5c7da6c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: csv description: CSV file parser for Dart. -dependencies: - unittest: { sdk: unittest } +# dependencies: +# unittest: any