File: autogb.c

package info (click to toggle)
zh-autoconvert 0.3.16-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 3,612 kB
  • sloc: ansic: 28,053; perl: 264; makefile: 84; python: 9
file content (150 lines) | stat: -rw-r--r-- 3,624 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
AutoConvert, a Chinese HZ/GB/Big5 encodings auto-converter
Copyright (C) 1999  于广辉  Yu Guanghui <ygh@debian.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*/
/* 
* author: Yu Guanghui <ygh@dlut.edu.cn>
*	  Network Center
* 	  Dalian Univ. of Tech.
*	  1999.5
*/
//$Header: /home/prog/autoconvert/autogb.c,v 1.7 2001/04/23 14:18:06 ygh Exp $

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "hz.h"


/* Print help and exit */
void print_help(char* filename){
	printf("\nUsage: %s [-OPTION] < input > output\n", filename);
	printf("Now autoconvert can only judge gb/big5/hz encoding, so if you want to  \n do other conversion, you must set the option --input.  Without --input, \n the program will guess the encoding of the input. \n OPTION is one of (case insensitive): \n -i encoding, --input encoding:			Set the input encoding \n -o encoding, --output encoding:			Set the output encoding \n The encoding should be gb, big5, hz, uni, utf7 or utf8. \n");

	exit(0);
}


int main(int argc,char*argv[])
{
	int incode,outcode,dist;
	char buff[MAX_BUFFER*3];
	char *pbuf;
	char *ps;
	int need_judge=1;


	int count;

	incode = 0;

	pbuf=&buff[2];

	if( strstr(argv[0],"autogb") != '\0' ){
		outcode=GB_CODE;
	}else if(strstr(argv[0],"autob5") != '\0'){
	
		outcode=BIG5_CODE;
	}
	/* We need one option for encoding at least*/
	if (argc >= 2){ 
	
		/*
		if(argv[1][0] != '-')
			print_help(argv[0]);

		for ( i = 1; i < strlen(argv[1]); i++)
			argv[1][i] = tolower(argv[1][i]);
		
		*/
		/* Get options here. */
		while(1){
			int c;
			int option_index ;
			static struct option long_options[] = 
			{
				{"input", 1, 0, 'i'},
				{"output", 1, 0, 'o'},
				{0, 0, 0, 0},
			};
//			c = getopt_long_only(argc, argv, "io", long_options,
//				&option_index);
			c = getopt_long(argc, argv, "i:o:", long_options,
				&option_index);

			/* No more option, break 'while' loop */
			if ( c == -1 ){
				break; /* While loop */
			}

			switch( c ){
				case 'i':
					need_judge=0;
					if(optarg){
						if((incode=conv_str(optarg))==OTHER_CODE) print_help(argv[0]);
					}else{
						print_help(argv[0]);
					}
					break;

				case 'o':
					if(optarg){
						if((outcode=conv_str(optarg))==OTHER_CODE) print_help(argv[0]);
					}else{
						print_help(argv[0]);
					}
					break;
				case '?':
				default:
					print_help(argv[0]);
					break;
			} /* End switch */

		} /* End while */
	} /* End else if */

	if((count=read(0,pbuf,MAX_BUFFER))< 0){
		fprintf(stderr,"Can't read stdin\n");
	}
	
	/* If incode is set in command line, don't judge. */
	if (need_judge==1)
		incode=j_code(pbuf,count);

	if (incode != outcode){
		hz_setup();
		if((dist=hz_search(incode,outcode,8))==0){
			printf("I couldn't do this conversion\n");
			exit(0);
		}
	}

	do{
		if(incode==outcode){
			write(1,pbuf,count);
		}else{
			ps=hz_convert(pbuf,&count,0);
			write(1,ps,count);
		}
		pbuf=&buff[2];
	}while((count=read(0,pbuf,MAX_BUFFER))>0);

	fflush(0);
	exit(0);
}