/*

 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.1c (30.05.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 \n");
fprintf(stderr," same name but proper extension according to.\n");
fprintf(stderr," the chosen format [.jis|.njis|.nec|.euc|.sjis]\n");

}

int main (int argc, char **argv)

{
char *progname = argv[0];

if (argc != 3)

 {
 fprintf(stderr,"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[80], 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")) {

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 */

char headerJIS[] = "$@";  /*This header identify the file as JIS */
char headerNJIS[] = "$B"; /*This header identify the file as NJIS */
char headerNEC[] = "K";   /*This header identify the file as NEC */
char headerEUC[] = "";     /*This header identify the file as EUC!SJIS */

char prefixJIS[] = "#"; /* Prefix for each JAscii character JIS!NJIS!NEC*/
char prefixEUC[] = "";    /* Prefix for each JAscii character EUC*/
char prefixSJIS[] = ""; /*Prefix for each JAscii character SJIS */

char EndJIS[] = "(J\n";    /* JIS!NJIS EOF */
char EndNec[] = "H";      /* NEC EOF */
char EndEUC[] = "";        /* EUC!SJIS EOF */

char spaceJIS[] = "!!"; /* Space in JIS!NJIS!NEC */
char spaceEUC[] = ""; /* Space in EUC */
char spaceSJIS[] = "@"; /* space in Shift-JIS */

if ((strcmp(argv[2], "NJIS")) == 0)
                      {
                      strcpy(header, headerNJIS);
                      strcpy(EndSeq, EndJIS);
                      strcpy(prefix, prefixJIS);
                      strcpy(space, spaceJIS);
                      alfa = 0;
                      }

else if ((strcmp(argv[2], "JIS")) == 0)
                      {
                      strcpy(header, headerJIS);
                      strcpy(EndSeq, EndJIS);
                      strcpy(prefix, prefixJIS);
                      strcpy(space, spaceJIS);
                      alfa = 0;
                      }

else if ((strcmp(argv[2], "NEC")) == 0)
                      {
                      strcpy(header, headerNEC);
                      strcpy(EndSeq, EndNec);
                      strcpy(prefix, prefixJIS);
                      strcpy(space, spaceJIS);
                      alfa = 0;
                      }

else if ((strcmp(argv[2], "EUC")) == 0)
                      {
                      strcpy(header, headerEUC);
                      strcpy(EndSeq, EndEUC);
                      strcpy(prefix, prefixEUC);
                      strcpy(space, spaceEUC);
                      alfa = 128;
                      }

else if ((strcmp(argv[2], "SJIS")) == 0)
                      {
                      strcpy(header, headerEUC);
                      strcpy(EndSeq, EndEUC);
                      strcpy(prefix, prefixSJIS);
                      strcpy(space, spaceSJIS);
                      alfa = 32;
                      }



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",header);
                                char *ins = buf;
                                int size;
                                size = strlen(buf);

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

                                       {

                                        if (ins[loop] == ' ')
                                                           {
                                                           fprintf(fp,"%s", space);
                                                           }

                                        else if (ins[loop] == '\n')
                                                           {
                                                           fprintf(fp,"%s", EndSeq);
                                                           }

                                        else
                                                           {
                                                           fprintf(fp,"%s%c",prefix,ins[loop] + alfa);
                                                           }
                                       } /* 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);
}

