📄 bvqxinifile.m
字号:
end
% parse file contents
[icontent, prefile, postfile, newerrors] = i_parse( ...
varargin{2}(:)', bvqxinif.fterm, iconvert);
% errors during parse
if ~isempty(newerrors)
error( ...
'BVQXinifile:IniParseError', ...
'Parsing errors occurred:\n\n%s', ...
gluetostring(newerrors, char(10)) ...
);
end
% add new file to matrix
bvqxinif.matrix(end + 1) = cfid;
ifid = length(bvqxinif.matrix);
bvqxinic(ifid).caller = extcaller(1);
bvqxinic(ifid).children = [];
bvqxinic(ifid).convert = iconvert;
bvqxinic(ifid).filename = '';
bvqxinic(ifid).icontent = icontent;
bvqxinic(ifid).parent = [];
bvqxinic(ifid).postfile = postfile;
bvqxinic(ifid).prefile = prefile;
bvqxinic(ifid).readwrite = 'a';
% init output object
varargout{1} = class(struct('L', cfid), 'BVQXinifile');
% user request to release IniFile
case {'release'}
% test for right caller or forced mode
ocaller = cfile.caller;
ncaller = extcaller(1);
if ~strcmp(ocaller, ncaller) && ...
(nargin < 3 || ...
~ischar(varargin{3}) || ...
isempty(varargin{3}) || ...
lower(varargin{3}(1)) ~= 'f')
warning( ...
'BVQXinifile:CantReleaseObject', ...
'ReleaseIniFile not forced or issued by caller.' ...
);
return;
end
% we're going to release this file, so make good output variable
varargout{1} = bvqxinif.xhfactory;
% inform parent
pfid = cfile.parent;
if ~isempty(pfid)
% lookup parent object in matrix
pfmpos = find(bvqxinif.matrix == pfid(1));
% bail out if parent no longer available
if isempty(pfmpos)
error( ...
'BVQXinifile:InvalidFileID', ...
'Parent object disappeared, memory glitch?' ...
);
end
% remove self from parent's children list
ifid = hIniFile.L;
bvqxinic(pfmpos).children = ...
bvqxinic(pfmpos).children(bvqxinic(pfmpos).children ~= ifid);
end
% inform children
chid = cfile.children;
if ~isempty(chid)
% iterate over children
for schid = chid
% lookup child object in matrix
cfmpos = find(bvqxinif.matrix == schid);
% bail out if child no longer available
if isempty(cfmpos)
error( ...
'BVQXinifile:InvalidFileID', ...
'Child object disappeared, memory glitch?' ...
);
end
% unset parent object in child
bvqxinic(cfmpos).parent = [];
end
end
% finally remove object itself
bvqxinic(cfid) = [];
bvqxinif.matrix(cfid) = [];
% user request to release all files from memory
case {'releaseallfiles'}
% only works for the factory object
if cfid ~= 0
error( ...
'BVQXinifile:CallingConvention', ...
'ReleaseAllFiles only works for the factory object.' ...
);
end
% iterate over all open files in matrix
bvqxinif.matrix = [];
bvqxinic(:) = [];
varargout{1} = bvqxinif.xhfactory;
% reload all files in memory from disk
case {'reloadallfiles'}
% only works for the factory object
if cfid ~= 0
error( ...
'BVQXinifile:CallingConvention', ...
'ReloadAllFiles only works for the factory object.' ...
);
end
% init output object
varargout{1} = bvqxinif.xhfactory;
% iterate over all open files in matrix
for rfid = bvqxinif.matrix
rfob.L = rfid;
BVQXinifile(class(rfob, 'BVQXinifile'), 'reloadinifile');
end
% reload ini file contents
case {'reloadinifile'}
% get filename and convert state
cfilename = cfile.filename;
cconvert = cfile.convert;
% check for file status
if isempty(cfilename)
return;
end
% mark filename, so we can reload it!
bvqxinic(cfid).filename = '!BEING_RELOADED!';
% try to load file as new
try
if cconvert > 1
reloaded = BVQXinifile(cfilename, 'exact');
elseif cconvert > 0
reloaded = BVQXinifile(cfilename, 'convert');
else
reloaded = BVQXinifile(cfilename);
end
catch
bvqxinic(cfid).filename = cfilename;
warning( ...
'BVQXinifile:ReloadFailed', ...
'Reload of file %s failed (%s).', ...
cfilename, ...
lasterr ...
);
return;
end
% restore filename
bvqxinic(cfid).filename = cfilename;
% locate new object
rfid = find(bvqxinif.matrix == reloaded.L(1));
% bail out if not found
if isempty(rfid)
error( ...
'BVQXinifile:InvalidFileID', ...
'Re-read file %s disappeared from memory. Glitch?', ...
cfilename ...
);
end
% set new file content
bvqxinic(cfid).icontent = bvqxinic(rfid(1)).icontent;
bvqxinic(cfid).postfile = bvqxinic(rfid(1)).postfile;
bvqxinic(cfid).prefile = bvqxinic(rfid(1)).prefile;
% clear re-loaded in matrix
bvqxinic(rfid(1)) = [];
bvqxinif.matrix(rfid(1)) = [];
% delete contents (!) of IniFile in memory
case {'removeinicomplete'}
% clear file contents
bvqxinic(cfid).icontent = [];
% remove one section from memory
case {'removeinisection'}
% check for section field in call
if nargin < 3 || ...
~ischar(varargin{3}) || ...
isempty(varargin{3})
error( ...
'BVQXinifile:CallingConvention', ...
'RemoveIniSection needs a CHAR section name in call.' ...
);
end
% test if section exists
if ~isstruct(cfile.icontent) || ...
~isfield(cfile.icontent, varargin{3}(:)')
return;
end
% remove section and get new names
bvqxinic(cfid).icontent = ...
rmfield(bvqxinic(cfid).icontent, varargin{3}(:)');
% remove one setting from an IniFile section
case {'removeinisetting'}
% check for section and setting fields in call
if nargin < 4 || ...
~ischar(varargin{3}) || ...
~ischar(varargin{4}) || ...
isempty(varargin{3}) || ...
isempty(varargin{4})
error( ...
'BVQXinifile:CallingConvention', ...
'RemoveIniSetting needs two CHAR arguments name in call.' ...
);
end
% test if section and setting exist
if ~isstruct(cfile.icontent) || ...
~isfield(cfile.icontent, varargin{3}(:)')
return;
end
tsection = cfile.icontent.(varargin{3}(:)');
if ~isstruct(tsection)
bvqxinic(cfid).icontent = struct;
return;
end
if ~isfield(tsection, varargin{4}(:)')
return;
end
% remove setting
bvqxinic(cfid).icontent.(varargin{3}(:)') = ...
rmfield(tsection, varargin{4}(:)');
% set file footer
case {'resetclass'}
% only works for the factory object
if cfid ~= 0
error( ...
'BVQXinifile:CallingConvention', ...
'NewIniFile only works for the BVQXinifile factory object.' ...
);
end
% clear persistent memory and re-run init code
bvqxinif = [];
bvqxinic = [];
BVQXinifile;
% return new factory as output
varargout{1} = bvqxinif.xhfactory;
% write IniFile back to disk
case {'save'}
% test whether we have a good filename
if isempty(cfile.filename)
error( ...
'BVQXinifile:MemoryOnlyID', ...
'Object is a new file, use SaveAs(FILENAME).' ...
);
end
% test if conversion is requested
if cfile.convert == 0
tconvert = 0;
if nargin > 2 && ...
~isempty(varargin{3})
tconvert = varargin{3}(:)';
if isnumeric(tconvert) && ...
numel(tconvert) == 1 && ...
~isnan(tconvert) && ...
~isinf(tconvert)
tconvert = min(2, max(0, floor(tconvert)));
elseif ischar(tconvert)
if strcmpi(tconvert, 'exact')
tconvert = 2;
else
tconvert = 1;
end
else
tconvert = 1;
end
end
if tconvert > 0
bvqxinic(cfid).convert = tconvert;
end
end
% open file
rfid = fopen(bvqxinic(cfid).filename, 'w');
if rfid < 1
error( ...
'BVQXinifile:FileNotWritable', ...
'Couldn''t write ini-file %s.', ...
bvqxinic(cfid).filename ...
);
end
% write file contents
frewind(rfid);
fwrite(rfid, BVQXinifile(hIniFile, 'display', 'saveinifile'), 'uchar');
fclose(rfid);
% write IniFile under a new name
case {'saveas'}
% test for asfilename field
if nargin < 3 || ...
~ischar(varargin{3}) || ...
isempty(varargin{3})
error( ...
'BVQXinifile:CallingConvention', ...
'SaveAs needs a non-empty CHAR filename.' ...
);
end
% try to write file
asfile = varargin{3}(:)';
rfid = fopen(asfile, 'w');
if rfid < 1
error( ...
'BVQXinifile:FileNotWritable', ...
'Couldn''t write BVQXinifile as %s.', ...
asfile ...
);
end
fclose(rfid);
% set new filename and write file
bvqxinic(cfid).filename = asfile;
BVQXinifile(hIniFile, 'saveinifile', varargin{4:end});
% set file footer
case {'setfoot'}
% test for foot
if nargin < 3 || ...
~ischar(strCmd.foot)
error( ...
'BVQXinifile:CallingConvention', ...
'SetFileFoot needs a CHAR foot option.' ...
);
end
% set footer
bvqxinic(cfid).postfile = varargin{3}(:)';
% set file header
case {'sethead'}
% test for head
if nargin < 3 || ...
~ischar(varargin{3})
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -