/* Stripper - a program to remove the comments from C source code */
/* Author   - Julyan Bristow                                      */
/* Date     - March - April 1993                                  */
/* Version  - 1.00                                                */

#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "event.h"
#include "baricon.h"
#include "res.h"
#include "resspr.h"
#include "menu.h"
#include "template.h"
#include "dbox.h"
#include "werr.h"
#include "saveas.h"
#include "xfersend.h"
#include "xferrecv.h"

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"

#define icon_stripper_i   1
#define icon_stripper_o   2
#define icon_stripper_q   3
#define options_m_miss    0
#define Info_version      3
#define Author_field      4
#define tft 0xfff

#define MAX_LEN  256

/******************************** GLOBAL DATA *******************************/
static char *Version = "1.00 (7 April 1993)";
static char *Author = "Julyan Bristow";

static menu iconbar_menu;
/*static menu options_menu;*/

char *pathname;
FILE *input_file,*output_file;
int miss_lines = 0;
              
char name[80],out_name[80],choice[2];
BOOL process_file(char *outname,void *handle);

FILE *file_open(char *open_name,char *mode,int err_level)
{
  FILE *temp;
/*  werr(err_level,"In file_open trying to open %s",open_name); */
  if ((temp=fopen(open_name,mode)) == NULL) {
    werr(err_level,"Error - cannot open file %s. Is it already open?",open_name);
    if (err_level) exit(0);
    else return NULL;        
    }
  return temp;    
}

void file_close(FILE *close_file,char *close_name)
{
  fclose(close_file);
}

char *new_name(char *string)
{
char *p,return_string[MAX_LEN];
  strcpy(return_string,string);
/*  werr(0,"string to manipulate = %s",return_string); */
  p = return_string + strlen(return_string) - 1; /* point to end of the string */
  while (*p != '.') p--;
  *++p = '\0';
/*  werr(0,"string after 1st bit = %s",return_string); */
  strcat(return_string,"Newfile");
/*  werr(0,"finally it is %s",return_string); */
  return (return_string);
}

BOOL convert(void)
{
char output_name[MAX_LEN];
/*  werr(0,"Going to open file %s",pathname); */
  input_file = file_open(pathname,"r",0);
  strcpy(output_name,new_name(pathname));
/*  werr(0,"string = %s",output_name); */
  saveas(tft,output_name,0,process_file,0,0,0);
  return TRUE;
}

BOOL process_file(char *outname,void *handle)
{
char input[MAX_LEN],output[MAX_LEN];
char *p1,*p2;
int comment = 0;

if ((output_file = file_open(outname,"w",0)) == NULL) {
  fclose(input_file);
  return TRUE;
  }

  while(!feof(input_file)) {
  strcpy(input,"\0");
  strcpy(output,"\0");
  fgets(input,MAX_LEN - 1,input_file);
  p1 = input; p2 = output;

  while (*p1) {
    if (*p1 == '/') {
      p1++; 
      if (*p1 == '*') {comment = 1; p1++;} 
      else p1--;
    }
    if (comment) {
      if (*p1 == '*') {
      p1++; 
      if (*p1 == '/') {comment = 0; p1++;} 
      else p1--;
      }
    }
    if (!comment) *p2++ = *p1++;
    else p1++;
  }
  *p2 = '\0';
  if (!comment) {
    if (strlen(output) > 1) fprintf(output_file,"%s",output);
    if (strlen(input) == 1 && !miss_lines) fprintf(output_file,"\n");
    /*tolower(miss_lines[0]) == 'n'*/
    }
  }
  if (miss_lines) fprintf(output_file,"\n"); /* allow last line to be blank */
  fclose(output_file);
  fclose(input_file);
  return TRUE;
}


/****************************** EVENT HANDLERS ******************************/

static void stripper_iconclick(wimp_i icon)
{
 /* do nothing */
}

static void stripper_info(void)
{
  dbox  d_info;
  if (d_info = dbox_new("ProgInfo"), d_info != NULL)
  {
    dbox_setfield(d_info, Info_version, Version);
    dbox_setfield(d_info, Author_field,Author);
    dbox_show(d_info);
    dbox_fillin(d_info);
    dbox_dispose(&d_info);
  }
}

static void iconbar_menuproc(void *handle, char *hit)
{
  handle = handle; 
  switch (hit[0])
  {
  case icon_stripper_i:
    stripper_info();
    break;

 case icon_stripper_o:
    if (!miss_lines) miss_lines++; /* yes - remove the lines */
    else miss_lines = 0;           /* else don't */
    menu_setflags(iconbar_menu,icon_stripper_o,miss_lines,0);
/*    werr(0,"value of miss_lines is %d",miss_lines); */
/*    options_submenuproc(hit); */
    break;

  case icon_stripper_q:
   exit(0);
   break;
  }
}

void iconbar_handler(wimp_eventstr *e, void *handle)
{
int filetype;
  e = e;            /* prevent warnings */
  handle = handle;
  if ((filetype = xferrecv_checkinsert(&pathname)) == tft) {
/*    werr(0,"Input filename is %s",pathname); */
    if (convert())
      xferrecv_insertfileok();
    }
  else werr(0,"Only text files can be processed by this application.");
}

static BOOL make_menus()
{
  if (iconbar_menu = menu_new("Stripper",">Info,No Blank Lines,Quit"),iconbar_menu == NULL)
    return FALSE;
/*  if (options_menu = menu_new("Choices","Blank"),options_menu == NULL)
    return FALSE; */
/*  menu_submenu(iconbar_menu,2,options_menu); */

  return TRUE;
}
/****************************** INITIALISATION ******************************/
static BOOL std_initialise(void)
{
  wimpt_init("Stripper");
  res_init("Stripper");
  resspr_init();
  template_init();
  dbox_init();

  if (!make_menus()) return FALSE;

  baricon("!stripper", (int)resspr_area(), stripper_iconclick);
  if (!event_attachmenu(win_ICONBAR,iconbar_menu,iconbar_menuproc,0))
    return FALSE;

  win_register_event_handler(win_ICONBARLOAD, iconbar_handler, 0);

/*  options = dbox_new("Options");
  dbox_raw_eventhandler(options,options_event_handler,0); */

  return TRUE;
}

/******************************* MAIN PROGRAM ********************************/

/*--- Main entry point. ---*/
int main()
{
  if (std_initialise())
  {
    while (TRUE)
      event_process();
  }

  return 0;
}
