//BankParser v2.0
//Part of the GuildBank package
//Parses the data saved in SavedVariables.lua by BankScan WoW AddOn
//into HTML file.
//
//Author: Roman Tarakanov (RTE/Arthas)
//Date: Sep 13 '05

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv){
  int i, j, k, desc_len=0;
  FILE *in, *out;
  char buf[1000], num[100], sname[100], name[100], desc[1000], pic[100], tmp[10];
  int quality, money;
  char head[30000], tail[10000], link[10000];

  char VER[7]="2.0";
  char TITLE[21]="My bank";
  long WIDTH=15;
  char SEARCHSITE[101]="http://www.thottbot.com/?s=";
  char IMAGESFOLDER[101]="images/";
  char STDIMAGESFOLDER[101]="images/";
  char IMAGEEXTENTION[11]=".jpg";
  char LUAFILE[101]="../SV/GuildTools.lua";
  char OUTPUT[101]="../html/index.html";

  head[0]= '\0';
  tail[0]= '\0';
  link[0]= '\0';

  for (i=1; i<argc; i++)
  {
    if (strcmp(argv[i],"-t")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>20) argv[i+1][20]=0;
      strncpy(TITLE, argv[i+1], strlen(argv[i+1]));
      TITLE[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-w")==0 && i<argc-1)
    {
      WIDTH=strtol(argv[i+1], NULL, 10);
      if (WIDTH<6) WIDTH=6;
      if (WIDTH>22) WIDTH=22;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-s")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>100) argv[i+1][100]=0;
      strncpy(SEARCHSITE, argv[i+1], strlen(argv[i+1]));
      SEARCHSITE[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-sif")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>100) argv[i+1][100]=0;
      strncpy(STDIMAGESFOLDER, argv[i+1], strlen(argv[i+1]));
      STDIMAGESFOLDER[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-if")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>100) argv[i+1][100]=0;
      strncpy(IMAGESFOLDER, argv[i+1], strlen(argv[i+1]));
      IMAGESFOLDER[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-ie")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>10) argv[i+1][10]=0;
      strncpy(IMAGEEXTENTION, argv[i+1], strlen(argv[i+1]));
      IMAGEEXTENTION[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-i")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>100) argv[i+1][100]=0;
      strncpy(LUAFILE, argv[i+1], strlen(argv[i+1]));
      LUAFILE[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-o")==0 && i<argc-1)
    {
      if (strlen(argv[i+1])>100) argv[i+1][100]=0;
      strncpy(OUTPUT, argv[i+1], strlen(argv[i+1]));
      OUTPUT[strlen(argv[i+1])]=0;
      i++;
      continue;
    }
    if (strcmp(argv[i],"-?")==0)
    {
      printf("\nBankParser v%s \nParses the data saved in SavedVariables.lua by BankScan into html format.\n\n", VER);
      printf("Any of the flags can be used in any order.\n");
      printf("If flag is used more than once in one statement last inputed value will be used\n");
      printf("'\"' character cannot be used in strings.\n");
      printf("If your string has spaces in it put it in \" \" Ex: '-t \"Ma bank\"'.\n");
      printf("\"\" sharacters are not necessary around a string statement if your string does\n");
      printf(" not contain spaces.\n\n");
      printf("Advanced mode flags:\n\n");
      printf("-t <string>    - Title. Set the title of the page and Bag name to the string\n");
      printf("                  passed.\n");
      printf("                 Ex: '-t \"Guild Bank\"'.\n");
      printf("                 Default: '%s'.\n",TITLE);
      printf("                 Max string length is 20 chars.\n");
      printf("-w <number>    - Width. Set the number of items in the backpack in one row.\n");
      printf("                 Ex: '-w 12'.\n");
      printf("                 Default: %d.\n",WIDTH);
      printf("                 Available range: 6-22.\n");
      printf("-s <string>    - Searchsite. Sets the site that will be linked upon the click\n");
      printf("                  on the item. Resulting link will be: <string><name of the\n");
      printf("                  item '+' separated>.\n");
      printf("                 Ex: '-s http://www.mysite.com/?s='.\n");
      printf("                 Default: '%s'.\n",SEARCHSITE);
      printf("                 Max string length is 100 chars.\n");
      printf("-if <string>   - Images foulder. Sets the folder where the images will be\n");
      printf("                  taken from.\n");
      printf("                 Ex: '-if http://www.somesite.com/images/'.\n");
      printf("                 Default: '%s'.\n",IMAGESFOLDER);
      printf("                 Max string length is 100 chars.\n");
      printf("-sif <string>  - Standart Images foulder. Sets the folder where the built-in\n");
      printf("                  images will be taken from. (those .bmp that are supplied)\n");
      printf("                 Ex: '-if http://www.somesite.com/images/'.\n");
      printf("                 Default: '%s'.\n",STDIMAGESFOLDER);
      printf("                 Max string length is 100 chars.\n");
      printf("-ie <string>   - Images extention. Sets the images extention.\n");
      printf("                 Ex: '-ie .jpg'.\n");
      printf("                 Default: '%s'.\n",IMAGEEXTENTION);
      printf("                 Max string length is 10 chars.\n");
      printf("-i <string>    - Input. Path to the input file (SavedVariables.lua).\n");
      printf("                 Ex: '-i \"../../WTF/Account/MyAccount/SavedVariables.lua\"'.\n");
      printf("                 Default: '%s'.\n",LUAFILE);
      printf("                 Max string length is 100 chars.\n");
      printf("-o <string>    - Output. Path to output file.\n");
      printf("                 Ex: '-o c:/mywebpage/guildbank.html'.\n");
      printf("                 Default: '%s'.\n",OUTPUT);
      printf("                 Max string length is 100 chars.\n");
      printf("\nCall Ex: 'parser -t \"Guild Bank\" -o ./bank.thml -w 17 -ie \".jpg\"'.\n");
      return 0;
    }
    printf("Improper use of arguments: check help (-?) for proper arguments list.\n");
    return 1;
  }

  printf("\nBankParser v%s \nParses the data saved in SavedVariables.lua by BankScan into html format.\n\n", VER);

  if (argc>1)
  {
    printf("   ******    Advanced mode.   *******\n\n");
    printf("Title:                    '%s'\n",TITLE);
    printf("Number of items per row:  '%d'\n",WIDTH);
    printf("Link to the searchsite:   '%s<item name>'\n",SEARCHSITE);
    printf("Link to image:            '%s<name>%s'\n",IMAGESFOLDER,IMAGEEXTENTION);
    printf("Location of input file:   '%s'\n",LUAFILE);
    printf("Output output html file:  '%s'\n\n",OUTPUT);
    printf("   **********************************\n\n");
  }

  //Open SavedVariables.lua file for reading
  if ((in=fopen(LUAFILE,"rt")) == NULL)
  {
    if (argc>1) printf("Error: Could not find input file. Make sure that you specified proper file.\n");
    else fprintf(stderr,"Error: Could not find input file. Make sure that you copied SavedVaribles.lua to the AddOn folder.\n");
    fclose(in);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  }
  //Create and open index.html for write
  if ((out=fopen(OUTPUT,"wt")) == NULL)
  {
    if (argc>1) printf("Error: Could not create output file. Make sure that you specified proper file.\n");
    else fprintf(stderr, "Error: Could not create output file (./html/index.html). Make sure you have permision to create files.\n");
    fclose(in);
    fclose(out);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  }

  //Scan for the GT_BS_Money saved variables
   while ((fscanf(in, "%s", buf)!=EOF) && (strncmp(buf,"GT_BS_Money",12)!=0)) ;
  //If variables are not found - halt
  if (strncmp(buf,"GT_BS_Money",12) != 0)
  {
    fprintf(stderr, "Error: Data was not found in SavedVariables.lua. Make sure you run BankScan script first.\n");
    fclose(in);
    fclose(out);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  } else {
    fscanf(in, " = \" %d \"", &money);
    fclose(in);
    if ((in=fopen(LUAFILE,"rt")) == NULL)
    {
      if (argc>1) printf("Error: Could not find input file. Make sure that you specified proper file.\n");
      else fprintf(stderr,"Error: Could not find input file. Make sure that you copied SavedVaribles.lua to the AddOn folder.\n");
      fclose(in);
      printf("Press Enter to exit.");
      getc(stdin);
      return 1;
    }
  }

  //Scan for the GT_BS_ScannedItems saved variables
  while ((fscanf(in, "%s", buf)!=EOF) && (strncmp(buf,"GT_BS_ScannedItems",19)!=0)) ;
  //If variables are not found - halt
  if (strncmp(buf,"GT_BS_ScannedItems",19) != 0)
  {
    fprintf(stderr, "Error: Data was not found in SavedVariables.lua. Make sure you run BankScan script first.\n");
    fclose(in);
    fclose(out);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  }

  //Make string with constant html text (begining and end)
  strcat(head,"<html>\n\n<head>\n");
  strcat(head,"<script language=\"JavaScript\">\n<!--\nns4 = document.layers\n");
  strcat(head,"ie4 = document.all\nnn6 = document.getElementById && !document.all\n");
  strcat(head,"function hideObject(id) {\n   if (ns4) {\n      document.id.visibility = \"hide\";\n");
  strcat(head,"   }   else if (ie4) {\n      document.all[id].style.visibility = \"hidden\";\n");
  strcat(head,"   }   else if (nn6) {\n      document.getElementById(id).style.visibility = \"hidden\";\n");
  strcat(head," }\n}\nfunction showObject(id) {\n   if (ns4) {\n      document.id.visibility = \"show\";\n");
  strcat(head,"   }   else if (ie4) {\n      document.all[id].style.visibility = \"visible\";\n");
  strcat(head,"   }   else if (nn6) {\n      document.getElementById(id).style.visibility = \"visible\";\n");
  strcat(head,"   }\n}\n//-->\n</script>\n<title>");
  strcat(head,TITLE);
  strcat(head,"</title>\n</head>\n\n<body lang=EN-US>\n");
  strcat(head,"  <table width=");
  sprintf(tmp,"%d",WIDTH*44+38);
  strcat(head,tmp);
  strcat(head," border=0 cellspacing=0 cellpadding=0>\n   <tr>\n");
  strcat(head,"    <td width=");
  strcat(head,tmp);
  strcat(head," height=48 valign=top>\n");
  strcat(head,"     <table width=");
  strcat(head,tmp);
  strcat(head," border=0 cellspacing=0 cellpadding=0>\n");
  strcat(head,"      <tr>\n       <td width=55 height=48 valign=top valign=top ");
  strcat(head,"align=center background = \"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head,"bp010.bmp\">\n       </td>\n");
  strcat(head,"       <td width=");
  sprintf(tmp, "%d",WIDTH*44+38-55-50);
  strcat(head, tmp);
  strcat(head," height=48 valign=top align=center background = \"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head,"bp011.bmp\">\n");
  strcat(head,"	<table width=* height=48 border=0 cellspacing=0 cellpadding=0>\n");
  strcat(head,"	 <tr height=26>\n");
  strcat(head,"	  <td width=");
  strcat(head, tmp);
  strcat(head," valign=bottom align=center>\n");
  strcat(head,"	   <p><span style='color:white'>");
  strcat(head,TITLE);
  strcat(head,"</span></p>\n");
  strcat(head,"	  </td>\n	 </tr>\n");
  strcat(head,"	 <tr height=22>\n");
  strcat(head,"	  <td width=");
  strcat(head, tmp);
  strcat(head," valign=bottom align=right>\n");
  strcat(head,"	   <p><span style='color:white'>");
  sprintf(tmp,"%d",money/10000);
  strcat(head, tmp);
  strcat(head,"<img src=\"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head, "gold.bmp\">");
  sprintf(tmp,"%d",(money/100)%100);
  strcat(head, tmp);
  strcat(head,"<img src=\"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head, "silver.bmp\">");
  sprintf(tmp,"%d",money%100);
  strcat(head, tmp);
  strcat(head,"<img src=\"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head, "copper.bmp\">");
  strcat(head,"</span></p>\n");
  strcat(head,"	  </td>\n	 </tr>\n");
  strcat(head,"	</table>\n       </td>\n");
  strcat(head,"       <td width=50 height=48 valign=top align=center ");
  strcat(head,"background = \"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head, "bp012.bmp\">\n");
  strcat(head,"       </td>\n      </tr>\n     </table>\n    </td>\n   </tr>\n   <tr>\n");
  strcat(head,"    <td width=");
  sprintf(tmp,"%d",WIDTH*44+38);
  strcat(head,tmp);
  strcat(head," valign=top>\n     <table border=0 cellspacing=0 cellpadding=0>\n");
  strcat(head,"      <tr>\n       <td width=16 valign=top background = \"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head, "bp020.bmp\">\n");
  strcat(head,"       </td>\n       <td width=");
  sprintf(tmp,"%d",WIDTH*44+38-16-22);
  strcat(head, tmp);
  strcat(head," valign=top background = \"");
  strcat(head, STDIMAGESFOLDER);
  strcat(head, "bp021.bmp\">\n");
  strcat(head,"	<table width=* border=0 cellspacing=0 cellpadding=0>\n	 <tr>\n");

  strcat(tail,"	  </td>\n	 </tr>\n	</table>\n       </td>\n");
  strcat(tail,"       <td width=22 valign=top background = \"");
  strcat(tail, STDIMAGESFOLDER);
  strcat(tail, "bp022.bmp\">\n");
  strcat(tail,"       </td>\n      </tr>\n     </table>\n    </td>\n   </tr>\n   <tr>\n");
  strcat(tail,"    <td width=");
  sprintf(tmp,"%d",WIDTH*44+38);
  strcat(tail,tmp);
  strcat(tail," height=10 valign=top>\n");
  strcat(tail,"     <table width=");
  strcat(tail,tmp);
  strcat(tail," border=0 cellspacing=0 cellpadding=0>\n      <tr>\n");
  strcat(tail,"       <td width=55 height=10 valign=top valign=top align=center background = \"");
  strcat(tail, STDIMAGESFOLDER);
  strcat(tail, "bp030.bmp\">\n       </td>\n");
  strcat(tail,"       <td width=");
  sprintf(tmp,"%d",WIDTH*44+38-55-50);
  strcat(tail,tmp);
  strcat(tail," height=10 valign=top align=center background = \"");
  strcat(tail, STDIMAGESFOLDER);
  strcat(tail, "bp031.bmp\">\n       </td>\n");
  strcat(tail,"       <td width=50 height=10 valign=top valign=top align=center ");
  strcat(tail,"background = \"");
  strcat(tail, STDIMAGESFOLDER);
  strcat(tail, "bp032.bmp\">\n");
  strcat(tail,"       </td>\n      </tr>\n     </table>\n    </td>\n   </tr>\n");
  strcat(tail,"  </table>\n<p>\n<span style=\"font-size: 12px; color: #5F5F5F;\">This page was generated by ");
  strcat(tail,"<a href=\"http://www.curse-gaming.com/mod.php?addid=1899#post58026\">");
  strcat(tail,"(GT)GuildBank v");
  strcat(tail, VER);
  strcat(tail,"</a>. Best viewed under IE 5+.</span>\n</body>\n\n</html>");

  //Write begining of the file into index.html
  fprintf(out, head);

  fscanf(in, "%s", buf);
  //untill the end of variables reached do:
  k=0;
  while (strncmp(buf, "}", 1)!=0)
  {
    //if new set of variables do:
    if (strncmp(buf, "{", 1))
    {
      k++;
      quality=0;
      sname[0]='\0';
      name[0]='\0';
      num[0]='\0';
      pic[0]='\0';
      desc[0]='\0';
      link[0]='\0';
      //untill the end of the set of variables do:
      while (strncmp(buf, "},", 2)!=0)
      {
	//if the variable is "number" write it's value to num
	if (strncmp(buf, "[\"number\"]", 10)==0)
	{
	  fscanf(in, " = \" %s \",", buf);
	  strncpy(num, buf, strlen(buf)+1);
	}
	//if the variable is "quality" write it's value to num
	if (strncmp(buf, "[\"quality\"]", 11)==0)
	{
	  fscanf(in, " = \" %d \",", &quality);
	}
	//if the variable is "pic" write it's value to pic
	if (strncmp(buf, "[\"pic\"]", 7)==0)
	{
	  fscanf(in, " = \" %s \",", buf);
	  strncpy(pic, buf, strlen(buf)+1);
	}
	//if the variable is "name" write it's value to name
	//with apropriate modifications
	if (strncmp(buf, "[\"name\"]", 8)==0)
	{
	  fscanf(in, " = \" %s", buf);
	  while (strncmp(buf, "\",", 2)!=0)
	  {
	    if (sname[0]!='\0')
	    {
	      strncat(sname, "+", 1);
	      strncat(name, " ", 1);
	    }
	    strncat(sname, buf, strlen(buf)+1);
	    strncat(name, buf, strlen(buf)+1);
	    fscanf(in, "%s", buf);
	  }
	}
	//if the variable is "description" write it's value to desc
	//with apropriate modifications
	if (strncmp(buf, "[\"description\"]", 15)==0)
	{
	  fscanf(in, " = \" %s", buf);
	  while (strncmp(buf, "<n>", 3)!=0 && strncmp(buf, "\",", 2)!=0)
	  {
	    fscanf(in, "%s", buf);
	  }
	  while (strncmp(buf, "\",", 2)!=0)
	  {
	    if (strncmp(buf, "<n>", 3)==0)
	    {
	      strncat(desc, "<br>", 5);
	      desc_len=0;
	    }
	    else
	    if (strncmp(buf, "<t>", 3)==0)
	    {
	      for (i = desc_len; i<40; i++)
		strncat(desc, "&nbsp;", 7);
	      strncat(desc, "\0", 1);

	    }
	    else
	    {
	      strncat(desc, " ", 1);
	      strncat(desc, buf, strlen(buf)+1);
	      desc_len+=strlen(buf)+1;
	    }
	    fscanf(in, "%s", buf);
	  }
	}
	fscanf(in, "%s", buf);
      }
      //end of the set of variables
      if (strncmp(buf, "},", 2)==0) buf[0]='\0';

      //Generate link's html gode int link variable
      strcat(link,"          <td width=44 height=44 valign=middle align=center>\n");
      strcat(link,"           <a onClick=javascript:window.open(\"");
      strcat(link, SEARCHSITE);
      strcat(link, sname);
      strcat(link,"\",\"blank\",\"\"); style=\"text-decoration: none\" onMouseOver=");
      strcat(link,"\"javascript:showObject('");
      sprintf(tmp,"n%d",k);
      strcat(link, tmp);
      strcat(link,"');\" onMouseOut=\"javascript:hideObject('");
      strcat(link, tmp);
      strcat(link,"');\">");
      strcat(link,"<span style=\"position:absolute; cursor:hand;\"><table width=36 height=40 ");
      strcat(link,"cellpadding=0 cellspacing=0 border=0><tr><td valign=bottom ");
      strcat(link,"align=right><span style=\"font-size: 12px;color: white; font-weight:bold;\">");
      if (strncmp(num, "1", 2)!=0) strcat(link, num);
      strcat(link,"</span></td></tr></table></span><img src=\"");
      strcat(link, IMAGESFOLDER);
      strcat(link, pic);
      strcat(link, IMAGEEXTENTION);
      strcat(link,"\" width=40 height=40 border=0");
      strcat(link," style=\"margin-right: 0px; border: 0px\"\"></a>\n");
      strcat(link,"<div id=\"");
      strcat(link,tmp);
      strcat(link,"\" style=\"position:absolute; z-index:1; visibility:hidden;");
      strcat(link,"\"><table bgcolor=\"#FFFFAA\" width=190 cellpadding=3 ");
      strcat(link,"cellspacing=0 border=0 valign=top align=left><tr><td><span style=\"font-size: 12px;");
      if (quality==0) strcat(link,"color:#5F5F5F;\"><b>");
      if (quality==1) strcat(link,"color:#000000;\"><b>");
      if (quality==2) strcat(link,"color:#00AF00;\"><b>");
      if (quality==3) strcat(link,"color:#0000FF;\"><b>");
      if (quality==4) strcat(link,"color:#BF00BF;\"><b>");
      if (quality==5) strcat(link,"color:#DD8800;\"><b>");
      strcat(link, name);
      strcat(link,"</b></span><span style=\"font-size: 12px; color:black\">");
      strcat(link, desc);
      strcat(link,"</td></tr></table></div></td>");
      if ((k%WIDTH)==0) strcat(link,"</tr>\n");
      else strcat(link,"\n");

      //Put link into index.html
      fprintf(out, link);
      fscanf(in, "%s", buf);
    }
  }
  //Put rest of the html code into index.html
  fprintf(out, tail);

  printf("Parsing is complete. Your html file is: %s\n", OUTPUT);
  fclose(in);
  fclose(out);
  return 0;
}

