1
0

Implement SAVE

This commit is contained in:
otya128
2016-12-27 14:38:07 +09:00
parent e0ea45636a
commit eccbb6b9f5
3 changed files with 90 additions and 5 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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)