Implement LOAD"DAT:FILE"
This commit is contained in:
@@ -2739,54 +2739,84 @@ class BuiltinFunction
|
|||||||
}
|
}
|
||||||
throw new IllegalFunctionCall("LOAD");
|
throw new IllegalFunctionCall("LOAD");
|
||||||
}
|
}
|
||||||
@BasicName("LOAD")
|
//LOAD"TXT:HOGE",ARRAY Type mismatch(LOAD:2)
|
||||||
static void LOAD2(PetitComputer p, wstring name, DefaultValue!(int, false) flag)
|
//LOAD"TXT:HOGE",STR$ Type mismatch(LOAD:2)
|
||||||
|
//LOAD"DAT:HOGE",STR$ Type mismatch(LOAD:2)
|
||||||
|
static void LOAD(PetitComputer p, wstring name)
|
||||||
{
|
{
|
||||||
import otya.smilebasic.project;
|
LOAD(p, name, Value(0));
|
||||||
flag.setDefaultValue(0);
|
}
|
||||||
auto type = Projects.splitResourceName(name);
|
static void LOAD(PetitComputer p, wstring name, Value data)
|
||||||
wstring txt;
|
{
|
||||||
wstring resname = type[0].toUpper;
|
void LOAD2(PetitComputer p, wstring name, int flag)
|
||||||
wstring projectname = type[1];
|
|
||||||
wstring filename = type[2];
|
|
||||||
if (!Projects.isValidFileName(filename))
|
|
||||||
{
|
{
|
||||||
throw new IllegalFunctionCall("LOAD");
|
import otya.smilebasic.project;
|
||||||
}
|
auto type = Projects.splitResourceName(name);
|
||||||
if(projectname != "" && projectname.toUpper != "SYS")
|
wstring txt;
|
||||||
{
|
wstring resname = type[0].toUpper;
|
||||||
throw new IllegalFunctionCall("LOAD");
|
wstring projectname = type[1];
|
||||||
}
|
wstring filename = type[2];
|
||||||
if(projectname == "")
|
if (!Projects.isValidFileName(filename))
|
||||||
{
|
|
||||||
projectname = p.project.currentProject;
|
|
||||||
}
|
|
||||||
if(resname == "" || resname.indexOf("PRG") == 0)
|
|
||||||
{
|
|
||||||
int lot = 0/*always 0...?*/;
|
|
||||||
if(resname != "" && resname != "PRG")
|
|
||||||
{
|
{
|
||||||
auto num = resname[3..$];
|
throw new IllegalFunctionCall("LOAD");
|
||||||
lot = num.to!int;
|
|
||||||
}
|
}
|
||||||
if(!p.project.loadFile(projectname, "TXT", filename, txt))
|
if(projectname != "" && projectname.toUpper != "SYS")
|
||||||
{
|
{
|
||||||
//not exist
|
throw new IllegalFunctionCall("LOAD");
|
||||||
|
}
|
||||||
|
if(projectname == "")
|
||||||
|
{
|
||||||
|
projectname = p.project.currentProject;
|
||||||
|
}
|
||||||
|
if(resname == "" || resname.indexOf("PRG") == 0)
|
||||||
|
{
|
||||||
|
int lot = 0/*always 0...?*/;
|
||||||
|
if(resname != "" && resname != "PRG")
|
||||||
|
{
|
||||||
|
auto num = resname[3..$];
|
||||||
|
lot = num.to!int;
|
||||||
|
}
|
||||||
|
if(!p.project.loadFile(projectname, "TXT", filename, txt))
|
||||||
|
{
|
||||||
|
//not exist
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
p.program.slot[lot].load(filename, txt);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
p.program.slot[lot].load(filename, txt);
|
if(resname.indexOf("GRP") == 0)
|
||||||
|
{
|
||||||
|
throw new IllegalFunctionCall("NOTIMPL:LOAD GRP");
|
||||||
|
}
|
||||||
|
throw new IllegalFunctionCall("LOAD");
|
||||||
|
}
|
||||||
|
if (data.isNumber)
|
||||||
|
{
|
||||||
|
LOAD2(p, name, data.castInteger);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(resname.indexOf("GRP") == 0)
|
LOAD(p, name, data, 0);
|
||||||
{
|
|
||||||
throw new IllegalFunctionCall("NOTIMPL:LOAD GRP");
|
|
||||||
}
|
|
||||||
throw new IllegalFunctionCall("LOAD");
|
|
||||||
}
|
}
|
||||||
static void LOAD(wstring name, Value arr, DefaultValue!(int, false) flag)
|
static void LOAD(PetitComputer p, wstring name, Value data, int flag)
|
||||||
{
|
{
|
||||||
flag.setDefaultValue(0);
|
auto file = Projects.parseFileName(name);
|
||||||
throw new IllegalFunctionCall("NOTIMPL:LOAD");
|
if (file.resource != Resource.data)
|
||||||
|
{
|
||||||
|
throw new IllegalFunctionCall("LOAD", 2);
|
||||||
|
}
|
||||||
|
if (data.isNumberArray)
|
||||||
|
{
|
||||||
|
if (data.type == ValueType.IntegerArray)
|
||||||
|
p.project.loadDataFile(file, data.integerArray);
|
||||||
|
else if (data.type == ValueType.DoubleArray)
|
||||||
|
p.project.loadDataFile(file, data.doubleArray);
|
||||||
|
else
|
||||||
|
throw new TypeMismatch("LOAD", 2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new TypeMismatch("LOAD", 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
static void SAVE(PetitComputer p, wstring name)
|
static void SAVE(PetitComputer p, wstring name)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -178,6 +178,76 @@ r"ファイルを書き込みます。
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool loadDataFile(T)(FileName file, ref Array!T contents)
|
||||||
|
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("LOAD");
|
||||||
|
}
|
||||||
|
if (!isValidFilename(file.name))
|
||||||
|
throw new IllegalFunctionCall("LOAD");
|
||||||
|
auto path = buildPath(projectPath, project, "DAT"w, file.name).to!string;
|
||||||
|
|
||||||
|
//show dialog
|
||||||
|
result = DialogResult.SUCCESS;
|
||||||
|
import std.stdio;
|
||||||
|
import std.algorithm;
|
||||||
|
File f = File(path, "rb");
|
||||||
|
DataHeader[1] harray;
|
||||||
|
f.rawRead(harray);
|
||||||
|
if (harray[0].dimension != contents.dimCount)
|
||||||
|
{
|
||||||
|
throw new TypeMismatch("LOAD");
|
||||||
|
}
|
||||||
|
auto dim = harray[0].dim[0..harray[0].dimension];
|
||||||
|
auto len = dim.reduce!((x, y) => x * y);
|
||||||
|
T[] result;
|
||||||
|
if (harray[0].type == DataType.int_)
|
||||||
|
{
|
||||||
|
int[] array = new int[len];
|
||||||
|
f.rawRead(array);
|
||||||
|
static if (!is(T == int))
|
||||||
|
{
|
||||||
|
result = array.map!(x => x.to!T).array;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (harray[0].type == DataType.double_)
|
||||||
|
{
|
||||||
|
double[] array = new double[len];
|
||||||
|
f.rawRead(array);
|
||||||
|
static if (!is(T == double))
|
||||||
|
{
|
||||||
|
result = array.map!(x => x.to!T).array;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (harray[0].type == DataType.ushort_)
|
||||||
|
{
|
||||||
|
ushort[] array = new ushort[len];
|
||||||
|
f.rawRead(array);
|
||||||
|
result = array.map!(x => x.to!T).array;
|
||||||
|
}
|
||||||
|
//配列に書き込むというより配列を差し替えている
|
||||||
|
contents.array = result;
|
||||||
|
contents.dim[] = harray[0].dim[];
|
||||||
|
contents.dimCount = harray[0].dimension;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void saveTextFile(FileName file, wstring content)
|
void saveTextFile(FileName file, wstring content)
|
||||||
{
|
{
|
||||||
wstring project;
|
wstring project;
|
||||||
@@ -236,6 +306,8 @@ r"ファイルを書き込みます。
|
|||||||
{
|
{
|
||||||
if (proj.empty)
|
if (proj.empty)
|
||||||
{
|
{
|
||||||
|
if (currentProject.empty)
|
||||||
|
return "[DEFAULT]";
|
||||||
return currentProject;
|
return currentProject;
|
||||||
}
|
}
|
||||||
return proj;
|
return proj;
|
||||||
|
|||||||
Reference in New Issue
Block a user