From eccbb6b9f55d27bbf99daf48da0d0e1301210208 Mon Sep 17 00:00:00 2001 From: otya128 Date: Tue, 27 Dec 2016 14:38:07 +0900 Subject: [PATCH] Implement SAVE --- SMILEBASIC/builtinfunctions.d | 25 ++++++++++++++ SMILEBASIC/data.d | 7 ++-- SMILEBASIC/project.d | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index 311f913..53d7107 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -2285,6 +2285,31 @@ class BuiltinFunction flag.setDefaultValue(0); throw new IllegalFunctionCall("NOTIMPL:LOAD"); } + static void SAVE(PetitComputer p, wstring name, Value data) + { + auto file = Projects.parseFileName(name); + if (file.resource != Resource.data && file.resource != Resource.text) + { + throw new IllegalFunctionCall("SAVE"/*, 1*/); + } + if (file.resource == Resource.text && data.isString) + { + p.project.saveTextFile(file, data.castDString); + } + else if (file.resource == Resource.data && data.isNumberArray) + { + if (data.type == ValueType.IntegerArray) + p.project.saveDataFile(file, data.integerArray); + else if (data.type == ValueType.DoubleArray) + p.project.saveDataFile(file, data.doubleArray); + else + throw new TypeMismatch("SAVE", 2); + } + else + { + throw new TypeMismatch("SAVE", 2); + } + } static void PROJECT(PetitComputer p, wstring name) { //DirectMode only diff --git a/SMILEBASIC/data.d b/SMILEBASIC/data.d index 0ad3676..0c539ef 100644 --- a/SMILEBASIC/data.d +++ b/SMILEBASIC/data.d @@ -9,11 +9,8 @@ enum DataType : byte align(1) struct DataHeader { - char[8] magic;//PCBN0001 + char[8] magic = "PCBN0001";//PCBN0001 DataType type; byte dimension; - int dim1; - int dim2; - int dim3; - int dim4; + int[4] dim; } diff --git a/SMILEBASIC/project.d b/SMILEBASIC/project.d index fcda457..03b6260 100644 --- a/SMILEBASIC/project.d +++ b/SMILEBASIC/project.d @@ -1,5 +1,7 @@ module otya.smilebasic.project; import otya.smilebasic.error; +import otya.smilebasic.data; +import otya.smilebasic.type; import std.path; import std.file; import std.string; @@ -22,6 +24,13 @@ class Projects wstring projectPath; string projectPathU8; DialogResult result = DialogResult.FAILURE;//Default value + wstring saveConfirm = +r"ファイルを書き込みます。 + + 名前: %s + +よろしいですか? +"; this(wstring root) { rootPath = root; @@ -169,6 +178,60 @@ class Projects return true; } + void saveTextFile(FileName file, wstring content) + { + wstring project; + result = DialogResult.FAILURE; + if (file.project.empty) + project = convertProjectName(currentProject); + else + { + project = file.project; + if (!isValidProjectName(project)) + throw new IllegalFunctionCall("SAVE"); + } + if (!isValidFilename(file.name)) + throw new IllegalFunctionCall("SAVE"); + auto path = buildPath(projectPath, project, "TXT"w, file.name).to!string; + write(path, content); + result = DialogResult.SUCCESS; + } + + void saveDataFile(T)(FileName file, Array!T content) + if (is(T == int) || is(T == double)) + { + wstring project; + result = DialogResult.FAILURE; + if (file.project.empty) + project = convertProjectName(currentProject); + else + { + project = file.project; + if (!isValidProjectName(project)) + throw new IllegalFunctionCall("SAVE"); + } + if (!isValidFilename(file.name)) + throw new IllegalFunctionCall("SAVE"); + auto path = buildPath(projectPath, project, "DAT"w, file.name).to!string; + DataHeader[1] harray; + DataHeader* head = harray.ptr; + static if (is(T == int)) + { + head.type = DataType.int_; + } + else + { + head.type = DataType.double_; + } + head.dim[] = content.dim[]; + head.dimension = cast(byte)content.dimCount; + import std.stdio; + File f = File(path, "wb"); + f.rawWrite(harray); + f.rawWrite(content.array); + result = DialogResult.SUCCESS; + } + wstring convertProjectName(wstring proj) { if (proj.empty)