lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Steve,

Turned out I need to set encoding to UTF8; this did the trick:

> winapi.set_encoding(winapi.CP_UTF8)
> winapi.short_path("d:/Lua/下载/foo.lua")
"d:/Lua/D7A9~1/foo.lua"

Paul.

On Thu, Aug 16, 2012 at 10:33 PM, Paul K <paulclinger@yahoo.com> wrote:
>> One option on Windows is to use winapi:
>> http://stevedonovan.github.com/winapi/api.html#short_path
>
> Thanks to Steve and all others who responded. I did use some of the
> suggestions and got most of the code working, but there are still
> couple of issues to resolve.
>
> It turned out that I can use wxwidgets methods for reading/writing
> file content and these are already Unicode aware (including windows),
> so I'm using wxFile methods for that.
>
> Where I still have a problem is dofile. Obviously, dofile
> [[some/Unicode/file/name]] is not going to work for the same reason
> io.open doesn't work. What I want to do is to use the short name in
> this case; this is where I have an issue.
>
> This is what I tried:
>
>> winapi.short_path("d:\\Lua\\下载\\foo.lua")
> nil     "The system cannot find the path specified."
>
>> winapi.short_path("d:\\Lua\\下载\\")
> nil     "The filename, directory name, or volume label syntax is incorrect."
>
>> winapi.short_path('d:/Lua/下载/foo.lua')
> nil     "The system cannot find the path specified."
>
>> winapi.short_path('D:/Lua/下载')
> "D:/Lua/FD31~1"
>
> For some reason it doesn't see the path, while it's definitely there.
> Also, it creates a *file* in that folder, while I need it to find a
> folder with the unicode name.
>
> The correct name is this "dofile[[d:/Lua/D7A9~1/foo.lua]]" (and the
> one that doesn't work is this: "dofile[[d:/Lua/下载/foo.lua]]"). Notice
> that the shortname returned by short_path is different as it is for a
> *file* with the same name, rather than a *folder*.
>
> Also, it would be nice if short_path would *not* create a file by
> default. Maybe a second parameter (true/FALSE) that would indicate if
> the name should be returned for a file that doesn't exist. I'm not
> sure why winapi.short_path('D:/Lua/下载') doesn't return the right value
> when this unicode name is a folder and winapi.short_path('D:/Lua/下载/')
> doesn't work at all (returns an error). It appears the current logic
> is not going to work for directories (per msdn docs
> http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx):
>
> - An application cannot create a directory by using CreateFile,
> therefore only the OPEN_EXISTING value is valid for
> dwCreationDisposition for this use case. To create a directory, the
> application must call CreateDirectory or CreateDirectoryEx.
> - To open a directory using CreateFile, specify the
> FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFlagsAndAttributes.
> Appropriate security checks still apply when this flag is used without
> SE_BACKUP_NAME and SE_RESTORE_NAME privileges.
> - When using CreateFile to open a directory during defragmentation of
> a FAT or FAT32 file system volume, do not specify the MAXIMUM_ALLOWED
> access right. Access to the directory is denied if this is done.
> Specify the GENERIC_READ access right instead.
>
> Thank you.
>
> Paul.
>
> On Wed, Aug 15, 2012 at 11:45 PM, steve donovan
> <steve.j.donovan@gmail.com> wrote:
>> On Thu, Aug 16, 2012 at 8:35 AM, Paul K <paulclinger@yahoo.com> wrote:
>>> information). What options do I have for working with unicode file
>>> names? I want to be able to read/write files with unicode paths. This
>>> is using Lua 5.1.4.
>>
>> One option on Windows is to use winapi:
>>
>> http://stevedonovan.github.com/winapi/api.html#short_path
>>
>> winapi.short_path takes an encoded path (UTF-8 or UTF-16) and returns
>> the equivalent short path, which can be opened with io.open.
>>
>> Sometimes a legacy option works nicely ;)
>>
>> steve d.
>>