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 |
Subject: [BETA] Unicode File Read Demo
This should be useful for internationalization of LB apps. Let me know it it works for you.
Subject: Re: [BETA] Unicode File Read Demo
Most amazing. I had to use wordpad for the unicode and save it as a unicode text document before it would work, though.
Page 1 of 1
You cannot post new topics in this forumYou 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