/*************************************************************************** "vnconvert" converts Vietnamese text files between different formats. It is intended to run on Unix platforms. The user shoul recompile on his/her specific platform. A very simple "Makefile" is provided. This program is similar to the VPS "convert" for DOS, and in fact, is based on the coding table provided by Vietnamese Professional Society (VPS). This program is also based on the TRICHLOR "vn7to8" package and takes advantage of its header files, converting functions between VISCII and VIQR formats. This program considers only the standard VISCII and VIQR formats. The package consists of the following files: - vnconvert.c (this file) - conv.c - vn78.c - vnkeys.h - conv.h - ctype.h - vn78.h - charset.h - charsetv.h - charseta.h - portable.h - vncompos.h - Makefile Run "make: to install "vnconvert". There may be a lot of things which can be improved and any comments or suggestions are welcome. Please forward them to Binh Do *****************************************************************************/ #include #include #include #include "vnkeys.h" #include "conv.h" main() { u_char buf[BUFSIZ]; char bufline[255]; char input_file[255], output_file[255]; int fd_in, fd_out; int n, k, icontinue; int from, to; u_char esc = VK_DEFAULT_ESC; u_char bs = VK_DEFAULT_BS; Vk_State state = VK_ST_VIETNAMESE; u_char obuf[4*BUFSIZ], newbuf[4*BUFSIZ]; int i, count; Vk_Fsm id; u_char FAR *ic, *oc; struct stat stbuf; short mode; /* Read in input_file, output_file and their formats */ id = vk_init(state, esc, bs); vk_set(id, 1, VK_SET_BATCH); while (1) { /* Read in input_file, output_file and their formats */ printf("\nVNCONVERT converts Vietnamese text files between the following formats :\n\n"); printf("\t0 - VISCII \t\t\t 7 - VN Thin-Art (PC)\n"); printf("\t1 - VNCII (VPSWin)\t\t 8 - Viet-Toan\n"); printf("\t2 - VPSedit (DOS)\t\t 9 - Plain Vietnamese\n"); printf("\t3 - VNI\t\t\t\t10 - Standard VIQR\n"); printf("\t4 - VNLab/VNU\n"); printf("\t5 - VNU (Windows)\n"); printf("\t6 - VN Thin-Art (Mac)\n\n"); printf("Input-file name (or Enter to quit): "); gets(bufline); if (strlen(bufline) == 0) break; sscanf(bufline, "%s", input_file); printf("currently in the format number: "); *bufline = '\0'; while (strlen(bufline)==0) { gets(bufline); sscanf(bufline, "%d", &from); } printf("\nOutput-file name: "); *bufline = '\0'; while (strlen(bufline)==0) { gets(bufline); sscanf(bufline, "%s", output_file); } printf("to the format number: "); *bufline = '\0'; while (strlen(bufline)==0) { gets(bufline); sscanf(bufline, "%d", &to); } /* Check for possible errors */ icontinue = 0; if (stat(input_file, &stbuf) == -1) { fprintf(stderr, "cannot access \"%s\"\n", input_file); icontinue = 1; } else if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { fprintf(stderr, "\"%s\" is a directory\n", input_file); icontinue = 1; } mode = stbuf.st_mode; if (from < 0 || from > FORMATS) { fprintf(stderr, "the format number (%d) of input file is out of range [0 - %d]\n", FORMATS, from); icontinue = 1; } if (to < 0 || to > FORMATS) { fprintf(stderr, "the format number (%d) of output file is out of range [0 - %d]\n", FORMATS, to); icontinue = 1; } if (stat(output_file, &stbuf) != -1) { if ((stbuf.st_mode & S_IFMT) == S_IFDIR) { fprintf(stderr, "\"%s\" is a directory\n", output_file); icontinue = 1; } else { fprintf(stderr, "\"%s\" already exists. Overwrite it (y/n)? ", output_file); gets(buf); if (toupper(buf[0]) != 'Y') icontinue = 1; } } if (from == to) { /* no need to convert */ fprintf(stderr, "Use \"cp\" to copy files of same formats\n"); icontinue = 1; } /* And procesing the files as follows: for formats 0 - 9, just find the corresponding chars in the table and done. (9 should be only destination, not source). If one of the formats is 10 (VIQR), then we must go thru format 0 (VISCII). Should be improved here */ if (icontinue) continue; else { if ((fd_in = open(input_file, 0)) == -1) { fprintf(stderr, "cannot open \"%s\"\n", input_file); continue; } if ((fd_out = creat(output_file, mode))==-1) { fprintf(stderr, " cannot create \"%s\"\n", output_file); continue; } } if (from == PLAIN_format) /* nonsense */ to = from; if (from != VIQR_format) { if (to != VIQR_format) { while ((n = read(fd_in, buf, BUFSIZ)) > 0) { for (k=0; k 0) { buf[n] = '\0'; if (from != VISCII_format) for (k=0; k 0) { ic = buf; oc = obuf; for (i = 0; i < n; i++) { vk_step(id, *ic++, oc, (int FAR *)&count); if (count) oc += count; } if (to == VISCII_format) write(fd_out, obuf, oc-obuf); else { k = oc - obuf; for (i=0; i