473,563 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can I know how to write a html parser in C

Hi

I am fairly familiar in C but not much.

I want to know how I can write a html parser in C that only parses for
the image file in the html file and display or print
all the images found in the html file.

How to go about it?

Should I have a file pointer and store the html file into an array
first and then look for the img src..
like do some string compare...

Is there a sample on the net(not a hifi code,, a simple one) that I can
look at to give me an idea on what I need to do.

Thanks again

Nov 14 '05 #1
14 3121
"WUV999U" <us************ **@gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .

I want to know how I can write a html parser in C that only parses for
the image file in the html file and display or print
all the images found in the html file.


You need to detect and print all img references that are found in an HTML
file? I'd use Perl, not C.
Nov 14 '05 #2
well,
that was a great suggestion Jarmo. As you said, I can use Perl.
But m afraid m not used to it.
I need to get this done in a day or so..
If I use C, how do I go about it?

Thanks

Nov 14 '05 #3
WUV999U wrote:

Hi

I am fairly familiar in C but not much.

I want to know how I can write a html parser in C that only parses for
the image file in the html file and display or print
all the images found in the html file.

How to go about it?

Should I have a file pointer and store the html file into an array
first and then look for the img src..
like do some string compare...
That's certainly a valid approach, if you are sure you have the
RAM to get the whole HTML file into memory (for this to be a problem,
it would have to be a tiny computer and one mother of a Web page!).

Use strchr to find a '<' character. Now you know you have a tag.
I don't recall whether whitespace is allowed before the 'i' or 'I'
of img; to be certain, skip past whitespace. isspace() will help
you there. When you get past the whitespace, compare the next
three characters, case-insensitively, to "img". If you have a
match, press on and look for "src", which isn't necessarily just
one whitespace away from "img", so be careful. Don't forget it
might be "SRC" or even "sRc". The rest of this bit should be
obvious.

If the first non-whitespace char after '<' is /not/ 'i' or 'I',
simply look for another '<'.

Keep going until you run out of file.
Is there a sample on the net(not a hifi code,, a simple one) that I can
look at to give me an idea on what I need to do.


Have a go at it yourself. If you get stuck, post your best-effort
code here, and I expect someone will help you get unstuck again.
Nov 14 '05 #4
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
WUV999U <us************ **@gmail.com> wrote:
:that was a great suggestion Jarmo. As you said, I can use Perl.
:But m afraid m not used to it.

Too bad, it'd be faster to write the program.

:I need to get this done in a day or so..
:If I use C, how do I go about it?

State machine.

set state = 0
On each iteration of the loop, fetch one character

State 0: if the character is < then set matchlen = 0 and transit to state 1
otherwise discard the character and stay in state 0

State 1: If you are in state 1 and tolower(charact er) is
"img"[matchlen] then matchlen++; if matchlen=3 then transit to
state 2 else stay in state 1
else if the character is ! then transit to state 3 else transit to state 4

State 2: recognize and discard whitespace (including newline).
When you get the first non-whitespace character, then if you had
no whitespace or if tolower(charact er) is not 'h' then transit to state 4
else transit to state 5

State 3: you might be in a comment. Do what you need to to figure out
if you have a valid start of comment. When you have determined that you
do, go to state 6; if you don't, go to state 4

State 4: you are either not in an IMG tag or you are recovering from
an error. In either case, you are not presently in quotes. accept and
discard characters until you either get a '>' or you hit quotes; if you
hit quotes, transit to a quote-absorbtion state

State 5: you have recognized up to "<img h". recognize and accept
characters that match "ref=\"" and then enter url acceptance mode;
if you hit something else, go to state 4

State 6: you are in a comment. accept and discard all characters until
you find an end-of-comment marker or you find quotes. At end of comment
go back to state 0; at quotes, go to state 7; otherwise stay in state 6

State 7: you are inside quotes inside a comment. accept and discard
all characters until you find an unescaped end of quote. When you
do, go back to state 6; until then stay in state 7
And so on. You can see the general outline -- and you can see some
of the complications. You must account for comments! You must account
for the possibility that what looks like the end of a comment is in
the middle of a quoted string! You should probably take into account
whether you are in an OBJ or javascript, since any IMG in those are
not necessarily going to be shown. You should probably take into
account that if you are within a LAYER that the layer might not be
visible. You should probably take into account that if you are
inside a FRAMES section that nothing there will ever be displayed:
FRAMES sections can only have references to the frame files they import.
You should probably take into account that if you are within a
FRAMES section that you should be chasing the URLs named there because
images referenced in them will be shown. You should probably take
into account that an IMG reference in a HEAD section will not be
displayed. And probably two or three fortnights worth of more complications.
Frankly, if your C and programming experience is not strong enough
that you didn't know how to go about starting this, then there is
virtually no chance that you can properly impliment it in C within
your "day or two" timeframe. HTML parsing has lots of Gotcha!'s.

It would probably be faster for you to learn the rudiments of Perl and
call upon the LWP moudle to extract the IMG tags for you, then it would
be for you to write the parser in C.
--
If a troll and a half can hook a reader and a half in a posting and a half,
how many readers can six trolls hook in six postings?
Nov 14 '05 #5
WUV999U wrote:
well,
that was a great suggestion Jarmo. As you said, I can use Perl.
But m afraid m not used to it.
I need to get this done in a day or so..
If I use C, how do I go about it?


One simple way:
- fopen() the file
- read characters with getc() until EOF
- implement a state stack with the following states:
1 outside of body
2 inside of body, normal text
3 dito, tag opens
4 dito, img tag
5 dito, not img tag
6 dito, comment starts
7 dito, inside comment
8 dito, comment ends
9 dito, tag closes
for 4 and 5, you also need
a) inside tag, outside quoted
b) dito, quote starts
c) dito, inside quote
d) dito, quote ends
for 4), you also need e)-h) which is like a)-d) for src="path/to/pic"
- enable only certain transitions
- add special code for 4g)

If you are certain that the only quoted part within an img tag
is the path to the image, or that no graphics are commented out,
etc., you can leave out the respective states.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #6
"WUV999U" <us************ **@gmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
well,
that was a great suggestion Jarmo. As you said, I can use Perl.
But m afraid m not used to it.
I need to get this done in a day or so..
If I use C, how do I go about it?

Thanks


If you have one day to do it then use Perl. Or a combination of existing
tools such as grep and sed. The fact that you don't currently know how to
do it in C indicates that you have little to no chance to learning how to do
it in C in one day.
Nov 14 '05 #7
oh OK,

well let me check if I can use PERL and write back soon.
Thank you sooooooo much for all your help.

Nov 14 '05 #8
"WUV999U" <us************ **@gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
oh OK,

well let me check if I can use PERL and write back soon.
Thank you sooooooo much for all your help.


A Perl newsgroup will be able to help pretty quickly. Finding and isolating
pieces of text is something that Perl's very good at.
Nov 14 '05 #9
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
us************* *@gmail.com says...
Hi

I am fairly familiar in C but not much.
A sentence that contradicts itself.
I want to know how I can write a html parser in C that only parses for
the image file in the html file and display or print
all the images found in the html file.

How to go about it?
Numerous examples should be available on www.sf.net. You may find
searching on freshmeat.net slightly easier, but they seem to all
go to the same place in the end.
Should I have a file pointer and store the html file into an array
first and then look for the img src..
like do some string compare...
That's certainly one way. It sort of depends upon how big the HTML
files might be, and how much horsepower your target system has.
Is there a sample on the net(not a hifi code,, a simple one) that I can
look at to give me an idea on what I need to do.


Google for "HTML parser C" found lots of them. Is there some reason
you prefer Usenet to google for web searching?

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2580
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form field beside name. I want to get the following but not sure how to modify the code below. 1. Field Name (to appear beside NAME:) 2. Field Type (to...
0
2712
by: Himanshu Garg | last post by:
Hello, I am using HTML::Parser to extract text from html pages from http://bbc.co.uk/urdu/ However the encoding of the input text seems to change to some unknown encoding in the output. The program is given below. The HTML is in a string to keep the example simple. The same problem appears with HTML in a file.
3
3117
by: Himanshu Garg | last post by:
Hello, I am trying to pinpoint an apparent bug in HTML::Parser. The encoding of the text seems to change incorrectly if the locale isn't set properly. However Parser.pm in the directory (/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/HTML/) doesn't seem to contain the "real" parsing statements.
6
12966
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java scripts. Being a javascript newbie and after significant testing, I suspect that the document.write fails after finding a </script> within pageVar....
12
3317
by: Radek Maciaszek | last post by:
Hi It's very interesting problem. I couldn't even find any inforamtion about it on the google. I think that the best way of explain will be this simple example: <html> <body> <script language="JavaScript" type="text/javascript" src="write.js"></script>
12
2829
by: Sean | last post by:
Hi, I have the following script: ----------------------------------------------------------------------------------- <script type="text/javaScript"> <!-- document.write('<div id=hello1>Hello1</div>'); document.write('<div id=hello2 style="display:none;"><script src="test.js"><\/script></div>');
82
6255
by: Eric Lindsay | last post by:
I have been trying to get a better understanding of simple HTML, but I am finding conflicting information is very common. Not only that, even in what seemed elementary and without any possibility of getting wrong it seems I am on very shaky ground . For example, pretty much every book and web course on html that I have read tells me I must...
0
1986
by: june | last post by:
Hi, I have a big problem with parsing HTML into a XHTML using Cberneko to validate the html. First I tried to work with a HTML-File. This solutions works fine: String aHTMLFile = "file:\\C:/work/Eclipse3.1.1/html-file.html"; org.xml.sax.InputSource pSource = new InputSource(aHTMLFile);
6
7369
by: Herby | last post by:
Hi, Im interested in Reverse Engineering C++ source code into a form more comprehensible than the source itself. I want to write a basic one myself, obviously i need to write a parser for the source code. Although this has some overlap with say a compiler it would also seem significantly different too.
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.