C source code line count function

L

lovecreatesbea...

It takes mu so time to finish this C source code line count function.
What do you think about it?


/
*******************************************************************************
* Function : size_t linecnt(char *filenm);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

******************************************************************************/

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#define LEN 1024
size_t linecnt(char *filenm)
{
const char lcmt[] = "/*";
const char rcmt[] = "*/";
const char C99cmt[] = "//";
const char esc = '\\';
const char dqm = '\"';
int lcmt_open = 0; /*1:last "/*" still opens; 0:not
yet.*/
int dqm_open = 0; /*1:last " still opens; 0:not yet.*/
size_t cnt = 0;
char buf[LEN] = {'\0'};
FILE *fileptr;
char *p1, *p2;

errno = 0;
if (!(fileptr = fopen(filenm, "r")) && errno)
{
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
return -1;
}
while (fgets(buf, LEN, fileptr))
{
/*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
*counted*/
if (!lcmt_open && (p1 = strstr(buf, lcmt)))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
{
if (p2 - 1 >= buf && *(p2 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
if (!dqm_open)
{
lcmt_open = 1;
}
if (lcmt_open && strstr(p1, rcmt))
{
lcmt_open = 0;
}
for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}

/*lcmt_open, not counted*/
else if (lcmt_open)
{
if (p1 = strstr(buf, rcmt))
{
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1) && *p1 != 0)
{
cnt++;
break;
}
}
}
}

/*C99cmt starts at the begining of a line, not counted*/
else if (p1 = strstr(buf, C99cmt))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
}

/*cnt++, deal with space lines and dqm_open state also*/
else
{
for (p1 = buf; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1))
{
cnt++;
break;
}
}
}
for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
fclose(fileptr);
return cnt;
}


#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2)
printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
else
printf("%d\n", linecnt(argv[1]));
return 0;
}


Thank you for your time!
 
U

user923005

It's a mistake not to count comments.  They are an important part of
the code.

My personal recommendation is to keep it simple like this:

/* wc: count lines, words, chars */
#include <stdio.h>

int main(int argc, char *argv[])
{
int c,
i,
inword;
FILE *fp;
long linect,
wordct,
charct;
long tlinect = 0,
twordct = 0,
tcharct = 0;

i = 1;
fp = stdin;
printf(" lines words chars file\n");
printf("======= ======= ======= =====\n");
do {
if (argc > 1 && (fp = fopen(argv, "r")) == NULL) {
fprintf(stderr, "wc: can't open %s\n", argv);
continue;
}
linect = wordct = charct = inword = 0;
while ((c = getc(fp)) != EOF) {
charct++;
if (c == '\n')
linect++;
if (c == ' ' || c == '\t' || c == '\n')
inword = 0;
else if (inword == 0) {
inword = 1;
wordct++;
}
}
printf("%7ld %7ld %7ld ", linect, wordct, charct);
printf(argc > 1 ? "%s\n" : "\n", argv);
fclose(fp);
tlinect += linect;
twordct += wordct;
tcharct += charct;
} while (++i < argc);
if (argc > 2) {
printf("======= ======= ======= =====\n");
printf("%7ld %7ld %7ld total\n", tlinect, twordct, tcharct);
}
return 0;
}
 
B

Barry Schwarz

It takes mu so time to finish this C source code line count function.
What do you think about it?


/
*******************************************************************************
* Function : size_t linecnt(char *filenm);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

******************************************************************************/

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#define LEN 1024
size_t linecnt(char *filenm)
{
const char lcmt[] = "/*";
const char rcmt[] = "*/";
const char C99cmt[] = "//";
const char esc = '\\';
const char dqm = '\"';
int lcmt_open = 0; /*1:last "/*" still opens; 0:not
yet.*/
int dqm_open = 0; /*1:last " still opens; 0:not yet.*/
size_t cnt = 0;
char buf[LEN] = {'\0'};
FILE *fileptr;
char *p1, *p2;

errno = 0;
if (!(fileptr = fopen(filenm, "r")) && errno)

fopen is not required to set errno. If it doesn't your extra
expression will prevent you from detecting that the file was not
opened.
{
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
return -1;

This function returns a size_t which is an unsigned type. It is
possible for this type to be wider than an int. The -1 will result in
a large positive value which would not fit in an int. You treat it in
main as an int which would invoke undefined behavior.
}
while (fgets(buf, LEN, fileptr))
{
/*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
*counted*/
if (!lcmt_open && (p1 = strstr(buf, lcmt)))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
{
if (p2 - 1 >= buf && *(p2 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
if (!dqm_open)
{
lcmt_open = 1;
}
if (lcmt_open && strstr(p1, rcmt))
{
lcmt_open = 0;
}
for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}

/*lcmt_open, not counted*/
else if (lcmt_open)
{
if (p1 = strstr(buf, rcmt))
{
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1) && *p1 != 0)
{
cnt++;
break;
}
}
}
}

/*C99cmt starts at the begining of a line, not counted*/
else if (p1 = strstr(buf, C99cmt))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
}

/*cnt++, deal with space lines and dqm_open state also*/
else
{
for (p1 = buf; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1))
{
cnt++;
break;
}
}
}
for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
fclose(fileptr);
return cnt;
}


#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2)
printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
else
printf("%d\n", linecnt(argv[1]));

linecnt returns a size_t, not an int. You cannot lie to printf like
this.
return 0;
}


Thank you for your time!


Remove del for email
 
L

lovecreatesbea...

It's a mistake not to count comments.  They are an important part of
the code.

Thank you.

Some companies that I ever worked with didn't count comments when they
did statistics on C code. This version is just for them :)
 
L

lovecreatesbea...

It takes mu so time to finish this C source code line count function.
What do you think about it?
/
**************************************************************************-*****
* Function : size_t linecnt(char *filenm);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#define LEN 1024
size_t linecnt(char *filenm)
{
const char lcmt[] = "/*";
const char rcmt[] = "*/";
const char C99cmt[] = "//";
const char esc = '\\';
const char dqm = '\"';
int lcmt_open = 0; /*1:last "/*" still opens; 0:not
yet.*/
int dqm_open = 0; /*1:last " still opens; 0:not yet.*/
size_t cnt = 0;
char buf[LEN] = {'\0'};
FILE *fileptr;
char *p1, *p2;
errno = 0;
if (!(fileptr = fopen(filenm, "r")) && errno)

fopen is not required to set errno. If it doesn't your extra
expression will prevent you from detecting that the file was not
opened.

The n1124.pdf doesn't mention errno on fopen. `C: A Reference Manual,
5th' mentions it in sec. 15.2. I will revise this. Thank you.
This function returns a size_t which is an unsigned type. It is
possible for this type to be wider than an int. The -1 will result in
a large positive value which would not fit in an int. You treat it in
main as an int which would invoke undefined behavior.

The code was wrong. How can I indicate an error return value with the
return type of size_t, or do I change size_t to int?
}
while (fgets(buf, LEN, fileptr))
{
/*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
*counted*/
if (!lcmt_open && (p1 = strstr(buf, lcmt)))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
{
if (p2 - 1 >= buf && *(p2 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
if (!dqm_open)
{
lcmt_open = 1;
}
if (lcmt_open && strstr(p1, rcmt))
{
lcmt_open = 0;
}
for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
/*lcmt_open, not counted*/
else if (lcmt_open)
{
if (p1 = strstr(buf, rcmt))
{
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1) && *p1 != 0)
{
cnt++;
break;
}
}
}
}
/*C99cmt starts at the begining of a line, not counted*/
else if (p1 = strstr(buf, C99cmt))
{
for (p2 = buf; p2 != p1; p2++)
{
if (!isspace(*p2))
{
cnt++;
break;
}
}
}
/*cnt++, deal with space lines and dqm_open state also*/
else
{
for (p1 = buf; p1 < buf + strlen(buf); p1++)
{
if (!isspace(*p1))
{
cnt++;
break;
}
}
}
for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
{
if (p1 - 1 >= buf && *(p1 - 1) != esc)
{
dqm_open = dqm_open ? 0 : 1;
}
}
}
fclose(fileptr);
return cnt;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2)
printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
else
printf("%d\n", linecnt(argv[1]));

linecnt returns a size_t, not an int. You cannot lie to printf like
this.

Thanks again.
 
K

Keith Thompson

user923005 said:
It's a mistake not to count comments. They are an important part of
the code.

True, but that doesn't mean it's not useful to count the non-comment
lines.
 
L

lovecreatesbea...

It takes mu so time to finish this C source code line count function.
What do you think about it?

/
***************************************************************************­****
 * Function   : size_t linecnt(char *filenm);
 * Author     : (e-mail address removed), remove foobar for email
 * Date       : 2008.4.12
 * Description: C source code line count. No comments & no space lines
counted.

***************************************************************************­***/

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#define LEN 1024
size_t linecnt(char *filenm)
{
  const char  lcmt[]    = "/*";
  const char  rcmt[]    = "*/";
  const char  C99cmt[]  = "//";
  const char  esc       = '\\';
  const char  dqm       = '\"';
  int         lcmt_open = 0;      /*1:last "/*" still opens; 0:not
yet.*/
  int         dqm_open  = 0;      /*1:last " still opens; 0:not yet.*/
  size_t      cnt       = 0;
  char        buf[LEN]  = {'\0'};
  FILE        *fileptr;
  char        *p1, *p2;

  errno = 0;
  if (!(fileptr = fopen(filenm, "r")) && errno)
  {
    printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
    return -1;
  }
  while (fgets(buf, LEN, fileptr))
  {
    /*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
     *counted*/
    if (!lcmt_open && (p1 = strstr(buf, lcmt)))
    {
      for (p2 = buf; p2 != p1; p2++)
      {
        if (!isspace(*p2))
        {
          cnt++;
          break;
        }
      }
      for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
      {
        if (p2 - 1 >= buf && *(p2 - 1) != esc)
        {
          dqm_open = dqm_open ? 0 : 1;
        }
      }
      if (!dqm_open)
      {
        lcmt_open = 1;
      }
      if (lcmt_open && strstr(p1, rcmt))
      {
        lcmt_open = 0;
      }
      for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
      {
        if (p1 - 1 >= buf && *(p1 - 1) != esc)
        {
          dqm_open = dqm_open ? 0 : 1;
        }
      }
    }

    /*lcmt_open, not counted*/
    else if (lcmt_open)
    {
      if (p1 = strstr(buf, rcmt))
      {
        lcmt_open = 0;
        for (p1 += 2; p1 < buf + strlen(buf); p1++)
        {
          if (!isspace(*p1) && *p1 != 0)
          {
            cnt++;
            break;
          }
        }
      }
    }

    /*C99cmt starts at the begining of a line, not counted*/
    else if (p1 = strstr(buf, C99cmt))
    {
      for (p2 = buf; p2 != p1; p2++)
      {
        if (!isspace(*p2))
        {
          cnt++;
          break;
        }
      }
    }

    /*cnt++, deal with space lines and dqm_open state also*/
    else
    {
      for (p1 = buf; p1 < buf + strlen(buf); p1++)
      {
        if (!isspace(*p1))
        {
          cnt++;
          break;
        }
      }
    }
    for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
    {
      if (p1 - 1 >= buf && *(p1 - 1) != esc)
      {
        dqm_open = dqm_open ? 0 : 1;
      }
    }
  }
  fclose(fileptr);
  return cnt;

}

#include <stdio.h>
int main(int argc, char *argv[])
{
  if (argc != 2)
    printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
  else
    printf("%d\n", linecnt(argv[1]));
  return 0;

}

Thank you for your time!


Updated in several places


/
*******************************************************************************
* Function : int linecnt(char *filenm);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

******************************************************************************/

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

int linecnt(char *filenm)
{
const char lcmt[] = "/*";
const char rcmt[] = "*/";
const char C99cmt[] = "//";
const char esc = '\\';
const char dqm = '\"';
int lcmt_open = 0; /*1:last "/*" still opens;
0:not yet.*/
int dqm_open = 0; /*1:last " still opens; 0:not
yet.*/
int cnt = 0;
char buf[1024] = {'\0'};
FILE *fileptr;
char *p1, *p2;

if (!(fileptr = fopen(filenm, "r"))){
printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm, "open
failed!");
return -1;
}
while (fgets(buf, sizeof buf, fileptr)){
if (!lcmt_open && (p1 = strstr(buf, lcmt))){
/*lcmt starts at the begining of a line (spaces before
lcmt omitted)
*not counted*/
if (!((p2 = strstr(buf, C99cmt)) && p2 < p1 ||
strchr(buf, dqm) && p2 > strchr(buf, dqm)))
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
if (p2 - 1 >= buf && *(p2 - 1) != esc)
dqm_open = dqm_open ? 0 : 1;
if (!dqm_open)
lcmt_open = 1;
if (lcmt_open && strstr(p1, rcmt))
lcmt_open = 0;
for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
if (p1 - 1 >= buf && *(p1 - 1) != esc)
dqm_open = dqm_open ? 0 : 1;
} else if (lcmt_open){
/*lcmt_open, not counted*/
if (p1 = strstr(buf, rcmt)){
lcmt_open = 0;
for (p1 += 2; p1 < buf + strlen(buf); p1++)
if (!isspace(*p1) && *p1 != 0){
cnt++;
break;
}
}
} else if (p1 = strstr(buf, C99cmt)){
/*C99cmt starts at the begining of a line, not counted*/
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
} else {
/*do count here, but space lines not counted*/
for (p1 = buf; p1 < buf + strlen(buf); p1++)
if (!isspace(*p1)){
cnt++;
break;
}
}

/*deal with dqm_open state, which may effect lcmt_open state*/
for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
if (p1 - 1 >= buf && *(p1 - 1) != esc)
dqm_open = dqm_open ? 0 : 1;
}
fclose(fileptr);
return cnt;
}


Thank you for your time.
 
L

lovecreatesbea...

It takes mu so time to finish this C source code line count function.
What do you think about it?
/
***************************************************************************­­****
 * Function   : size_t linecnt(char *filenm);
 * Author     : (e-mail address removed), remove foobar for email
 * Date       : 2008.4.12
 * Description: C source code line count. No comments & no space lines
counted.

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#define LEN 1024
size_t linecnt(char *filenm)
{
  const char  lcmt[]    = "/*";
  const char  rcmt[]    = "*/";
  const char  C99cmt[]  = "//";
  const char  esc       = '\\';
  const char  dqm       = '\"';
  int         lcmt_open = 0;      /*1:last "/*" still opens; 0:not
yet.*/
  int         dqm_open  = 0;      /*1:last " still opens; 0:not yet.*/
  size_t      cnt       = 0;
  char        buf[LEN]  = {'\0'};
  FILE        *fileptr;
  char        *p1, *p2;
  errno = 0;
  if (!(fileptr = fopen(filenm, "r")) && errno)
  {
    printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm,
strerror(errno));
    return -1;
  }
  while (fgets(buf, LEN, fileptr))
  {
    /*lcmt starts at the begining of a line (spaces before lcmt
omitted), not
     *counted*/
    if (!lcmt_open && (p1 = strstr(buf, lcmt)))
    {
      for (p2 = buf; p2 != p1; p2++)
      {
        if (!isspace(*p2))
        {
          cnt++;
          break;
        }
      }
      for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
      {
        if (p2 - 1 >= buf && *(p2 - 1) != esc)
        {
          dqm_open = dqm_open ? 0 : 1;
        }
      }
      if (!dqm_open)
      {
        lcmt_open = 1;
      }
      if (lcmt_open && strstr(p1, rcmt))
      {
        lcmt_open = 0;
      }
      for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
      {
        if (p1 - 1 >= buf && *(p1 - 1) != esc)
        {
          dqm_open = dqm_open ? 0 : 1;
        }
      }
    }
    /*lcmt_open, not counted*/
    else if (lcmt_open)
    {
      if (p1 = strstr(buf, rcmt))
      {
        lcmt_open = 0;
        for (p1 += 2; p1 < buf + strlen(buf); p1++)
        {
          if (!isspace(*p1) && *p1 != 0)
          {
            cnt++;
            break;
          }
        }
      }
    }
    /*C99cmt starts at the begining of a line, not counted*/
    else if (p1 = strstr(buf, C99cmt))
    {
      for (p2 = buf; p2 != p1; p2++)
      {
        if (!isspace(*p2))
        {
          cnt++;
          break;
        }
      }
    }
    /*cnt++, deal with space lines and dqm_open state also*/
    else
    {
      for (p1 = buf; p1 < buf + strlen(buf); p1++)
      {
        if (!isspace(*p1))
        {
          cnt++;
          break;
        }
      }
    }
    for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
    {
      if (p1 - 1 >= buf && *(p1 - 1) != esc)
      {
        dqm_open = dqm_open ? 0 : 1;
      }
    }
  }
  fclose(fileptr);
  return cnt;

#include <stdio.h>
int main(int argc, char *argv[])
{
  if (argc != 2)
    printf("Usage: %s <filename>, C source code line count. -jhl\n",
argv[0]);
  else
    printf("%d\n", linecnt(argv[1]));
  return 0;

Thank you for your time!

Updated in several places

/
***************************************************************************­****
 * Function   : int linecnt(char *filenm);
 * Author     : (e-mail address removed), remove foobar for email
 * Date       : 2008.4.12
 * Description: C source code line count. No comments & no space lines
counted.

***************************************************************************­***/

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

int linecnt(char *filenm)
{
    const char  lcmt[]      = "/*";
    const char  rcmt[]      = "*/";
    const char  C99cmt[]    = "//";
    const char  esc         = '\\';
    const char  dqm         = '\"';
    int         lcmt_open   = 0;        /*1:last "/*" still opens;
0:not yet.*/
    int         dqm_open    = 0;        /*1:last " still opens; 0:not
yet.*/
    int         cnt         = 0;
    char        buf[1024]   = {'\0'};
    FILE        *fileptr;
    char        *p1, *p2;

    if (!(fileptr = fopen(filenm, "r"))){
        printf("%s:%d: %s, %s\n", __FILE__, __LINE__, filenm, "open
failed!");
        return -1;
    }
    while (fgets(buf, sizeof buf, fileptr)){
        if (!lcmt_open && (p1 = strstr(buf, lcmt))){
            /*lcmt starts at the begining of a line (spaces before
lcmt omitted)
             *not counted*/
            if (!((p2 = strstr(buf, C99cmt)) && p2 < p1 ||
                    strchr(buf, dqm) && p2 > strchr(buf, dqm)))
                for (p2 = buf; p2 != p1; p2++)
                    if (!isspace(*p2)){
                        cnt++;
                        break;
                    }
            for (p2 = buf; (p2 = strchr(p2, dqm)) && p2 < p1; p2++)
                if (p2 - 1 >= buf && *(p2 - 1) != esc)
                    dqm_open = dqm_open ? 0 : 1;
            if (!dqm_open)
                lcmt_open = 1;
            if (lcmt_open && strstr(p1, rcmt))
                lcmt_open = 0;
            for (; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
                if (p1 - 1 >= buf && *(p1 - 1) != esc)
                    dqm_open = dqm_open ? 0 : 1;
        } else if (lcmt_open){
            /*lcmt_open, not counted*/
            if (p1 = strstr(buf, rcmt)){
                lcmt_open = 0;
                for (p1 += 2; p1 < buf + strlen(buf); p1++)
                    if (!isspace(*p1) && *p1 != 0){
                        cnt++;
                        break;
                    }
            }
        } else if (p1 = strstr(buf, C99cmt)){
            /*C99cmt starts at the begining of a line, not counted*/
            for (p2 = buf; p2 != p1; p2++)
                if (!isspace(*p2)){
                    cnt++;
                    break;
                }
        } else {
            /*do count here, but space lines not counted*/
            for (p1 = buf; p1 < buf + strlen(buf); p1++)
                if (!isspace(*p1)){
                    cnt++;
                    break;
                }
        }

        /*deal with dqm_open state, which may effect lcmt_open state*/
        for (p1 = buf; !lcmt_open && (p1 = strchr(p1, dqm)); p1++)
            if (p1 - 1 >= buf && *(p1 - 1) != esc)
                dqm_open = dqm_open ? 0 : 1;
    }
    fclose(fileptr);
    return cnt;

}

Thank you for your time.- Hide quoted text -

- Show quoted text -


I made another simpler version.


/
*******************************************************************************
* Function : int linecnt(char *file);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

******************************************************************************/

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

int linecnt(char *file)
{
const char lcmt[] = "/*",
rcmt[] = "*/",
C99cmt[] = "//";
int lcmt_open = 0, /*1:last lcmt still opens;
0:not yet. */
cnt = 0;
char buf[1024] = {'\0'},
*p1 = NULL,
*p2 = NULL;
FILE *fp = NULL;

if (!(fp = fopen(file, "r"))){
fprintf(stderr, "%s: File open failed: %s\n", __FILE__, file);
return -1;
}
while (fgets(buf, sizeof buf, fp)){
if (strstr(buf, lcmt) || strstr(buf, rcmt)){
if (p1 = strstr(buf, lcmt)){
lcmt_open = 1;
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
}
if (p1 = strstr(buf, rcmt))
lcmt_open = 0;
} else if (p1 = strstr(buf, C99cmt)){
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
} else if (!lcmt_open){
for (p1 = buf; p1 != buf + strlen(buf); p1++)
if (!isspace(*p1)){
cnt++;
break;
}
}
}
fclose(fp);
return cnt;
}
 
K

Keith Thompson

It takes mu so time to finish this C source code line count function.
What do you think about it?
[143 lines deleted]
Thank you for your time!

Updated in several places

/ [91 lines deleted]

Thank you for your time.- Hide quoted text -

- Show quoted text -


I made another simpler version.
[60 lines deleted]

Please trim quoted text when you post a followup. There was no need
to post the to previous versions in their entirety. Most of us who
don't use Google Groups have to wade through 251 lines of quoted text
to get to your new version.
 
L

lovecreatesbea...

Please trim quoted text when you post a followup.  There was no need
to post the to previous versions in their entirety.  Most of us who
don't use Google Groups have to wade through 251 lines of quoted text
to get to your new version.

Sorry!
 
B

Barry Schwarz

I made another simpler version.


/
*******************************************************************************
* Function : int linecnt(char *file);
* Author : (e-mail address removed), remove foobar for email
* Date : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

******************************************************************************/

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

int linecnt(char *file)
{
const char lcmt[] = "/*",
rcmt[] = "*/",
C99cmt[] = "//";
int lcmt_open = 0, /*1:last lcmt still opens;
0:not yet. */
cnt = 0;
char buf[1024] = {'\0'},
*p1 = NULL,
*p2 = NULL;
FILE *fp = NULL;

if (!(fp = fopen(file, "r"))){
fprintf(stderr, "%s: File open failed: %s\n", __FILE__, file);
return -1;
}
while (fgets(buf, sizeof buf, fp)){
if (strstr(buf, lcmt) || strstr(buf, rcmt)){
if (p1 = strstr(buf, lcmt)){

The code does not properly handle the statement
char *start_of_comment = "/*";
lcmt_open = 1;
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}
}
if (p1 = strstr(buf, rcmt))
lcmt_open = 0;
} else if (p1 = strstr(buf, C99cmt)){
for (p2 = buf; p2 != p1; p2++)
if (!isspace(*p2)){
cnt++;
break;
}

The code will not properly handle the sequence of lines
/* start of comment
continuation of comment // still continued
end of comment */
} else if (!lcmt_open){
for (p1 = buf; p1 != buf + strlen(buf); p1++)
if (!isspace(*p1)){
cnt++;
break;
}
}
}
fclose(fp);
return cnt;
}


Remove del for email
 
L

lovecreatesbea...

I made another simpler version.
/
**************************************************************************­*****
* Function   : int linecnt(char *file);
* Author     : (e-mail address removed), remove foobar for email
* Date       : 2008.4.12
* Description: C source code line count. No comments & no space lines
counted.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int linecnt(char *file)
{
   const char  lcmt[]      = "/*",
               rcmt[]      = "*/",
               C99cmt[]    = "//";
   int         lcmt_open   = 0,        /*1:last lcmt still opens;
0:not yet. */
               cnt         = 0;
   char        buf[1024]   = {'\0'},
               *p1         = NULL,
               *p2         = NULL;
   FILE        *fp         = NULL;
   if (!(fp = fopen(file, "r"))){
       fprintf(stderr, "%s: File open failed: %s\n", __FILE__, file);
       return -1;
   }
   while (fgets(buf, sizeof buf, fp)){
       if (strstr(buf, lcmt) || strstr(buf, rcmt)){
           if (p1 = strstr(buf, lcmt)){

The code does not properly handle the statement
        char *start_of_comment = "/*";
               lcmt_open = 1;
               for (p2 = buf; p2 != p1; p2++)
                   if (!isspace(*p2)){
                       cnt++;
                       break;
                   }
           }
           if (p1 = strstr(buf, rcmt))
               lcmt_open = 0;
       } else if (p1 = strstr(buf, C99cmt)){
           for (p2 = buf; p2 != p1; p2++)
               if (!isspace(*p2)){
                   cnt++;
                   break;
               }

The code will not properly handle the sequence of lines
        /* start of comment
           continuation of comment // still continued
        end of comment */
       } else if (!lcmt_open){
           for (p1 = buf; p1 != buf + strlen(buf); p1++)
               if (!isspace(*p1)){
                   cnt++;
                   break;
               }
       }
   }
   fclose(fp);
   return cnt;
}

It doesn't now. I will update this line count function and it should
count lines like this:

/**/ /**/ i++; /**/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top