functions.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. basic_functions.has_value = function(tab, val)
  2. -- test if val is in tab
  3. for index, value in ipairs(tab) do
  4. if value == val then
  5. return true
  6. end
  7. end
  8. return false
  9. end
  10. local has_value=basic_functions.has_value
  11. -- read table "infile" where in "def" is defined, which cols are numbers and which belongs to a group
  12. -- First line has to be the header.
  13. -- def.as_numeric: all values not stated in col_num, col_tab or with name "name" are interpreted as numeric
  14. -- def.seperator: character to use as field delimiter
  15. -- def.col_num: turn this elements to numbers
  16. -- def.groups_num: put this elements as numbers into matrix groups
  17. basic_functions.import_csv = function(infile,def)
  18. local file = io.open(infile, "r")
  19. local outdata = {}
  20. -- reading header with column names
  21. local splitchar=","
  22. if def.seperator then
  23. splitchar=def.seperator
  24. end
  25. local as_numeric=false
  26. if def.as_numeric then
  27. as_numeric = true
  28. end
  29. local header = file:read():gsub("\r",""):split(splitchar,true)
  30. -- read each line, split in separat fields and stores in array
  31. -- by header the value is stored as numeric, in the group environment or as text
  32. for line in file:lines() do
  33. if line:sub(1,1) ~= "#" then -- lines starting with # are handled as comment
  34. local attribs = line:gsub("\r",""):split(splitchar,true)
  35. local nrow={groups={}}
  36. for i,d in ipairs(attribs) do
  37. if d ~= "" then
  38. local th=header[i]
  39. local dsaved = false
  40. if def.col_num then
  41. if has_value(def.col_num,th) then
  42. nrow[th] = tonumber(d)
  43. dsaved = true
  44. end
  45. end
  46. if def.groups_num then
  47. if has_value(def.groups_num,th) then
  48. nrow.groups[th]=tonumber(d)
  49. dsaved = true
  50. end
  51. end
  52. if th == "name" then
  53. nrow[th] = d
  54. else
  55. if not dsaved then
  56. if as_numeric then
  57. nrow[th] = tonumber(d)
  58. else
  59. nrow[th]=d
  60. end
  61. end
  62. end
  63. end
  64. end
  65. if nrow.name then
  66. outdata[nrow.name] = nrow
  67. else
  68. outdata[#outdata+1] = nrow
  69. end
  70. end
  71. end
  72. file:close()
  73. return outdata
  74. end
  75. -- split name of value (ind) by "_" and store content in nested matrix
  76. -- inside "mat"
  77. basic_functions.parse_tree=function(mat,ind,val)
  78. if string.find(ind,"_") == nil then
  79. if tonumber(ind) ~= nil then
  80. mat[tonumber(ind)] = {}
  81. mat[tonumber(ind)] = tonumber(val)
  82. else
  83. mat[ind] = {}
  84. mat[ind] = tonumber(val)
  85. end
  86. else
  87. local ind_split=string.split(ind,"_")
  88. local first=ind_split[1]
  89. local second=string.split(ind,"_")[2]
  90. if #ind_split > 2 then
  91. for n=3,#ind_split do
  92. second = second.."_"..ind_split[n]
  93. end
  94. end
  95. if mat[first] == nil then
  96. mat[first]={}
  97. end
  98. mat[first]=basic_functions.parse_tree(mat[first],second,val)
  99. end
  100. return(mat)
  101. end
  102. -- function to read settingtypes.txt and insert values into minetest.setting
  103. basic_functions.import_settingtype = function(infile)
  104. local file = io.open(infile, "r")
  105. local outdata = {}
  106. -- reading header with column names
  107. local splitchar=" "
  108. local setname=minetest.settings:get_names()
  109. for line in file:lines() do
  110. if line:sub(1,1) ~= "#" then -- lines starting with # are handled as comment
  111. local attrib = line:gsub("\"",""):gsub("%(.*%) ",""):gsub("\r",""):split(splitchar,true)
  112. if(#attrib >= 3) then -- there should be at least 3 fields in a row (without the description)
  113. if has_value(setname,attrib[1]) == false then
  114. if attrib[2] == "bool" then
  115. minetest.settings:set_bool(attrib[1],attrib[3] == "true")
  116. else
  117. minetest.settings:set(attrib[1],attrib[3])
  118. end
  119. end
  120. end
  121. end
  122. end
  123. end
  124. basic_functions.read_file=function(filename)
  125. local file=io.open(filename,"r")
  126. local out=""
  127. if file then
  128. for line in file:lines() do
  129. out=out..line
  130. end
  131. file:close()
  132. end
  133. return(out)
  134. end