Recent

Author Topic: Reading inifiles without section tags  (Read 4495 times)

otoien

  • Jr. Member
  • **
  • Posts: 89
Reading inifiles without section tags
« on: September 04, 2015, 09:00:50 am »
I have some configuration files from a commercial program stored in an ini file like format that I would like to read, however the files do not have any section headers at all. I tried supplying a emtpy section string in the call, but it did not work. An obvious solution would be to make a copy of the file with a dummy section header inserted before reading it as an ini file, however I wounder if anyone knows of a direct method to lure the inifiles unit to read everything as if it detected a valid section string?
Example (left out some lines in the beginning containing # and text):
Code: [Select]
...snip...
########################################################
 
USE_PHEAT_POS_P=0
QT_LIMITER_LENGTH=100
ECX_VERSION=D
 
QTLIM_ON=0
QTLIM_GROUP_A=-1
QTLIM_GROUP_B=-1
QTLIM_REF_A=-1
QTLIM_REF_B=-1
QTLIM_RANGE=0.200000
QTLIM_SINGLE=1
QTLIM_MANUAL=0
QTLIM_QTVAL=100
 
SAMPLE_RATE=500
CHANNELS=20
OUTPUT_RATE=60
OUTPUT_BEAT_FILE=1
RESULTS_WINDOW_TIME_TYPE=1
SYSTOLIC_DELAY=10
BEAT_REPORT=0
 
############ Barometric Setup ######################
BAROMETRICINPUTCHANNEL=0
ADJUSTFORBAROMETRICPRESSURE[1]=0
ADJUSTFORBAROMETRICPRESSURE[2]=0
ADJUSTFORBAROMETRICPRESSURE[3]=0
ADJUSTFORBAROMETRICPRESSURE[4]=0
ADJUSTFORBAROMETRICPRESSURE[5]=0
ADJUSTFORBAROMETRICPRESSURE[6]=0
ADJUSTFORBAROMETRICPRESSURE[7]=0
ADJUSTFORBAROMETRICPRESSURE[8]=0
ADJUSTFORBAROMETRICPRESSURE[9]=0
ADJUSTFORBAROMETRICPRESSURE[10]=0
ADJUSTFORBAROMETRICPRESSURE[11]=0
ADJUSTFORBAROMETRICPRESSURE[12]=0
ADJUSTFORBAROMETRICPRESSURE[13]=0
ADJUSTFORBAROMETRICPRESSURE[14]=0
ADJUSTFORBAROMETRICPRESSURE[15]=0
ADJUSTFORBAROMETRICPRESSURE[16]=0
ADJUSTFORBAROMETRICPRESSURE[17]=0
ADJUSTFORBAROMETRICPRESSURE[18]=0
ADJUSTFORBAROMETRICPRESSURE[19]=0
ADJUSTFORBAROMETRICPRESSURE[20]=0
BAROMETRIC_MMHG=760.0

....snip....

(I have already code capable of automated generation of tags with index values.)
Unless otherwise noted I always use the latest stable version of Lasarus/FPC x86_64-win64-win32/win64

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Reading inifiles without section tags
« Reply #1 on: September 04, 2015, 10:03:06 am »
Maybe simply load it into a TStringList and access the values via:
var sl: TStringList;
...
range := sl.Values['QTLIM_RANGE'];
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

otoien

  • Jr. Member
  • **
  • Posts: 89
Re: Reading inifiles without section tags
« Reply #2 on: September 04, 2015, 11:37:10 am »
Thanks for the reply, 
"range" is a string, right? I need to learn more about stringlists, they look very useful.  In this case I was hoping for a solution where I could use my existing code that expands the inifiles unit to automate all this. It is my own class based on TMemIniFile, and I can read a whole array (of different numerical or text types) with just a single command.
Unless otherwise noted I always use the latest stable version of Lasarus/FPC x86_64-win64-win32/win64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Reading inifiles without section tags
« Reply #3 on: September 04, 2015, 11:54:10 am »
Use streams

  • write('[dummysection]') to some memorystream
  • read the rest of the ini file to the memorystream
[li] set position to 0 and feed the stream to the normal ini reader.
[/li][/list]


derek.john.evans

  • Guest
Re: Reading inifiles without section tags
« Reply #4 on: September 04, 2015, 11:55:21 am »
Code: Pascal  [Select][+][-]
  1. procedure LoadFromCfgFile(const AIniFile: TMemIniFile; const AFileName, ASectionName: String);
  2. var
  3.   LStrings: TStrings;
  4. begin
  5.   LStrings := TStringList.Create;
  6.   try
  7.     LStrings.LoadFromFile(AFileName);
  8.     LStrings.Insert(0, '[' + ASectionName + ']');
  9.     AIniFile.SetStrings(LStrings);
  10.   finally
  11.     FreeAndNil(LStrings);
  12.   end;
  13. end;  
  14.  
« Last Edit: October 02, 2015, 03:13:24 am by Geepster »

derek.john.evans

  • Guest
Re: Reading inifiles without section tags
« Reply #5 on: September 04, 2015, 11:58:41 am »
Just some notes: If you look at TIniFile.Create, the file is loaded into a stringlist and then parsed using FillSectionList. (This is the same for stream reading).

Problem is, FillSectionList is private, but, TMemIniFile exposed FillSectionList via SetStrings. Hence my code.

If FillSectionList was a protected virtual, then you could have just overrided that.

otoien

  • Jr. Member
  • **
  • Posts: 89
Re: Reading inifiles without section tags
« Reply #6 on: September 04, 2015, 03:27:33 pm »
Thanks for the code.  :)
I am trying to wrap my head around this. If I understand it right I call LoadFromCfgFile after I call create. This seems to work OK when tested with my code. I am seeing channels of the file displayed in my test routine. (I replaced TMemIniFile with the name of my derived class).

« Last Edit: September 04, 2015, 03:30:10 pm by otoien »
Unless otherwise noted I always use the latest stable version of Lasarus/FPC x86_64-win64-win32/win64

derek.john.evans

  • Guest
Re: Reading inifiles without section tags
« Reply #7 on: September 04, 2015, 03:39:45 pm »
Yep. Create the object first using noname. Then use the function to import the file.

This is actually a useful concept, because Ive seen config files where the sections were separated by spaces, or other know characters. The same concept would of made it easy to read those files.



otoien

  • Jr. Member
  • **
  • Posts: 89
Re: Reading inifiles without section tags
« Reply #8 on: September 05, 2015, 12:13:10 am »
Thanks again, this was exactly what I hoped for.

Lat me add that I am learning a lot from these helpful posts beyond solving the actual problem.
« Last Edit: September 05, 2015, 12:15:41 am by otoien »
Unless otherwise noted I always use the latest stable version of Lasarus/FPC x86_64-win64-win32/win64

otoien

  • Jr. Member
  • **
  • Posts: 89
Re: Reading inifiles without section tags
« Reply #9 on: September 05, 2015, 12:29:39 am »
One little followup: I notice that the call of the procedure lists:
const AIniFile: TMemIniFile;
I would have expected to use a variable type parameter here. Normally I const should not be writable? Perhaps it works as all that is needed to be handed the pointer?
Unless otherwise noted I always use the latest stable version of Lasarus/FPC x86_64-win64-win32/win64

 

TinyPortal © 2005-2018