행위

"Autoit iniread utf()"의 두 판 사이의 차이

DB CAFE

 
1번째 줄: 1번째 줄:
=== autoit iniread_utf() ===
+
== autoit fileread utf8 ==
 +
=== IniRead UTF8 방식 ===
 
<source lang=sql>
 
<source lang=sql>
 
Func _IniRead($filename, $section, $key, $default)
 
Func _IniRead($filename, $section, $key, $default)
72번째 줄: 73번째 줄:
  
 
EndFunc
 
EndFunc
 +
</source>
 +
 +
=== IniReadSectionUTF8 ===
 +
<source lang=sql>
 +
func IniReadSectionUTF8(const $filename, const $section)
 +
    local const $file = FileOpen($filename, $FO_UTF8_NOBOM)
 +
    if @error then return SetError(1)
 +
    FileSetPos($file, 0, 0)
 +
    while FileReadLine($file) <> "[" & $section & "]"
 +
        if @error then
 +
            FileClose($filename)
 +
            return SetError(2)
 +
        endif
 +
    wend
 +
    local $result[1][2]
 +
    $result[0][0] = 0
 +
    local $line
 +
    while True
 +
        $line = FileReadLine($file)
 +
        if @error or StringRegExp($line, "^\[.*\]$") then exitloop
 +
        $line = StringSplit($line, "=", $STR_NOCOUNT)
 +
        if @error then continueloop
 +
        redim $result[UBound($result) + 1][2]
 +
        $result[0][0] += 1
 +
        $result[UBound($result) - 1][0] = $line[0]
 +
        $result[UBound($result) - 1][1] = $line[1]
 +
    wend
 +
    FileClose($file)
 +
    return $result
 +
endfunc
 
</source>
 
</source>

2024년 8월 8일 (목) 17:57 기준 최신판

thumb_up 추천메뉴 바로가기


1 autoit fileread utf8[편집]

1.1 IniRead UTF8 방식[편집]

Func _IniRead($filename, $section, $key, $default)

  Local $filename_format = 0
  Local $hFile
  Local $data_ini = ""
  Local $filename_temp = "_temp.ini"
  Local $key_ini = ""

  SetError(0)

  $filename_format = FileGetEncoding($filename, 1)
  If (@error) Then
    SetError(1)
    Return
  EndIf

  If ($filename_format = 512) Then
    $key_ini = IniRead($filename, $section, $key, $default)
    If (@error) Then
      SetError(2)
      Return
    EndIf
  Else
    $hFile = FileOpen($filename, 0+$filename_format)
    If (@error) Then
      SetError(3)
      Return
    Else
      $data_ini = FileRead($hFile)
      If (@error) Then
        SetError(4)
        Return
      EndIf
    EndIf

    FileClose($hFile)
    If (@error) Then
      SetError(5)
      Return
    EndIf

    $hFile = FileOpen($filename_temp, 1+512)
    If (@error) Then
      SetError(6)
      Return
    EndIf

    FileWrite($hFile, $data_ini)
    If (@error) Then
      SetError(7)
      Return
    EndIf

    FileClose($hFile)
    If (@error) Then
      SetError(8)
      Return
    EndIf

    $key_ini = IniRead($filename_temp, $section, $key, $default)

    FileDelete($filename_temp)
    If (@error) Then
      SetError(9)
      Return
    EndIf
  EndIf

  Return ($key_ini)

EndFunc

1.2 IniReadSectionUTF8[편집]

func IniReadSectionUTF8(const $filename, const $section)
    local const $file = FileOpen($filename, $FO_UTF8_NOBOM)
    if @error then return SetError(1)
    FileSetPos($file, 0, 0)
    while FileReadLine($file) <> "[" & $section & "]"
        if @error then
            FileClose($filename)
            return SetError(2)
        endif
    wend
    local $result[1][2]
    $result[0][0] = 0
    local $line
    while True
        $line = FileReadLine($file)
        if @error or StringRegExp($line, "^\[.*\]$") then exitloop
        $line = StringSplit($line, "=", $STR_NOCOUNT)
        if @error then continueloop
        redim $result[UBound($result) + 1][2]
        $result[0][0] += 1
        $result[UBound($result) - 1][0] = $line[0]
        $result[UBound($result) - 1][1] = $line[1]
    wend
    FileClose($file)
    return $result
endfunc