Advertisement
AZJIO

_IniVirtualSPE_RenameSection

May 17th, 2013
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.06 KB | None | 0 0
  1. $s_INI = @ScriptDir & "\Sample.ini"
  2. $Res = _IniVirtualSPE_RenameSection($s_INI, 'Section2', '-- New Section --', 1)
  3.  
  4. Func _IniVirtualSPE_RenameSection($s_INI, $sSection, $sNewName, $flag = 0)
  5.     $s_INI_Text = FileRead($s_INI)
  6.     If Not _SectionExists($s_INI_Text, $sSection) Then Return SetError(1, 0, 0) ; если старая секция не существует, то завершение функции
  7.     If $flag Then ; Если перезаписывать, то
  8.         _IniVirtualSPE_Delete($s_INI_Text, $sNewName) ; удаляем новую секцию на случай если она существует
  9.         $sSection = StringRegExpReplace($sSection, '[][{}()*+?.\\^$|=<>#]', '\\$0')
  10.         $s_INI_Text = StringRegExpReplace($s_INI_Text, '(?m)^(\h*\[\h*)(' & $sSection & ')(\h*\]\h*\r?)$', '\1' & $sNewName & '\3') ; переименование секции
  11.     Else
  12.         If _SectionExists($s_INI_Text, $sNewName) Then
  13.             Return SetError(2, 0, 0)
  14.         Else
  15.             $sSection = StringRegExpReplace($sSection, '[][{}()*+?.\\^$|=<>#]', '\\$0')
  16.             $s_INI_Text = StringRegExpReplace($s_INI_Text, '(?m)^(\h*\[\h*)(' & $sSection & ')(\h*\]\h*\r?)$', '\1' & $sNewName & '\3') ; переименование секции
  17.         EndIf
  18.     EndIf
  19.     Local $hFile = FileOpen($s_INI, 2)
  20.     FileWrite($hFile, $s_INI_Text)
  21.     FileClose($hFile)
  22.     Return 1
  23. EndFunc   ;==>_IniVirtualSPE_RenameSection
  24.  
  25. Func _SectionExists($s_INI_Text, $sSection)
  26.     $sSection = StringRegExpReplace($sSection, '[][{}()*+?.\\^$|=<>#]', '\\$0')
  27.     Return StringRegExp($s_INI_Text, '(?s)(?:\r\n|\A)\h*\[\h*' & $sSection & '\h*\]\h*(?=\r\n|\s*\z)') ; извлечение контента секции
  28. EndFunc   ;==>_SectionExists
  29.  
  30. Func _IniVirtualSPE_Delete(ByRef $s_INI_Text, $sSection, $sKey = Default)
  31.     If $sKey = Default Then
  32.         $sSection = StringRegExpReplace($sSection, '[][{}()*+?.\\^$|=<>#]', '\\$0')
  33.         $s_INI_Text = StringRegExpReplace($s_INI_Text, '(?s)(?:\r\n|\A)(\h*\[\h*' & $sSection & '\h*\]\h*(?:\r\n\s*[^\[\s].*?|))(?=\r\n\h*\[|\s*\z)', '') ; удаление секции
  34.         If @error Then
  35.             Return SetError(0, 1, 1)
  36.         Else
  37.             Return SetError(0, @extended, 1)
  38.         EndIf
  39.     Else
  40.         Local $aSection = StringRegExp($s_ini_Text, '(?s)(?:\r\n|\A)\h*\[\h*' & StringRegExpReplace($sSection, '[][{}()*+?.\\^$|=<>#]', '\\$0') & '\h*\]\h*(\r\n\s*[^\[\s].*?\h*)(\s*\[\h*.*?\h*\]|\s*\z)', 2) ; извлечение контента секции
  41.         If @error Then Return SetError(0, 1, 1)
  42.         Local $iEndSection = @extended - StringLen($aSection[2]) - 1 ; конец секции
  43.         Local $aKey = StringRegExp($aSection[1], '(?s)(\r\n\h*' & StringRegExpReplace($sKey, '[][{}()*+?.\\^$|=<>#]', '\\$0') & '\h*=\h*.*?\h*)(\s*\r\n|\s*\z)', 2) ; извлечение параметра
  44.         If @error Then Return SetError(0, 2, 1)
  45.         Local $iStart = @extended - StringLen($aKey[1]) - StringLen($aKey[2]) - 1 + $iEndSection - StringLen($aSection[1]) ; начало секции + начало параметра
  46.         $s_INI_Text = StringLeft($s_INI_Text, $iStart) & StringTrimLeft($s_INI_Text, $iStart + StringLen($aKey[1]))
  47.         Return 1
  48.     EndIf
  49. EndFunc   ;==>_IniVirtualSPE_Delete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement