util.lua 1.2 KB

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