CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Needing infos on parsing...

 



freddo
User

Mar 2, 2002, 11:53 AM

Post #1 of 9 (732 views)
Needing infos on parsing... Can't Post

Hello everybody,

I'm trying to make a little (well, now it grows big) hexeditor with perl, i'd like to add a function to edit some offsets of a file and i want this to be done by parsing a text file.

This is not interactive, it parse a file, or the command line, and can do two things:
1) Output a text file from a binary file
2) From the text file, it can patch an existing binary file/create a new file.

Although it is really easy to parse one line of the definition file, i'd like to use complex structures in them. I've read Jeffrey Goff's Parse::RecDescent Tutorial and few other ressources. I was wondering if someone already used that, and if he/she can give me a (example|link|whatever_to_start_with) on how it works, it looks a little weird for me... Pirate

Actually, a definition file for a gif may look like this:

Code
6    S    width 
8 S height


so, the parsing is:

Code
offset    packstring    hash_key    # a comment can be here


Quite easy to parse, a hash is populated with the values with something like:
[perl]open F, "<test.gif"; binmode F;
$_= "8 S height";

# while(<gif_file_description_here>) # {
@currentLine = split /\s+/;
### some code to remove trailling/leading spaces, comments etc etc
seek(F, $currentLine[0], 0);
$hash{$currentLine[2]} = unpack($currentLine[1], <F>);
# } # end of while

print $hash{width} ."x". $hash{height} ."\n";[/perl]

It is generaly loaded with:[perl]use frhexedit;
load($filename, \%file);[/perl]
This code is too easy, for example, gif files can hold several images and i'd like to be able to express this with rules... I dont ask for someone to do my work, i just ask for a good starting point on parsing...

Btw, i just found this nothing to do with the stuff here, but it's funny...

Thanks in advance,
Freddo
;---


mhx
Enthusiast / Moderator

Mar 2, 2002, 2:43 PM

Post #2 of 9 (722 views)
Re: [freddo] Needing infos on parsing... [In reply to] Can't Post

Hi Freddo,

I'm not really sure if this will help you, but I'm currently writing a Perl module that allows to parse C data structures and use them with Perl. In most cases, you'll find C implementations for most file formats (such as GIF, for example), and generally, C structures are a bit more expressive (at least in my opinion) than Perl pack strings. If you have some experience with C, I can give you a pre-alpha version of the module so you can have a look if it might be of any use for you.

If you're interested and want more information, just tell me!

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



freddo
User

Mar 3, 2002, 3:36 AM

Post #3 of 9 (717 views)
Re: [mhx] Needing infos on parsing... [In reply to] Can't Post

Hi Mhx,

Well, for sure i would be interested Smile, i cant promise i can do something on your project, but are you using parse::recdescent, or another pre-written parser, i dont feel i'm able to write a really complex parsing program in perl Frown

I hope you know about this invaluable website about file formats (wget or teleport, a must to have)... Smile

I must do this, generally people get stunned Sly:

In Reply To
and generally, C structures are a bit more expressive (at least in my opinion) than Perl pack strings.


I can read hexadecimal dumps like it is assembly on various cpu Crazy... perl pack is "just another way to do it" (jawtdi -- in walloon, a belgian local language, it's also a tv show).

freddo
;---


freddo
User

Mar 3, 2002, 3:58 AM

Post #4 of 9 (713 views)
Re: [freddo] Needing infos on parsing... [In reply to] Can't Post

Btw, my email is:[perl](my $email = q(ffreddo-ibelgique.com)) =~ tr/-/@/;
print $email;[/perl]

I guess you can help me on that one, if i do it in a script, the upper prog works, if i do:

Code
perl -e '(my $email = q(ffreddo-ibelgique.com)) =~ tr/-/@/; print $email;'

it works fine, but if i use it in "perl -de 1" it doesnt???

Code
Default die handler restored. 
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(-e:1): 1
DB<1> (my $email = q(ffreddo-ibelgique.com)) =~ tr/-/@/;
DB<2> print $email;
DB<3>


Freddo
;---


mhx
Enthusiast / Moderator

Mar 3, 2002, 4:04 AM

Post #5 of 9 (710 views)
Re: [freddo] Needing infos on parsing... [In reply to] Can't Post


In Reply To
Well, for sure i would be interested , i cant promise i can do something on your project,


If you don't mind, I'd like to send you the package to the email you've given in your profile (BTW, is the double f in the address correct?). I really don't want to attach it, because the .tar.gz is over 700k in size. If this is ok for you, I'll assemble a package today.

And by testing and using the package and giving me feedback you can surely help me to improve it!

Cheers,

-- mhx

PS: I've attached the manpage if you're curious...

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo

Attachments: C-Type.txt (35.9 KB)


freddo
User

Mar 3, 2002, 4:37 AM

Post #6 of 9 (705 views)
Re: [mhx] Needing infos on parsing... [In reply to] Can't Post

Hello Mhx,

I would be really happy you mail me the package (dont expect feedback before tuesday anyway, this is carnival here Wink).

Yes, there's two F at the start (freddo was already taken Frown)

If you dont want to send it thru email, tell me (by email) and i'll give you and ftp account somewhere...

Freddo
;---


yapp
User

Mar 3, 2002, 12:27 PM

Post #7 of 9 (696 views)
Re: [mhx] Needing infos on parsing... [In reply to] Can't Post


In Reply To
and generally, C structures are a bit more expressive (at least in my opinion) than Perl pack strings


That's right. Pretty cool actually.

Since I can't speak C that much, I don't know if this is already possible (or not). In VB, there is a pretty cool way of saving/getting a struct from a file. Is this also possible in C? I'm really missing that feature in Perl here!


Code
' As you can see, VB hides all pointers here. 

Type Coord
X As Long
Y As Long
End Type

Type SomeObject
Name As String ' BSTR in VB
Coords() As Coord ' Dynamic Array
End Type

..
Dim Variable As SomeObject
ReDim Variable.Coords(0 To 4) As Coord ' resize array

put #FH, Variable ' Puts it

get #FH,,Variable ' Fills the variable


Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


freddo
User

Mar 3, 2002, 3:00 PM

Post #8 of 9 (693 views)
Re: [yapp] Needing infos on parsing... [In reply to] Can't Post

Hi Yapp,

Now i'm bad on this one, imagine, i had to write a script parser in vbscript... yarglahou! When a hash could do, i had to use if/else thingies coz nobody understood hashes... sometimes, i could kill myself, for sure!

freddo
;---


yapp
User

Mar 4, 2002, 8:28 AM

Post #9 of 9 (687 views)
Re: [freddo] Needing infos on parsing... [In reply to] Can't Post

Guess this is getting very offtopic BlushPirate

Yep writing a parser in VB isn't that cool.

I don't use VB that much anymore (just for simple GUI apps only)
I've written a parser for HTML-color highlighting in Perl code. This parser is written in perl indeed.. I use that for my website online listings. Almost everything is colored correctly.

Can't imagine that would ever be working in VB or Java. Without regexps, code pointers and hashes? No Way!

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Mar 4, 2002, 8:29 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives