473,780 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(preferab ly) 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 2700
Nalla wrote:
Hi,
I want a program. It should be a command line one. you can input
the path of a folder(preferab ly) 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**********@f astmail.fm> wrote:
I want a program. It should be a command line one. you can input the
path of a folder(preferab ly) 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.want ed 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***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #3

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

I want a program. It should be a command line one. you can input the
path of a folder(preferab ly) 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**********@f astmail.fm> wrote in message
news:ea******** *************** **@posting.goog le.com...
Hi,
I want a program. It should be a command line one. you can input the
path of a folder(preferab ly) 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(co nst 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(lin e, 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******@mkwah ler.net> wrote in message
news:VO******** ********@newsre ad4.news.pas.ea rthlink.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**********@f astmail.fm> wrote in message
news:ea******** *************** **@posting.goog le.com...
Hi,
I want a program. It should be a command line one. you can input the
path of a folder(preferab ly) 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(fil epath, "r");
fseek(cfile,SEE K_SET);//not sure if this is correctly formulated
for(;strcmp(tst r,"#ifdef win32")!=0;fget s(cfile,tstr));//value for strcmp
may be wrong, fgets args may also be wrong
for(;strcmp(tst r,"#endif")!=0; fgets(cfile,tst r)) lines++;
printf("There were %d lines between the pre-processor commands\n",lin es);
//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**********@m ade.invalid> wrote in message
news:3F******** *******@made.in valid...
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

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

Similar topics

3
2455
by: Jacek Dziedzic | last post by:
.... making things like #endif // of the "#ifdef DEBUG" part valid? Or not? - J.
67
4284
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. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
8
2924
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 should be inserted. So, is it possible to write a macro that does this: MACRO(alpha) expands to nothing
21
7650
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
2936
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 statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
5
3622
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 anyone explain why loop unrolling doesn't play well with templates? Or better, can someone submit a code fragment to get desired results? Here are the command-lines I use to generate code: "g++ -DTEMPLATE=0 -o gotofun0 gotofun.cpp" works exactly as...
3
2901
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 being in the int main(). I am using visual studio on windows XP. In my code I've defined a local variable in a function but when I run the program and it gets to that function it says I have an undeclared variable. The code for the program...
14
2601
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 options I choose (possibly via some kind of user input screen). Does such a tool exist? A Visual Studio plugin would be even better. FYI: I have requirements to rid my code of all conditionally compiled
0
9636
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
9474
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8961
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
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
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4037
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
3
2869
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.