-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathToolboxStorage.m
More file actions
276 lines (253 loc) · 8.55 KB
/
ToolboxStorage.m
File metadata and controls
276 lines (253 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
classdef ToolboxStorage < handle
% Helps you easily store any data within installed toolbox, i.e. user settings
% By Pavel Roslovets, ETMC Exponenta
% https://github.com/ETMC-Exponenta/ToolboxExtender
properties
ext % Toolbox Extender
type {mustBeMember(type,{'mat','json','pref'})} = 'mat' % Storage type
root % data folder
fdir % folder directory in the root
fname % file name
data % storage data
local % save data file locally
auto % automatically save and load data
jsonopts % json files options
end
methods
function obj = ToolboxStorage(varargin)
%% Constructor
p = inputParser();
p.addParameter('ext', []);
p.addParameter('root', []);
p.addParameter('type', 'mat');
p.addParameter('fname', '', @(x)ischar(x)||isstring(x));
p.addParameter('fdir', '', @(x)ischar(x)||isstring(x));
p.addParameter('local', false);
p.addParameter('auto', false);
p.addParameter('jsonopts', struct, @isstruct);
p.parse(varargin{:});
args = p.Results;
if ~isempty(args.ext)
obj.ext = args.ext;
%else
% obj.ext = ToolboxExtender;
end
obj.type = args.type;
obj.local = args.local;
obj.auto = args.auto;
obj.getroot();
obj.fdir = args.fdir;
fname = string(args.fname);
if fname == ""
fname = matlab.lang.makeValidName(obj.ext.name);
end
obj.fname = fname;
obj.jsonopts = args.jsonopts;
end
function set.fname(obj, fname)
%% Set file name
obj.fname = fname;
if obj.auto
obj.load();
end
end
function set.fdir(obj, fdir)
%% Set file directory in the root
if obj.type == "mat" || obj.type == "json"
obj.fdir = fdir;
if obj.auto
obj.load();
end
end
end
function set.jsonopts(obj, opts)
%% Set json storage options
p = inputParser();
p.addParameter('useJsonlab', false);
p.addParameter('encoding', 'UTF-8', @(x)ischar(x)||isstring(x));
p.addParameter('readTable', true);
p.addParameter('writeArray', true);
opts = [fieldnames(opts) struct2cell(opts)]';
p.parse(opts{:});
obj.jsonopts = p.Results;
end
function [fpath, fname] = getpath(obj)
%% Generate data file name
switch obj.type
case 'mat'
ex = ".mat";
case 'json'
ex = ".json";
otherwise
ex = "";
end
fname = obj.fname + ex;
fpath = fullfile(obj.root, obj.fdir, fname);
end
function open(obj)
%% Open data folder
if ispc
winopen(obj.root);
else
unix("open " + obj.root);
end
end
function data = load(obj, fpath)
%% Load data from file or preferences
data = [];
switch obj.type
case "mat"
if nargin < 2
fpath = obj.getpath();
end
if isfile(fpath)
data = load(fpath);
end
case "pref"
data = getpref(obj.fname);
case "json"
data = obj.json_read(obj.getpath());
end
obj.data = data;
end
function save(obj, data, fpath)
%% Save data to file
if nargin < 2
data = obj.data;
else
obj.data = data;
end
switch obj.type
case "mat"
if nargin < 3
fpath = obj.getpath();
end
save(fpath, '-struct', 'data');
case "pref"
fs = string(fieldnames(data));
for i = 1 : length(fs)
setpref(obj.fname, fs(i), data.(fs(i)));
end
case "json"
obj.json_write(obj.getpath(), obj.data);
end
end
function [value, isf] = get(obj, varname, type, def)
%% Get variable from data
if isstruct(obj.data) && isfield(obj.data, varname)
value = obj.data.(varname);
isf = true;
else
value = [];
isf = false;
end
if nargin > 2 && ~isempty(type)
value = cast(value, type);
end
if nargin > 3 && isempty(value)
value = def;
end
end
function data = set(obj, varname, value, type)
%% Set variable in data
if nargin > 3
value = cast(value, type);
end
obj.data.(varname) = value;
if obj.auto
obj.save();
end
data = obj.data;
end
function data = import(obj, fpath)
%% Import data from file
obj.load(fpath);
if obj.auto
obj.save();
end
data = obj.data;
end
function export(obj, fpath)
%% Export data to file
obj.save(obj.data, fpath);
end
function clear(obj)
%% Clear data and delete data file
obj.data = [];
fpath = obj.getpath;
if isfile(fpath)
delete(fpath);
end
end
end
methods (Hidden)
function root = getroot(obj)
%% Get root folder
if isempty(obj.ext)
root = obj.root;
else
if obj.local
root = obj.ext.root;
else
root = obj.ext.root;
target = "MATLAB Add-Ons";
path = extractBefore(root, target);
if ~isempty(path)
root = fullfile(path + target, 'Data');
if ~isfolder(root)
mkdir(root);
end
end
end
obj.root = root;
end
end
function data = json_read(obj, fpath)
%% Read data from .json file
enc = obj.jsonopts.encoding;
if obj.jsonopts.useJsonlab
obj.check_jsonlab()
data = loadjson(fpath, 'SimplifyCell', 1, 'ParseLogical', 1,...
'Encoding', enc);
else
fid = fopen(fpath, 'r', 'n', enc);
txt = fread(fid, '*char')';
fclose(fid);
data = jsondecode(txt);
end
if obj.jsonopts.readTable && ~(isstruct(data) && isscalar(data))
data = struct2table(data, 'AsArray', true);
end
end
function json_write(obj, fpath, data)
%% Write data to .json file
enc = obj.jsonopts.encoding;
if obj.jsonopts.useJsonlab
obj.check_jsonlab()
if istable(data)
data = reshape(table2struct(data), 1, []);
end
if obj.jsonopts.writeArray
data = arrayfun(@(x) {x}, data);
end
savejson('', data, 'FileName', fpath, 'ParseLogical', 1,...
'Encoding', enc);
else
txt = jsonencode(data);
fid = fopen(fpath, 'wt', 'n', enc);
fwrite(fid, txt, 'char');
fclose(fid);
end
end
end
methods (Static)
function check_jsonlab()
%% Check jsonlab is installed
w1 = which('loadjson');
w2 = which('savejson');
if isempty(w1) || isempty(w2)
error('You need to install <a href="https://github.com/fangq/jsonlab">jsonlab</a> to use it');
end
end
end
end