From d5784a01fdbf152c122c9bdc7401ceb4243a7e4b Mon Sep 17 00:00:00 2001 From: Octavian-Zhang Date: Sat, 7 May 2022 16:26:56 +0800 Subject: [PATCH] Update readFromIniFile.m 1. allow comments to sections (code change on line 108) 2. trim comments on the same line with key-value pair 3. code generation ready, for C/C++ embedded deployment Test with the following dummy.ini: [Section 1] ; allowed the comment to section key11=10 ; numeric scalar key12=1.45, 19.5, 0.6, -1.4 ; numeric vector key13=1.5+3i, -2-2i ; complex numeric vector key14=Hello, Matlab! ; string --- toolbox/RWTHMindstormsNXT/readFromIniFile.m | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/toolbox/RWTHMindstormsNXT/readFromIniFile.m b/toolbox/RWTHMindstormsNXT/readFromIniFile.m index 505a1f9..e10a163 100644 --- a/toolbox/RWTHMindstormsNXT/readFromIniFile.m +++ b/toolbox/RWTHMindstormsNXT/readFromIniFile.m @@ -1,4 +1,4 @@ -function ret = readFromIniFile(AppName, KeyName, filename) +function ret = readFromIniFile(AppName, KeyName, filename) %#codegen % Reads parameters from a configuration file (usually *.ini) % % Syntax @@ -86,8 +86,8 @@ %% Try to read in inifile fid = fopen(filename, 'r'); if fid < 0 - warning('MATLAB:RWTHMindstormsNXT:couldNotOpenInifile', 'Ini-file "%s" was not found or could not be read.', filename); - return + error('MATLAB:RWTHMindstormsNXT:couldNotOpenInifile', ... + 'Ini-file "%s" was not found or could not be read.', filename); end%if % input to a string @@ -98,13 +98,14 @@ %% Separate lines and parse away curSection = ''; curKey = ''; +curVal = ''; while(~isempty(data)) % get next line by LFs - [curLine, data] = strtok(data, char(10)); %#ok + [curLine, data] = strtok(data, newline); %#ok - % remove whitespace (includes removing CRs if present) - curLine = strtrim(curLine); + % remove whitespace & comments (includes removing CRs if present) + curLine = strtrim(strtok(curLine, ';')); % if line's empty or too short if length(curLine) < 2 @@ -123,8 +124,8 @@ % ignore this line, it's a comment % or if we have a key - elseif ~isempty(strfind(curLine, '=')) % found a =, it's a key - [curKey remainder] = strtok(curLine, '='); + elseif contains(curLine, '=') % found a =, it's a key + [curKey, remainder] = strtok(curLine, '='); curVal = strtok(remainder, '='); % remove whitespaces from tokens curKey = strtrim(curKey); @@ -144,4 +145,3 @@ end%function -