Advertisement
AZJIO

_Setting_

Aug 12th, 2012
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 12.24 KB | None | 0 0
  1. #include-once
  2.  
  3. ; =======================================
  4. ; Title .........: Setting
  5. ; AutoIt Version : 3.2.12.1+
  6. ; Language ......: English
  7. ; Description ...: Операции с конфигурационными данными
  8. ; Author .........: AZJIO
  9. ; =======================================
  10.  
  11. ; #CURRENT# =============================
  12. ; _Setting_Read
  13. ; _Setting_Write
  14. ; _Setting_Delete
  15. ; _Setting_ReadSection
  16. ; _Setting_ReadSectionNames
  17. ; _Setting_WriteSection
  18. ; _Setting_RenameSection
  19. ; _Setting_MigrateIniToReg
  20. ; _Setting_MigrateRegToIni
  21. ; =======================================
  22.  
  23. ; #FUNCTION# ;=================================================================================
  24. ; Description ........: Reads a value from ini-file or registry.
  25. ; Parameters:
  26. ;       $sPath - Path or Key
  27. ;       $sSection - Section or KeyName
  28. ;       $sValueName - Key or ValueName
  29. ;       $sDefault - by default
  30. ;       $iReg - (0,1)
  31. ;                  |0 - ini-file
  32. ;                  |1 - registry
  33. ; Return values ....: Success - String
  34. ; Author(s) ..........: AZJIO
  35. ; ============================================================================================
  36. Func _Setting_Read($sPath, $sSection, $sValueName, $sDefault, $iReg = 0)
  37.     Local $sValue
  38.     If $iReg Then
  39.         $sValue = RegRead($sPath & '\' & $sSection, $sValueName)
  40.         If @error Then $sValue = $sDefault
  41.     Else
  42.         $sValue = IniRead($sPath, $sSection, $sValueName, $sDefault)
  43.     EndIf
  44.     Return $sValue
  45. EndFunc
  46.  
  47. ; #FUNCTION# ;=================================================================================
  48. ; Description ........: Writes a value to a ini-file or registry.
  49. ; Parameters:
  50. ;       $sPath - Path or Key
  51. ;       $sSection - Section or KeyName
  52. ;       $sValueName - Key or ValueName
  53. ;       $sValue - Value
  54. ;       $iReg - (0,1)
  55. ;                  |0 - ini-file
  56. ;                  |1 - registry
  57. ; Return values ....: Success - 1
  58. ;                   Failure - 0, @error = 1
  59. ; Author(s) ..........: AZJIO
  60. ; ============================================================================================
  61. Func _Setting_Write($sPath, $sSection, $sValueName, $sValue, $iReg = 0)
  62.     If $iReg Then
  63.         $sValue = StringRegExpReplace($sValue, '^"(.*?)"$', '\1')
  64.         $iRes = RegWrite($sPath & '\' & $sSection, $sValueName, "REG_SZ", $sValue)
  65.     Else
  66.         $iRes = IniWrite($sPath, $sSection, $sValueName, $sValue)
  67.     EndIf
  68.     Return SetError(Not $iRes, 0, $iRes)
  69. EndFunc
  70.  
  71. ; #FUNCTION# ;=================================================================================
  72. ; Description ........: Writes a section to a ini-file or registry.
  73. ; Parameters:
  74. ;       $sPath - Path or Key
  75. ;       $sSection - Section or KeyName
  76. ;       $Data - Data, key=value, array or string, delimited by @LF
  77. ;       $iIndex - If an array is passed as data, this specifies the index to start writing from
  78. ;       $iReg - (0,1)
  79. ;                  |0 - ini-file
  80. ;                  |1 - registry
  81. ; Return values ....: Success - 1
  82. ;                   Failure - 0, @error = 1, @extended = count
  83. ; Author(s) ..........: AZJIO
  84. ; ============================================================================================
  85. Func _Setting_WriteSection($sPath, $sSection, $Data, $iIndex = 1, $iReg = 0)
  86.     Local $error = 0
  87.     If $iReg Then
  88.         If RegDelete($sPath & '\' & $sSection) = 2 Then Return SetError(2, 0, 0)
  89.         If IsString($Data) Then
  90.             $iIndex = 1
  91.             Local $aString = StringSplit(StringStripWS($Data, 3), @LF)
  92.             Dim $Data[$aString[0] + 1][2] = [[$aString[0]]]
  93.             $j = 0
  94.             For $i = 1 To $aString[0]
  95.                 $aTmp = StringRegExp($aString[$i], '^(\S+?)\s*=\s*(.*?)\s*$', 3)
  96.                 If @error Then
  97.                     $error += 1
  98.                     ContinueLoop
  99.                 EndIf
  100.                 $j += 1
  101.                 $aTmp[1] = StringRegExpReplace($aTmp[1], '^"(.*?)"$', '\1')
  102.                 $Data[$j][0] = $aTmp[0]
  103.                 $Data[$j][1] = $aTmp[1]
  104.             Next
  105.             ReDim $Data[$j + 1][2]
  106.         EndIf
  107.         For $i = $iIndex To UBound($Data) - 1
  108.             If Not RegWrite($sPath & '\' & $sSection, $Data[$i][0], "REG_SZ", $Data[$i][1]) Then $error += 1
  109.         Next
  110.         If $error Then
  111.             $iRes = 0
  112.         Else
  113.             $iRes = 1
  114.         EndIf
  115.     Else
  116.         $iRes = IniWriteSection($sPath, $sSection, $Data, $iIndex)
  117.     EndIf
  118.     Return SetError(Not $iRes, $error, $iRes)
  119. EndFunc
  120.  
  121. ; #FUNCTION# ;=================================================================================
  122. ; Description ........: Renames a section in a ini-file or registry.
  123. ; Parameters:
  124. ;       $sPath - Path or Key
  125. ;       $sSection - Section or KeyName
  126. ;       $sNewSection - The new Section/KeyName name
  127. ;       $flag - (0,1)
  128. ;                  |0 - Fail if "new section" already exists
  129. ;                  |1 - Overwrite "new section".
  130. ;       $iReg - (0,1)
  131. ;                  |0 - ini-file
  132. ;                  |1 - registry
  133. ; Return values ....: Success - 1
  134. ;                   Failure - 0, @error = 1
  135. ; Author(s) ..........: AZJIO
  136. ; ============================================================================================
  137. Func _Setting_RenameSection($sPath, $sSection, $sNewSection, $flag = 0, $iReg = 0)
  138.     If $iReg Then
  139.         $aKeyValue = _Setting_ReadSection($sPath, $sSection, 1)
  140.         If @error Then Return SetError(1, 0, 0)
  141.         If _RegExists($sPath & '\' & $sNewSection) Then
  142.             If $flag Then
  143.                 If RegDelete($sPath & '\' & $sNewSection) = 2 Then Return SetError(2, 0, 0)
  144.             Else
  145.                 Return SetError(1, 0, 0)
  146.             EndIf
  147.         EndIf
  148.         _Setting_WriteSection($sPath, $sNewSection, $aKeyValue, 1, 1)
  149.         If @error Then
  150.             $iRes = 0
  151.         Else
  152.             $iRes = 1
  153.         EndIf
  154.         If RegDelete($sPath & '\' & $sSection) = 2 Then Return SetError(3, 0, $iRes)
  155.     Else
  156.         $iRes = IniRenameSection($sPath, $sSection, $sNewSection, $flag)
  157.     EndIf
  158.     Return SetError(Not $iRes, 0, $iRes)
  159. EndFunc
  160.  
  161. ; #FUNCTION# ;=================================================================================
  162. ; Description ........: Deletes a value from a ini-file or registry.
  163. ; Parameters:
  164. ;       $sPath - Path or Key
  165. ;       $sSection - Section or KeyName
  166. ;       $sValueName - Key or ValueName
  167. ;       $iReg - (0,1)
  168. ;                  |0 - ini-file
  169. ;                  |1 - registry
  170. ; Return values ....: Success - True
  171. ;                   Failure - False, @error = 1
  172. ; Author(s) ..........: AZJIO
  173. ; ============================================================================================
  174. Func _Setting_Delete($sPath, $sSection, $sValueName = '', $iReg = 0)
  175.     If $iReg Then
  176.         If $sValueName == '' Then
  177.             $fRes = (RegDelete($sPath & '\' & $sSection) = 2)
  178.         Else
  179.             $fRes = (RegDelete($sPath & '\' & $sSection, $sValueName) = 2)
  180.         EndIf
  181.     Else
  182.         If $sValueName == '' Then
  183.             $fRes = (IniDelete($sPath, $sSection) = 0)
  184.         Else
  185.             $fRes = (IniDelete($sPath, $sSection, $sValueName) = 0)
  186.         EndIf
  187.     EndIf
  188.     Return SetError($fRes, 0, Not $fRes)
  189. EndFunc
  190.  
  191. ; #FUNCTION# ;=================================================================================
  192. ; Description ........: Reads all key/value pairs from a section in ini-file or registry.
  193. ; Parameters:
  194. ;       $sPath - Path or Key
  195. ;       $sSection - Section or KeyName
  196. ;       $iReg - (0,1)
  197. ;                  |0 - ini-file
  198. ;                  |1 - registry
  199. ; Return values ....: Success - Returns a 2 dimensional array where Array[n][0] is the key and Array[n][1] is the value
  200. ;                   Failure - @error = 1
  201. ; Remarks ..........: Array[0][0] = number of elements
  202. ; Author(s) ..........: AZJIO
  203. ; ============================================================================================
  204. Func _Setting_ReadSection($sPath, $sSection, $iReg = 0)
  205.     If $iReg Then
  206.         Local $aKeyValue[1][2] = [[0]], $i = 0, $sValueName
  207.         While 1
  208.             $i += 1
  209.             $sValueName = RegEnumVal($sPath & '\' & $sSection, $i)
  210.             If @error Then ExitLoop
  211.             ReDim $aKeyValue[$i + 1][2]
  212.             $aKeyValue[$i][0] = $sValueName
  213.             $aKeyValue[$i][1] = RegRead($sPath & '\' & $sSection, $sValueName)
  214.         WEnd
  215.         If $i = 1 Then Return SetError(1, 0, 1)
  216.         $aKeyValue[0][0] = $i - 1
  217.     Else
  218.         Local $aKeyValue = IniReadSection($sPath, $sSection)
  219.         If @error Then Return SetError(1, 0, 1)
  220.     EndIf
  221.     Return $aKeyValue
  222. EndFunc
  223.  
  224. ; #FUNCTION# ;=================================================================================
  225. ; Description ........: Reads all sections in a ini-file or registry.
  226. ; Parameters:
  227. ;       $sPath - Path or Key
  228. ;       $iReg - (0,1)
  229. ;                  |0 - ini-file
  230. ;                  |1 - registry
  231. ; Return values ....: Success - Returns an array of all section names
  232. ;                   Failure - 0, @error = 1
  233. ; Author(s) ..........: AZJIO
  234. ; ============================================================================================
  235. Func _Setting_ReadSectionNames($sPath, $iReg = 0)
  236.     If $iReg Then
  237.         Local $sSection[1] = [0], $i = 0, $sKey
  238.         While 1
  239.             $i += 1
  240.             $sKey = RegEnumKey($sPath, $i)
  241.             If @error Then ExitLoop
  242.             ReDim $sSection[$i + 1]
  243.             $sSection[$i] = $sKey
  244.         WEnd
  245.         If $i = 0 Then Return SetError(1, 0, 0)
  246.         $sSection[0] = $i - 1
  247.     Else
  248.         Local $sSection = IniReadSectionNames($sPath)
  249.         If @error Then Return SetError(1, 0, 0)
  250.     EndIf
  251.     Return $sSection
  252. EndFunc
  253.  
  254. ; #FUNCTION# ;=================================================================================
  255. ; Description ........: Copies given from registry in ini-file.
  256. ; Parameters:
  257. ;       $sPath - Path
  258. ;       $sKey - Key
  259. ;       $flag - (0, 1, 2)
  260. ;                  |0 - none (Default)
  261. ;                  |1 - Deletes the data in the registry before the migration
  262. ;                  |2 - Removes the .ini file after migration
  263. ; Return values ....: Success True - @error = 0, @extended = number of lines
  264. ;                   Failure False - @error = number of errors, @extended = number of successful lines
  265. ; Author(s) ..........: AZJIO
  266. ; ============================================================================================
  267. Func _Setting_MigrateIniToReg($sPath, $sKey, $flag = 0)
  268.     Local $iFailure, $iSuccess, $aKeyValue, $i, $j, $sSection, $tmp
  269.     If BitAND($flag, 1) Then RegDelete($sKey)
  270.     $sSection = IniReadSectionNames($sPath)
  271.     If @error Then Return SetError(1, 0, 0)
  272.     For $i = 1 To $sSection[0]
  273.         $aKeyValue = IniReadSection($sPath, $sSection[$i])
  274.         If Not @error Then
  275.             For $j = 1 To $aKeyValue[0][0]
  276.                 If RegWrite($sKey & '\' & $sSection[$i], $aKeyValue[$j][0], "REG_SZ", $aKeyValue[$j][1]) Then
  277.                     $iSuccess += 1
  278.                 Else
  279.                     $iFailure += 1
  280.                 EndIf
  281.             Next
  282.         EndIf
  283.     Next
  284.     If BitAND($flag, 2) Then FileDelete($sPath)
  285.     Return SetError($iFailure, $iSuccess, Not $iFailure)
  286. EndFunc
  287.  
  288. ; #FUNCTION# ;=================================================================================
  289. ; Description ........: Copies given from ini-file in registry.
  290. ; Parameters:
  291. ;       $sKey - Key
  292. ;       $sPath - Path
  293. ;       $flag - (0, 1, 2)
  294. ;                  |0 - none (Default)
  295. ;                  |1 - Removes all sections in .ini file before migration
  296. ;                  |2 - Deletes the data in the registry after the migration
  297. ;                  |4 - use IniWrite instead of IniWriteSection, without removing existing
  298. ; Return values ....: Success - @error = 0, @extended = number of lines
  299. ;                   Failure - @error = number of errors, @extended = number of successful lines
  300. ; Author(s) ..........: AZJIO
  301. ; ============================================================================================
  302. Func _Setting_MigrateRegToIni($sKey, $sPath, $flag = 0)
  303.     Local $iFailure, $iSuccess, $aKeyValue, $i, $j, $sSection, $tmp
  304.     If BitAND($flag, 1) Then
  305.         $tmp = IniReadSectionNames($sPath)
  306.         If Not @error Then
  307.             For $i = 1 To $tmp[0]
  308.                 IniDelete($sPath, $tmp[0])
  309.             Next
  310.         EndIf
  311.     EndIf
  312.     $sSection = _Setting_ReadSectionNames($sKey, 1)
  313.     If @error Then Return SetError(1, 0, 0)
  314.     For $i = 1 To $sSection[0]
  315.         $aKeyValue = _Setting_ReadSection($sKey, $sSection[$i], 1)
  316.         If Not @error Then
  317.             For $j = 1 To $aKeyValue[0][0]
  318.                 If StringLeft($aKeyValue[$j][1], 1) = '"' And StringRight($aKeyValue[$j][1], 1) = '"' Then $aKeyValue[$j][1] = '"' & $aKeyValue[$j][1] & '"'
  319.                 If StringRegExp(StringLeft($aKeyValue[$j][1], 1) & StringRight($aKeyValue[$j][1], 1), '\h') Then $aKeyValue[$j][1] = '"' & $aKeyValue[$j][1] & '"'
  320.             Next
  321.             If BitAND($flag, 4) Then
  322.                 For $j = 1 To $aKeyValue[0][0]
  323.                     If IniWrite($sPath, $sSection[$i], $aKeyValue[$j][0], $aKeyValue[$j][1]) Then
  324.                         $iSuccess += 1
  325.                     Else
  326.                         $iFailure += 1
  327.                     EndIf
  328.                 Next
  329.             Else
  330.                 If _Setting_WriteSection($sPath, $sSection[$i], $aKeyValue, 1, 0) Then
  331.                     $iSuccess += 1
  332.                 Else
  333.                     $iFailure += 1
  334.                 EndIf
  335.             EndIf
  336.         EndIf
  337.     Next
  338.     If BitAND($flag, 2) Then RegDelete($sKey)
  339.     Return SetError($iFailure, $iSuccess, Not $iFailure)
  340. EndFunc
  341.  
  342. Func _RegExists($sKey)
  343.     RegRead($sKey, '')
  344.     Return Not (@error > 0)
  345. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement