//					COMMENTS.CPP

#include <windows.h>

#include "textout.h"
#include "comments.h"

comments::comments(void)
{ clist=new struct commentlist;
  // comment list head and tail
  // simplifies operations.
  clist->next=new struct commentlist;
  clist->next->next=NULL;
  clist->addr=0;
  clist->type=COMMENT_HEADMARKER;
  clist->data=NULL;
  clist->next->addr=0xffffffff;
  clist->next->type=COMMENT_TAILMARKER;
  clist->next->data=NULL;
  lastcomment=clist;
  cdepth=0;
}

comments::~comments(void)
{ struct commentlist *prev;
  while(clist!=NULL)
  { prev=clist;
	 clist=clist->next;
	 delete prev;
  }
}

void comments::addcomment(unsigned long addr,unsigned char comment,BOOL first)
{ struct commentlist *addit,*prev;
  addit=clist;
  prev=NULL;
  while((addr>addit->addr)||((addr==addit->addr)&&(!first)))
  { prev=addit;
	 addit=addit->next;
  }
  prev->next=new struct commentlist;
  prev->next->next=addit;
  addit=prev->next;
  addit->addr=addr;
  addit->type=comment;
  lastcomment=addit;
}

void comments::addoffscomment(unsigned long addr,unsigned char comment,unsigned char *data)
{ addcomment(addr,comment,TRUE);
  lastcomment->data=data;
}


void comments::resetiterator(void)
{ lastcomment=clist;
}

void comments::printcomment(unsigned long address)
{ struct commentlist *ilist;
  int i;
  BOOL printed;
  printed=FALSE;
  ilist=lastcomment;
  while(TRUE)
  { if(ilist->addr>address)return;
	 if(address==ilist->addr)
	 { switch(ilist->type)
		{ case COMMENT_IF:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 i=cdepth;
			 if(i>10)i=10;
			 while(i--)LastPrintBuff("  ");
			 LastPrintBuff("IF{");
			 cdepth++;
			 printed=TRUE;
			 break;
		  case COMMENT_ENDIF:
			 if(printed)PrintBuff("");
			 cdepth--;
			 if(cdepth<0)cdepth=0;
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 i=cdepth;
			 if(i>10)i=10;
			 while(i--)LastPrintBuff("  ");
			 LastPrintBuff("}ENDIF");
			 printed=TRUE;
			 break;
		  case COMMENT_ELSE:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 i=cdepth;
			 if(i>10)i=10;
			 while(i--)LastPrintBuff("  ");
			 LastPrintBuff("}ELSE{");
			 printed=TRUE;
			 break;
		  case COMMENT_DO:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 i=cdepth;
			 if(i>10)i=10;
			 while(i--)LastPrintBuff("  ");
			 LastPrintBuff("DO{");
			 cdepth++;
			 printed=TRUE;
			 break;
		  case COMMENT_DOWHILE:
			 if(printed)PrintBuff("");
			 cdepth--;
			 if(cdepth<0)cdepth=0;
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 i=cdepth;
			 if(i>10)i=10;
			 while(i--)LastPrintBuff("  ");
			 LastPrintBuff("}WHILE");
			 printed=TRUE;
			 break;
		  case COMMENT_PROC:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 LastPrintBuff("PROC{");
			 printed=TRUE;
			 break;
		  case COMMENT_ENDPROC:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 LastPrintBuff("}ENDPROC");
			 printed=TRUE;
			 break;
		  case COMMENT_RET:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";");
			 i=cdepth;
			 if(i>10)i=10;
			 while(i--)LastPrintBuff("  ");
			 LastPrintBuff("}RET");
			 printed=TRUE;
			 break;
		  case COMMENT_DS:
			 if(printed)PrintBuff("");
			 LastPrintBuffEpos(60);
			 LastPrintBuff(";DS \"%s\"",ilist->data);
			 printed=TRUE;
			 break;
		  default:
			 break;
		}
	 }
	 ilist=ilist->next;
	 lastcomment=ilist;
  }
}
