Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by pub on 2019-07-08 12:24:25.090026.
csv:lib/
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
2 changes: 1 addition & 1 deletion example/simple_csv_file_parse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<List<String>>> f = parseCsvFile(csvFile);
f.then((List<List<String>> csv) {
Expand Down
2 changes: 1 addition & 1 deletion example/simple_csv_file_write.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>> csvFileContent = new List<List<String>>();
List<String> row1 = new List<String>();
Expand Down
30 changes: 14 additions & 16 deletions lib/csv.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
library csv;

import 'dart:io';
import 'dart:async';

/**
* Parses the given `csvFile` and return a `Future<List<List<String>>>` that
* can be use conveniently to read the contents of the file.
*/
Future<List<List<String>>> parseCsvFile(File csvFile) {
Completer completer = new Completer();
InputStream inputStream = csvFile.openInputStream();
Completer<List<List<String>>> completer = new Completer<List<List<String>>>();
Future<RandomAccessFile> inputStream = csvFile.open();
String csvContent = '';

inputStream.onError = (e) => throw e;
inputStream.catchError((e) => throw e);

inputStream.onData = () {
List<int> bytes = inputStream.read();
inputStream.then((RandomAccessFile file) {
List<int> bytes = file.readSync(file.lengthSync());
String dataString = new String.fromCharCodes(bytes);
csvContent = '$csvContent$dataString';
};

inputStream.onClosed = () {
completer.complete(parseCsvContent(csvContent));
};
});

return completer.future;
}
Expand All @@ -37,7 +35,7 @@ List<List<String>> parseCsvContent(String csvFileContent) {
String column = '';
bool foundDoubleQuote = false;
List<String> columns = new List<String>();
List<String> chars = contentSplitted.trim().splitChars();
List<String> chars = contentSplitted.trim().split('');
for (String c in chars) {
if (c != ',' && c != '"') {
column = '$column$c';
Expand Down Expand Up @@ -73,24 +71,24 @@ List<List<String>> parseCsvContent(String csvFileContent) {
* Writes the `csvFileContent` to `csvFile`.
*/
void writeCsvFile(File csvFile, List<List<String>> 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<String> 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();
Expand Down
5 changes: 5 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
packages: {}
sdks:
dart: any
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: csv
description: CSV file parser for Dart.

dependencies:
unittest: { sdk: unittest }
# dependencies:
# unittest: any