// linelength.c
//
// a smal program wich counts the number of characters per line
// needs a text via stdin
//
// author: Altinger Harald 0630xxx
// group: group 5,6,20, study assistant AltiHar
// date: 2007-10-15
// version: 0.1
// last change: none

#include <stdio.h>

//forward declarations
int checkLinesPerFunktion(int count_lines_per_funktion, int line_of_actuall_funktion);

//--------------------------------------------------------------------
// main funtion controlles the program
int main()
{
  int count_chars = 0;
  int count_lines = 0;
  int count_funktions = 0;
  int line_of_actuall_funktion = 0;
  
  int count_lines_per_funktion = 0;
  int count_chars_per_line = 0;

  int count_positon = 0;  
  char *function_termination = "//--------------------------------------------------------------------";
  char input_char;

  while ((input_char = getchar()) && input_char != EOF)
  {
    count_chars = count_chars + 1;
    count_chars_per_line = count_chars_per_line + 1;

    //compare the next 70 characters if they symbole a new function
    //based on codingstandard spec.
    if (input_char == *(function_termination + count_positon++))
    {
      if(count_positon == 70)  
      {      
        checkLinesPerFunktion(count_lines_per_funktion, 
          line_of_actuall_funktion);
          
        //+1 because \n will be found after end of funktion terminator
        line_of_actuall_funktion = count_lines;
        
        count_funktions = count_funktions + 1;
        count_lines_per_funktion = 0;
        count_positon = 0;
      }
    }
    else
      count_positon = 0; //reset because one sign is missmatching
    
    //every line will be terminated by a "new line" symbol
    if(input_char == '\n')
    {
      count_lines = count_lines + 1;
      count_lines_per_funktion = count_lines_per_funktion + 1;
      
      //based on codingstandard, 72 characters are allowed
      if(count_chars_per_line > 73)
        printf("Line: %d has got %d characters\n",
          count_lines,count_chars_per_line);
      
      //reset charcount per line for every new line
      count_chars_per_line = 0; 
    }
  }
  
  //the last funktion will end at eof!
  checkLinesPerFunktion(count_lines_per_funktion, 
    line_of_actuall_funktion);
  
  printf("Program has got %d characters and %d lines and %d functions at all\n",count_chars,count_lines,count_funktions);  
  return 0;
}
//--------------------------------------------------------------------
// funkction checks how many lines per funktion are written and
// will output an error if they are more than codingstandrad spec.  
int checkLinesPerFunktion(int count_lines_per_funktion, int line_of_actuall_funktion)
{
  //based on codingstandard 60 lines is maximum
  if (count_lines_per_funktion > 60)
    printf("Funktion at line %d has got %d lines\n",line_of_actuall_funktion,
     count_lines_per_funktion);  
     
  return 0;
}


