/************************* 
* Owen Sullivan
* CPSC 2311 F22 Section 002
* opsulli@clemson.edu
*************************/ 

// Include stdio.h for FILE pointers, feof(), fgetc(), and fputc()
#include <stdio.h>

// Include Lab3Fgetc.h for function prototypes
#include "Lab3Fgetc.h"

// Function which gets the next character from the input file, checks
// if it is a slash (used at start of both types of comment), and calls
// rcomment() if so or fputc() passing current read character if not.
void start(FILE* in) {

    char current;

    // Check that end of file hasn't been reached
    while (!feof(in)) {

        // Get the next character from input file
        current = fgetc(in);

        // Check if current character is a slash
        if (current == '/') {

            // Pass file to rcomment()
            rcomment(in);

        } else {

            // Check that current character is not
            // EOF, and print to stdout if not
            if (current != EOF && !feof(in)) {
                
                fputc(current, stdout);
            
            }

        }

    }

}

// Function which determines whether a comment has been found
// by checking if the next read character is a "/" (single-line comment)
// or a "*" (multi-line comment).
void rcomment(FILE* in) {

    char current;

    // Check that end of file hasn't been reached
    while (!feof(in)) {

        // Get the next character from input file
        current = fgetc(in);

        // Check if current character is a slash
        if (current == '/') {

            // Get the next character from input file
            current = fgetc(in);

            // Check if current character is an asterisk
            if (current == '*') {

                // Pass input file to skipM_comment()
                skipM_comment(in);

            } else {

                // Move the value of SEEK_CUR back one
                // character
                fseek(in, -1, SEEK_CUR);

            }
            
            // Move the value of SEEK_CUR back two
            // characters, so that skipS_comment()
            // is starting at the first slash
            fseek(in, -2, SEEK_CUR);
            
            // Pass input file to skipS_comment()
            skipS_comment(in);

          // Check if current character is an asterisk
        } else if (current == '*') {

            // Move the value of SEEK_CUR back two
            // characters, so that skipM_comment()
            // is starting at the first slash
            fseek(in, -2, SEEK_CUR);

            // Pass input file to skipM_comment()
            skipM_comment(in);

          // If current character is neither slash
          // nor asterisk
        } else {

            // Check that current character is not
            // EOF, and print to stdout if not
            if (current != EOF && !feof(in)) {
                
                fputc(current, stdout);
            
            }

        }

    }

}

// Function which reads characters from input file
// (and does not print them) until the end of a
// multi-line comment is found.
void skipM_comment(FILE* in) {

    char current = fgetc(in);
    char next;

    // Check that end of file hasn't been reached
    while (!feof(in)) {

        // Check if current character is an asterisk
        if (current == '*') {

            // Get the next character from input file
            // (again, for comparisons)
            next = fgetc(in);

            // Check if current character is a slash
            if (next == '/') {

                // End the loop (comment has ended)
                break;

              // If not, move SEEK_CUR back one
              // so that the correct number of
              // characters are skipped
            } else {

                
                fseek(in, -1, SEEK_CUR);

            }

        }

        // Get the next character from input file
        current = fgetc(in);

    }

}

// Function which reads characters from input file
// (and does not print them) until the end of a
// single-line comment is found.
void skipS_comment(FILE* in) {

    // Get the next character from input file
    char current = fgetc(in);

    // Check that end of file has not been reached
    // and that the current character is not a new
    // line (signifies end of comment)
    while (!feof(in) && current != '\n') {

        // Get the next character from input file
        current = fgetc(in);

        // Check that current character is not a
        // new line (accidentally removing a line)
        if (current == '\n') {

            // If so, move back one character
            // in input file for next read
            fseek(in, -1, SEEK_CUR);

        }

    }

}