Forum: wxRuby Incompatible encoding from drag and drop?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Christian S. (Guest)
on 2009-03-05 00:40
Attachment: testfolder.zip (0 Bytes)
Hi all,

I'm trying to get a little app running which receives filenames via drag
and drop. While it mostly runs fine, when I'm dropping a file with e.g.
german umlauts or french accents (è, é), I'm not able to open the file
given by the droptarget-class. Seemingly the special characters get
corrupted.

See the small attached program to reproduce my problem. I condensed it
to the absolute minimum to evoke the error.

Thanks in advance for every hint and forgive me if that's a noobs
question!

Cheers,
Raskolnikov.


ruby 1.8.6
wxruby (2.0.0, 1.9.9)
Alex F. (Guest)
on 2009-03-05 01:44
(Received via mailing list)
Christian Schmidt wrote:
> I'm trying to get a little app running which receives filenames via drag
> and drop. While it mostly runs fine, when I'm dropping a file with e.g.
> german umlauts or french accents (è, é), I'm not able to open the file
> given by the droptarget-class. Seemingly the special characters get
> corrupted.
>

You need to be sure that the text content of the file you're dropping in
is in UTF-8 encoding. You should be able to set this in a text editor.

If the text isn't in UTF-8, you can read it then convert it to UTF-8
using Ruby's iconv library.

> See the small attached program to reproduce my problem. I condensed it
> to the absolute minimum to evoke the error.

Also, you should return true or false from on_drop_files, to signal to
wxRuby whether or not the drop files action succeeded.

hth
alex
Christian S. (Guest)
on 2009-03-05 11:14
Hi Alex,

thank you for the reply!

The Problem isn't reading the file but opening it...! The program throws
an exception if, for instance, you try to drop the file in the attached
zip onto it.

My program will read in mp3-files and examine them with id3lib-ruby, so
I don't have influence on the file's contents anyway.

Thanks in advance,
Christian.


Alex F. wrote:
> Christian Schmidt wrote:
>> I'm trying to get a little app running which receives filenames via drag
>> and drop. While it mostly runs fine, when I'm dropping a file with e.g.
>> german umlauts or french accents (è, é), I'm not able to open the file
>> given by the droptarget-class. Seemingly the special characters get
>> corrupted.
>>
>
> You need to be sure that the text content of the file you're dropping in
> is in UTF-8 encoding. You should be able to set this in a text editor.
>
> If the text isn't in UTF-8, you can read it then convert it to UTF-8
> using Ruby's iconv library.
>
>> See the small attached program to reproduce my problem. I condensed it
>> to the absolute minimum to evoke the error.
>
> Also, you should return true or false from on_drop_files, to signal to
> wxRuby whether or not the drop files action succeeded.
>
> hth
> alex
Alex F. (Guest)
on 2009-03-05 19:05
(Received via mailing list)
Christian Schmidt wrote:
> The Problem isn't reading the file but opening it...! The program throws
> an exception if, for instance, you try to drop the file in the attached
> zip onto it.

OK - it helps if you post the error message you get so we can see what's
going wrong.

I have a file whose name has accented characters, and it works fine for
me (Ruby 1.8, 1.9, OS X, wxRuby 2.0.0). Can you say what platform and
version of wxRuby you're using?

alex
Christian S. (Guest)
on 2009-03-05 21:17
Hi again,

it really works on your box?
Ok, on my system, when I use the example file, it throws:

"No such file or directory - C:\ööäèé.txt"

I'm working on XP-Home, SP3. German locales
ruby -v: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
gem list wxruby: wxruby (2.0.0, 1.9.9)

Looking forward to your answer,
Christian.


Alex F. wrote:
> Christian Schmidt wrote:
>> The Problem isn't reading the file but opening it...! The program throws
>> an exception if, for instance, you try to drop the file in the attached
>> zip onto it.
>
> OK - it helps if you post the error message you get so we can see what's
> going wrong.
>
> I have a file whose name has accented characters, and it works fine for
> me (Ruby 1.8, 1.9, OS X, wxRuby 2.0.0). Can you say what platform and
> version of wxRuby you're using?
>
> alex
Alex F. (Guest)
on 2009-03-06 17:57
(Received via mailing list)
Christian Schmidt wrote:
> it really works on your box?
>

Yes, the reason being that in OS X, file names are encoded in UTF-8, and
this is the encoding of the string file name that wxRuby returns.
> Ok, on my system, when I use the example file, it throws:
>
> "No such file or directory - C:\ööäèé.txt"
>
> I'm working on XP-Home, SP3. German locales
> ruby -v: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

On Windows, file names aren't encoded in UTF-8, but in a local encoding
(for me, and probably you, CP1252). So the filename returned by wxRuby
doesn't exist.

I think we should fix this so that wxRuby methods that accept or return
file paths return those strings in an encoding that can be passed
straight to Ruby's File/Dir methods.

In the meantime, you can work around this by manually converting the
filepaths to the correct encoding before opening them:

require 'iconv' # at the top of your script

files.each do | file |
  if Wx::PLATFORM == 'WXMSW'
    file = Iconv.conv('CP1252', 'UTF-8', file)
  end
  .... process file as normal
end

alex
Christian S. (Guest)
on 2009-03-07 12:18
Hi Alex,

thanks a lot! It works like a charm.

Have a nice weekend,
Christian.

Alex F. wrote:
> Christian Schmidt wrote:
>> it really works on your box?
>>
>
> Yes, the reason being that in OS X, file names are encoded in UTF-8, and
> this is the encoding of the string file name that wxRuby returns.
>> Ok, on my system, when I use the example file, it throws:
>>
>> "No such file or directory - C:\ööäèé.txt"
>>
>> I'm working on XP-Home, SP3. German locales
>> ruby -v: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
>
> On Windows, file names aren't encoded in UTF-8, but in a local encoding
> (for me, and probably you, CP1252). So the filename returned by wxRuby
> doesn't exist.
>
> I think we should fix this so that wxRuby methods that accept or return
> file paths return those strings in an encoding that can be passed
> straight to Ruby's File/Dir methods.
>
> In the meantime, you can work around this by manually converting the
> filepaths to the correct encoding before opening them:
>
> require 'iconv' # at the top of your script
>
> files.each do | file |
>   if Wx::PLATFORM == 'WXMSW'
>     file = Iconv.conv('CP1252', 'UTF-8', file)
>   end
>   .... process file as normal
> end
>
> alex
This topic is locked and can not be replied to.