Advertisement
AZJIO

reconf

Jan 17th, 2021 (edited)
2,952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. EnableExplicit
  4.  
  5. CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  6.     #PathSeparator = "\"
  7.     #StringCase = #PB_String_NoCase
  8. CompilerElse
  9.     #PathSeparator = "/"
  10.     #StringCase = #PB_String_CaseSensitive
  11. CompilerEndIf
  12.  
  13. Structure udtFileInfo
  14.     file.s
  15.     ;   size.q
  16.     ;   date.i
  17. EndStructure
  18.  
  19. Declare GetFileInfoRecursive(List FileInfo.udtFileInfo(), Directory.s, Recursive = #True, Include.s = "", Trim = 0)
  20.  
  21. Global Stop, PathMirror$, PathOrig$, Value$, FileOrig$, FileMirror$, Format, Text$
  22.  
  23. ; ——————————————————————————————————————————————————
  24.  
  25. ; PathMirror$ = PathRequester("Выберите зеркало", GetHomeDirectory())
  26. PathMirror$ = "/home/user/Загрузки/config_my/"
  27. If PathMirror$ = ""
  28.     End
  29. EndIf
  30. ; PathOrig$ = PathRequester("Выберите оригинальный", GetPathPart(RTrim(GetUserDirectory( #PB_Directory_Desktop), "/")) + ".config")
  31. PathOrig$ = "/home/user/Загрузки/config_alien/"
  32. If PathOrig$ = ""
  33.     End
  34. EndIf
  35.  
  36. Global NewList FileInfo.udtFileInfo()
  37. Global NewList GroupsConf.s()
  38. Global NewMap KeyValueConf.s()
  39.  
  40. ForEach FileInfo()
  41.     Debug FileInfo()\file
  42. Next
  43.  
  44.  
  45. Define count = GetFileInfoRecursive(FileInfo(), PathMirror$, #True, "ini;conf", Len(PathMirror$)+1)
  46.  
  47. ForEach FileInfo()
  48. ;   Debug PathOrig$+FileInfo()\file
  49. ;   Debug PathMirror$+FileInfo()\file
  50.    
  51.     FileOrig$ = PathOrig$+FileInfo()\file
  52.     If FileSize(FileOrig$) > 1
  53.         If ReadFile(0, FileOrig$)
  54.             Format=ReadStringFormat(0) ; читаем метку файла BOM
  55.             CompilerIf #PB_Compiler_Debugger = 1
  56.                 Debug FileOrig$
  57.                 Select Format
  58.                     Case #PB_Ascii
  59.                         Debug "Ascii или UTF без BOM"
  60.                     Case #PB_UTF8
  61.                         Debug "UTF-8 с BOM"
  62.                     Case #PB_Unicode
  63.                         Debug "UTF-16 LE с BOM"
  64.                     Case #PB_UTF16BE
  65.                         Debug "UTF-16 BE с BOM"
  66.                     Case #PB_UTF32
  67.                         Debug "UTF-32 LE с BOM"
  68.                     Case #PB_UTF32BE
  69.                         Debug "UTF-32 BE с BOM"
  70.                     Default
  71.                         Debug "неизвестный формат"
  72.                 EndSelect
  73.             CompilerEndIf
  74.             CloseFile(0)
  75.         EndIf
  76.         FileMirror$ = PathMirror$+FileInfo()\file
  77.         If OpenPreferences(FileMirror$)
  78.             ExaminePreferenceGroups()
  79.             While NextPreferenceGroup() ; Пока находит группы
  80.                 AddElement(GroupsConf())
  81.                 GroupsConf() = PreferenceGroupName()
  82. ;               MessageRequester("группы", GroupsConf())
  83.             Wend
  84.             ClosePreferences()
  85.         EndIf
  86.        
  87.         ForEach GroupsConf()
  88.             If OpenPreferences(FileMirror$) And PreferenceGroup(GroupsConf())
  89.                 ExaminePreferenceKeys()
  90.                 While NextPreferenceKey() ; Следующий ключ
  91.                     KeyValueConf(PreferenceKeyName()) = PreferenceKeyValue()
  92.                 Wend
  93.                 ClosePreferences()
  94.             EndIf
  95.            
  96.            
  97.             If OpenPreferences(FileOrig$) And PreferenceGroup(GroupsConf())
  98.                 ForEach KeyValueConf()
  99.                     Value$ = ReadPreferenceString(MapKey(KeyValueConf()) , "|-|")
  100.                     If KeyValueConf() <> Value$
  101.                         Debug "пишем: " + MapKey(KeyValueConf()) + " = " + KeyValueConf()
  102.                         WritePreferenceString(MapKey(KeyValueConf()), KeyValueConf())
  103.                     EndIf
  104.                 Next
  105.                 ClosePreferences()
  106.             EndIf
  107.             ClearMap(KeyValueConf())
  108.         Next
  109.         ClearList(GroupsConf())
  110. ;       переоткрываем файл, чтобы записать его в оригинальном формате
  111.         If ReadFile(0, FileOrig$)
  112.             Text$ = ReadString(0, #PB_UTF8 | #PB_File_IgnoreEOL)
  113.             CloseFile(0)
  114.             If CreateFile(0, FileOrig$, Format)
  115.                 WriteStringFormat(0 , Format)
  116.                 Text$ = ReplaceString(Text$, " = ", "=")
  117.                 WriteString(0, Text$)
  118.                 CloseFile(0)
  119.             EndIf
  120.         EndIf
  121.     EndIf
  122. Next
  123.  
  124. ; ——————————————————————————————————————————————————
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. Procedure GetFileInfoRecursive(List FileInfo.udtFileInfo(), Directory.s, Recursive = #True, Include.s = "", Trim = 0)
  132.    
  133.     Protected ID, name.s, extension.s
  134.    
  135.     ID = ExamineDirectory(#PB_Any, Directory, "*.*")
  136.     If ID
  137.         While NextDirectoryEntry(ID)
  138.             name = DirectoryEntryName(ID)
  139.             If DirectoryEntryType(ID) = #PB_DirectoryEntry_Directory
  140.                 If Recursive And name <> ".." And name <> "."
  141.                     GetFileInfoRecursive(FileInfo(), Directory+name+#PathSeparator, Recursive, Include, Trim)
  142.                 EndIf
  143.             Else
  144.                 extension = GetExtensionPart(name)
  145.                 If extension And FindString(";" + Include + ";", ";" + extension + ";", 1, #StringCase)
  146.                     If AddElement(FileInfo()) = 0
  147.                         ; Out Of Memory
  148.                         Break
  149.                     EndIf
  150.                     With FileInfo()
  151.                         ; \file = StringField(Directory + name , 2 , PathMirror$)
  152.                         If Trim
  153.                             \file = Mid(Directory + name, Trim)
  154.                         Else
  155.                             \file = Directory + name
  156.                         EndIf
  157.                         ; \size = DirectoryEntrySize(ID)
  158.                         ;  \date = DirectoryEntryDate(ID, #PB_Date_Modified)
  159.                     EndWith
  160.                 EndIf
  161.             EndIf
  162.             ; Abbruch
  163.             If Stop
  164.                 Break
  165.             EndIf
  166.         Wend
  167.         FinishDirectory(ID)
  168.     EndIf
  169.    
  170.     ProcedureReturn ListSize(FileInfo())
  171.    
  172. EndProcedure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement