📄 inifile.m
字号:
fullInd(from:to) = ind(ind2);
ii = ii + n;
else
ii = ii + 1;
end
end
% Final (re)sorting
so = so(fullInd);
eo = eo(fullInd);
keysIn = keysIn(fullInd,:);
keysExist = keysExist(fullInd);
secsExist = secsExist(fullInd);
subSecsExist = subSecsExist(fullInd);
readValues = readValues(fullInd);
values = keysIn(:,4);
% Refined data - datain
datain = [];
for ii=1:nKeys % go through all the keys, existing and non-existing ones
if ii==1
from = 1; % from byte-offset of original data (dataout)
else
from = eo(ii-1);
if keysExist(ii-1)
from = from + 1;
end
end
to = min(so(ii)-1,fs); % to byte-offset of original data (dataout)
if ~isempty(dataout)
datain = [datain dataout(from:to)]; % the lines before the key
end
if length(datain) & (~(datain(end)==RETURN | datain(end)==NEWLINE))
datain = [datain, sprintf(NL_CHAR)];
end
tab = [];
if ~keysExist(ii)
if ~secsExist(ii) && ~isempty(keysIn(ii,1))
if ~isempty(keysIn{ii,1})
datain = [datain sprintf(['%s' NL_CHAR],['[' keysIn{ii,1} ']'])];
end
% Key-indices with the same section as this, ii-th key (even empty sections are considered)
ind = find( strcmpi( keysIn(:,1), keysIn(ii,1)) );
% This section exists at all keys corresponding to the same section from know on (even the empty ones)
secsExist(ind) = 1;
end
if ~subSecsExist(ii) && ~isempty(keysIn(ii,2))
if ~isempty( keysIn{ii,2})
if secsExist(ii); tab = tab1; end;
datain = [datain sprintf(['%s' NL_CHAR],[tab '{' keysIn{ii,2} '}'])];
end
% Key-indices with the same section AND subsection as this, ii-th key
% (even empty sections and subsections are considered)
ind = find( strcmpi( keysIn(:,1), keysIn(ii,1)) & strcmpi( keysIn(:,2), keysIn(ii,2)) );
% This subsection exists at all keys corresponding to the
% same section and subsection from know on (even the empty ones)
subSecsExist(ind) = 1;
end
end
if secsExist(ii) & (~isempty(keysIn{ii,1})); tab = tab1; end;
if subSecsExist(ii) & (~isempty(keysIn{ii,2})); tab = [tab tab1]; end;
datain = [datain sprintf(['%s' NL_CHAR],[tab keysIn{ii,3} ' = ' values{ii}])];
end
from = eo(ii);
if keysExist(ii)
from = from + 1;
end
to = length(dataout);
if from < to
datain = [datain dataout(from:to)];
end
fwrite(fh,datain,'char');
catch
fclose(fh);
error(['Error writing keys to file: ''' fileName ''' : ' lasterr]);
end
fclose(fh);
%------------------------------------
%------------------------------------
function deletekeys(fileName,keys)
% Deletes keys and their values out; keys must have at least 3 columns:
% section, subsection, and the key
[m,n] = size(keys);
if n < 3
error('Keys to be deleted are given in an invalid format.');
end
% Get keys position first
keysIn = keys;
[secsExist,subSecsExist,keysExist,readValues,so,eo] = findkeys(fileName,keys(:,1:3));
% Read the whole file's contents out
fh = fopen(fileName,'r');
if fh == -1
error(['File: ''' fileName ''' does not exist or can not be opened.']);
end
try
dataout = fread(fh,'char=>char')';
catch
fclose(fh);
rethrow(lasterror);
end
fclose(fh);
%--- Rewriting the file -> writing the refined content
fh = fopen(fileName,'w');
if fh == -1
error(['File: ''' fileName ''' does not exist or can not be opened.']);
end
try
ind = find(keysExist);
nExistingKeys = length(ind);
datain = dataout;
if nExistingKeys
% Filtering - retain only the existing keys...
fs = length(dataout); % file size in bytes
so = so(ind);
eo = eo(ind);
keysIn = keysIn(ind,:);
% ...and sorting
[so,ind] = sort(so);
eo = eo(ind);
keysIn = keysIn(ind,:);
% Refined data - datain
datain = [];
for ii=1:nExistingKeys % go through all the existing keys
if ii==1
from = 1; % from byte-offset of original data (dataout)
else
from = eo(ii-1)+1;
end
to = so(ii)-1; % to byte-offset of original data (dataout)
if ~isempty(dataout)
datain = [datain dataout(from:to)]; % the lines before the key
end
end
from = eo(ii)+1;
to = length(dataout);
if from < to
datain = [datain dataout(from:to)];
end
end
fwrite(fh,datain,'char');
catch
fclose(fh);
error(['Error deleting keys from file: ''' fileName ''' : ' lasterr]);
end
fclose(fh);
%------------------------------------
%------------------------------------
function [keys,sections,subsections] = readallkeys(fileName)
% Reads all the keys out as well as the sections and subsections
keys = [];
sections = [];
subsections = [];
% Read the whole file's contents out
try
dataout = textread(fileName,'%s','delimiter','\n');
catch
error(['File: ''' fileName ''' does not exist or can not be opened.']);
end
nLines = size(dataout,1);
% Go through all the lines and construct the keys variable
keys = cell(nLines,4);
sections = cell(nLines,1);
subsections = cell(nLines,2);
keyN = 0;
secN = 0;
subsecN = 0;
secStr = '';
subsecStr = '';
for ii=1:nLines
[status,value,key] = processiniline(dataout{ii});
if status == 1
secN = secN + 1;
secStr = value;
sections(secN) = {secStr};
elseif status == 2
subsecN = subsecN + 1;
subsecStr = value;
subsections(subsecN,:) = {secStr,subsecStr};
elseif status == 3
keyN = keyN + 1;
keys(keyN,:) = {secStr,subsecStr,key,value};
end
end
keys(keyN+1:end,:) = [];
sections(secN+1:end,:) = [];
subsections(subsecN+1:end,:) = [];
%------------------------------------
%------------------------------------
function [status,value,key] = processiniline(line)
% Processes a line read from the ini file and
% returns the following values:
% - status: -1 => unknown string found
% 0 => empty line found
% 1 => section found
% 2 => subsection found
% 3 => key-value pair found
% 4 => comment line found (starting with ;)
% - value: value-string of a key, section, subsection, comment, or unknown string
% - key: key as string
status = 0;
value = [];
key = [];
line = strim(line); % removes any leading and trailing spaces
if isempty(line) % empty line
return
end
if strcmpi(line(1),';') % comment found
status = 4;
value = line(2:end);
elseif (line(1) == '[') & (line(end) == ']') & (length(line) >= 3) % section found
value = lower(line(2:end-1));
status = 1;
elseif (line(1) == '{') &... % subsection found
(line(end) == '}') & (length(line) >= 3)
value = lower(line(2:end-1));
status = 2;
else % either key-value pair or unknown string
pos = findstr(line,'=');
if ~isempty(pos) % key-value pair found
status = 3;
key = lower(line(1:pos-1));
value = line(pos+1:end);
key = strim(key); % removes any leading and trailing spaces
value = strim(value); % removes any leading and trailing spaces
if isempty(key) % empty keys are not allowed
status = 0;
key = [];
value = [];
end
else % unknown string found
status = -1;
value = line;
end
end
%------------------------------------
function outstr = strim(str)
% Removes leading and trailing spaces (spaces, tabs, endlines,...)
% from the str string.
if isnumeric(str);
outstr = str;
return
end
ind = find( ~isspace(str) ); % indices of the non-space characters in the str
if isempty(ind)
outstr = [];
else
outstr = str( ind(1):ind(end) );
end
%------------------------------------
function cs = cellstrings(m,n)
% Creates a m x n cell array of empty strings - ''
cs = cell(m,n);
cs(:) = {''};
%------------------------------------
function y = n2s(x)
% Converts numeric matrix to string representation.
% Example: x given as [1 2;3 4] returns y = '1,2;3;4'
if ischar(x) | isempty(x)
y = x;
return
end
[m,n] = size(x);
y = [num2str(x(1,:),'%15.6g')];
for ii=2:m
y = [y ';' num2str(x(ii,:),'%15.6g')];
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -