Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[BETA] Unicode File Read Demo

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> Snippet Testing
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Nov 3rd, 2007, 11:42pm    Post subject: [BETA] Unicode File Read Demo Reply with quote

This should be useful for internationalization of LB apps. Let me know it it works for you.

Code:
'== Unicode File Read Demo
'== By Brent D. Thorn, 11/2007
'== License: Public Domain
'== Description:
'   Prompts for a Unicode text file, opens the file, and reads it one line at a
'   time, displaying each line in a message box.

    ' form a string to display in the box's caption
    cap$ = AtoW$("- Notice -")

    ' get the name of a file
    FileDialog "Open Unicode file", "*.txt", file$

    If file$ = "" Then End

    ' open the file for input as #1
    If OpenForInputW(file$, "#1") Then
        Do Until EOF(#1)
            ' read a single line into msg$
            Call LineInputW "#1", msg$

            ' display the text in a message box
            ' Note this is calling the Unicode version

            CallDLL #user32, "MessageBoxW", _
                _NULL As ULong, _
                msg$ As Ptr, _
                cap$ As Ptr, _
                _MB_ICONEXCLAMATION As ULong, _
                ret As Long
        Loop

        ' reached end of file, close it
        Close #1
    Else
        Notice "The selected file is not valid."
    End If

    End

Function OpenForInputW( FileName$, Handle$ )
'-- Opens a file and checks it for UTF-16/Unicode, little-endianness.
'-- Parameters:
'   FileName$ - string holds name of file to open
'   Handle$   - string holds name of handle to map to file
'-- Returns:
'   False (zero) for failure. The handle is invalid.
'   True (nonzero) for success. The handle is valid.

    On Error GoTo [FileError]
    Open FileName$ For Binary As #local1
    hdr$ = Input$(#local1, 2)
    If hdr$ = Chr$(255) + Chr$(254) Then
        OpenForInputW = 1
        MapHandle #local1, Handle$
    Else
        Close #local1
    End If
    Exit Function
[FileError]
End Function

Sub LineInputW Handle$, ByRef Text$
'-- Reads a line of text from a UTF-16/Unicode, little-endian file.
'-- Parameters:
'   Handle$ - string holding a handle's name
'   Text$   - string variable to hold the result

    crlf$ = Chr$(13) + Chr$(0) + Chr$(10) + Chr$(0)
    Text$ = ""
    Do Until EOF(#Handle$)
        Text$ = Text$ + Input$(#Handle$, 1)
        If Right$(Text$, 4) = crlf$ Then Exit Do
    Loop
    If Right$(Text$, 4) = crlf$ Then
        Text$ = Left$(Text$, Len(Text$) - 4)
    End If
End Sub

Function AtoW$( A$ )
'-- Converts an ASCII/ANSI string to a UTF-16/Unicode little-endian
'   string by adding NUL [CHR$(0)] characters.

    For c = 1 To Len(A$)
        AtoW$ = AtoW$ + Mid$(A$, c, 1) + Chr$(0)
    Next
End Function

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
JosephE
Junior Member


Joined: 01 Nov 2007
Posts: 21

PostPosted: Nov 4th, 2007, 3:32am    Post subject: Re: [BETA] Unicode File Read Demo Reply with quote

Most amazing. I had to use wordpad for the unicode and save it as a unicode text document before it would work, though.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> Snippet Testing All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group