'@Folder modUtf8File
Option Explicit

'==========================================================
' UTF-8 파일 입출력 유틸리티
'==========================================================

Public Sub SaveUtf8NoBom(ByVal filePath As String, ByVal textData As String)

    Dim stm As Object
    Dim bytes() As Byte
    
    Set stm = CreateObject("ADODB.Stream")
    
    With stm
        .Type = 2                  ' text
        .Charset = "utf-8"
        .Open
        .WriteText textData
        
        .Position = 0
        .Type = 1                  ' binary
        bytes = .Read
        .Close
    End With
    
    If UBound(bytes) >= 2 Then
        If bytes(0) = &HEF And bytes(1) = &HBB And bytes(2) = &HBF Then
            Dim i As Long
            Dim newBytes() As Byte
            
            ReDim newBytes(UBound(bytes) - 3)
            
            For i = 3 To UBound(bytes)
                newBytes(i - 3) = bytes(i)
            Next i
            
            bytes = newBytes
        End If
    End If
    
    Set stm = CreateObject("ADODB.Stream")
    
    With stm
        .Type = 1                  ' binary
        .Open
        .Write bytes
        .SaveToFile filePath, 2    ' adSaveCreateOverWrite
        .Close
    End With

End Sub

Public Function ReadUtf8Text(ByVal filePath As String) As String

    Dim stm As Object
    
    Set stm = CreateObject("ADODB.Stream")
    
    With stm
        .Type = 2
        .Charset = "utf-8"
        .Open
        .LoadFromFile filePath
        ReadUtf8Text = .ReadText
        .Close
    End With

End Function

Public Sub ConvertFileToUtf8(ByVal filePath As String)

    Dim fso As Object
    Dim ts As Object
    Dim textData As String

    Set fso = CreateObject("Scripting.FileSystemObject")

    '--- 파일 존재 확인 ---
    If Not fso.FileExists(filePath) Then
        Debug.Print "파일 없음: " & filePath
        Exit Sub
    End If

    On Error GoTo ReadErr

    '--- 읽기 ---
    Set ts = fso.OpenTextFile(filePath, 1, False)
    
    If ts.AtEndOfStream Then
        textData = ""
    Else
        textData = ts.ReadAll
    End If
    
    ts.Close

    '--- UTF-8로 저장 ---
    Call SaveUtf8NoBom(filePath, textData)

    Exit Sub

ReadErr:
    Debug.Print "읽기 오류: " & filePath
    Debug.Print Err.Number & " : " & Err.Description

    On Error Resume Next
    If Not ts Is Nothing Then ts.Close

End Sub

Public Function RemoveAttributeLines(ByVal codeText As String) As String

    Dim lines() As String
    Dim result As String
    Dim i As Long
    Dim oneLine As String
    
    codeText = Replace(codeText, vbCrLf, vbLf)
    codeText = Replace(codeText, vbCr, vbLf)
    
    lines = Split(codeText, vbLf)
    
    For i = LBound(lines) To UBound(lines)
        oneLine = lines(i)
        
        If Left$(Trim$(oneLine), 10) <> "Attribute " Then
            result = result & oneLine & vbCrLf
        End If
    Next i
    
    RemoveAttributeLines = result

End Function

Public Function ReadTextFile(ByVal filePath As String) As String

    Dim fso As Object
    Dim ts As Object
    
    If Dir$(filePath) = vbNullString Then Exit Function
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Set ts = fso.OpenTextFile(filePath, 1, False)
    
    If Not ts.AtEndOfStream Then
        ReadTextFile = ts.ReadAll
    End If
    
    ts.Close

End Function


'==========================================================
' vbComp.Export 결과에서 "기본값" Attribute 줄만 제거
'
' - VB_Name 줄과, 값이 기본값(False)인 클래스 속성 줄은 제거
' - VB_PredeclaredId = True 같은 의미 있는 속성 줄과
'   프로시저 단위 Attribute(Item.VB_UserMemId = 0 등)는 남긴다
'   (지우면 다시 가져올 때 클래스 동작이 바뀌기 때문)
'==========================================================
Public Function RemoveDefaultAttributeLines(ByVal codeText As String) As String

    Dim lines() As String
    Dim result As String
    Dim i As Long
    Dim oneLine As String
    
    codeText = Replace(codeText, vbCrLf, vbLf)
    codeText = Replace(codeText, vbCr, vbLf)
    
    lines = Split(codeText, vbLf)
    
    For i = LBound(lines) To UBound(lines)
        oneLine = lines(i)
        
        If Not IsDefaultAttributeLine(oneLine) Then
            result = result & oneLine & vbCrLf
        End If
    Next i
    
    RemoveDefaultAttributeLines = result

End Function

Private Function IsDefaultAttributeLine(ByVal oneLine As String) As Boolean

    Dim compact As String
    
    compact = Replace(Trim$(oneLine), " ", "")
    
    Select Case compact
        Case "AttributeVB_GlobalNameSpace=False", _
             "AttributeVB_Creatable=False", _
             "AttributeVB_PredeclaredId=False", _
             "AttributeVB_Exposed=False"
            IsDefaultAttributeLine = True
        Case Else
            IsDefaultAttributeLine = (Left$(compact, 17) = "AttributeVB_Name=")
    End Select

End Function

