Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Upgrade #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
70 changes: 27 additions & 43 deletions files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,6 @@ void initFileSystem()
}
}

/**
* Appends text and a new line to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="files_append_line" block="file %filename|append line %text"
//% blockExternalInputs=1 weight=90 blockGap=8
void appendLine(StringData *filename, StringData *text)
{
initFileSystem();
ManagedString fn(filename);
ManagedString t(text);
MicroBitFile f(fn);
f.append(t);
f.append("\r\n");
f.close();
}

/**
* Appends text to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="fs_append_string" block="file %filename|append string %text"
//% blockExternalInputs=1 weight=86 blockGap=8
void appendString(StringData *filename, StringData *text)
{
initFileSystem();
ManagedString fn(filename);
ManagedString t(text);
MicroBitFile f(fn);
f.append(t);
f.close();
}

/**
* Reads the content of the file to send it to serial
* @param filename file name, eg: "output.txt"
Expand Down Expand Up @@ -223,16 +188,35 @@ Buffer fsReadBuffer(int fd, int length) {
return mkBuffer(NULL, 0);

initFileSystem();
Buffer buf = mkBuffer(NULL, length);

// compute position, length
int pos = MicroBitFileSystem::defaultFileSystem->seek(fd, 0, FileSystemSeekFlags::Current);
int end = MicroBitFileSystem::defaultFileSystem->seek(fd, 0, FileSystemSeekFlags::End);
MicroBitFileSystem::defaultFileSystem->seek(fd, pos, FileSystemSeekFlags::Set);
// cap length
length = min(length, end - pos);
auto buf = mkBuffer(NULL, length);
int ret = MicroBitFileSystem::defaultFileSystem->read(fd, PXT_BUFFER_DATA(buf), buf->length);

if (ret < 0) return mkBuffer(NULL, 0);
else if (ret != length) {
auto sbuf = mkBuffer(PXT_BUFFER_DATA(buf), ret);
decrRC(buf);
return sbuf;
}
else return buf;
}

/**
*/
//% weight=0 advanced=true
String fsReadString(int fd, int length) {
if (fd < 0 || length < 0)
return mkString(NULL, 0);

initFileSystem();
// compute position, length
int pos = MicroBitFileSystem::defaultFileSystem->seek(fd, 0, FileSystemSeekFlags::Current);
int end = MicroBitFileSystem::defaultFileSystem->seek(fd, 0, FileSystemSeekFlags::End);
MicroBitFileSystem::defaultFileSystem->seek(fd, pos, FileSystemSeekFlags::Set);
// cap length
length = min(length, end - pos);
auto buf = mkString(NULL, length);
int ret = MicroBitFileSystem::defaultFileSystem->read(fd, (uint8_t*)buf->data, buf->length);
if (ret < 0) return mkString(NULL, 0);
else return buf;
}

Expand Down
127 changes: 122 additions & 5 deletions files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@
*/
//% weight=5 color=#002050 icon="\uf0a0"
namespace files {
export let NEW_LINE = "\r\n";
export let TAB = "\t";

/**
* Appends text and a new line to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="files_append_line" block="file %filename|append line %text"
//% blockExternalInputs=1 weight=90 blockGap=8
export function appendLine(filename: string, text: string): void {
const file = open(filename);
file.seek(0, FileSystemSeekFlags.End);
file.writeString(text);
file.writeString(NEW_LINE);
file.close();
}

/**
* Appends text to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="fs_append_string" block="file %filename|append string %text"
//% blockExternalInputs=1 weight=86 blockGap=8
export function appendString(filename: string, text: string): void {
const file = open(filename);
file.seek(0, FileSystemSeekFlags.End);
file.writeString(text);
file.close();
}

/**
* Appends a number to a file
* @param filename file name, eg: "output.txt"
Expand All @@ -11,7 +43,24 @@ namespace files {
//% blockId="fs_append_number" block="file %filename|append number %value"
//% blockExternalInputs=1 weight=85
export function appendNumber(filename: string, value: number) {
files.appendString(filename, value.toString());
const file = open(filename);
file.seek(0, FileSystemSeekFlags.End);
file.writeString(value.toString());
file.close();
}

/**
* Appends a buffer to the end of the file
* @param filename
* @param buffer
*/
//% blockId="fs_append_buffer" block="file %filename|append buffer $buffer"
//% weight=84
export function appendBuffer(filename: string, buffer: Buffer) {
const file = open(filename);
file.seek(0, FileSystemSeekFlags.End);
file.writeBuffer(buffer);
file.close();
}

/**
Expand Down Expand Up @@ -110,7 +159,7 @@ namespace files {
*/
//% blockGap=8
//% blockId=fs_file_position block="%this|position" advanced=true
public position(): number {
public get position(): number {
return files.fsSeek(this.fd, 0, FileSystemSeekFlags.Current);
}

Expand All @@ -120,10 +169,22 @@ namespace files {
*/
//% blockGap=8 advanced=true
//% blockId=fs_file_set_position block="%this|set position %position"
public setPosition(position: number): void {
public set position(position: number) {
files.fsSeek(this.fd, position, FileSystemSeekFlags.Set);
}

/**
* Returns the file length in bytes
*/
//% blockGap=8 advanced=true
//% blockId=fs_file_length block="%this|length"
public get length(): number {
files.fsSeek(this.fd, 0, FileSystemSeekFlags.End);
const l = files.fsSeek(this.fd, 0, FileSystemSeekFlags.Current);
files.fsSeek(this.fd, 0, FileSystemSeekFlags.Set);
return l;
}

/**
* Write a string to the file.
*/
Expand All @@ -144,14 +205,34 @@ namespace files {

/**
* Reads the file at the current position and fills a buffer
* @param length maximum number of bytes to read, eg: 64
* @param length maximum number of bytes to read, if negative reads the entire file eg: 64
*/
//% blockGap=8
//% blockId=fs_file_read_buffer block="%this|read buffer (bytes) %length" advanced=true
//% blockId=fs_file_read_buffer block="%this|read buffer $length (bytes)" advanced=true
public readBuffer(length: number): Buffer {
const p = this.position;
const l = this.length;
if (length < 0)
length = l;
length = Math.min(length, l - p);
return files.fsReadBuffer(this.fd, length);
}

/**
* Reads the file at the current position and a string.
* @param length maximum number of bytes to read, if negative reads the entire file eg: 64
*/
//% blockGap=8
//% blockId=fs_file_read_string block="%this|read string $length (chars)" advanced=true
public readString(length: number): String {
const p = this.position;
const l = this.length;
if (length < 0)
length = l;
length = Math.min(length, l - p);
return files.fsReadString(this.fd, length);
}

/**
* Reads the next character in the file at the current position
*/
Expand All @@ -160,5 +241,41 @@ namespace files {
public read(): number {
return files.fsRead(this.fd);
}

/**
* Reads the next line in the file at the current position
*/
//% blockGap=8
//% blockId=fs_file_read_line block="%this|read line" advanced=true
public readLine(): string {
const nl = NEW_LINE;
const length = this.length;
const start = this.position;
// scan until new line or eol
let end = start;
while (end < length) {
let c = this.read();
if (c == 13 /* \r */ && this.position < length) {
// try scanning for next character
c = this.read();
}
if (c == 10 /* \n */) {
// found a new line
break;
}
// update end position
end = this.position;
}

// end of file shortcut
if (start == end) return "";

// remember current position
const pos = this.position;
this.position = start; // reset current cursor
const line = fsReadString(this.fd, end - start); // read line
this.position = pos; // skip new line
return line;
}
}
}
23 changes: 5 additions & 18 deletions shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@
//% weight=5 color=#002050 icon="\uf0a0"
declare namespace files {

/**
* Appends text and a new line to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="files_append_line" block="file %filename|append line %text"
//% blockExternalInputs=1 weight=90 blockGap=8 shim=files::appendLine
function appendLine(filename: string, text: string): void;

/**
* Appends text to a file
* @param filename file name, eg: "output.txt"
* @param text the string to append to the end of the file
*/
//% blockId="fs_append_string" block="file %filename|append string %text"
//% blockExternalInputs=1 weight=86 blockGap=8 shim=files::appendString
function appendString(filename: string, text: string): void;

/**
* Reads the content of the file to send it to serial
* @param filename file name, eg: "output.txt"
Expand Down Expand Up @@ -104,6 +86,11 @@ declare namespace files {
//% weight=0 advanced=true shim=files::fsReadBuffer
function fsReadBuffer(fd: int32, length: int32): Buffer;

/**
*/
//% weight=0 advanced=true shim=files::fsReadString
function fsReadString(fd: int32, length: int32): string;

/**
*
*/
Expand Down
Loading