ASP에서 CodePage 고찰

ASP에서 CodePage 설정에 따라 문자열이 어떻게 처리가 되는지를 살펴보자.

<%@ CodePage=65001 Language="VBScript"%>

<%  
Option Explicit

Dim sFileName, sBuffer

sFileName = "utf8.txt"  
sBuffer = ReadFromTextFile(sFileName, "utf-8")  
ShowStringInfo sBuffer

'read utf-8  
Function ReadFromTextFile (sFile, sCharSet)  
    Dim oStream, str

    Set oStream = Server.CreateObject("adodb.stream")  
    oStream.Type = 2 'for text type  
    oStream.Mode = 3 'adModeWrite  
    oStream.Charset = sCharSet  
    oStream.Open  
    oStream.LoadFromFile server.MapPath(sFile)  
    str = oStream.ReadText  
    oStream.Close  
    Set oStream=nothing

    ReadFromTextFile=str  
End Function

Function ShowStringInfo(s)  
    Dim sHex

    sHex = GetHexString(s)  
    Response.Write s & ", Length=" & Len(s) & ", Hex=" & sHex  
End Function

Function GetHexString(s)  
    Dim i  
    Dim char  
    Dim result

    result = "0x"  
    For i = 1 To Len(s)  
        char = AscW(Mid(s, i, 1))  
        result = result & "[" & hex(char) & "]"  
    Next

    GetHexString = result  
End Function

%>    

위의 결과는

한글.txt, Length=6, Hex=0x\[D55C\]\[AE00\]\[2E\]\[74\]\[78\]\[74\]  

만약 CodePage를 949로 바꾸었다면

한글.txt, Length=6, Hex=0x\[D55C\]\[AE00\]\[2E\]\[74\]\[78\]\[74\]  

브라우져로 보면 “한글.txt”가 같아 보이지만 전자는 UTF-8 코드이고, 후자는 CP949이다.

ReadFromTextFile에서 리턴되는 문자열은 CodePage에 상관없이 유니코드페이지가 사용되고, Response.Write 하면서 CodePage에 따라서 문자열 변환이 이루어 진다.

PHP의 print_r 함수를 ASP로 구현해보자.

PHP에는 변수의 내용을 쉽게 볼 수 있는 print_r 이란 함수가 있다. 변수형태와 상관없이 변수명만 인자로 넘겨주면 변수에 저장된 내용을 재귀적으로 표시해 줘서 디버깅시 특히 요긴하게 쓸 수 있다. ASP에서는 안타깝게도 유사한 함수가 없어서 비슷한 기능을 하는 함수를 만들어 봤다.

<%  
Sub print_r(var)  
    Dim myPrint  

    Set myPrint = New clsPrint  
    myPrint.print var, 0  
End Sub  

Class clsPrint  
    Public Sub print(var, nIndent)  
        Dim nType, sTypeName  
          
        'Response.Write TypeName(var)  

        nType = VarType(var)  
        Select Case nType  
        Case 0:  
            Response.Write "(vbEmpty)"  
        Case 1:  
            Response.Write "(vbNull)"  
        Case 2:  
            Response.Write var & "(vbIngerger)"  
        Case 3:  
            Response.Write var & "(vbLong)"  
        Case 4,5:  
            Response.Write var & "(vbSingle)"  
        Case 6:  
            Response.Write var & "(vbCurrency)"  
        Case 7:  
            Response.Write var & "(vbDate)"  
        Case 8:  
            Response.Write var & "(vbString)"  
        Case 9:'vbObject  
            sTypeName = TypeName(var)  
            Select Case sTypeName  
            Case "ISessionObject": 'Session  
                print_session var, nIndent  
            Case "IRequest": 'Request  
                print_request var, nIndent  
            Case "IApplicationObject": 'Application  
                print_application var, nIndent  
            Case Else  
                Response.Write "Unimpleneted TypeName:" & sTypeName  
            End Select  
        Case 10:  
            Response.Write "(vbError)"  
        Case 11:  
            Response.Write var & "(vbBoolean)"  
        Case 12:  
            Response.Write "(vbDataObject)"  
        Case 13:  
            Response.Write "(vbDecimal)"  
        Case 14:  
            Response.Write "(vbByte)"  
        Case 8192,8204:  
            print_array var, nIndent + 1  
        Case Else:  
            Response.Write "VarType:" & nType  
        End Select  

        Response.Write vbCrLf  
    End Sub  

    Sub print_array(var, nIndent)  
        print_list "Array", var, nIndent  
    End Sub  

    Sub print_request(var, nIndent)  
        print_name "Request", nIndent  
        print_openbrace nIndent  

        print_dict "Request.Form", var.Form, nIndent + 1  
        print_dict "Request.QueryString", var.QueryString, nIndent + 1  
        print_dict "Request.Cookies", var.Cookies, nIndent + 1  
        print_dict "Request.ServerVariables", var.ServerVariables, nIndent + 1  

        print_closebrace nIndent  
    End Sub  

    Sub print_application(var, nIndent)  
        print_name "Application", nIndent  
        print_openbrace nIndent  

        print_dict "Application.StaticObjects", var.StaticObjects, nIndent + 1  
        print_dict "Application.Contents", var.Contents, nIndent + 1  

        print_closebrace nIndent  
    End Sub  

    Sub print_session(var, nIndent)  
        print_name "Session", nIndent  
        print_openbrace nIndent  

        print_dict "Session.StaticObjects", var.StaticObjects, nIndent + 1  
        print_dict "Session.Contents", var.Contents, nIndent + 1  

        print_closebrace nIndent  
    End Sub  

    Sub print_dict(sTypeName, var, nIndent)  
        Dim sKey  

        print_name sTypeName, nIndent  
        print_openbrace nIndent  

        For Each sKey In var  
            print_key sKey, nIndent + 1  
            print var(sKey), nIndent + 1  
        Next  

        print_closebrace nIndent  
    End Sub  

    Sub print_list(sTypeName, var, nIndent)  
        Dim vValue, nIndex  

        Response.Write sTypeName & vbCrLf  
        print_openbrace nIndent  

        nIndex = 0  
        For Each vValue In var  
            print_key nIndex, nIndent + 1  
            print vValue, nIndent + 1  
            nIndex = nIndex + 1  
        Next  

        print_closebrace nIndent  
    End Sub  

    Private Sub print_name(sName, nIndent)  
        print_indent nIndent  
        Response.Write sName & vbCrLf  
    End Sub  

    Private Sub print_key(sKey, nIndent)  
        print_indent nIndent  
        Response.Write "[" & sKey & "] => "  
    End Sub  

    Private Sub print_openbrace(nIndent)  
        print_indent nIndent  
        Response.Write "(" & vbCrLf  
    End Sub  

    Private Sub print_closebrace(nIndent)  
        print_indent nIndent  
        Response.Write ")" & vbCrLf  
    End Sub  

    Private Sub print_indent(nIndent)  
        Response.Write String(nIndent * 4, " ")  
    End Sub  
End Class  


a = "123"  
b = Split("1 2 3 4 5", " ")  
print_r a  
print_r b  
print_r Session  
'print_r Application  
'print_r Request  
%>   

위를 브라우져에서 실행해서 소스보기를 하면 다음의 결과를 얻을 수 있다.

123(vbString)
Array
    (
        [0] => 1(vbString)
        [1] => 2(vbString)
        [2] => 3(vbString)
        [3] => 4(vbString)
        [4] => 5(vbString)
    )

Session
(
    Session.StaticObjects
    (
    )
    Session.Contents
    (
    )
)

배열도 지원하고, Application, Session, Request 객체의 내용도 볼 수 있다. 자체적으로 쓰는 객체는 “Unimpleneted TypeName”이란 결과가 나올텐데, 이는 소스에다 해당 객체를 출력하는 부분을 추가해 주면 된다.

VBScript의 GetRef을 이용하여 간단한 이벤트 드리븐 프로그래밍하기

VBScript는 객체 지향적인 측면에서는 많이 부족한 언어다. 상속이나 다형성을 전혀 제공하고 있지 않아서 클래스를 만드는게 오히려 불현할 때가 더 많은 게 사실이다. 조금이나마 이런 단점을 보안해 줄 수 있는 함수가 하나 있는데 GetRef 이다.

GetRef는 인자로 넘긴 문자열에 해당하는 함수 포인터를 반환하는 기능을 하는 함수이다.  이 함수를 이용하여 VBscript에서 간단하게 이벤트 드리븐(event driven) 프로그래밍하는 좋은 예제가 있어서 소개할려고 한다.



VBScript doesn’t have an event implementation so if you fancy having features like attaching handlers which will respond to specific events on your object you can do it simply by using the GetRef function and a bit of “syntactic sugar”.


I’m using ASP in these examples cos it’s easy.


Example 1 – Simple Events

‘Create a handler
Function MyHandler()
Response.Write “Hello from the handler!”
End Function

‘Create an event
Dim OnLoad
Set OnLoad = GetRef(“MyHandler”)

‘Fire the event
OnLoad()


Here we’ve created a simple event which takes one handler function and fired the event which in turn has called the function we attached.


To turn this in to a more useful event system we can use an array for the OnLoad event variable thus…

‘Create some handlers
Function MyHandler1()
Response.Write “Hello from handler 1!”
End Function

Function MyHandler2()
Response.Write “Hello from handler 2!”
End Function

‘Create an event
Dim OnLoad
OnLoad = Array(GetRef(“MyHandler1”), GetRef(“MyHandler2”))

‘Fire the event
For Each handler In OnLoad
handler()
Next


Example 2 – Event Arguments


In most event implementations the event handlers take one argument, passed to them by the fired event, which contains things like the type of event and a reference to the object on which it was fired etc.

‘Create a handler which takes one argument
Function MyHandler(e)
Response.Write “Hello from the handler – i was called by ” & e
End Function

‘Create two events
Dim OnLoad
Set OnLoad = GetRef(“MyHandler”)

Dim OnUnload
Set OnUnload = GetRef(“MyHandler”)

‘Fire the events
OnLoad(“Load”)
OnUnload(“Unload”)


Wrapping it up


We’ve established we can do all the basics of events, now all we need to do is wrap it up in a few classes to make it usable.


First we need an Event class that we can instantiate for each event we want. This will have to expose an event arguments property and methods for attaching handlers and firing the event. It will also have to keep track internally of the attached handlers. Lets have a go…

Class clsEvent

‘An array to keep track of our handlers
Private aryHandlers()

‘Our event arguments object to be passed
‘to the handlers
Public EventArgs

Private Sub Class_Initialize()
ReDim aryHandlers(-1)
Set EventArgs = New clsEventArgs
End Sub

Private Sub Class_Terminate()
Set EventArgs = Nothing
Erase aryHandlers
End Sub

‘Method for adding a handler
Public Function AddHandler(strFunctionName)
ReDim Preserve aryHandlers(UBound(aryHandlers) + 1)
Set aryHandlers(UBound(aryHandlers)) = _
GetRef(strFunctionName)
End Function

‘Method for firing the event
Public Function Fire(strType, objCaller)
EventArgs.EventType = strType
Set EventArgs.Caller = objCaller
For Each f In aryHandlers
f(EventArgs)
Next
End Function

End Class


Next we need an EventArgs class for passing data about the event to the handlers. This just needs three properties; event type, caller and an arguments collection for event type specific things.

Class clsEventArgs

Public EventType, Caller, Args

Private Sub Class_Initialize()
Set Args = CreateObject(“Scripting.Dictionary”)
End Sub

Private Sub Class_Terminate()
Args.RemoveAll
Set Args = Nothing
End Sub

End Class


Next our class that has an event, in this case an OnLoad which fires after the object’s Load method is called. We’ll also create a few handlers and do a trial run.

Class MyClass

Public OnLoad

Private Sub Class_Initialize()
‘Setting up our event
Set OnLoad = New clsEvent

‘Adding an argument
OnLoad.EventArgs.Args.Add “arg1”, “Hello”
End Sub

Public Function Load()
Response.Write “loading the object here!<br />”

‘Firing the event
OnLoad.Fire “load”, Me
End Function

End Class

‘A couple of handling function for the events
Function EventHandler(e)
Response.Write “<h2>EventHandler</h2>”
Response.Write “<p>Event “”” & e.EventType & “”” fired by object
of type ” & TypeName(e.Caller) & “.</p>”
End Function

Function EventHandler2(e)
Response.Write “<h2>EventHandler2</h2>”
For Each x In e.Args
Response.Write x & “: ” & e.Args(x) & “<br />”
Next
End Function

‘instantiate the object, attach the handlers and call the load
Set myObj = New MyClass
myObj.OnLoad.AddHandler(“EventHandler”)
myObj.OnLoad.AddHandler(“EventHandler2”)
myObj.Load()


Event based programming reverses the responsibility for code execution within your program. In conventional procedural programming it would be the responsibility of the myObj class to make sure the two event handlers were fired when it’s Load method was called. By using an OnLoad event instead myObj doesn’t have to know anything about the environment in which its executing, it just blindly fires the event and any attached handlers will be called. In this way you can add additional functions which run when myObj’s Load method is called without modifying MyClass.


In more complex systems being able to add functionality with a minimum of intrusion into other parts of the system is a big bonus and event based programming is an easy way of achieving it.


출처: http://derek-says.blogspot.com/2006/10/simple-event-driven-programming-using.html