Fritz wrote:I just want to ask about future politics: will newLISP in the future have an operator like *nix iconv
You can use iconv in Windows too. A lot of Unix-like applications for Windows comes with iconv library today: in my system there are... let me count... six instances of
iconv.dll (yes, this IS the dll hell!). If you have no one, download it form
GnuWin32 (the library file is named
libiconv2.dll in this package, but it is all the same). The library interface is a bit low-level, but newLISP is doing a very good job accessing low-level interfaces. The following code is demo, but it works for me:
Code: Select all
(import "iconv.dll" "libiconv_open") ; see 1
(import "iconv.dll" "libiconv")
(import "iconv.dll" "libiconv_close")
(setq cd (libiconv_open "cp866" "utf-8")) ; see 2, 3
(setq in ((directory ".") -1)) ; see 4
(setq out (pack "n1024")) ; see 5
(setq inbuf (pack "lu" (address in)))
(setq inlen (pack "lu" (length in)))
(setq outbuf (pack "lu" (address out)))
(setq outlen (pack "lu" (length out)))
(libiconv cd (address inbuf) (address inlen) (address outbuf) (address outlen))
(libiconv_close cd)
(println out)
(exit)
Some comments:
1) Put
iconv.dll in the current directory, or write the full path in import statements; replace "iconv.dll" with "libiconv2.dll" if you have downloaded it from the location mentioned above; all the rest is the same;
2) I suppose you are using utf-8-enabled build of newLISP, in plain 8-bit build
(directory) just returns cp1251;
3) I convert the name to cp866, not to cp1251, cause console window is cp866; with 1251 it works the same;
4) I have created the file "привет.txt" just for this demo, so the last file in directory name is converted;
5) I hope 1024 byte buffer is enough;
6) Absolutely NO error check in this demo, in production code must be some;
This demo happily prints "привет.txt". I hope you can elaborate this to production-quality code. ;-)
Update (two hours later):
The code above was incomplete: the
out buffer was still containing 1024-byte string, although most of them was zeros. Such string can be printed, but not of much use otherwise. To extract the converted string from the buffer, one must subtract the resulting
outlen, modified by
libiconv function (in fact it is the "bytes left" value), from the original one. So, instead of printing the
out value directly, write the following at the end:
Code: Select all
(setq result (slice out 0 (- (length out) (get-int outlen))))
(println result)
Hope this helps.