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 __dirpath of" + __dirpath);
print("script has __filepath of" + __filepath); print("script has __filepath of" + __filepath);
if (storage.exists(__dirpath + "/math.js")) { if (storage.fileExists(__dirpath + "/math.js")) {
print("math.js exist here."); print("math.js exist here.");
} else { } else {
print("math.js does not exist here."); print("math.js does not exist here.");

View File

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

View File

@ -56,6 +56,15 @@ declare const __filepath: string;
*/ */
declare function load(path: string): any; 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 * @brief mJS Foreign Pointer type
* *
@ -189,6 +198,18 @@ declare class String {
* See `charCodeAt` * See `charCodeAt`
*/ */
at(index: number): number; 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 { } declare class Boolean { }