Getinisetting- basic problem

Getinisetting- basic problem

Anonymous
Not applicable
1,180 Views
6 Replies
Message 1 of 7

Getinisetting- basic problem

Anonymous
Not applicable

Hi,

I am stuck on a getinisetting issue

 

1. I am writing a script which reads RGB values from an INI file use getinisetting

 

contexts of the ini:

[P11A9]
NAME=ABBOT
R=77
G=59
B=45

[P31A8]
NAME=ABYSS
R=38
G=77
B=80

Is there a way I can search the entire ini file for the NAME variable without knowing the Category code ie [P31A8] and then return the RGB variables?

 

thanks for any assitance

Mike

 

 

 

0 Likes
Accepted solutions (1)
1,181 Views
6 Replies
Replies (6)
Message 2 of 7

blakestone
Collaborator
Collaborator

I have done something similar with XML files, here is how I did it:

 

 

 

(
	
	local my_file			= "c:\\my_file.ini"
	local my_fileOpen	= undefined
	local my_fileLine		= undefined
	local my_fileResult	= undefined
	local my_filterString	= undefined

-- check if file exists
	if doesFileExist my_file then
	(
	-- open file
		my_fileOpen = memStreamMgr.openFile my_file
		
	-- read file (line-by-line)
		while not my_fileOpen.eos () do
		(
		-- read current line
			my_fileLine = my_fileOpen.readLine()
			
		-- reset variable
			my_fileResult = false
			
		-- match pattern (returns true if found something)
			my_fileResult = matchPattern my_fileLine pattern:"*R=*"
			
		-- filter results
			if my_fileResult then 
			(
			-- filter string
				my_filterString = filterString my_fileLine "="
				
			-- print last array entry
				print my_filterString[my_filterString.count]
			)
			
		) -- end while
		
	-- close file
		memStreamMgr.close my_fileOpen
		
	) -- end doesFileExist
	
)

 

0 Likes
Message 3 of 7

Swordslayer
Advisor
Advisor

What about something like this:

 

fn getColorFromFile path colorName =
(
	local file = openFile path
	local found = false
	local colorValue

	while NOT found AND NOT EOF file do
	(
		skipToString file "NAME="
		found = readDelimitedString file "\r\n" == colorName
	)

	if NOT EOF file do colorValue = color \
		((trimLeft (readDelimitedString file "\r\n") "RGB=") as integer) \
		((trimLeft (readDelimitedString file "\r\n") "RGB=") as integer) \
		((trimLeft (readDelimitedString file "\r\n") "RGB=") as integer)

	close file
	colorValue
)

getColorFromFile "C:\\TEMP\\test.ini" "ABYSS" 

 

 

0 Likes
Message 4 of 7

Anonymous
Not applicable

Thanks for the responses have not had time to really pull it apart.

Had a quick go at both and could not get either to work but I am only a novice at scripting.

I will have another look and see if I can get it to work and will report back

 

thanks

Mike

0 Likes
Message 5 of 7

Steve_Curley
Mentor
Mentor
Accepted solution
May as well use the built-in functions to read ini files - may not be as fast as reading it as a text file, but is probably easier to understand ;)
(
fn getRGBFromName iniFileName colorName =
   (
   local found = false
   local result = color 0 0 0
   sections = getIniSetting iniFileName
   for s in sections while (not found) do
      (
      if (getIniSetting iniFileName s "NAME") == colorName then
         (
         result = color ((getIniSetting iniFileName s "R") as integer) \
                        ((getIniSetting iniFileName s "G") as integer) \
                        ((getIniSetting iniFileName s "B") as integer) 
         found = true
         )
      )
   result
   )

getRGBFromName "c:/Some.ini" "ABYSS"
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 6 of 7

Anonymous
Not applicable

The syntax is so much simpler for me to grasp- with a few tweaks I got it to work

 

thanks

Mike

 

0 Likes
Message 7 of 7

Steve_Curley
Mentor
Mentor
if (getIniSetting iniFileName s "NAME") == colorName then
I changed the name in the function declaration and forgot to change it in the line above. Post above edited to correct that. Slapped wrists 😞

Glad it helped anyway 🙂

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes