473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help needed to count lines between preprocessor directives

Hi,
I want a program. It should be a command line one. you can input the
path of a folder(preferably) or a file...it should count the no. of
lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...
Nov 13 '05 #1
24 2643
Nalla wrote:
Hi,
I want a program. It should be a command line one. you can input
the path of a folder(preferably) or a file...it should count the
no. of lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...


Glad to.

Write a program that accepts arguments from the command line. Decide
what to do with input that doesn't make sense in terms of your
assignment, and write the code to do that.

For valid input, which will, I assume, be the name of a file that
contains at least one compiler directive, open the file. If it opens
correctly, read it one line at a time and determine whether the line
contains a compiler directive. If it does, begin counting with the
next line. When you reach the next compiler directive, emit a message
that tells you how many lines you have counted, and prepare to start
counting again. Continue until you run out of lines. Decide how you
will handle reporting on the last set of lines (if there are any after
the last compiler directive). Close the file. Emit any concluding
messages you think appropriate.

I can attest that I am writing this on a machine running Windows 2000.

Hope that helps.

Here's some code (untested):
#include <stdlib.h>

int main( int argc, char * argv[] )
{

/* do the stuff I talked about */

return EXIT_SUCCESS;
}

--
rzed

Nov 13 '05 #2
Nalla <na**********@fastmail.fm> wrote:
I want a program. It should be a command line one. you can input the
path of a folder(preferably) or a file...it should count the no. of
lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...


Sorry, but clc is neither alt.source.wanted nor has your question
anything to do with C.

<OT>
Since I feel like being nice today here's an (completely untested!)
Perl script (I don't know what you mean by "path of folder" because
a folder rarely has lines in it. and even less compiler directives.
I also would usually try to avoid doing something like this in C
because I am too lazy or got better things to do):

#!/usr/bin/perl -w

use strict;

my $f;
my ( $level, $lc, $found, $in_sec ) = ( 0, 0, 0, 0 );

open( $f, "<$ARGV[ 0 ]" ) or die "Can't open file $ARGV[ 0 ]: $!\n";

while ( <$f> ) {
if ( /\s*#endif\s*/ ) {
$level-- ;
die "More \"#endif\" than \"#ifxxx\" found in file $ARGV[ 0 ]\n"
if $level < 0;
$in_sec = 0;
}
$lc++ if $in_sec;
if ( /\s*#if\s+win32\s*/ ) {
$found++;
$in_sec = 1;
}
$level++ if /\s*#if/;
}

die "Missing #endif in file $ARGV[ 0 ]\n" unless ! $level;
if ( $lc ) {
print "Found $lc lines between \"#if win32\" and the corresponding " .
"\"#endif\" directives (in $found sections)\n";
} else {
print "No \"#if win32\" directive found in file $ARGV[ 0 ]\n";
}

Not counting continuation lines is left as an exercise for the reader ;-)
</OT>
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #3

"Nalla" <na**********@fastmail.fm> wrote in message

I want a program. It should be a command line one. you can input the
path of a folder(preferably) or a file...it should count the no. of
lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...

Unfortunately none of the regs are likely to have time to offer a free
programming service.
However your problem shouldn't be too difficult, particularly if you are not
too fussy about being robust for degenerate cases such as preprocessor
directives being themselves redefined or placed in comments.

C offers no directory services so to do a whole folder at once you need a
platform-specific extension.

Simply open the file, read in the lines one at a time, and search for #ifdef
win32.
Then search though counting #if... s and #endifs, until you come to the
matching #endif.
Nov 13 '05 #4
Nalla <na**********@fastmail.fm> wrote in message
news:ea*************************@posting.google.co m...
Hi,
I want a program. It should be a command line one. you can input the
path of a folder(preferably) or a file...it should count the no. of
lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...


Since we don't do people's work for them here, I'll offer
a more general solution that you should be able to adapt
to your specific needs. This shows the line number where
each directive appears (and the line itself), and how many
lines appear between successive directives. A directive is
considered to exist on any line whose first non-whitespace
character is a '#' character.

#define COMPILE_THIS
#ifdef COMPILE_THIS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_SIZE 128
#define DIRECTIVE_CHAR '#'

int is_directive(const char *line, size_t len)
{
static char tok[LINE_SIZE];
return sscanf(line, "%s", tok) != EOF &&
*tok == DIRECTIVE_CHAR;
}

int main(int argc, char **argv)
{
char line[512] = {0};
size_t line_no = 0;
size_t between = 0;
FILE *input = NULL;

if(argc < 2)
{
puts("Requires file name argument");
return EXIT_FAILURE;
}

if(!(input = fopen(argv[1], "r")))
{
puts("Cannot open input\n");
return EXIT_FAILURE;
}

while(fgets(line, sizeof line, input))
{
++line_no;

if(is_directive(line, sizeof line))
{
if(between)
printf("[%lu line%c]\n",
(unsigned long)between, " s"[between > 1]);

printf("[Line %lu] %s", (unsigned long)line_no, line);
between = 0;
}
else
++between;
}

fclose(input);
return 0;
}

#endif

Output when given its own source:

[Line 1] #define COMPILE_THIS
[2 lines]
[Line 4] #ifdef COMPILE_THIS
[1 line ]
[Line 6] #include <stdio.h>
[Line 7] #include <stdlib.h>
[Line 8] #include <string.h>
[1 line ]
[Line 10] #define LINE_SIZE 128
[Line 11] #define DIRECTIVE_CHAR '#'
[48 lines]
[Line 60] #endif
I did not test this thoroughly; I'll let you discover
and fix any bugs that might exist. :-)

HTH,
-Mike


Nov 13 '05 #5

Mike Wahler <mk******@mkwahler.net> wrote in message
news:VO****************@newsread4.news.pas.earthli nk.net...

[snip]
int main(int argc, char **argv)
{
char line[512] = {0};


Oops, forgot to change all my 'hardcodes' to #defined values.
Make that:

char line[LINE_SIZE] = {0};

[snip]

Sorry about that.

-Mike

Nov 13 '05 #6

"Nalla" <na**********@fastmail.fm> wrote in message
news:ea*************************@posting.google.co m...
Hi,
I want a program. It should be a command line one. you can input the
path of a folder(preferably) or a file...it should count the no. of
lines between the compiler directives,
ifdef win32 and # endif.can u pls help me out...


Not robust or anything, but should work...
#include <stdio.h>
char filepath[100];
char tstr[10];
int lines;
FILE* cfile
int main(void){
lines=0
printf("OK, so what's the file then?\n");
gets(filepath);
cfile=fopen(filepath, "r");
fseek(cfile,SEEK_SET);//not sure if this is correctly formulated
for(;strcmp(tstr,"#ifdef win32")!=0;fgets(cfile,tstr));//value for strcmp
may be wrong, fgets args may also be wrong
for(;strcmp(tstr,"#endif")!=0;fgets(cfile,tstr)) lines++;
printf("There were %d lines between the pre-processor commands\n",lines);
//is this how you want it returned?
return lines; //or return 0;
}
Nov 13 '05 #7
MikeyD wrote:
Not robust or anything, but should work...
And is grossly antisocial. The lack of indentation and the gratuitous use
of filescope variables, and '//' comments on very long lines suggests that
yours is a joke post (but not a very good one). Be that as it may, gets(filepath);

is a terrible thing to suggest. Not even the most clueless poster deserves
to be treated that way.

--
Martin Ambuhl

Nov 13 '05 #8
Mike Wahler wrote:
.... snip ...
lines appear between successive directives. A directive is
considered to exist on any line whose first non-whitespace
character is a '#' character.


#define whatsit "I am a \
# marker containing string "

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #9

LibraryUser <de**********@made.invalid> wrote in message
news:3F***************@made.invalid...
Mike Wahler wrote:

... snip ...

lines appear between successive directives. A directive is
considered to exist on any line whose first non-whitespace
character is a '#' character.


#define whatsit "I am a \
# marker containing string "


OK, you broke my toy program. When someone pays me,
I'll make it better. :-)

-Mike

Nov 13 '05 #10
> > Not robust or anything, but should work...

And is grossly antisocial. The lack of indentation and the gratuitous use
of filescope variables, and '//' comments on very long lines suggests that
yours is a joke post (but not a very good one). Be that as it may,
gets(filepath); is a terrible thing to suggest. Not even the most clueless poster

deserves to be treated that way.

What? He asked for a program and I gave him one. Not a very good one, but it
would work. No-one else even bothered to write one.
Nov 13 '05 #11

MikeyD <m_*********@hotmail.com> wrote in message
news:10****************@eunomia.uk.clara.net...
Not robust or anything, but should work...
And is grossly antisocial. The lack of indentation and the gratuitous use of filescope variables, and '//' comments on very long lines suggests that yours is a joke post (but not a very good one). Be that as it may,
gets(filepath);

is a terrible thing to suggest. Not even the most clueless poster

deserves
to be treated that way.

What? He asked for a program and I gave him one. Not a very good one, but

it would work. No-one else even bothered to write one.


Really? My example can beat up your example. :-)

-Mike

Nov 13 '05 #12
> > What? He asked for a program and I gave him one. Not a very good one,
but
it
would work. No-one else even bothered to write one.


Really? My example can beat up your example. :-)

-Mike

Sorry, yes, you're right as a general program. But if he just wants to count
between those two particular directives then mine'll be easier for him to
use. I'll just fix it for commandline input, but that was chapter...actually
it wasn't in C for dummies at all.
Maybe this should be used as an example of how not to code whilst still
getting a working program.
#include <stdio.h>
char filepath[100];
char tstr[10];
int lines;
FILE* cfile
int main(int whatever, char**notaclue){
lines=0
if(whatever==1)goto meaninglesslabel;
printf("I didn't understand the arguments you gave me, so what's the file
then?\n");
gets(filepath);
goto evenworse;
meaninglesslabel: filepath=notaclue[1];//or should this be 0? I never really
//worked out how the ags are arranged
evenworse:
cfile=fopen(filepath, "r");
fseek(cfile,SEEK_SET);//not sure if this is correctly formulated
for(;strcmp(tstr,"#ifdef win32")!=0;fgets(cfile,tstr));//value for strcmp
//may be wrong, fgets args may also be wrong
for(;strcmp(tstr,"#endif")!=0;fgets(cfile,tstr)) lines++;
printf("There were %d lines between the pre-processor commands\n",lines);
//is this how you want it returned?
return lines; //or return 0;
}

Nov 13 '05 #13
MikeyD wrote:
gets(filepath);


Please stop this. No one deserves to be treated to this antisocial "advise."

--
Martin Ambuhl

Nov 13 '05 #14
Martin Ambuhl wrote:

MikeyD wrote:
gets(filepath);


Please stop this.
No one deserves to be treated to this antisocial "advise."


http://www.eskimo.com/~scs/C-faq/q12.23.html
Nov 13 '05 #15
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:Zb***************@newsread1.news.atl.earthlin k.net...
MikeyD wrote:
gets(filepath);
Please stop this. No one deserves to be treated to this antisocial

"advise."

It's just there as a backup now that I've fixed the commandline bit. There
are flaws with fgets() as well (the site mentions its failure to delete \n
at the end, I'm sure I've read of others) Obviously it's best to use your
own getstring() function but I can't rely on him having my personal function
library. For a utility program like this, gets() is perfectly suitable.
And on what grounds do you claim it's antisocial??
Nov 13 '05 #16
"MikeyD" <m_*********@hotmail.com> wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:Zb***************@newsread1.news.atl.earthli nk.net...
MikeyD wrote:
> gets(filepath);


Please stop this. No one deserves to be treated to this antisocial

"advise."

It's just there as a backup now that I've fixed the commandline bit. There
are flaws with fgets() as well (the site mentions its failure to delete \n
at the end, I'm sure I've read of others) Obviously it's best to use your
own getstring() function but I can't rely on him having my personal function
library. For a utility program like this, gets() is perfectly suitable.
And on what grounds do you claim it's antisocial??

gets() is suitable for absolutely nothing, except breaking your code.
And AFICS that's why Martin claimed it antisocial to suggest to use it.

Irrwahn
--
Close your eyes and press escape three times.
Nov 13 '05 #17
MikeyD wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:Zb***************@newsread1.news.atl.earthlin k.net...
MikeyD wrote:

gets(filepath);
Please stop this. No one deserves to be treated to this antisocial


"advise."

It's just there as a backup now that I've fixed the commandline bit. There
are flaws with fgets() as well (the site mentions its failure to delete \n
at the end, I'm sure I've read of others)


There may be flaws in fgets(), but not deleting '\n' is not one of them.
Dan Pop will probably provide a list of what's *really* wrong (on his vies)
with fgets(). If you think not deleting '\n' is a flaw, then you haven't
been programming long enough.

There is no such thing as using gets "as a backup." Using an inherently
dangerous function that ought never be used is not "a backup."
Obviously it's best to use your
own getstring() function
That's not obvious. There are situation in which having your own
getstring() function has attractions, though.
but I can't rely on him having my personal function
library.
What crap. If you want your own getstring() function just write the damn
thing and stick it in and you will be 100% sure of having it.
For a utility program like this, gets() is perfectly suitable.
Bullshit.
And on what grounds do you claim it's antisocial??


You are suggesting the use of inherently unsafe functions for which use
there is no excuse. Why not just hand out guns to children?


--
Martin Ambuhl

Nov 13 '05 #18
Fine then.
#include <stdio.h>
#include <stdlib.h>
char filepath[100];
char tstr[10];
int lines;
FILE* cfile
int main(int whatever, char**notaclue){
lines=0
if(whatever==1)goto meaninglesslabel;
printf("I didn't understand the arguments you gave me, so what's the file
then?\n");
for(int i=0;i<100;i++){filepath[i]=getchar();
if(filepath[i]=='\n'){
filepath[i]='\0';
break;}
if(i==99){
printf("You entered too long a filename");
exit(1);
}}
goto evenworse;
meaninglesslabel: strcpy(filepath,notaclue[1]);//or should this be 0? I
never really
//worked out how the ags are arranged
evenworse:
cfile=fopen(filepath, "r");
fseek(cfile,SEEK_SET);//not sure if this is correctly formulated
for(;strcmp(tstr,"#ifdef win32")!=0;fgets(cfile,tstr));//value for strcmp
//may be wrong, fgets args may also be wrong
for(;strcmp(tstr,"#endif")!=0;fgets(cfile,tstr)) lines++;
printf("There were %d lines between the pre-processor commands\n",lines);
//is this how you want it returned?
return lines; //or return 0;
}
Happy now?

Nov 13 '05 #19
"MikeyD" <m_*********@hotmail.com> wrote in message
news:10****************@dyke.uk.clara.net...
char tstr[10]; [...] cfile=fopen(filepath, "r");
fseek(cfile,SEEK_SET);//not sure if this is correctly formulated
Or indeed even needed -- why seek to the beginning a file you've just
opened?
for(;strcmp(tstr,"#ifdef win32")!=0;fgets(cfile,tstr));//value for strcmp


tstr is undefined for the first iteration here.

James
Nov 13 '05 #20
> > char tstr[10];
[...]
cfile=fopen(filepath, "r");
fseek(cfile,SEEK_SET);//not sure if this is correctly formulated
Or indeed even needed -- why seek to the beginning a file you've just
opened?


To make sure the cursor is at the start. I don't know if it's necessary or
not.
for(;strcmp(tstr,"#ifdef win32")!=0;fgets(cfile,tstr));//value for
strcmp
tstr is undefined for the first iteration here.

Okay then, replace char tstr[10]; with char tstr[]="aaaaaaaaaaa";
Nov 13 '05 #21
On Mon, 6 Oct 2003 16:56:23 +0100, "MikeyD" <m_*********@hotmail.com>
wrote:
> char tstr[10];

[...]
> cfile=fopen(filepath, "r");
> fseek(cfile,SEEK_SET);//not sure if this is correctly formulated


Or indeed even needed -- why seek to the beginning a file you've just
opened?


To make sure the cursor is at the start. I don't know if it's necessary or
not.


What cursor?

What does you manual say that fopen will do if it successfully opens a
file for read?

<<Remove the del for email>>
Nov 13 '05 #22

"Barry Schwarz" <sc******@deloz.net> wrote in message
news:bl**********@216.39.135.219...
On Mon, 6 Oct 2003 16:56:23 +0100, "MikeyD" <m_*********@hotmail.com>
wrote:
> char tstr[10];
[...]
> cfile=fopen(filepath, "r");
> fseek(cfile,SEEK_SET);//not sure if this is correctly formulated

Or indeed even needed -- why seek to the beginning a file you've just
opened?
To make sure the cursor is at the start. I don't know if it's necessary ornot.


What cursor?


The file cursor
What does you manual say that fopen will do if it successfully opens a
file for read?

It says it will return a pointer to that file. Then it moves on to fclose().
Nov 13 '05 #23
On Tue, 7 Oct 2003 18:06:40 +0100, in comp.lang.c , "MikeyD"
<m_*********@hotmail.com> wrote:

"Barry Schwarz" <sc******@deloz.net> wrote in message
news:bl**********@216.39.135.219...
>> Or indeed even needed -- why seek to the beginning a file you've just
>> opened?
>
>To make sure the cursor is at the start. I don't know if it's necessary or
>not.


What cursor?


The file cursor


its called the file position indicator in the C standard. "cursor" is
a term more often associated with databases or screens.
What does you manual say that fopen will do if it successfully opens a
file for read?

It says it will return a pointer to that file.


It doesn't mention the effect of the 2nd parameter to fopen()? If it
came with your compiler, complain to the vendor, otherwise throw it
away and get a proper manual. For any decent manual will tell you
about that parameter and thus explain where the position indicator is
set to when you open the file.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #24
> >> What does you manual say that fopen will do if it successfully opens a
file for read?

It says it will return a pointer to that file.


It doesn't mention the effect of the 2nd parameter to fopen()? If it
came with your compiler, complain to the vendor, otherwise throw it
away and get a proper manual. For any decent manual will tell you
about that parameter and thus explain where the position indicator is
set to when you open the file.


What I can remember of my manual entry. I may have just forgotten about the
file position indicator.
"...fopen([const?]char* filename, const char* mode)
Opens the file <i>filename</i> in the mode indicated by <i>mode</i>. Mode
list:
<table>
r Opens the file for reading
w Opens the file for writing
.....
</table>
<b>Returns:</b> A pointer to that file.
fclose(FILE* f)..."
Nov 13 '05 #25

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

Similar topics

3
by: Jacek Dziedzic | last post by:
.... making things like #endif // of the "#ifdef DEBUG" part valid? Or not? - J.
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
8
by: claus.tondering | last post by:
I need to write a macro that inserts someStruct m_someStruct; into another struct declaration. The problem is that if the programmer specifies one particluar struct (called alpha), nothing...
21
by: Bogdan | last post by:
Can anyone recommend a program for indentation of C preprocessor directives. My file looks like this: #ifdef a #define b #else #define c #endif int main() {
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
3
by: TamaThps | last post by:
I have to write a program that lets the user play the game of Craps. As of right now it is incomplete and I have run into some errors. It's my first time using functions instead of the program all...
14
by: lagman | last post by:
All, I'm looking for a tool that is able to take my code base (which is full of preprocessor directives) and spit out source code that is "clean" of any preprocessor directives based on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.