473,785 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems parsing a flat text file with pointers.. sort of named.conf layout..

Hello,

I am having some problems understanding (most likely), parsing a text
file. I would like to parse a file like:

block1 {
stuff;
...
stuffN;
};

Just like in BIND config. (Silly project but important to me)

I would not like to use string.h functions such as strstr(), strtok()
(maybe make use of strncpy, strncat if I have to though..).

You will all probably balk at my general logic when looking at the
code, so perhaps someone might suggest a better methodology for parsing
files? (apart from lex/yacc).

I have the following code, but it doesn't seem to get out of the first
block. Would anyone mind sifting through the rubbish code and helping
me out? (i'll post at bottom of this post).

Thanks a million, 2 virtual pints of guinness for you all!

Hugh.
------ begin code -------

#include <stdio.h>

#define MAX 512

int main(int argc, char **argv)
{
int i,j,ctr,in_bloc k=0;
char line[MAX];
FILE *fp;

if((fp=fopen(ar gv[1],"r+")) == NULL) {
fprintf(stderr, "Unable to open %s\n",argv[1]);
exit(-1);
}

while(fgets(lin e,MAX,fp) != NULL) {
for(i=0;i<=MAX; i++) {
if(line[i] == '\0' || line[i] == '#') {
i++;
continue;
}

if(line[i] == '{') {
in_block=1;
printf("Enterin g block\n");
j=i;
ctr++;
while(line[j] != '}') {
if(line[j] == '\0' || line[j] =='#')
continue;
printf("--> within block\n");
j++;

} in_block=0;
i += (j-i);
printf("Leaving block\n\n");

}
}
}
return 0;
}
------- end code --------

Nov 14 '05 #1
4 1727
Each APRD Chain may contain a variable number of entries (APRDs). The APRD
entry shall be physically continuous, locked in memory, and Qword-aligned in
physical address space. The information in the APRD entry is derived by the
host, and describes the physical addresses corresponding to the logical
buffer address in the original I/O request. There can be several APRDs to
describe a transfer buffer because some processors fragment physical memory
by the use of paging registers.

Nov 14 '05 #2
Hi,

I'm pretty sure that's not intended for this post....

Mercier Beaucoupoo

Nov 14 '05 #3
"Hugh" <hu***@kiaro.ne t> wrote:
I am having some problems understanding (most likely), parsing a text
file. I would like to parse a file like:

block1 {
stuff;
...
stuffN;
};

Just like in BIND config. (Silly project but important to me)

I would not like to use string.h functions such as strstr(), strtok()
(maybe make use of strncpy, strncat if I have to though..).
Whyever not? BTW, strncpy() is rarely the right function to use.
strncat() can do the job more efficiently nearly everywhere.
#include <stdio.h>

#define MAX 512

int main(int argc, char **argv)
{
int i,j,ctr,in_bloc k=0;
Tabstops on Usenet. Ow.
char line[MAX];
FILE *fp;

if((fp=fopen(ar gv[1],"r+")) == NULL) {
fprintf(stderr, "Unable to open %s\n",argv[1]);
exit(-1);
This is an odd status to return to the OS.
if(line[i] == '{') { while(line[j] != '}') {
And what happens when the '}' is not on the same line as the '{'?
fgets() only ever reads one line.
i += (j-i);


That's just i=j writ large.

Richard
Nov 14 '05 #4
Hugh wrote:

I am having some problems understanding (most likely), parsing a
text file. I would like to parse a file like:

block1 {
stuff;
...
stuffN;
};

Just like in BIND config. (Silly project but important to me)

I would not like to use string.h functions such as strstr(), strtok()
(maybe make use of strncpy, strncat if I have to though..).

You will all probably balk at my general logic when looking at the
code, so perhaps someone might suggest a better methodology for
parsing files? (apart from lex/yacc).


First, get hold of your lines. If you don't want to worry about
line length etc. just use ggets (or fggets) as follows:

#include "ggets.h"
....

char *ln;
int err;

....
while (0 == (err = fggets(&ln, fp)) {
/* process the line */
free(ln); /* assuming it hasn't been saved */
}
/* the file has been processed or an error encountered */
if (err > 0) puts("Ran out of memory, truncating");

You can get the source code module (portable) at:

<http://cbfalconer.home .att.net/download/ggets.zip>

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5

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

Similar topics

1
2668
by: .d.hos | last post by:
ok, fairly new to python, relatively familiar w/ ms-sql. here's my issue: my .py script parses the contents of a (tab delim.) flat file, then attempts to insert the information into the db. I've been fighting this for a day or so, and i'm stuck on the db insertion... basically the script uses .readlines() to capture the flat file contents, and stick the *record* into a container list...
3
1291
by: Aaron Walker | last post by:
At the beginning of my program, I open a config file and load the contents into a structure (please disregard the non-portable sockaddr_in struct as it is irrelevant to the problem): struct conf_data { char *config_file; char *root; /* SERVER_ROOT */ char *pid_file; /* PID_FILE */ char *log_dir; /* LOG_DIR */ struct sockaddr_in local_addr;
9
1998
by: BMarsh | last post by:
Hi all, I have the following piece of code which i've been wrestling with for sometime, which I attribute, still, to my lack of understanding of pointers. (this is probably rining alarm bells atm for you seasoned pro's...). I am trying to parse a file with the following format: hostname {
3
2400
by: Ali Sahin | last post by:
Hi there, I'd like to transform a XML-File to PDF. The XML-File ist build like followed: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <?xml-stylesheet type="text/xsl" href="D:\app\jboss-3.2.5\server\default\deploy\xifs.war\WEB-INF\classes\de\xifs\resource\xml\de\xifs\resource\xml\dunningaccountreport_de.xsl"?> <!DOCTYPE entities >
26
6878
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What is in my Watch:
0
3304
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections. Each section has a list of questions. Each question has responses. I need to display a list of responses to the questions (i.e. set:distinct), once and only once, each section. My second problem is that these questions can also have corrective...
12
2562
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawk¹ parsing an XHTML file containing a hCard², without luck. I wonder if you guys could give me a push... Let's say I have the following XHTML file: #v+
15
5277
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
1
1905
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating forms / html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
9647
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.