MX 2024-10-16 15:09:51 +03:00
parent f37799e0f4
commit cd46d8ca07
No known key found for this signature in database
GPG key ID: 7CCC66B7DBDD1C83
4 changed files with 36 additions and 29 deletions

View file

@ -2,7 +2,7 @@ let storage = require("storage");
print("script has __dirpath of" + __dirpath);
print("script has __filepath of" + __filepath);
if (storage.exists(__dirpath + "/math.js")) {
if (storage.fileExists(__dirpath + "/math.js")) {
print("math.js exist here.");
} else {
print("math.js does not exist here.");

View file

@ -1,43 +1,29 @@
let storage = require("storage");
let path = "/ext/storage.test";
function arraybuf_to_string(arraybuf) {
let string = "";
let data_view = Uint8Array(arraybuf);
for (let i = 0; i < data_view.length; i++) {
string += chr(data_view[i]);
}
return string;
}
print("File exists:", storage.exists(path));
print("File exists:", storage.fileExists(path));
print("Writing...");
// write(path, data, offset)
// If offset is specified, the file is not cleared, content is kept and data is written at specified offset
// Takes both strings and array buffers
storage.write(path, "Hello ");
let file = storage.openFile(path, "w", "create_always");
file.write("Hello ");
file.close();
print("File exists:", storage.exists(path));
print("File exists:", storage.fileExists(path));
// Append will create the file even if it doesnt exist!
// Takes both strings and array buffers
storage.append(path, "World!");
file = storage.openFile(path, "w", "open_append");
file.write("World!");
file.close();
print("Reading...");
// read(path, size, offset)
// If no size specified, total filesize is used
// If offset is specified, size is capped at (filesize - offset)
let data = storage.read(path);
// read returns an array buffer, to allow proper usage of raw binary data
print(arraybuf_to_string(data));
file = storage.openFile(path, "r", "open_existing");
let text = file.read("ascii", 128);
file.close();
print(text);
print("Removing...")
storage.remove(path);
print("Done")
// There's also:
// storage.copy(old_path, new_path);
// storage.move(old_path, new_path);
// storage.mkdir(path);
// You don't need to close the file after each operation, this is just to show some different ways to use
// There's also many more functions and options, check types docs in firmware repo

View file

@ -56,6 +56,15 @@ declare const __filepath: string;
*/
declare function load(path: string): any;
/**
* @brief Return 1-byte string whose ASCII code is the integer `n`
*
* If `n` is not numeric or outside of `0-255` range, `null` is returned
*
* @param n The ASCII code to convert to string
*/
declare function chr(n: number): string | null;
/**
* @brief mJS Foreign Pointer type
*
@ -189,6 +198,18 @@ declare class String {
* See `charCodeAt`
*/
at(index: number): number;
/**
* @brief Return index of first occurence of substr within the string or `-1` if not found
* @param substr The string to search for
* @param fromIndex The index to start searching from
*/
indexOf(substr: string, fromIndex?: number): number;
/**
* @brief Return a substring between two indices
* @param start The index to start substring at
* @param end The index to end substring at
*/
slice(start: number, end?: number): string;
}
declare class Boolean { }