[ create a new paste ] login | about

Link: http://codepad.org/UuSFtHdy    [ raw code | output | fork ]

C, pasted on Nov 24:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//#include "xmalloc.h"
#define xmalloc malloc
#define xfree free
#define xrealloc realloc

#define BUFFSIZE 3 /* >= 2 */
char *getline(FILE *fp)
{
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p;
  int fEOL, fEOF;
  if ((outbuff_malloc = xmalloc(1)) == NULL) {
    return NULL;
  }
  *outbuff_malloc = '\0';
  fEOF = fEOL = 0;
  do {
    if (fgets(inbuff, BUFFSIZE, fp) == NULL) {
      fEOF = 1;
      break;
    }
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n') {
      *(p - 1) = '\0';
      fEOL = 1;
    }
    if ((tmpbuff = xrealloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) == NULL) {
      xfree(outbuff_malloc);
      return NULL;
    }
    strcat(tmpbuff, inbuff);
    outbuff_malloc = tmpbuff;
  } while (!fEOL);
  if (fEOF > 0) {
    xfree(outbuff_malloc);
    return NULL;
  }
  return outbuff_malloc;
}

void task_step(FILE *fp) {
  char *text, *p;
  int totalln, commentln, executeln;
  int state_commentin, state_stringin, flag_commentexist, flag_bracesexist;
  int flag_nobracesexist, flag_alphabetexist, flag_commentonly;
  totalln = commentln = executeln = 0;
  state_commentin = 0;
  while ((text = getline(fp)) != NULL) {
    flag_commentexist = flag_bracesexist = flag_alphabetexist = flag_nobracesexist = 0;
    flag_commentonly = 1;
    /*---*/
    if (state_commentin > 0)
      flag_commentexist = 1;
    /*---*/
    state_stringin = 0;
    for (p = text; *p != '\0'; p++) {
      if (*p == '\"' && state_stringin > 0)
        state_stringin = 0;
      if (*p == '\"' && state_stringin == 0)
        state_stringin = 1;
      if (state_stringin > 0)
        continue;
      if (*p == ' ' || *p == '\t')
        continue;
      if (*p == '/') {
        if (*(p + 1) == '/') {
          flag_commentexist = 1;
          flag_nobracesexist = 1;
          break;
        } else if (*(p + 1) == '*') {
          flag_commentexist = 1;
          flag_nobracesexist = 1;
          state_commentin = 1;
          continue;
        }
      } else if (*p == '*') {
        if (*(p + 1) == '/') {
          flag_commentexist = 1;
          flag_nobracesexist = 1;
          state_commentin = 0;
          continue;
        }
      }
      if (state_commentin == 0) {
        if (*p == '{' || *p == '}' ) {
          flag_bracesexist = 1;
        }
        if (isalpha(*p & 0xff)) {
          flag_alphabetexist = 1;
          flag_nobracesexist = 1;
          flag_commentonly = 0;
        }
      }
    }
    /*---*/
    printf("%s: ", text);
    if (flag_commentexist > 0)
      commentln++;
    if ((flag_bracesexist == 1 && flag_nobracesexist == 0) || flag_commentonly == 1) {
    } else {
      executeln++;
      printf(" execute++ ");
    }
    totalln++;
    printf("comment=%d, alphabet=%d, brace=%d, nobrace=%d\n",
           flag_commentexist,
           flag_alphabetexist,
           flag_bracesexist,
           flag_nobracesexist);
    xfree(text);
  }
  printf("line: %d, executive: %d, comment: %d, comment-percentage %d %%\n",
         totalln,
         executeln,
         commentln,
         (int)((double)commentln / (double)totalln * 100.0));
  return;
}

int main(int argc, char *argv[]) {
  FILE *fp;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <c-file>\n", argv[0]);
    exit(-1);
  }
  if ((fp = fopen(argv[1], "rt")) == NULL) {
    fprintf(stderr, "%s: cannot open the file %s.\n", argv[0], argv[1]);
    exit(-1);
  }
  task_step(fp);
  fclose(fp);
//  xmallocdump();
  return 0;
}
/* end */


Output:
1
2
3
usage: /t <c-file>

Exited: ExitFailure 255


Create a new paste based on this one


Comments: