#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
static char *Version = "1.00 (7 April 1993)";
static char *Author = "Julyan Bristow";
static menu iconbar_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;
  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);
  p = return_string + strlen(return_string) - 1; 
  while (*p != '.') p--;
  *++p = '\0';
  strcat(return_string,"Newfile");
  return (return_string);
}
BOOL convert(void)
{
char output_name[MAX_LEN];
  input_file = file_open(pathname,"r",0);
  strcpy(output_name,new_name(pathname));
  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");
    
    }
  }
  if (miss_lines) fprintf(output_file,"\n"); 
  fclose(output_file);
  fclose(input_file);
  return TRUE;
}
static void stripper_iconclick(wimp_i icon)
{
 
}
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++; 
    else miss_lines = 0;           
    menu_setflags(iconbar_menu,icon_stripper_o,miss_lines,0);
    break;
  case icon_stripper_q:
   exit(0);
   break;
  }
}
void iconbar_handler(wimp_eventstr *e, void *handle)
{
int filetype;
  e = e;            
  handle = handle;
  if ((filetype = xferrecv_checkinsert(&pathname)) == tft) {
    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;
  return TRUE;
}
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);
  return TRUE;
}
int main()
{
  if (std_initialise())
  {
    while (TRUE)
      event_process();
  }
  return 0;
}

