Quantcast
Channel: Rainmeter Forums
Viewing all articles
Browse latest Browse all 482

Read and Write UTF-16 LE BOM (lua)

$
0
0
This is not a question.

I've been working on this for a while and it still needs testing so I make it public for that reason.

utf16BomHandler.lua

It should be able to read and write UTF-16 LE BOM files. All it does is, in general terms, reads a UTF-16 LE BOM file, converts it to UTF-8 to handle it on lua like you'd normally do, then when it's time for writing it converts it back to UTF-16 LE BOM.

I have tested it for generating\deleting meters and variables only, I also tested with a couple of Russian, Chinese, Japanese, Arabic and German poems and it seems to behave properly.
It doesn't have options, all it does is what I said, so, no append and such options. Write will overwrite files completely so first read the file and then manually append\prepend to the original content to write it back along with the new information, or split the content by lines to inject somewhere else or whatever.

If you feed it with a UTF-8 (with or without BOM) file (instead of a UTF-16 LE BOM) it should also work normally, when you write it, it will be converted to UTF-16 LE BOM. UTF-16 BE BOM files should also be converted.

in short, there are only 2 usable functions, read, and write. There's also detect, but that is only to detect file encodings with no much usability but for internal purposes, I left it as a easy to use function however just in case it could be of use.

With that being said, let's make an example.

Let's prepend a Russian title to a file with Russian poems, the goal, to preserve UTF-16 LE BOM encoding after writing to the file.

RussianPoems.inc (UTF-16 LE BOM)

Code:

Я вас любил: любовь еще, быть может,В душе моей угасла не совсем;Но пусть она вас больше не тревожит;Я не хочу печалить вас ничем.Я вас любил безмолвно, безнадежно,То робостью, то ревностью томим;Я вас любил так искренно, так нежно,Как дай вам бог любимой быть другим.Белая берёзаПод моим окномПринакрылась снегом,Точно серебром.На пушистых веткахСнежною каймойРаспустились кистиБелой бахромой.И стоит берёзаВ сонной тишине,И горят снежинкиВ золотом огне.А заря, ленивоОбходя кругом,Обсыпает веткиНовым серебром.
Our Title will be: Знаменитые русские стихи (Famous Russian Poems)

Just for people that may not understand the problem, let's do this normally first and see the result:

init.lua

Code:

function Initialize()--Get PathrussianPoems= SKIN:MakePathAbsolute("@Resources\\RussianPoems.inc")--Get Contentfile= io.open(russianPoems, 'r')originalContent= file:read("*a")file:close()title = 'Знаменитые русские стихи'--Write Title Before Original Contentfile = io.open(russianPoems, "w")file:write(title ..'\n\n'.. originalContent)file:close()end
result: RussianPoems.inc (UTF-8)
yeei our title is there :lol:

Code:

Знаменитые русские стихиÿþ/  20A  ;N18;:   ;N1>2L  5I5,   1KBL  <>65B,     4CH5  <>59  C30A;0  =5  A>2A5<;   >  ?CABL  >=0  20A  1>;LH5  =5  B@52>68B;   /  =5  E>GC  ?5G0;8BL  20A  =8G5<.   /  20A  ;N18;  157<>;2=>,   157=0456=>,   ">  @>1>ABLN ,   B>  @52=>ABLN  B><8<;   /  20A  ;N18;  B0:  8A:@5==>,   B0:  =56=>, 
In case you feel like reading more about this limitations:
Unicode in Rainmeter
Unicode and external files in Lua
Unicode support for Lua scripting


Now let's do it with utf16BomHandler.lua (yah that's what I called it, pretty original huh?:

init.lua

Code:

function Initialize()-- Load utf16BomHandlerutf16BomHandler= dofile(SKIN:MakePathAbsolute("@Resources\\utf16BomHandler.lua"))-- Get PathrussianPoems= SKIN:MakePathAbsolute("@Resources\\RussianPoems.inc")--Get ContentoriginalContent, err= utf16BomHandler.read(russianPoems)if err then return false, print('Error: ' .. err) endtitle= 'Знаменитые русские стихи'--Write Title Before Original ContentnewContent, err= utf16BomHandler.write(russianPoems, title ..'\n\n'.. originalContent)if err then return false, print('Error: ' .. err) endend
Result: RussianPoems.inc (UTF-16 LE BOM)
Nice :amazed

Code:

Знаменитые русские стихиЯ вас любил: любовь еще, быть может,В душе моей угасла не совсем;Но пусть она вас больше не тревожит;Я не хочу печалить вас ничем.Я вас любил безмолвно, безнадежно,То робостью, то ревностью томим;Я вас любил так искренно, так нежно,Как дай вам бог любимой быть другим.Белая берёзаПод моим окномПринакрылась снегом,Точно серебром.На пушистых веткахСнежною каймойРаспустились кистиБелой бахромой.И стоит берёзаВ сонной тишине,И горят снежинкиВ золотом огне.А заря, ленивоОбходя кругом,Обсыпает веткиНовым серебром.
Please test it, I haven't had problems so far, but again, I've only used it for pretty basic stuff, may be useful for some of you anyway.

Statistics: Posted by RicardoTM — Today, 7:14 am — Replies 0 — Views 46



Viewing all articles
Browse latest Browse all 482

Trending Articles