File: setjson.m

package info (click to toggle)
octave-iso2mesh 1.9.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 11,982; ansic: 10,158; sh: 365; makefile: 59
file content (45 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download
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
function val = updatejson(fname, mmap, varargin)

if (isoctavemesh)
    if (regexp(fname, '^\s*(?:\[.*\])|(?:\{.*\})\s*$', 'once'))
        string = fname;
    elseif (exist(fname, 'file'))
        try
            encoding = jsonopt('Encoding', '', opt);
            if (isempty(encoding))
                string = fileread(fname);
            else
                fid = fopen(fname, 'r', 'n', encoding);
                string = fread(fid, '*char')';
                fclose(fid);
            end
        catch
            try
                string = urlread(['file://', fname]);
            catch
                string = urlread(['file://', fullfile(pwd, fname)]);
            end
        end
    else
        error_pos('input file does not exist');
    end
end

mmap = [mmap{:}];
keylist = mmap(1:2:end);

for i = 1:2:length(varargin)
    if (regexp(varargin{i}, '^$'))
        [tf, loc] = ismember(varargin{i}, keylist);
        if (tf)
            rec = {'uint8', [1, mmap{loc * 2}{1} - 1], 'o'; ...
                   'uint8', [1, mmap{loc * 2}{2}],  'x'};
            fmap = memmapfile(fname, 'writable', true, 'format', rec);
            val = savejson('', varargin{i + 1}, 'compact', 1);
            if (length(var) <= rec{2, 2}{2})
                val = [val repmat(' ', [1, rec{2, 2}{2} - length(var)])];
                fmap.x = val;
            end
        end
    end
end