Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Unpack binary files.

Reply
Thread Tools

Unpack binary files.

 
 
dave
Guest
Posts: n/a
 
      08-07-2005
--nextPart9687245.5BTV5vb76H
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline


Hy list,


I would like to append to a ruby script a tarball and a license file.

Both are in the form:

# data........................................
# data........................................
# data........................................

How would you convert the tarball binary data?


The licence text should be centred in a 55 char space.




If I split the license text lines and then gsub(/\n/, '\n') them=20
i have a bad output:

[55 char]
# use pieces of it
# in new free programs; and that you know you can do


=2D----------------
x =3D 0
y =3D 54
lines =3D []

loop {
i =3D f[x..y]
break if i.nil?()
lines.push(("# " + i))
x +=3D 54
y +=3D 54
}

lines.each{|i| puts i }
=2D--------------


Tnx.


=2D-=20
>here are more things in heaven and earth,

horatio, than are dreamt of in your philosophy.

--nextPart9687245.5BTV5vb76H
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iQIVAwUAQvWxGKNg16HTgg0AAQJfvw//cHxL5u3viL1c4xfDlgBkdArwLkIsuUoN
8vXRasfPDsWteAD3ijYyT8Wct5OEA+uXw3z9ZY3QOaCim6xJz/Cb+9cp0iS9MbLr
1kxq90NwWowDgCrUFfrhTlXrsb/dtbcFycvv3hYFNy3iv3E3bCFxS9gSKtSC1gWu
kD7cGuEDl+gNNlQ2OTF8GIAWHy/OTCtk1oXYs9Xr8gCpb/TsZKW7cf5+iz53s4II
T3NgXd8iETf7M/v8VjwjfXNTX9/xjF1/TV/oBbnEmdiE7mvU8PNfXX1slcylYFRZ
VbQCXgNbK0qpJ0EFd7l1IlPJoTalMMRtHvWTZzpzauF9p9FAjT RUE8ZPiak/Rwah
6W5QcSpZjjGLMExaHobm/cTak9WpAnFGyuyJgIEP6eCTek+r6VF6Ng7gww4JOu9Q
KVTduchHX8kQ9FwXjUEi94uTG96YiXNRbvxQsZ2BTqYs1nbMwX A5l3iHBpRQc5WR
ltCofPw9cbnmMeuhPJTdKe/ndOOxVcHYpzzFfKFO/ze1dt1T2X+X/m4+PQWILZmZ
tqr5Ia9FWK2olslwS2HVy7wt8W3V/GghcJA6rBrV5g82WBiRirPgh8lFiFKX9XlM
Y9kShaDrVr7qnECW9sjBqSoqo5h9rr37TkUL0/I7iTrOvnBZ7cp1Hs1LYAgGxMSf
WVcv7a/t8lo=
=W7NE
-----END PGP SIGNATURE-----

--nextPart9687245.5BTV5vb76H--


 
Reply With Quote
 
 
 
 
daz
Guest
Posts: n/a
 
      08-07-2005

dave.m wrote:

>
> Hy list,
>
>
> I would like to append to a ruby script a tarball and a license file.
>
> Both are in the form:
>
> # data........................................
>
> How would you convert the tarball binary data?



tar = "i6\253m\251e" # in: binary content
b64 = [tar].pack('m') # out: Base64 encoded string
p b64 #-> "aTarball\n"


>
> If I split the license text lines and then gsub(/\n/, '\n') them
> i have a bad output:


If you split on /\n/, the newline will disappear so it's
not now available to gsub.


What about this ?

# make some input data
licin = <<EOL
LIC ............A
LIC ............B
LIC ............C
EOL

licout = licin.map {|line| '# ' << line}
puts licout

# LIC ............A
# LIC ............B
# LIC ............C


daz



 
Reply With Quote
 
 
 
 
dave
Guest
Posts: n/a
 
      08-07-2005
--nextPart1421223.oZN6hic0xX
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline


> tar =3D "i6\253m\251e" =A0 =A0# in: =A0binary content
> b64 =3D [tar].pack('m') =A0 # out: Base64 encoded string
> p b64 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #-> "aTarball\n"


excellent!


> If you split on /\n/, the newline will disappear so it's
> not now available to gsub.


I dont split on "\n".

I need to center the license text in a TextEdit Window, this widget
is 55 char large.
I split the file in lines of 55 char:

x =3D 0
y =3D 54
lines =3D []

loop {
i =3D f[x..y]
break if i.nil?()
lines.push(("# " + i))
x +=3D 55
y +=3D 55
}

lines.each{|i| puts i }


The problem 1 (tab related) is in centred input:
in:
[ tab space ] Gnu Gpl [ tab space ]| <- 70 char
|
out:
[ tab space ] Gnu Gpl [ tab ]| <- 55 char

So the output text is right aligned.
How can i center the text in a 55 char space?


The problem 2 (\n related) is in carriage return:
in:
bla bla bla ...................................bl
a
out:
bla bla bla ...
bl
a

Should i delete all "\n" that not follow a '.'?

=2D-=20
>here are more things in heaven and earth,

horatio, than are dreamt of in your philosophy.

--nextPart1421223.oZN6hic0xX
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iQIVAwUAQvXTy6Ng16HTgg0AAQLoFhAAnepU9BE06gkR2l8C27 hq9UVtLwa77zaf
XGFUXaviT63nkfqZ5MY4kbCGwwZtEF2DJ+ljbIYBjQNM72LuLB dstthc3X6YLAo0
aelHz0ayTbu+OOhebXVbAVK2r+S6NI/mZFlH5INqFj4m5a2QtDZtrQyQP6cpxoe4
OTUlyDgMsjhoJrKrayLaHj8asaZ6JxzJb1yThIoscQWXleLUCL p0RsF3YsmfHCD1
UNSLTRPazYvpBLwqMyWBFGL1mjMxWBY+8eweM7vLzAXeUu1S0u rnJ/8Yc6A+esfV
e6BIvLWRnyVyB4/tMzdQciEMoktCr54Da6nYl6vmQ06ZZvBCTS35W2Bky4yWyOa7
ZqpviGJFuE6ChrPvcHMMGnQSwvm47orngP20NyrcOtMIuGcsP5 2A7tPtQ78wfLm/
F97SfGMia99Hk8JPHZzdsAD2Xznkr7I9ulz+rf854YwU+973ex AVdE3D1rzuDw4k
02+C6lBJ6rvgale1W3cxWXIwt/eSmTDPThsZ7qzo4SYPSZTrqtEAz8stqsy4Qser
9BnDGvNiigCgH0zDASxZ4aejKAkKxo98l/AtFIJQ4SNAhM2yndJc2VTIyUVcugGq
VF1ScW3XdJ9hl/SL1XAj6KGk4v6MwVTlq0+/tQDKon6nScQbflcKlGFR976FiPUb
z+MPrSQ1H5k=
=ZaXv
-----END PGP SIGNATURE-----

--nextPart1421223.oZN6hic0xX--


 
Reply With Quote
 
daz
Guest
Posts: n/a
 
      08-07-2005

dave.m wrote:


> I need to center the license text in a TextEdit Window, this widget
> is 55 char large.
> I split the file in lines of 55 char:
>
>
> The problem 1 (tab related) is in centred input:
> in:
> [ tab space ] Gnu Gpl [ tab space ]| <- 70 char
> |
> out:
> [ tab space ] Gnu Gpl [ tab ]| <- 55 char
>
> So the output text is right aligned.
> How can i center the text in a 55 char space?
>
>
> The problem 2 (\n related) is in carriage return:
> in:
> bla bla bla ...................................bl
> a
> out:
> bla bla bla ...
> bl
> a
>
> Should i delete all "\n" that not follow a '.'?
>


No.
I think you should reformat gpl.txt in a text editor
so that it's exactly the way you want it to look.
Then display your new file in the widget ??

Otherwise you will be writing a text formatter


daz



 
Reply With Quote
 
dave
Guest
Posts: n/a
 
      08-07-2005
--nextPart4374033.O6JLPgNT76
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline


> Otherwise you will be writing a text formatter


Thank you again. I'll format the text

=2D-=20
>here are more things in heaven and earth,

horatio, than are dreamt of in your philosophy.

--nextPart4374033.O6JLPgNT76
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iQIVAwUAQvXn6qNg16HTgg0AAQLYTA//RYD7eRdhyuOU5Qhjw29tLfh41+inaIoa
x3I1a/hT9YGFdOnNTPBx26m0x+zVERP/To6B3+VkxlFa3aRvufB6KAOxvdNgVCQN
NLfyBYjE55rdZMC0pNJJxGX2ZsI9L0C0rlTeu83LODCst4xYVZ YPZZ77FQM+vX7T
gqbd2akFxVtHaFIDu4inYEU7BnHUZoL8GfdIoTZF7kQzjWiagv KtczPN1OostK8g
Q94n7PsJjZytibfNo1lurPYVe7lQTjMec2R73nc7qv9OOlKLGR 5GbMpss1LsRjTk
SgMAC0QO/0PM3kUKuS4aNv3BS5YEQtSV5ltQJ/aw9gMWh4wBUyjFkKdKEafFW4xH
FlE3fgPe+EeHgvUdF/kh59PyElQGIotKqT4fpJD0ngqr0Vt3/Os52kiiOS+ZS/li
8i7ZfA+dPDsOFBnDWOJjrJkMy81OeLOe/1I9d4GrfYCLViFIl90fmykH/GqrRq8F
/TUPO0fFOnpIEIIc+MZWznKFQeMkt/ulZblg7rtmfieG+LqCMBlcTUAyFyAcm13a
bOjn6B/wOkp5vIFJN0BfRSv1KEvtvDu8DIsSn92pNgINpRux6OwLa1gxe XJi7b0F
v7MIn8ElQkuh0/vyCo4BwakjDPwNyw2UGpcqjTkf6H4pgHblm7puJMFX0S4WA4hk
Qk0yEyLUxtc=
=89lY
-----END PGP SIGNATURE-----

--nextPart4374033.O6JLPgNT76--


 
Reply With Quote
 
Erik Veenstra
Guest
Posts: n/a
 
      08-07-2005
> I think you should reformat gpl.txt in a text editor so that
> it's exactly the way you want it to look. Then display your
> new file in the widget ??
>
> Otherwise you will be writing a text formatter


I've once written this String#reformat. It might help you. I
added a test to the script.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

----------------------------------------------------------------

class String
def wrap(width, prefix="", prefixoneveryline=true)
prefix = prefix.to_s
text = self.to_s
res = []

while not text.empty?
line = text[0..width-2-prefix.length]
line = text.gsub(/ .*/, "") unless line.include?(" ")
line = line.gsub(/ [^ ]*$/, "") unless line == text or text[line.length..-1] =~ /^ /

res << prefix + line

prefix = prefix.gsub(/./, " ") unless prefixoneveryline
text = text[line.length..-1]
text = text.gsub(/^ /, "")
end

text = res.join("\n")
end

def reformat(width, prefix="", prefixoneveryline=true)
paragraphs = self.split(/\s*\r*\n\r*\n[\r\n]*\s*/)
paragraphs.collect! do |paragraph|
paragraph.gsub!(/^[\r\n\s]+/, "")
paragraph.gsub!(/[\r\n\s]+$/, "")
paragraph.gsub!(/[\r\n\s]+/, " ")
paragraph.wrap(width, prefix, prefixoneveryline)
end
paragraphs.join("\n\n")
end
end

text = DATA.read

puts text.reformat(55, "# ")

__END__

Version 2, June 1991

Copyright (C) 1989, 1991 Free Software Foundation, Inc.
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General
Public License is intended to guarantee your freedom to share
and change free software--to make sure the software is free for
all its users. This General Public License applies to most of
the Free Software Foundation's software and to any other
program whose authors commit to using it. (Some other Free
Software Foundation software is covered by the GNU Library
General Public License instead.) You can apply it to your
programs, too.

When we speak of free software, we are referring to freedom,
not price. Our General Public Licenses are designed to make
sure that you have the freedom to distribute copies of free
software (and charge for this service if you wish), that you
receive source code or can get it if you want it, that you can
change the software or use pieces of it in new free programs;
and that you know you can do these things.

.................................................. ...............

 
Reply With Quote
 
dave
Guest
Posts: n/a
 
      08-07-2005
--nextPart1282301.jt5OWCvOkQ
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline


> I've once written this String#reformat. It might help you. I
> added a test to the script.

very, very, good!

*I changed paragraphs.join("\n\n") to paragraphs.join("\n#\n") and
now it's perfect.

=2D-=20
>here are more things in heaven and earth,

horatio, than are dreamt of in your philosophy.

--nextPart1282301.jt5OWCvOkQ
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iQIVAwUAQvYCCqNg16HTgg0AAQL4Ew//fCxhvplY2jBwmM4+uAQqME1VSwGAYUhY
kCdTf+J8wsg5UzG3NFS7mVA5rEvyUQFZN6xkAtmYfnKXngVjiS 7TiQpB0GbKYmEp
s73Pa65MgiHcIGtCIIvbsEEFT0mu1YEEvoo+kh4C3rSl/baYZulhxe/Jf4p96JhN
JYGJuGK6+xuZcet9Xg2nTAl+bwRJhujaDfsatG6kpqPdC61iGA QYudSykAG2naf0
UtKUyZHDe1z9YlrrcRhTMFRnDUh+v9pZWqaoZjOC/xKtRg7f+pe7VHpG35PG+G3U
kLF/Qk35taVqPec5aMOvqCrV36N9WyFL8LRoGWqqZa4Ru6V8cGwqUj x+5AgMwZUK
zyGrflPDK/ej6xnPzr0NahOvd+R9saWKo3lXGvXZji6B0EXCD3e/RLrHNHXr9yZM
CIJF/VOLLBeziTbCn/Lt0Eh5iZXr7Hv4HE6uBGoySYIoGpYdA+wHApAoJ3XYUoWF
RBodKBsy0VrsprwnoWKLBWd5hMZQjHwweP1+0qFZzkC+R4tDjB mBRtq5mfqV/7Uq
tH/Ps/mRv1OdtJb476iYuWVyape2SB1bnoIROJ43TO0PZosHXix8+hP3 AvsskgTW
UfyXuCqV5UXKeHcKlOSfV7PSSoARmbFdAy6Bez/o3C6dOlu8yAZqHJna9N/a8Ovy
Mn1oXvjzvmg=
=Lehg
-----END PGP SIGNATURE-----

--nextPart1282301.jt5OWCvOkQ--


 
Reply With Quote
 
Erik Veenstra
Guest
Posts: n/a
 
      08-07-2005
> > I've once written this String#reformat. It might help you.
> > I added a test to the script.

>
> very, very, good!
>
> *I changed paragraphs.join("\n\n") to
> paragraphs.join("\n#\n") and now it's perfect.


Use the prefix: paragraphs.join("\n#{prefix}\n")

gegroet,
Erik V. - http://www.erikveen.dds.nl/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
A 64-bit binary returning a value to a 32-bit binary? spammenotplui31@yahoo.ca C Programming 12 04-08-2007 07:02 AM
How to unpack unusual binary structure? Leo -- Ruby 3 12-07-2006 12:44 PM
Unpack binary file with C Struct to text? RT Perl Misc 2 03-07-2004 01:19 PM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments