Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help needed to count lines between preprocessor directives

Reply
Thread Tools

Help needed to count lines between preprocessor directives

 
 
Nalla
Guest
Posts: n/a
 
      09-08-2003
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...
 
Reply With Quote
 
 
 
 
rzed
Guest
Posts: n/a
 
      09-08-2003
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





 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      09-08-2003
Nalla <(E-Mail Removed)> 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
--
_ _____ _____
| ||_ _||_ _| http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      09-08-2003

"Nalla" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-08-2003
Nalla <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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




 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-08-2003

Mike Wahler <(E-Mail Removed)> wrote in message
news:VO37b.3619$(E-Mail Removed) 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



 
Reply With Quote
 
richard
Guest
Posts: n/a
 
      09-08-2003
On Mon, 8 Sep 2003 08:48:13 -0400, "rzed" <(E-Mail Removed)> wrote:

>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.


LOL!

>Hope that helps.


It does. I feel much better after reading your post.

>Here's some code (untested):
>
>
>#include <stdlib.h>
>
>int main( int argc, char * argv[] )
>{
>
> /* do the stuff I talked about */
>
> return EXIT_SUCCESS;
>}


I got warning message about unused parameters argc and argv, but otherwise
worked perfectly in my tests.

 
Reply With Quote
 
MikeyD
Guest
Posts: n/a
 
      09-08-2003

"Nalla" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
}


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-08-2003
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

 
Reply With Quote
 
LibraryUser
Guest
Posts: n/a
 
      09-09-2003
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preprocessor directives in codebehind file Theon Greyjoy ASP .Net 1 10-06-2006 08:17 PM
Preprocessor directives must appear as the first non-whitespace character on a line IndyChris ASP .Net 1 08-09-2006 01:21 AM
Preprocessor directives must appear as the first non-whitespace character on a line erin.sebastian@cowaninsurancegroup.com ASP .Net 0 02-14-2006 03:11 PM
Preprocessor Directives Water Cooler v2 C Programming 5 07-05-2005 06:42 PM
how to make elegant use of preprocessor directives seemanta dutta C Programming 13 12-01-2003 02:39 PM



Advertisments