Skip to content

Commit 1d15be8

Browse files
authored
Merge pull request #135 from mpsonntag/file
Add missing file functions
2 parents 67cb2d6 + 117587e commit 1d15be8

15 files changed

+336
-224
lines changed

+nix/File.m

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,45 @@
2525
% assign relations
2626
nix.Dynamic.add_dyn_relation(obj, 'blocks', @nix.Block);
2727
nix.Dynamic.add_dyn_relation(obj, 'sections', @nix.Section);
28-
28+
2929
obj.info = nix_mx('File::describe', obj.nix_handle);
3030
end
31-
31+
32+
% braindead...
33+
function check = is_open(obj)
34+
check = nix_mx('File::isOpen', obj.nix_handle);
35+
end
36+
37+
function mode = file_mode(obj)
38+
mode = nix_mx('File::fileMode', obj.nix_handle);
39+
end
40+
41+
function res = validate(obj)
42+
res = nix_mx('File::validate', obj.nix_handle);
43+
end
44+
3245
% ----------------
3346
% Block methods
3447
% ----------------
3548

36-
function newBlock = createBlock(obj, name, type)
49+
function newBlock = create_block(obj, name, type)
3750
newBlock = nix.Block(nix_mx('File::createBlock', obj.nix_handle, name, type));
3851
end;
3952

40-
function c = blockCount(obj)
53+
function c = block_count(obj)
4154
c = nix_mx('File::blockCount', obj.nix_handle);
4255
end
4356

44-
function hasBlock = hasBlock(obj, id_or_name)
57+
function hasBlock = has_block(obj, id_or_name)
4558
hasBlock = nix_mx('File::hasBlock', obj.nix_handle, id_or_name);
4659
end;
4760

48-
function retObj = openBlock(obj, id_or_name)
61+
function retObj = open_block(obj, id_or_name)
4962
retObj = nix.Utils.open_entity(obj, ...
5063
'File::openBlock', id_or_name, @nix.Block);
5164
end
5265

53-
function delCheck = deleteBlock(obj, del)
66+
function delCheck = delete_block(obj, del)
5467
delCheck = nix.Utils.delete_entity(obj, ...
5568
del, 'nix.Block', 'File::deleteBlock');
5669
end;
@@ -59,24 +72,24 @@
5972
% Section methods
6073
% ----------------
6174

62-
function newSec = createSection(obj, name, type)
75+
function newSec = create_section(obj, name, type)
6376
newSec = nix.Section(nix_mx('File::createSection', obj.nix_handle, name, type));
6477
end;
6578

66-
function c = sectionCount(obj)
79+
function c = section_count(obj)
6780
c = nix_mx('File::sectionCount', obj.nix_handle);
6881
end
6982

70-
function hasSec = hasSection(obj, id_or_name)
83+
function hasSec = has_section(obj, id_or_name)
7184
hasSec = nix_mx('File::hasSection', obj.nix_handle, id_or_name);
7285
end;
7386

74-
function retObj = openSection(obj, id_or_name)
87+
function retObj = open_section(obj, id_or_name)
7588
retObj = nix.Utils.open_entity(obj, ...
7689
'File::openSection', id_or_name, @nix.Section);
7790
end
7891

79-
function delCheck = deleteSection(obj, del)
92+
function delCheck = delete_section(obj, del)
8093
delCheck = nix.Utils.delete_entity(obj, del, 'nix.Section', 'File::deleteSection');
8194
end;
8295
end

nix_mx.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ void mexFunction(int nlhs,
115115
.reg("createBlock", &nix::File::createBlock)
116116
.reg("createSection", &nix::File::createSection)
117117
.reg("blockCount", GETTER(nix::ndsize_t, nix::File, blockCount))
118-
.reg("sectionCount", GETTER(nix::ndsize_t, nix::File, sectionCount));
118+
.reg("sectionCount", GETTER(nix::ndsize_t, nix::File, sectionCount))
119+
.reg("isOpen", GETTER(bool, nix::File, isOpen));
120+
methods->add("File::fileMode", nixfile::fileMode);
121+
methods->add("File::validate", nixfile::validate);
119122

120123
classdef<nix::Block>("Block", methods)
121124
.desc(&nixblock::describe)

src/nixfile.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,31 @@
1111
#include "mex.h"
1212

1313
#include <nix.hpp>
14+
#include <nix/valid/validate.hpp>
1415

1516
#include "handle.h"
1617
#include "arguments.h"
1718
#include "struct.h"
1819

20+
// helper function
21+
mxArray *message(std::vector<nix::valid::Message> mes) {
22+
const mwSize size = static_cast<mwSize>(mes.size());
23+
mxArray *list = mxCreateCellArray(1, &size);
24+
25+
for (size_t i = 0; i < mes.size(); i++) {
26+
27+
nix::valid::Message curr = mes[i];
28+
29+
struct_builder msb({ 1 }, { "id", "msg" });
30+
msb.set(curr.id);
31+
msb.set(curr.msg);
32+
33+
mxSetCell(list, i, msb.array());
34+
}
35+
36+
return list;
37+
}
38+
1939
namespace nixfile {
2040

2141
void open(const extractor &input, infusor &output) {
@@ -36,6 +56,41 @@ namespace nixfile {
3656
output.set(0, h);
3757
}
3858

59+
void fileMode(const extractor &input, infusor &output) {
60+
nix::File currObj = input.entity<nix::File>(1);
61+
nix::FileMode mode = currObj.fileMode();
62+
uint8_t omode;
63+
64+
switch (mode) {
65+
case nix::FileMode::ReadOnly: omode = 0; break;
66+
case nix::FileMode::ReadWrite: omode = 1; break;
67+
case nix::FileMode::Overwrite: omode = 2; break;
68+
default: throw std::invalid_argument("unknown open mode");
69+
}
70+
71+
output.set(0, omode);
72+
}
73+
74+
void validate(const extractor &input, infusor &output) {
75+
nix::File f = input.entity<nix::File>(1);
76+
nix::valid::Result res = f.validate();
77+
78+
std::vector<nix::valid::Message> err = res.getErrors();
79+
mxArray *err_list = message(err);
80+
81+
std::vector<nix::valid::Message> warn = res.getWarnings();
82+
mxArray *warn_list = message(warn);
83+
84+
struct_builder sb({ 1 }, { "ok", "hasErrors", "hasWarnings", "errors", "warnings" });
85+
sb.set(res.ok());
86+
sb.set(res.hasErrors());
87+
sb.set(res.hasWarnings());
88+
sb.set(err_list);
89+
sb.set(warn_list);
90+
91+
output.set(0, sb.array());
92+
}
93+
3994
mxArray *describe(const nix::File &fd) {
4095
struct_builder sb({ 1 }, { "format", "version", "location", "createdAt", "updatedAt" });
4196
sb.set(fd.format());

src/nixfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ namespace nixfile {
1515

1616
void open(const extractor &input, infusor &output);
1717

18+
void fileMode(const extractor &input, infusor &output);
19+
20+
void validate(const extractor &input, infusor &output);
21+
1822
mxArray *describe(const nix::File &f);
1923

2024
} // namespace nixfile

0 commit comments

Comments
 (0)