/*

 Ascii 2 JAscii

 Converts standard international ASCII text into Japanese ASCII,
 currently supported are JIS, NewJIS, NEC, EUC and Shift-JIS formats.

 by Fabrizio Bartoloni <lanch@caribusiness.it>
 
 This source code is freely distributable but I retain the copyright
 on it, you cannot spread modified versions of this source code without
 my permission.

 Write me for suggestions, bug reports, improvements, etc.
 
 !BIG! Thanks to: Emiliano "Skywalk3r" Esposito
*/

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

static char verstr[] = "$VER: Ascii2Jascii 1.1f (16.06.00)";

void usage(void)

{
fprintf(stderr,"\n Where infile is the input file to be converted,\n");
fprintf(stderr," the destination file will have the same \n");
fprintf(stderr," name but proper extension according to\n");
fprintf(stderr," the chosen format [.jis|.njis|.nec|.euc|.sjis].\n\n");
}

int main (int argc, char **argv)

{
char *progname = argv[0];

if (argc != 3)

 {
 fprintf(stderr,"\n Ascii2Jascii 2000 by Fabrizio 'Lanch' Bartoloni\n");
 fprintf(stderr,"\n lanch@caribusiness.it\t lanch@tiscalinet.it\n");
 fprintf(stderr,"\n Usage: %s infile [JIS|NJIS|NEC|EUC|SJIS]\n",progname);
 usage();
 exit(1);
 }

char buf[256];

FILE *fi,*fp;
char *infile = argv[1];
char *temp1[80], *temp2[5], dot[] = "."; /* Very big mess to          */
strcpy(temp1,argv[1]);                    /* concatenate the proper    */
strcat(temp1, dot);                       /* suffix to the resulting   */
strcpy(temp2,argv[2]);                    /* file, hope to improve it  */
char *destfile;                           /* as soon as possible.      */
destfile = strcat(temp1,temp2);

if (fi = fopen(infile,"r")) {

struct tipoheader {
char *ID;
char *header;    /* This header identify the file    */
char *prefix;    /* Prefix for each JAscii character */
char *EndSeq;    /* EOF */
char *space;     /* how empty spaces are represented */
int alfa;        /* how much the JAscii will be shifted from ASCII */
                  };

struct tipoheader codes[] =
 {
 "JIS", "$@", "#", "(J\n", "!!", 0,
 "NJIS", "$B", "#", "(J\n", "!!", 0,
 "SJIS", "", "", "", "@", 32,
 "NEC", "K", "#", "H", "!!", 0,
 "EUC", "", "", "", "", 128
 };

int x; /* position inside the structure */

if ((strcmp(argv[2], "JIS")) == 0)
                  {
                  x = 0;
                  }
else if ((strcmp(argv[2], "NJIS")) == 0)
                  {
                  x = 1;
                  }
else if ((strcmp(argv[2], "SJIS")) == 0)
                  {
                  x = 2;
                  }
else if ((strcmp(argv[2], "NEC")) == 0)
                  {
                  x = 3;
                  }
else if ((strcmp(argv[2], "EUC")) == 0)
                  {
                  x = 4;
                  }

else                  {
                      fprintf(stderr," Required format is either unknown or unavailable\n");
                      exit(1);
                      }

 if (fp = fopen(destfile,"w")) {

                                while (fgets(buf,sizeof(buf), fi)) {
                                fprintf(fp,"%s",codes[x].header);
                                char *ins = buf;
                                int size;
                                size = strlen(buf);

                                   for (int loop = 0;loop < size; loop++)

                                       {

                                        switch (ins[loop])
                                                          
                                                          {
                                                          case ' ' :
                                                          fprintf(fp,"%s", codes[x].space);
                                                                      break;

                                                          case '\n' :
                                                          fprintf(fp,"%s", codes[x].EndSeq);
                                                                      break;

                                                          default :
                                                          fprintf(fp,"%s%c",codes[x].prefix,ins[loop] + codes[x].alfa);
                                                                    break;
                                                          } /* endswitch */
                                       } /* endloop */
 
                                                                   } /* endwhile */

                               } /* endif outfile   */

                               else {
                                    fprintf(stderr,"unable to create output file: %s\n",argv[2]);
                                    exit(1);
                                    }


                            } /* endif open file */

                            else {
                                  fprintf(stderr,"unable to open file: %s!\n",argv[1]);
                                  exit(1);
                                 }

fclose(fp);
fclose(fi);

puts("\n Conversion Done!\n");
return(0);
}

