[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Reading arbitrary data into a c-like structure

Graham Nicholls

7/6/2005 7:54:00 AM

I mentioned before that I'm writing a tool to extract data from a (dataease)
database file according to a schema. In 'C' I'd do something like

struct data
{
char header 4;
int recno;
char name[30];
char add_1[30];
...
unsigned int cr_limit ;
} data;

and if I set the compiler options right so the structure was closely packed,
I could simply read the file into this structure, and print out the results
using

printf("%s ... %d\n"data.name, ... data,cr_limit);

I looked at struct in Ruby, and I'm perusing the pickaxe, but no idea yet
how to do this. I really want to do it in Ruby rather than C because its
better at dynamic stuff. Any help much appreciated, thanks
GN
--
The answer's always "Yes".
Now, what was the question?

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.new... The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
4 Answers

Michael Neumann

7/6/2005 8:48:00 AM

0

Graham Nicholls wrote:
> I mentioned before that I'm writing a tool to extract data from a (dataease)
> database file according to a schema. In 'C' I'd do something like
>
> struct data
> {
> char header 4;
> int recno;
> char name[30];
> char add_1[30];
> ...
> unsigned int cr_limit ;
> } data;
>
> and if I set the compiler options right so the structure was closely packed,
> I could simply read the file into this structure, and print out the results
> using

You need to read sizeof(data) bytes as a String, then unpack it.

Simple example:

C:

struct data {
long a;
long b;
};

Ruby:

str = handle.read(8)
a, b = str.unpack('LL')

Regards,

Michael


Graham Nicholls

7/6/2005 9:02:00 AM

0

Michael Neumann wrote:

> Graham Nicholls wrote:
>> I mentioned before that I'm writing a tool to extract data from a
>> (dataease)
>> database file according to a schema. In 'C' I'd do something like
>>
>> struct data
>> {
>> char header 4;
>> int recno;
>> char name[30];
>> char add_1[30];
>> ...
>> unsigned int cr_limit ;
>> } data;
>>
>> and if I set the compiler options right so the structure was closely
>> packed, I could simply read the file into this structure, and print out
>> the results using
>
> You need to read sizeof(data) bytes as a String, then unpack it.
>
> Simple example:
>
> C:
>
> struct data {
> long a;
> long b;
> };
>
> Ruby:
>
> str = handle.read(8)
> a, b = str.unpack('LL')
>
> Regards,
>
> Michael
Excellent - just what I (think!) I want. I looked at pack, but didn't think
of unpack - you don't know what you don't know, IYSWIM. The pickaxe book
is great, but its a reference - I'd love to see "Text Processing in Ruby"
or something similar (I've got "Text Processing in Python", but Ruby is
much nicer , IMO, but don't crosspost to c.l.python!). Thank you.
GN
--
The answer's always "Yes".
Now, what was the question?

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.new... The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Robert Klemme

7/6/2005 9:04:00 AM

0

Michael Neumann wrote:
> Graham Nicholls wrote:
>> I mentioned before that I'm writing a tool to extract data from a
>> (dataease) database file according to a schema. In 'C' I'd do
>> something like
>>
>> struct data
>> {
>> char header 4;
>> int recno;
>> char name[30];
>> char add_1[30];
>> ...
>> unsigned int cr_limit ;
>> } data;
>>
>> and if I set the compiler options right so the structure was closely
>> packed, I could simply read the file into this structure, and print
>> out the results using
>
> You need to read sizeof(data) bytes as a String, then unpack it.
>
> Simple example:
>
> C:
>
> struct data {
> long a;
> long b;
> };
>
> Ruby:
>
> str = handle.read(8)
> a, b = str.unpack('LL')
>
> Regards,
>
> Michael

Plus, if you want to do further processing, you can use any of Hash,
Struct or OpenStruct for storage. If you create a struct for your special
case you mighe even implement conversion methods:

# untested
Foo = Struct.new(:a, :b)

class Foo
def to_binary()
[a,b].pack('LL')
end

def self.from_io(io)
new *io.read(8).unpack('LL')
end
end

Kind regards

robert

Michael Neumann

7/6/2005 9:19:00 AM

0

Graham Nicholls wrote:
> Michael Neumann wrote:
>
>
>>Graham Nicholls wrote:
>>
>>>I mentioned before that I'm writing a tool to extract data from a
>>>(dataease)
>>>database file according to a schema. In 'C' I'd do something like
>>>
>>>struct data
>>>{
>>> char header 4;
>>> int recno;
>>> char name[30];
>>> char add_1[30];
>>> ...
>>> unsigned int cr_limit ;
>>>} data;
>>>
>>>and if I set the compiler options right so the structure was closely
>>>packed, I could simply read the file into this structure, and print out
>>>the results using
>>
>>You need to read sizeof(data) bytes as a String, then unpack it.
>>
>>Simple example:
>>
>> C:
>>
>> struct data {
>> long a;
>> long b;
>> };
>>
>> Ruby:
>>
>> str = handle.read(8)
>> a, b = str.unpack('LL')
>>
>>Regards,
>>
>> Michael
>
> Excellent - just what I (think!) I want. I looked at pack, but didn't think
> of unpack - you don't know what you don't know, IYSWIM. The pickaxe book
> is great, but its a reference - I'd love to see "Text Processing in Ruby"
> or something similar (I've got "Text Processing in Python", but Ruby is
> much nicer , IMO, but don't crosspost to c.l.python!). Thank you.

Also have a look at my BinaryReaderMixin. This allows you to use methods
like 'read_int32' or 'read_int32_little' etc. instead.

http://www.ntecs.de/viewcvs/viewcvs/postgres-pr/trunk/lib/binary_reader.rb?rev=448&root=ruby-dbi&...

Regards,

Michael