[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Bug in String#unpack on windows or ??

Damjan Rems

6/13/2007 1:12:00 PM


I am trying to save attachment(s) from mail read with net/imap.
Attachment is a tif file.

att = imap.fetch(msgID, "BODY[2]")[0].attr["BODY[2]"].unpack('m')
File.new('att.tif','w+').write(att)

I tried it on linux and it works withouth glitch, but on windows every
LF (x0A) character is replaced with CRLF sequence. Needless to say file
is unreadable.

Is this a bug or am I missing something?


Thank you

TheR

--
Posted via http://www.ruby-....

4 Answers

Alex Young

6/13/2007 1:20:00 PM

0

Damjan Rems wrote:
> I am trying to save attachment(s) from mail read with net/imap.
> Attachment is a tif file.
>
> att = imap.fetch(msgID, "BODY[2]")[0].attr["BODY[2]"].unpack('m')
> File.new('att.tif','w+').write(att)
>
> I tried it on linux and it works withouth glitch, but on windows every
> LF (x0A) character is replaced with CRLF sequence. Needless to say file
> is unreadable.
>
> Is this a bug or am I missing something?

Try File.new('att.tif','wb+').write(att). From memory, the 'b' in 'wb+'
turns off newline conversion.

--
Alex

F. Senault

6/13/2007 1:28:00 PM

0

Le 13 juin à 15:20, Alex Young a écrit :

> Damjan Rems wrote:

>> Is this a bug or am I missing something?
>
> Try File.new('att.tif','wb+').write(att). From memory, the 'b' in 'wb+'
> turns off newline conversion.

Yep. From the PickAxe :

Mode Meaning
r Read-only, starts at beginning of file (default mode).
r+ Read/write, starts at beginning of file.
w Write-only, truncates an existing file to zero length or creates
a new file for writing.
w+ Read/write, truncates existing file to zero length or creates a
new file for reading and writing.
a Write-only, starts at end of file if file exists ; otherwise
creates a new file for writing.
a+ Read/write, starts at end of file if file exists ; otherwise
creates a new file for reading and writing.
b (DOS/Windows only) Binary file mode (may appear with any of the
key letters listed above).

I'd add that b has no effect whatsoever on other OSes. (In other words,
you may leave "wb+" and stay platform independent.)

Fred
--
I won't bother trying to set anyone straight as to my rather complex
feelings about Microsoft, but I must admit that I do hate Windows
because it has so shamefully lowered our expectations of what quality
software should be. (Nicholas Petreley in Infoworld)

Robert Klemme

6/13/2007 1:36:00 PM

0

On 13.06.2007 15:28, F. Senault wrote:
> Le 13 juin à 15:20, Alex Young a écrit :
>
>> Damjan Rems wrote:
>
>>> Is this a bug or am I missing something?
>> Try File.new('att.tif','wb+').write(att). From memory, the 'b' in 'wb+'
>> turns off newline conversion.
>
> Yep. From the PickAxe :
>
> Mode Meaning
> r Read-only, starts at beginning of file (default mode).
> r+ Read/write, starts at beginning of file.
> w Write-only, truncates an existing file to zero length or creates
> a new file for writing.
> w+ Read/write, truncates existing file to zero length or creates a
> new file for reading and writing.
> a Write-only, starts at end of file if file exists ; otherwise
> creates a new file for writing.
> a+ Read/write, starts at end of file if file exists ; otherwise
> creates a new file for reading and writing.
> b (DOS/Windows only) Binary file mode (may appear with any of the
> key letters listed above).
>
> I'd add that b has no effect whatsoever on other OSes. (In other words,
> you may leave "wb+" and stay platform independent.)

That's what I would do for binary files. It also helps documenting.

Kind regards

robert

Damjan Rems

6/14/2007 5:31:00 AM

0


Yep. Thats it. Thank you guys.

by
TheR

--
Posted via http://www.ruby-....