#66359 - 2002-06-08 05:57 AM
Unicode Converter UDF
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I was attempting to write a UDF that would convert a (unicode file to an ascii file format) and an (ascii file format to a unicode file).
I have been successful in going from unicode to Ascii, but seem to have hit a limitation with KiXtart going from Ascii to unicode. KiXtart doesn't seem to handle Chr(0) at all when adding it to a string. Does anyone have any insight?
I know this isn't a true unicode converter but it works for single byte languages.
code:
UnicodeConvert("c:\data\scripts\unicode.sql")
Function UnicodeConvert($File) Dim $FH, $x, $null, $rc
$FH = OpenFile($File, "R") $x = ReadLine($FH) if @error = 0 if instr($x, Chr(255)+Chr(254)) $Data = Right($x, 1) While (@error=0) $x = ReadLine(1) if @error = 0 if Asc($x)<>0 $Data = $Data + $x else $null = $null + 1 if $null = 4 $Data = $Data + @CRLF $null = 0 endif endif endif Loop else $Data = Chr(255)+Chr(254) Gosub "ProcessLine" $x = ReadLine($FH) While (@error=0) Gosub "ProcessLine" $x = ReadLine(1) Loop endif else exit 1 endif
$rc = close($FH) Del $File $FH = OpenFile($File, "W", 1, 1) $rc = WriteLine($FH, $Data) $rc = close($FH) ? For $i=1 to len($Data) Asc(substr($Data,$i,1)) " " Next
exit 0
:ProcessLine For Each $x IN Split($x,"") $Data = $Data + $x + Chr(0) Next $Data = $Data + Chr(13) + Chr(0) + Chr(10) + Chr(0)
return
Endfunction
;FUNCTION OpenFile() ; ;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com) ; ;ACTION Opens file based on specified parameters. Supports overwriting a file. ; ;SYNTAX OpenFile(FileName, R|W, [0|1], [0|1]) ; ;PARAMETERS $FileName (Required) - String value ; $ReadWrite (Required) - String value ; $Create (Optional) Default(0) Do not create a new file. (1 or 0) ; $OverWrite (Optional) - Default(0) Append (1 or 0) ; ; ;REMARKS This function attempts to open the specified file. @error is set on exit. ; The UDF will search for the first available file number. ; ;RETURNS File handle (integer 1-10) if successful; nothing if error ; ;DEPENDENCIES None ; ;EXAMPLES ; Open file for write; do not create the file; append ; $FH = OpenFile("junk.txt", "W") ; if @error = 0 ; Writeline($FH, "Some text") ; endif ; ; Open file for write; create the file; delete old file ; $FH2 = OpenFile("junk2.txt", "W", 1, 1) ; if @error = 0 ; Writeline($FH2, "Some text") ; endif; ; Function OpenFile($FileName, $ReadWrite, optional $Create, optional $Overwrite) Dim $FileName, $ReadWrite, $Create, $Overwrite Dim $FH, $RC, $Mode
; Validate input parameters if VarType($Create) = 0 $Create = 0 endif if VarType($Overwrite) = 0 $Overwrite = 0 endif if not (VarType($FileName) = 8 and ($ReadWrite = "R" or $ReadWrite = "W") and ($Create = 0 or $Create = 1) and ($Overwrite = 0 or $Overwrite = 1) ) exit 1 endif if $Create < $OverWrite exit 1 endif
; Build Mode value if $Create = 0 $Mode = 0 else $Mode = 1 endif if $ReadWrite = "R" $Mode = $Mode + 2 else $Mode = $Mode + 4 endif
;Handle OverWrite option if $OverWrite = 1 if exist ($Filename) del $Filename endif endif
; Open file $FH=1 $RC=Open ($FH, $FileName, $Mode) ? "@error @serror" if $RC <> 0 select case $RC=2 exit $RC case $RC=-3 while $RC = -3 and $FH < 11 $RC=Open ($FH, $FileName, $Mode) if $RC = -3 $FH=$FH +1 endif Loop if $RC = -3 exit $RC endif case $RC=-2 exit $RC case $RC=-1 exit $RC case 1 exit $RC endselect endif
$OpenFile = $FH exit 0 Endfunction
[ 08 June 2002, 15:24: Message edited by: Howard Bullock ]
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 718 anonymous users online.
|
|
|