/*

 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.0t (01.05.00)";

void usage(void)

{
fprintf(stderr,"\n Where string is the input string to be converted\n");
fprintf(stderr," and destfile is the destination file where the \n");
fprintf(stderr," output resulting will be redirect.\n");
fprintf(stderr,"\n Take care the destfile ends in .jis|.njis|.nec|.euc|.sjis\n");
fprintf(stderr," to be promptly recognized.\n");
}

int main (int argc, char **argv)

{
char *progname = argv[0];

if (argc == 1 || argc > 4)

 {
 fprintf(stderr,"Usage: %s string destfile [JIS|NJIS|NEC|EUC|SJIS]\n",progname);
 usage();
 exit(1);
 }

FILE *fp;
char *destfile = argv[2];
fp = fopen(destfile,"w");

char *ins = argv[1];
int size;
size = strlen(argv[1]);

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 ";    /* 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[3], "NJIS")) == 0)
                      {
                      strcpy(header, headerNJIS);
                      strcpy(EndSeq, EndJIS);
                      strcpy(prefix, prefixJIS);
                      strcpy(space, spaceJIS);
                      alfa = 0;
                      }

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

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

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

else if ((strcmp(argv[3], "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);
     }

fprintf(fp,"%s",header);

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

 {

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

 fprintf(fp,"%s%c",prefix,ins[loop] + alfa);
 }
fprintf(fp,"%s",EndSeq);
fclose(fp);
return(0);
}

