From c7f9bfbd61bece6938c47189f148675a3a289ed9 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Sun, 17 May 2020 22:25:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20io=20=E4=B8=8E=20path=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tiny/io.ts | 169 +++++++++++++++++++++++++++++++++++++++++++++++ src/tiny/path.ts | 54 +++++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 src/tiny/io.ts create mode 100644 src/tiny/path.ts diff --git a/src/tiny/io.ts b/src/tiny/io.ts new file mode 100644 index 0000000..aafa10c --- /dev/null +++ b/src/tiny/io.ts @@ -0,0 +1,169 @@ +export enum AccessType { + ACCESS_RESOURCES, + ACCESS_USERDATA, + ACCESS_FILESYSTEM, + ACCESS_MAX +}; + +export enum ModeFlags { + READ = 1, + WRITE = 2, + READ_WRITE = 3, + WRITE_READ = 7, +}; + +export class FileAccess { + + protected path: string; + + /** close a file */ + close(): void {}; + /** true when file is open */ + is_open(): boolean { return false; } + + /** returns the path for the current open file */ + get_path(): string { return ""; } + /** returns the absolute path for the current open file */ + get_path_absolute(): string { return ""; } + + /** seek to a given position */ + seek(p_position: number) {} + /** seek from the end of file */ + seek_end(p_position: number) {} + + /* get position in the file */ + get_position(): number { return -1; } + /* get size of the file */ + get_len(): number { return -1; } + + /** reading passed EOF */ + eof_reached(): boolean { return false; } + + + get_as_utf8_string(): string { return ""; } + save_as_utf8_string(text: string) {} + + get_as_array(): Uint8Array { return null; } + save_as_array(buffer: Uint8Array) {} + + store_string(text: string) {} + + static create(path: string): FileAccess { + return null; + } + + static open(path: string, mode_flags: ModeFlags): FileAccess { + switch (get_runtime()) { + case JavaScriptRuntime.NodeJS: + return NodeJSFileAccess.open(path, mode_flags); + default: + return null; + } + } + + static exists(path: string): boolean { + switch (get_runtime()) { + case JavaScriptRuntime.NodeJS: + return NodeJSFileAccess.exists(path); + default: + return false; + } + } +} + +export class DirAccess { + static exists(path: string): boolean { + switch (get_runtime()) { + case JavaScriptRuntime.NodeJS: + return NodeJSFileAccess.exists(path); + default: + return false; + } + } + + static make_dir(p_dir: string, recursive: boolean = false) { + switch (get_runtime()) { + case JavaScriptRuntime.NodeJS: + return NodeJSDirAccess.make_dir(p_dir, recursive); + default: + return false; + } + }; +} + +import * as fs from "fs"; +import {O_RDONLY, O_WRONLY, O_RDWR, O_CREAT } from 'constants'; +import { get_runtime, JavaScriptRuntime } from "./env"; + +class NodeJSFileAccess extends FileAccess { + + protected fd: number = -1; + protected pos: number = 0; + + constructor(path, flags: number) { + super(); + this.fd = fs.openSync(path, flags); + this.path = path; + } + + close(): void { + fs.closeSync(this.fd); + }; + + static exists(path: string): boolean { + return fs.existsSync(path); + } + + static open(path: string, mode_flags: ModeFlags): NodeJSFileAccess { + let flags = 0; + switch (mode_flags) { + case ModeFlags.READ: + flags = O_RDONLY; + break; + case ModeFlags.WRITE: + flags = O_CREAT | O_WRONLY; + break; + case ModeFlags.READ_WRITE: + flags = O_CREAT | O_RDWR; + break; + case ModeFlags.WRITE_READ: + flags = O_CREAT | O_RDWR; + break; + default: + break; + } + return new NodeJSFileAccess(path, flags); + } + + get_len(): number { + return fs.statSync(this.path).size; + } + + get_as_utf8_string(): string { + return fs.readFileSync(this.path, {encoding: 'utf-8'}); + } + + save_as_utf8_string(text: string) { + return fs.writeFileSync(this.path, text, {encoding: 'utf-8'}); + } + + get_as_array(): Uint8Array { + let buff = fs.readFileSync(this.path); + return buff; + } + + save_as_array(buffer: Uint8Array) { + fs.writeFileSync(this.path, buffer); + } + +} + +export class NodeJSDirAccess extends DirAccess { + static exists(path: string): boolean { + return fs.statSync(path).isDirectory(); + } + + static make_dir(p_dir: string, recursive: boolean = false) { + fs.mkdirSync(p_dir, {recursive: true}); + } +} \ No newline at end of file diff --git a/src/tiny/path.ts b/src/tiny/path.ts new file mode 100644 index 0000000..347f4f5 --- /dev/null +++ b/src/tiny/path.ts @@ -0,0 +1,54 @@ +/** + * 对文件路径的一些操作,针对的是 C:/A/B/C/D/example.ts这种格式 + */ +export const path = { + + /** + * 格式化文件路径,"C:/A/B//C//D//example.ts"=>"C:/A/B/C/D/example.ts" + * @param filename 传入的文件路径 + */ + normalize: (filename: string): string => { + const arr = filename.split("/"); + return arr.filter((value, index) => !!value || index == arr.length - 1).join("/"); + }, + + /** + * 根据文件路径得到文件名字,"C:/A/B/example.ts"=>"example.ts" + * @param filename 传入的文件路径 + * @return 文件的名字 + */ + basename: (filename: string): string => { + return filename.substr(filename.lastIndexOf("/") + 1); + }, + + /** + * 文件所在文件夹路径,"C:/A/B/example.ts"=>"C:/A/B" + * @param path 传入的文件路径 + * @return 文件所在文件夹的地址 + */ + dirname: (path: string): string => { + return path.substr(0, path.lastIndexOf("/")); + }, + + /** + * 获得文件拓展名 " text.txt" => "txt" + * @param path 传入的文件路径 + */ + extension: (filename: string): string => { + let pos = filename.lastIndexOf('.'); + if (pos >= 0 && pos < filename.length - 1) { + return filename.substring(pos + 1, filename.length); + } + return ""; + }, + + join(dir: string, ...pathes: string[]) { + let ret = dir; + for (const p of pathes) { + if (p) { + ret += '/' + p; + } + } + return ret; + } +} \ No newline at end of file