main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. ArOZ Online System - fscov File Naming Format Conversion Tool
  3. This micro-servie is designed to convert filename from and to umfilename (ArOZ Online System File Explorer Custom Format)
  4. and standard UTF-8 File System filename.
  5. Usage:
  6. ./fsconv
  7. (Convert all the files, folder and sub-directories from and to umfilename to UTF8 filename.)
  8. ./fsconv {target} {mode (um / utf)}
  9. (Convert all the files, folder and sub-directories to filename in given mode, target can be file or directory.)
  10. */
  11. package main
  12. import (
  13. "fmt"
  14. "io/ioutil"
  15. "os"
  16. "path"
  17. "path/filepath"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. )
  22. //Commonly used functions
  23. func in_array(val interface{}, array interface{}) (exists bool, index int) {
  24. exists = false
  25. index = -1
  26. switch reflect.TypeOf(array).Kind() {
  27. case reflect.Slice:
  28. s := reflect.ValueOf(array)
  29. for i := 0; i < s.Len(); i++ {
  30. if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
  31. index = i
  32. exists = true
  33. return
  34. }
  35. }
  36. }
  37. return
  38. }
  39. func file_exists(path string) bool {
  40. if _, err := os.Stat(path); os.IsNotExist(err) {
  41. return false
  42. }
  43. return true
  44. }
  45. func file_get_contents(path string) string {
  46. dat, err := ioutil.ReadFile(path)
  47. if err != nil {
  48. panic("Unable to read file: " + path)
  49. }
  50. return (string(dat))
  51. }
  52. func scan_recursive(dir_path string, ignore []string) ([]string, []string) {
  53. folders := []string{}
  54. files := []string{}
  55. // Scan
  56. filepath.Walk(dir_path, func(path string, f os.FileInfo, err error) error {
  57. _continue := false
  58. // Loop : Ignore Files & Folders
  59. for _, i := range ignore {
  60. // If ignored path
  61. if strings.Index(path, i) != -1 {
  62. // Continue
  63. _continue = true
  64. }
  65. }
  66. if _continue == false {
  67. f, err = os.Stat(path)
  68. // If no error
  69. if err != nil {
  70. panic("ERROR. " + err.Error())
  71. }
  72. // File & Folder Mode
  73. f_mode := f.Mode()
  74. // Is folder
  75. if f_mode.IsDir() {
  76. // Append to Folders Array
  77. folders = append(folders, path)
  78. // Is file
  79. } else if f_mode.IsRegular() {
  80. // Append to Files Array
  81. files = append(files, path)
  82. }
  83. }
  84. return nil
  85. })
  86. return folders, files
  87. }
  88. func Hex2Bin(hex string) (string, error) {
  89. ui, err := strconv.ParseUint(hex, 16, 64)
  90. if err != nil {
  91. return "", err
  92. }
  93. return fmt.Sprintf("%016b", ui), nil
  94. }
  95. func Bin2hex(str string) (string, error) {
  96. i, err := strconv.ParseInt(str, 2, 0)
  97. if err != nil {
  98. return "", err
  99. }
  100. return strconv.FormatInt(i, 16), nil
  101. }
  102. func main() {
  103. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  104. if len(os.Args) < 2 {
  105. //Default options
  106. folders, files := scan_recursive(dir, []string{os.Args[0]})
  107. for i := 0; i < len(files); i++ {
  108. thisFilepath := strings.ReplaceAll(files[i], "\\", "/")
  109. filename := path.Base(thisFilepath)
  110. extension := filepath.Ext(filename)
  111. name := filename[0 : len(filename)-len(extension)]
  112. fmt.Println(name)
  113. }
  114. fmt.Println(folders)
  115. } else {
  116. //Given target directory
  117. }
  118. }