C source code line count function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lovecreatesbea...@gmail.com

    C source code line count function

    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 : jhlicfoocbar@gm ail.com, 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!
  • lovecreatesbea...@gmail.com

    #2
    Re: C source code line count function

    On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
    <lovecreatesbea ...@gmail.comwr ote:
    It takes mu so time to finish this C source code line count function.
    Sorry for the error spell, I meant "It takes me some time".

    Comment

    • user923005

      #3
      Re: C source code line count function

      On Apr 11, 12:38 pm, "lovecreatesbea ...@gmail.com"
      <lovecreatesbea ...@gmail.comwr ote:
      On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
      >
      <lovecreatesbea ...@gmail.comwr ote:
      It takes mu so time to finish this C source code line count function.
      >
      Sorry for the error spell, I meant "It takes me some time".
      It's a mistake not to count comments. They are an important part of
      the code.

      Comment

      • user923005

        #4
        Re: C source code line count function

        On Apr 11, 12:49 pm, user923005 <dcor...@connx. comwrote:
        On Apr 11, 12:38 pm, "lovecreatesbea ...@gmail.com"
        >
        <lovecreatesbea ...@gmail.comwr ote:
        On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
        >
        <lovecreatesbea ...@gmail.comwr ote:
        It takes mu so time to finish this C source code line count function.
        >
        Sorry for the error spell, I meant "It takes me some time".
        >
        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[i], "r")) == NULL) {
        fprintf(stderr, "wc: can't open %s\n", argv[i]);
        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[i]);
        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;
        }

        Comment

        • Barry Schwarz

          #5
          Re: C source code line count function

          On Fri, 11 Apr 2008 12:35:43 -0700 (PDT),
          "lovecreatesbea ...@gmail.com" <lovecreatesbea uty@gmail.comwr ote:
          >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 : jhlicfoocbar@gm ail.com, 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

          Comment

          • lovecreatesbea...@gmail.com

            #6
            Re: C source code line count function

            On Apr 12, 3:49 am, user923005 <dcor...@connx. comwrote:
            On Apr 11, 12:38 pm, "lovecreatesbea ...@gmail.com"
            >
            <lovecreatesbea ...@gmail.comwr ote:
            On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
            >
            <lovecreatesbea ...@gmail.comwr ote:
            It takes mu so time to finish this C source code line count function.
            >
            Sorry for the error spell, I meant "It takes me some time".
            >
            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 :)

            Comment

            • Gordon Burditt

              #7
              Re: C source code line count function

              It takes mu so time to finish this C source code line count function.
              >>
              >Sorry for the error spell, I meant "It takes me some time".
              >
              >It's a mistake not to count comments. They are an important part of
              >the code.
              It's a mistake to let managers get hold of the output of your
              line-count program. Consider what happens when managers start
              paying by the line, and then programmers write to maximize their
              pay, or worse, write the usual way, then run the program through
              "line enhancer" programs .

              # \
              include\
              <stdio.h>

              int
              main
              (
              argc
              ,
              argv
              [
              ]
              )
              {
              printf
              (
              "H"
              "e"
              "l"
              "l"
              "o"
              ","
              " "
              "w"
              "o"
              "r"
              "l"
              "d"
              "\n"
              )
              ;
              return
              0
              ;
              }

              I'm sure it's possible to make it a lot ugler than I have shown here.

              Comment

              • lovecreatesbea...@gmail.com

                #8
                Re: C source code line count function

                On Apr 12, 12:47 pm, Barry Schwarz <schwa...@dqel. comwrote:
                On Fri, 11 Apr 2008 12:35:43 -0700 (PDT),
                >
                "lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ote:
                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 : jhlicfooc...@gm ail.com, 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.
                {
                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.
                >
                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.
                return 0;
                }
                >

                Comment

                • Keith Thompson

                  #9
                  Re: C source code line count function

                  user923005 <dcorbit@connx. comwrites:
                  On Apr 11, 12:38 pm, "lovecreatesbea ...@gmail.com"
                  <lovecreatesbea ...@gmail.comwr ote:
                  >On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
                  >>
                  ><lovecreatesbe a...@gmail.comw rote:
                  It takes mu so time to finish this C source code line count function.
                  >>
                  >Sorry for the error spell, I meant "It takes me some time".
                  >
                  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.

                  --
                  Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Ian Collins

                    #10
                    Re: C source code line count function

                    Richard Heathfield wrote:
                    Keith Thompson said:
                    >
                    >user923005 <dcorbit@connx. comwrites:
                    >>On Apr 11, 12:38 pm, "lovecreatesbea ...@gmail.com"
                    >><lovecreatesb ea...@gmail.com wrote:
                    >>>On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
                    >>>>
                    >>><lovecreates bea...@gmail.co mwrote:
                    >>>>It takes mu so time to finish this C source code line count function.
                    >>>Sorry for the error spell, I meant "It takes me some time".
                    >>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.
                    >
                    A wise man once said:
                    >
                    An even wiser man asked, why bother counting lines?

                    --
                    Ian Collins.

                    Comment

                    • lovecreatesbea...@gmail.com

                      #11
                      Re: C source code line count function

                      On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
                      <lovecreatesbea ...@gmail.comwr ote:
                      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     : jhlicfooc...@gm ail.com, 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 : jhlicfoocbar@gm ail.com, 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.

                      Comment

                      • Morris Dovey

                        #12
                        Re: C source code line count function

                        user923005 wrote:
                        It's a mistake not to count comments. They are an important part of
                        the code.
                        Agreed. The best code counter I've written reports total lines, C
                        statements, and comments separately.

                        --
                        Morris Dovey
                        DeSoto Solar
                        DeSoto, Iowa USA

                        Comment

                        • lovecreatesbea...@gmail.com

                          #13
                          Re: C source code line count function

                          On Apr 12, 5:00 pm, "lovecreatesbea ...@gmail.com"
                          <lovecreatesbea ...@gmail.comwr ote:
                          On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
                          >
                          >
                          >
                          >
                          >
                          <lovecreatesbea ...@gmail.comwr ote:
                          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     : jhlicfooc...@gm ail.com, 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     : jhlicfooc...@gm ail.com, 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 : jhlicfoocbar@gm ail.com, 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;
                          }

                          Comment

                          • Keith Thompson

                            #14
                            Re: C source code line count function

                            "lovecreatesbea ...@gmail.com" <lovecreatesbea uty@gmail.comwr ites:
                            On Apr 12, 5:00 pm, "lovecreatesbea ...@gmail.com"
                            <lovecreatesbea ...@gmail.comwr ote:
                            >On Apr 12, 3:35 am, "lovecreatesbea ...@gmail.com"
                            >>
                            ><lovecreatesbe a...@gmail.comw rote:
                            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.

                            --
                            Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • lovecreatesbea...@gmail.com

                              #15
                              Re: C source code line count function

                              On Apr 13, 11:40 pm, Keith Thompson <ks...@mib.orgw rote:
                              "lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ites:
                              I made another simpler version.
                              >
                              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!

                              Comment

                              Working...