Guest User

Untitled

a guest
Jan 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -- Returns value of given key from given INI file.
  3. --
  4. -- License: Public Domain
  5. --
  6. -- @param key [String]
  7. -- @param ini_path [String]
  8. -- @return s [String] or nil
  9. function FetchUserPreference(key,ini_path)
  10.  
  11. local f = io.open(ini_path,"rb")
  12. local s = f:read("*all")
  13. f:close()
  14.  
  15. local s1,s2 = string.find(s,key.."=",1,true)
  16. if s1 == nil then return nil end
  17. local s3 = string.find(s,"\n",s1,true)
  18. s = s:sub(s2+1,s3-1):gsub("[\r]","")--linux,osx: \n
  19. --windows: \r\n
  20. --reaper.ShowConsoleMsg(key.."="..s.."\n")--debug
  21. return s
  22.  
  23. end
  24.  
  25.  
  26. -------------------------------------------------------------------------------
  27. -- Returns value of given key from given INI file.
  28. --
  29. --License:
  30. -- Adapted from "Lua INI Parser"
  31. --
  32. -- Copyright (c) 2012 Carreras Nicolas
  33. --
  34. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  35. -- of this software and associated documentation files (the "Software"), to deal
  36. -- in the Software without restriction, including without limitation the rights
  37. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  38. -- copies of the Software, and to permit persons to whom the Software is
  39. -- furnished to do so, subject to the following conditions:
  40. --
  41. -- The above copyright notice and this permission notice shall be included in all
  42. -- copies or substantial portions of the Software.
  43. --
  44. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  45. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  46. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  47. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  48. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  49. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  50. -- SOFTWARE.
  51. -- @param key [String]
  52. -- @param ini_path [String]
  53. -- @return val [String] or nil
  54. function GetUserPreference(key,ini_path)
  55.  
  56. local f = io.open(ini_path,"r")
  57.  
  58. for l in f:lines() do
  59. local _,val = l:match('^([%w|_]+)%s-=%s-(.+)$')
  60. if _ == key then
  61. --reaper.ShowConsoleMsg(_.."="..val.."\n")--debug
  62. f:close()
  63. return val
  64. end
  65. end
  66.  
  67. f:close()
  68. return nil
  69. end
Add Comment
Please, Sign In to add comment