util_funcs.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package UI
  2. import (
  3. "os"
  4. "log"
  5. "path/filepath"
  6. "strings"
  7. "fmt"
  8. "bufio"
  9. "bytes"
  10. "io"
  11. "strconv"
  12. "syscall"
  13. "os/exec"
  14. "runtime"
  15. "github.com/cuu/gogame/display"
  16. "github.com/clockworkpi/LauncherGoDev/sysgo"
  17. )
  18. func ShowErr(e error) {
  19. if e != nil {
  20. fmt.Println(e)
  21. }
  22. }
  23. func Assert(e error) {
  24. if e != nil {
  25. log.Fatal("Assert: " , e)
  26. }
  27. }
  28. func CheckAndPanic(e error) {
  29. if e != nil {
  30. panic(e)
  31. }
  32. }
  33. func Abs(n int) int {
  34. y := n >> 63
  35. return (n ^ y) - y
  36. }
  37. func SkinMap(orig_file_or_dir string) string {
  38. DefaultSkin := "skin/default"
  39. ret := ""
  40. lastpad := ""
  41. if string(orig_file_or_dir[len(orig_file_or_dir)-1]) == "/" {
  42. lastpad = string(orig_file_or_dir[len(orig_file_or_dir)-1])
  43. }
  44. if strings.HasPrefix(orig_file_or_dir, "/home/cpi/apps/Menu") {
  45. ret = strings.Replace(orig_file_or_dir,"/home/cpi/apps/Menu/", filepath.Join(sysgo.SKIN,"/Menu/GameShell/"),-1)
  46. if FileExists(ret) == false {
  47. ret = filepath.Join(DefaultSkin,orig_file_or_dir)
  48. }
  49. }else { // there is no need to add insert "sysgo" in the middle
  50. ret = filepath.Join(sysgo.SKIN,orig_file_or_dir)
  51. if FileExists(ret) == false {
  52. ret = filepath.Join(DefaultSkin,orig_file_or_dir)
  53. }
  54. }
  55. if FileExists(ret) {
  56. return ret+lastpad
  57. }else { // if not existed both in default or custom skin ,return where it is
  58. return orig_file_or_dir
  59. }
  60. }
  61. func CmdClean(cmdpath string) string {
  62. spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
  63. for _,v:= range spchars {
  64. cmdpath = strings.Replace(cmdpath,string(v),"\\"+string(v),-1)
  65. }
  66. return cmdpath
  67. }
  68. func FileExists(name string) bool {
  69. if _, err := os.Stat(name ); err == nil {
  70. return true
  71. }else {
  72. return false
  73. }
  74. }
  75. func IsDirectory(path string) bool {
  76. fileInfo, err := os.Stat(path)
  77. if err != nil {
  78. return false
  79. }else {
  80. return fileInfo.IsDir()
  81. }
  82. }
  83. func IsAFile(path string) bool {
  84. fileInfo, err := os.Stat(path)
  85. if err != nil {
  86. return false
  87. }else {
  88. return fileInfo.Mode().IsRegular()
  89. }
  90. }
  91. func MakeExecutable(path string) {
  92. fileInfo, err := os.Stat(path)
  93. if err != nil {
  94. log.Fatalf("os.Stat %s failed", path)
  95. return
  96. }
  97. mode := fileInfo.Mode()
  98. mode |= (mode & 0444) >> 2
  99. os.Chmod(path,mode)
  100. }
  101. func GetExePath() string {
  102. dir, err := os.Getwd()
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. return dir
  107. }
  108. func ReplaceSuffix(orig_file_str string, new_ext string) string {
  109. orig_ext := filepath.Ext(orig_file_str)
  110. if orig_ext!= "" {
  111. las_pos := strings.LastIndex(orig_file_str,".")
  112. return orig_file_str[0:las_pos]+"."+new_ext
  113. }
  114. return orig_file_str // failed just return back where it came
  115. }
  116. func SwapAndShow() {
  117. display.Flip()
  118. }
  119. func ReadLines(path string)(lines [] string,err error){
  120. var (
  121. file *os.File
  122. part [] byte
  123. prefix bool
  124. )
  125. if file, err = os.Open(path); err != nil {
  126. return
  127. }
  128. reader := bufio.NewReader(file)
  129. buffer := bytes.NewBuffer(make([]byte,0))
  130. for {
  131. if part, prefix, err = reader.ReadLine();err != nil {
  132. break
  133. }
  134. buffer.Write(part)
  135. if !prefix {
  136. lines = append(lines,buffer.String())
  137. buffer.Reset()
  138. }
  139. }
  140. if err == io.EOF {
  141. err = nil
  142. }
  143. return
  144. }
  145. func WriteLines(lines [] string,path string)(err error){
  146. var file *os.File
  147. if file,err = os.Create(path); err != nil{
  148. return
  149. }
  150. defer file.Close()
  151. for _,elem := range lines {
  152. _,err := file.WriteString(strings.TrimSpace(elem)+"\n")
  153. if err != nil {
  154. fmt.Println(err)
  155. break
  156. }
  157. }
  158. return
  159. }
  160. func GetGid(path string) int {
  161. s, err := os.Stat(path)
  162. if err != nil {
  163. return -1
  164. }
  165. sys_interface := s.(os.FileInfo).Sys()
  166. if sys_interface == nil {
  167. return -1
  168. }
  169. return int(sys_interface.(*syscall.Stat_t).Gid)
  170. }
  171. func GetUid(path string) int {
  172. s, err := os.Stat(path)
  173. if err != nil {
  174. return -1
  175. }
  176. sys_interface := s.(os.FileInfo).Sys()
  177. if sys_interface == nil {
  178. return -1
  179. }
  180. return int(sys_interface.(*syscall.Stat_t).Uid)
  181. }
  182. func CheckBattery() int {
  183. if FileExists(sysgo.Battery) == false {
  184. return -1
  185. }
  186. batinfos,err := ReadLines(sysgo.Battery)
  187. if err == nil {
  188. for _,v := range batinfos {
  189. if strings.HasPrefix(v,"POWER_SUPPLY_CAPACITY") {
  190. parts := strings.Split(v,"=")
  191. if len(parts) > 1 {
  192. cur_cap,err := strconv.Atoi(parts[1])
  193. if err == nil {
  194. return cur_cap
  195. }else {
  196. return 0
  197. }
  198. }
  199. }
  200. }
  201. }else{
  202. fmt.Println(err)
  203. }
  204. return 0
  205. }
  206. func System(cmd string) string {
  207. ret := ""
  208. out,err := exec.Command("bash","-c",cmd).Output()
  209. if err != nil {
  210. if _, ok := err.(*exec.ExitError); ok {
  211. //exit code !=0 ,but it can be ignored
  212. }else{
  213. fmt.Println(err)
  214. }
  215. }else {
  216. ret = string(out)
  217. }
  218. return ret
  219. }
  220. func ArmSystem(cmd string) string {
  221. if strings.Contains(runtime.GOARCH,"arm") == true {
  222. return System(cmd)
  223. }else {
  224. return ""
  225. }
  226. }
  227. func SystemTrim(cmd string) string {
  228. ret := ""
  229. out,err := exec.Command("bash","-c",cmd).Output()
  230. if err != nil {
  231. if _, ok := err.(*exec.ExitError); ok {
  232. //exit code !=0 ,but it can be ignored
  233. }else{
  234. fmt.Println(err)
  235. }
  236. }else {
  237. ret = string(out)
  238. }
  239. return strings.Trim(ret,"\r\n")
  240. }