#include <stdio.h>
#include <ctype.h>
#include "garray.h"


int main(int argc, char ** argv) {
  char oldc, c;
  int quotes = 0;
  int squote = 0;
  int escape = 0;
  int comment = 0;
  int newComment = 0;
  int preprocessor = 0;
  garray whitespace;
  
  
  if (argc < 1) {
    printf("Usage:  whiteSpaceBeGone\n");
    exit(1);
  }
  
  c = getc(stdin);
  while (!feof(stdin)) {
    if (!escape || comment || newComment) {
      if (c == '\\') escape = 1;
      else if (c == '\"') quotes = !quotes;
      else if (!quotes) 
        if (c == '\'') squote = !squote;
        else if (!squote)
          if (c == '#') preprocessor = 1;
          else if (c == '/') {
            oldc = c;
            c = getc(stdin);
            if (c == '*') comment = 1;
            else if (c == '/') newComment = 1;
            else { 
              putc(oldc, stdout);
              putc(c, stdout);
            } 
          } else if (c == '*' && comment) {
            c = getc(stdin);
            if (c == '/') {
              quotes = 0; squote = 0; preprocessor = 0; escape = 0;
              comment = 0; 
              c = getc(stdin);
            }
          }
    } else escape = 0;
     
    if (!comment && !newComment)
      if (isspace(c) && !(quotes || squote || preprocessor)) whitespace.Put(c);
      else {
        if (whitespace[0]) { whitespace.clear(); putc(' ', stdout); }
        putc(c, stdout);
      }
    if (c == '\n') {
      preprocessor = 0;
      if (newComment) { 
        quotes = 0; squote = 0; preprocessor = 0; escape = 0;
        newComment = 0;
      }
    }
    c = getc(stdin);
  }

  putc('\n', stdout);
  return 0;
}
