util.lua 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. util = {}
  2. util.string = {}
  3. function util.get_content(filename)
  4. local f = assert(io.open(filename, "r"))
  5. local t = f:read('*all')
  6. f:close()
  7. return t
  8. end
  9. function util.shell(command)
  10. local handle=io.popen(command.." 2>&1")
  11. local result=handle:read('*all')
  12. handle:close()
  13. -- komut çıktısı sonu yeni satır karakterin silinmesi - en sondaki \n
  14. if result:sub(-1) == "\n" then
  15. --result=result:gsub("\n", "")
  16. result=result:sub(1,-2)
  17. end
  18. return result
  19. end
  20. function util.check_command(command)
  21. local check_cmd="LC_ALL=C type %s"
  22. local state=util.shell(check_cmd:format(command))
  23. if state:find("not found") then
  24. return nil
  25. end
  26. return 1
  27. end
  28. function util.string:split(delimiter)
  29. local result = { }
  30. local from = 1
  31. local delim_from, delim_to = string.find( self, delimiter, from )
  32. while delim_from do
  33. table.insert( result, string.sub( self, from , delim_from-1 ) )
  34. from = delim_to + 1
  35. delim_from, delim_to = string.find( self, delimiter, from )
  36. end
  37. table.insert( result, string.sub( self, from ) )
  38. return result
  39. end
  40. return util