메뉴 여닫기
개인 메뉴 토글
로그인하지 않음
만약 지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.

Autoit ini파일 memory 캐시: 두 판 사이의 차이

데브카페
편집 요약 없음
18번째 줄: 18번째 줄:


==== .ini 파일을 메모리에 로드하고 읽기 ====
==== .ini 파일을 메모리에 로드하고 읽기 ====
<source lang=python>
<source lang=autoit>
; .ini 파일 경로
#include <Array.au3>
Local $sIniFile = "config.ini"
#include <MsgBoxConstants.au3>


; .ini 파일을 메모리에 로드
Global $g_Config = _ReadIniToMemory("config.ini")
Global $iniData = IniLoad($sIniFile)


; 설정 값을 가져오는 함수
; 예시 조회
Func GetIniValue($section, $key, $default = "")
MsgBox(0, "MYKEY", _GetConfig("CONFIG", "MYKEY"))
    ; 섹션이 존재하는지 확인
MsgBox(0, "FONT_NAME", _GetConfig("CONFIG", "FONT_NAME"))
     If Not MapExists($iniData[$section]) Then
 
         Return $default
; ============================
; INI 읽고 메모리에 저장하는 함수
Func _ReadIniToMemory($sFilePath)
    Local $config = ObjCreate("Scripting.Dictionary")
     If Not FileExists($sFilePath) Then
         MsgBox(16, "오류", "INI 파일을 찾을 수 없습니다: " & $sFilePath)
        Exit
     EndIf
     EndIf
     ; 키가 존재하는지 확인
 
     If Not MapExists($iniData[$section][$key]) Then
     Local $sections = IniReadSectionNames($sFilePath)
         Return $default
     If @error Then Return $config
    EndIf
 
    ; 값 반환
    For $i = 1 To $sections[0]
    Return $iniData[$section][$key]
        Local $section = $sections[$i]
        Local $keyValues = IniReadSection($sFilePath, $section)
        If @error Then ContinueLoop
 
         Local $innerDict = ObjCreate("Scripting.Dictionary")
        For $j = 1 To $keyValues[0][0]
            $innerDict.Add($keyValues[$j][0], $keyValues[$j][1])
        Next
        $config.Add($section, $innerDict)
    Next
 
    Return $config
EndFunc
EndFunc


; 예시: 데이터 읽기
; 설정값을 조회하는 함수
Local $username = GetIniValue("Settings", "username", "defaultUser")
Func _GetConfig($sSection, $sKey)
Local $password = GetIniValue("Settings", "password", "defaultPass")
    If Not $g_Config.Exists($sSection) Then Return ""
Local $ip = GetIniValue("Network", "ip", "127.0.0.1")
    Local $sectionDict = $g_Config.Item($sSection)
Local $port = GetIniValue("Network", "port", "80")
    If Not $sectionDict.Exists($sKey) Then Return ""
 
    Return $sectionDict.Item($sKey)
; 출력
EndFunc
MsgBox(0, "INI Data", "Username: " & $username & @CRLF & "Password: " & $password & @CRLF & "IP: " & $ip & @CRLF & "Port: " & $port)
</source>
</source>
* 코드 설명
</pre>
1. IniLoad 함수: .ini 파일을 메모리에 로드합니다. IniLoad는 각 섹션을 딕셔너리로 읽고, 각 섹션 아래 키-값 쌍으로 저장.
2. GetIniValue 함수: 메모리에 저장된 데이터를 읽기 위해 섹션, 키, 그리고 기본값(값이 없을 경우)을 인자로 받아 해당 값을 반환.
3. 예제 읽기: Settings 섹션에서 username, password와 Network 섹션에서 ip, port 값을 읽어옵니다.
</pre>
* .ini 파일 변경시 리로딩 기능 추거개발 필요


[[category:autoit]]
[[category:autoit]]

2025년 7월 28일 (월) 22:45 판

Autoit ini파일 memory 캐시

  • AutoIt에서 .ini 파일을 메모리에 로드해두고 필요할 때마다 읽어오는 방법은 IniRead 또는 IniReadSection 함수를 활용하여 파일의 내용을 변수에 저장하는 것임.
  • .ini 파일 전체를 메모리에 올려두려면 IniReadSectionNames와 IniReadSection 함수 사용
  • ini 파일의 내용을 메모리에 저장해두면, 파일을 매번 다시 읽지 않고도 데이터를 손쉽게 접근.

.ini 파일을 메모리에 로드한 후 필요할 때마다 데이터를 불러오는 방법

.ini 파일 예시

  • 예를 들어 config.ini 파일이라고 가정
[Settings]
username=admin
password=1234

[Network]
ip=192.168.1.1
port=8080

.ini 파일을 메모리에 로드하고 읽기

#include <Array.au3>
#include <MsgBoxConstants.au3>

Global $g_Config = _ReadIniToMemory("config.ini")

; 예시 조회
MsgBox(0, "MYKEY", _GetConfig("CONFIG", "MYKEY"))
MsgBox(0, "FONT_NAME", _GetConfig("CONFIG", "FONT_NAME"))

; ============================
; INI 읽고 메모리에 저장하는 함수
Func _ReadIniToMemory($sFilePath)
    Local $config = ObjCreate("Scripting.Dictionary")
    If Not FileExists($sFilePath) Then
        MsgBox(16, "오류", "INI 파일을 찾을 수 없습니다: " & $sFilePath)
        Exit
    EndIf

    Local $sections = IniReadSectionNames($sFilePath)
    If @error Then Return $config

    For $i = 1 To $sections[0]
        Local $section = $sections[$i]
        Local $keyValues = IniReadSection($sFilePath, $section)
        If @error Then ContinueLoop

        Local $innerDict = ObjCreate("Scripting.Dictionary")
        For $j = 1 To $keyValues[0][0]
            $innerDict.Add($keyValues[$j][0], $keyValues[$j][1])
        Next
        $config.Add($section, $innerDict)
    Next

    Return $config
EndFunc

; 설정값을 조회하는 함수
Func _GetConfig($sSection, $sKey)
    If Not $g_Config.Exists($sSection) Then Return ""
    Local $sectionDict = $g_Config.Item($sSection)
    If Not $sectionDict.Exists($sKey) Then Return ""
    Return $sectionDict.Item($sKey)
EndFunc

Comments