Convert a string to a table. Extract XML data...


(Raul (ulneiz)) #1

I need to convert a string into a table.

The string: string = "8,34,255"

The resulting table: table = {8,34,255}

What is the best method to do that? Does not LUA have anything direct for such a conversion?

Each number separated by a comma can be 1, 2 or 3 characters. For example:

  1. “1,10,100”
  2. “10,100,1”
  3. “100,10,1”
  4. etc

Could this be useful?:

-- Create a new, or convert an exiting table to an object that uses the global
-- 'table.XXX' functions as methods, just like strings in Lua do.
-- > examples: t = table.create(); t:insert("a"); rprint(t) -> [1] = a;  
-- t = table.create{1,2,3}; print(t:concat("|")); -> "1|2|3";
table.create([t]) -> [table]

Thanks!


(joule) #2

Nope. Use gmatch like it says here: https://stackoverflow.com/questions/12211025/get-an-array-from-a-string-of-numbers-separated-with-comma

local output = { }
local str = "10,100, 1"

for match in str:gmatch("([^,%s]+)") do
 output[#output+1] = tonumber(match)
end

(Raul (ulneiz)) #3

Nope. Use gmatch like it says here: https://stackoverflow.com/questions/12211025/get-an-array-from-a-string-of-numbers-separated-with-comma

local output = { }
local str = "10,100, 1"

for match in str:gmatch("([^,%s]+)") do
output[#output+1] = tonumber(match)
end

Thanks!It seems one of the most direct ways. I’ve seen several weird or overly extensive things looking for google.

I have used something very similar. The result of the function:

--capture the native color of mark (C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1\Config.xml)
function crk_capture_clr_mrk()
  --print ( os.currentdir() )
  local filename = ( "%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
  print( filename )
  
  --RenoisePrefs
    --SkinColors
      --Selected_Button_Back

  local pref_data = renoise.Document.create("RenoisePrefs") { SkinColors = { Selected_Button_Back = "" } }
  pref_data:load_from( filename )
  --print( pref_data.SkinColors.Selected_Button_Back )
  local rgb = tostring(pref_data.SkinColors.Selected_Button_Back)
  local one, two, thr = rgb:match( "([^,]+),([^,]+),([^,]+)" )
  CRK_CLR.WHITE[1] = tonumber(one)
  CRK_CLR.WHITE[2] = tonumber(two)
  CRK_CLR.WHITE[3] = tonumber(thr)
end
crk_capture_clr_mrk()

The table CRK_CLR.WHITEget the RGB values of the “Selected_Button_Back” inside the Config.xml.

Config.xml:

<!--Skin Colors-->
  <SkinColors>
    <Main_Back>16,16,16</Main_Back>
    <Main_Font>219,219,219</Main_Font>
    <Alternate_Main_Back>15,16,16</Alternate_Main_Back>
    <Alternate_Main_Font>212,212,212</Alternate_Main_Font>
    <Body_Back>64,64,64</Body_Back>
    <Body_Font>170,170,170</Body_Font>
    <Strong_Body_Font>58,190,255</Strong_Body_Font>
    <Button_Back>76,77,77</Button_Back>
    <Button_Font>255,255,255</Button_Font>
    <Button_Highlight_Font>255,181,35</Button_Highlight_Font>
    <Selected_Button_Back>255,181,35</Selected_Button_Back>
    <Selected_Button_Font>24,24,24</Selected_Button_Font>

I have a little doubt with the path to Config.xml.**os.getenv(“APPDATA”)**also serves for OSX and Linux? Or is it necessary to restrict it only for Windows?

What are the path of Config.xml for OSX or for Linux?


(joule) #4

Nobody knows. There’s already a dedicated topic about the subject. https://forum.renoise.com/t/snippet-reading-theme-skin-colors/49366


#5

As far as I know Raul with Renoise under Linux the Config.xml file is situated at (well on my machine anyway):

$HOME/.renoise/V3.1.1/Config.xml

Where $HOME is the users home directory. So maybe(?) you could join a path string together like:

print(io.open(os.getenv("HOME").."/.renoise/V3.1.1/Config.xml"))

Just a thought.


(Raul (ulneiz)) #6

I use Windows only. The idea is to use a function like this:

...

local rns_version = renoise.RENOISE_VERSION
local filename = ""

if ( os.platform() = "WINDOWS" ) then
  filename = ( "%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
end

if ( os.platform() = "MACINTOSH" ) then
 filename = --the same for WINDOWS?
end

if ( os.platform() = "LINUX" ) then
 filename = ( "%s\\.renoise\\V%s\\Config.xml"):format( os.getenv("HOME"), rns_version )
end

...

The case is that it must works for the 3 operating systems, or at least that does not cause any error…

inside linux the route is like this exactly?: /.renoise/

I think Renoise has a corrector for the slash. If the path use …, change automatically for /…/…/…Unfortunately I can not try these things on other platforms than Windows.


#7

Remember Raul that on UN*X systems you tend to use forward slash to separate directories, so I’d probably write:

filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )

As for Mac, IDK.


(Raul (ulneiz)) #8

Remember Raul that on UN*X systems you tend to use forward slash to separate directories, so I’d probably write:

filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )

As for Mac, IDK.

Thanks! The result:

Click to view contents
function crk_capture_clr_mrk()
  --print ( os.currentdir() )
  local filename = ""
  if ( os.platform() == "WINDOWS" ) then
    filename = ("%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
  elseif ( os.platform() == "MACINTOSH" ) then
    filename = ("%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version ) --???
  elseif ( os.platform() == "LINUX" ) then
    filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )
  end
  print( filename )
  
  --RenoisePrefs
    --SkinColors
      --Selected_Button_Back
  if ( io.exists( filename ) ) then
    local pref_data = renoise.Document.create("RenoisePrefs") { SkinColors = { Selected_Button_Back = "" } }
    pref_data:load_from( filename )
    --print( pref_data.SkinColors.Selected_Button_Back )
    local rgb = tostring(pref_data.SkinColors.Selected_Button_Back)
    local one, two, thr = rgb:match( "([^,]+),([^,]+),([^,]+)" )
    CRK_CLR.WHITE[1] = tonumber(one)
    CRK_CLR.WHITE[2] = tonumber(two)
    CRK_CLR.WHITE[3] = tonumber(thr)
  end
end

Can you copy the print of entire path ofos.currentdir() inside Linux?This should detect the type of operating system to return the path.

It would be great if there API was an **os.preferdir()**to direct to the Config.xml directory…


#9

This should detect the type of operating system to return the path.

I’m not sure what you mean here Raul, os.currentdir() just returns the current directory string (usually set to the directory that Renoise was launched from?) So in my case (as I run Renoise from the terminal):

/home/4Tey/Applications/Renoise/renoise/

os.platform() gives you the OS string (as you are already doing?)
I think it is safer to say Raul that the path (whatever it is) for Mac would be closer to the Linux version rather than the Windows version, as MacOSX is a UN*X based BSD variant OS.

Ah, a sign…
Attachment 8188 not found.


(Raul (ulneiz)) #10

I’m not sure what you mean here Raul, os.currentdir() just returns the current directory string (usually set to the directory that Renoise was launched from?) So in my case (as I run Renoise from the terminal):

/home/4Tey/Applications/Renoise/renoise/

os.platform() gives you the OS string (as you are already doing?)
I think it is safer to say Raul that the path (whatever it is) for Mac would be closer to the Linux version rather than the Windows version, as MacOSX is a UN*X based BSD variant OS.

Ah, a sign…

Nice image! :smiley:

-- Returns the platform the script is running on: "WINDOWS", "MACINTOSH" or "LINUX".
os.platform() -> [string]

-- Returns the current working dir. Will always be the scripts directory when executing a script from a file.
os.currentdir() -> [string]

I believe that the os.currentdir() only returns the current address if you print it from the terminal. If you execute it from a script, it will always return the entire path of the folder of your tool, compatible with the 3 operating systems.

Then, a new “os.preferdir()” would be feasible, which would return the preferences folder that contains the Config.xml file. In fact, I think it would be possible to extract the direction by cutting on the right the route of the os.currentdir().

For example, with Windows and from a script, os.currentdir() return the path:

C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1 Scripts\Tools\com.tool_name.xrnx\

and the path of Config.xml is:

C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1 Config.xml

I think that this piece \Scripts\Tools\com.tool_name.xrnx always has the same number of charactersregardless of the operating system.

Maybe it would be possible to cut right to get the path of Config.xml from os.currentdir() and it would work for the 3 operating systems. It’s just an idea.

By the way, I have the beach a few meters from my house. I live on the coast of the Mediterranean, east of Spain.

Edit: I need the print path within Linux using os.currentdir() since a script, not since the terminal.I need to know what happens from the right of the path.


(Raul (ulneiz)) #11

According to my search by google, it seems that the correct routes are the following:

local filename = “”

if ( os.platform() == “WINDOWS” ) then

filename = ("%s\Renoise\V%s\Config.xml"):format( os.getenv(“APPDATA”), rns_version )

elseif ( os.platform() == “MACINTOSH” ) then

filename = ("%s/Library/Preferences/Renoise/V%s/Config.xml"):format( os.getenv(“HOME”), rns_version )

elseif ( os.platform() == “LINUX” ) then

filename = ( “%s/.renoise/V%s/Config.xml”):format( os.getenv(“HOME”), rns_version )

end

print(filename)

Click to view contents
-------------------------------------------------------------------------------------------------
--capture the native color of marker (for Windows: C:\Users\USER_NAME\AppData\Roaming\Renoise\V3.1.1\Config.xml)
function crk_capture_clr_mrk()
  --print ( os.currentdir() )
  --Config.xml path:  
    --Windows: %appdata%\Renoise\V3.1.1\Config.xml
    --MacOS: ~/Library/Logs/, ~/Library/Preferences/Renoise/V3.1.1/Config.xml
    --Linux: ~/.renoise/V3.1.1/Config.xml
  local filename = ""
  if ( os.platform() == "WINDOWS" ) then
    filename = ("%s\\Renoise\\V%s\\Config.xml"):format( os.getenv("APPDATA"), rns_version )
    --print("Windows:", filename )
  elseif ( os.platform() == "MACINTOSH" ) then
    filename = ("%s/Library/Preferences/Renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version ) --???
    --print("MacOS:", filename )
  elseif ( os.platform() == "LINUX" ) then
    filename = ( "%s/.renoise/V%s/Config.xml"):format( os.getenv("HOME"), rns_version )
    --print("Linux:", filename )
  end
  --print( filename )
  
  --RenoisePrefs
    --SkinColors
      --Selected_Button_Back
  if ( io.exists( filename ) ) then
    local pref_data = renoise.Document.create("RenoisePrefs") { SkinColors = { Selected_Button_Back = "" } }
    pref_data:load_from( filename )
    --print( pref_data.SkinColors.Selected_Button_Back )
    local rgb = tostring(pref_data.SkinColors.Selected_Button_Back)
    local one, two, thr = rgb:match( "([^,]+),([^,]+),([^,]+)" )
    CRK_CLR.MARKER[1] = tonumber(one)
    CRK_CLR.MARKER[2] = tonumber(two)
    CRK_CLR.MARKER[3] = tonumber(thr)
  end
end
crk_capture_clr_mrk()

I think that this code should work with the 3 operating systems, also in older versions of Renoise, at least until version V2.6.0.

It would be nice if a MAC user and another LINUX user confirmed the path (for each case).It must be the folder that contains the Config.xml file!

This last function should also correctly return the path (filename). This method cuts the route by the word “Scripts”. In theory, this folder is used in all operating systems, always after the Vx.y.z/.

function crk_test()
  local currentdir = os.currentdir()
  print( currentdir )
  local preferdir_length = string.find(currentdir, "Scripts" )
  print( preferdir_length )
  local preferdir = string.sub( currentdir, 1, preferdir_length -1 )
  print( preferdir )
  local filename = ("%sConfig.xml"):format( preferdir )
  print( filename )
end

#12

Ok, fair enough Raul, I can run os.currentdir from a tool. I get:

/home/4Tey/.renoise/V3.1.1/Scripts/Tools/com.4Tey.TotheBeach.xrnx/

So maybe you can truncate the string (or what not) :slight_smile:

You’re lucky, you can probably see the sign from your window :wink:


(Raul (ulneiz)) #13

Ok, fair enough Raul, I can run os.currentdir from a tool. I get:

/home/4Tey/.renoise/V3.1.1/Scripts/Tools/com.4Tey.TotheBeach.xrnx/

So maybe you can truncate the string (or what not) :slight_smile:

You’re lucky, you can probably see the sign from your window :wink:

4Tey,I have updated my KangarooX120 tool, including the color capture discussed in this thread.Could you try this tool in Linux? The marking of objects (buttons, checkboxes, etc) should take the Selected_Button_Back color of your Renoise theme (skin).On the other hand, if the marking is white, something is wrong inside the code.

Thanks! :slight_smile:


#14

Could you try this tool in Linux?

Seems fine Raul (well it hasn’t crashed or anything), maybe rest at ease on that deck chair (until someone finds a bug :wink: )


(Raul (ulneiz)) #15

Seems fine Raul (well it hasn’t crashed or anything), maybe rest at ease on that deck chair (until someone finds a bug :wink: )

Thanks!So, if inside the Linux it takes the correct color of the skin, it only needs that Danoise confirms if it works correctly within MAC.Apparently, we have a good team like that: :slight_smile:

  • I (ulneiz) can try tools with Windows.
  • You (4Tey) can try tools with Linux.
  • The chief (Danoise) can try tools with Macintosh.

KangarooX120 is quite robust. I would even challenge you to find a bug that produces a crash… :smiley: