xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/translit.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1*41fbaed0Stron /*	$NetBSD: translit.c,v 1.1.1.1 2009/06/23 10:09:01 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	translit 3
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	transliterate characters
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	#include <stringops.h>
10*41fbaed0Stron /*
11*41fbaed0Stron /*	char	*translit(buf, original, replacement)
12*41fbaed0Stron /*	char	*buf;
13*41fbaed0Stron /*	char	*original;
14*41fbaed0Stron /*	char	*replacement;
15*41fbaed0Stron /* DESCRIPTION
16*41fbaed0Stron /*	translit() takes a null-terminated string, and replaces characters
17*41fbaed0Stron /*	given in its \fIoriginal\fR argument by the corresponding characters
18*41fbaed0Stron /*	in the \fIreplacement\fR string. The result value is the \fIbuf\fR
19*41fbaed0Stron /*	argument.
20*41fbaed0Stron /* BUGS
21*41fbaed0Stron /*	Cannot replace null characters.
22*41fbaed0Stron /* LICENSE
23*41fbaed0Stron /* .ad
24*41fbaed0Stron /* .fi
25*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
26*41fbaed0Stron /* AUTHOR(S)
27*41fbaed0Stron /*	Wietse Venema
28*41fbaed0Stron /*	IBM T.J. Watson Research
29*41fbaed0Stron /*	P.O. Box 704
30*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
31*41fbaed0Stron /*--*/
32*41fbaed0Stron 
33*41fbaed0Stron /* System library. */
34*41fbaed0Stron 
35*41fbaed0Stron #include "sys_defs.h"
36*41fbaed0Stron #include <string.h>
37*41fbaed0Stron 
38*41fbaed0Stron /* Utility library. */
39*41fbaed0Stron 
40*41fbaed0Stron #include "stringops.h"
41*41fbaed0Stron 
translit(char * string,const char * original,const char * replacement)42*41fbaed0Stron char   *translit(char *string, const char *original, const char *replacement)
43*41fbaed0Stron {
44*41fbaed0Stron     char   *cp;
45*41fbaed0Stron     const char *op;
46*41fbaed0Stron 
47*41fbaed0Stron     /*
48*41fbaed0Stron      * For large inputs, should use a lookup table.
49*41fbaed0Stron      */
50*41fbaed0Stron     for (cp = string; *cp != 0; cp++) {
51*41fbaed0Stron 	for (op = original; *op != 0; op++) {
52*41fbaed0Stron 	    if (*cp == *op) {
53*41fbaed0Stron 		*cp = replacement[op - original];
54*41fbaed0Stron 		break;
55*41fbaed0Stron 	    }
56*41fbaed0Stron 	}
57*41fbaed0Stron     }
58*41fbaed0Stron     return (string);
59*41fbaed0Stron }
60*41fbaed0Stron 
61*41fbaed0Stron #ifdef TEST
62*41fbaed0Stron 
63*41fbaed0Stron  /*
64*41fbaed0Stron   * Usage: translit string1 string2
65*41fbaed0Stron   *
66*41fbaed0Stron   * test program to perform the most basic operation of the UNIX tr command.
67*41fbaed0Stron   */
68*41fbaed0Stron #include <msg.h>
69*41fbaed0Stron #include <vstring.h>
70*41fbaed0Stron #include <vstream.h>
71*41fbaed0Stron #include <vstring_vstream.h>
72*41fbaed0Stron 
73*41fbaed0Stron #define STR	vstring_str
74*41fbaed0Stron 
main(int argc,char ** argv)75*41fbaed0Stron int     main(int argc, char **argv)
76*41fbaed0Stron {
77*41fbaed0Stron     VSTRING *buf = vstring_alloc(100);
78*41fbaed0Stron 
79*41fbaed0Stron     if (argc != 3)
80*41fbaed0Stron 	msg_fatal("usage: %s string1 string2", argv[0]);
81*41fbaed0Stron     while (vstring_fgets(buf, VSTREAM_IN))
82*41fbaed0Stron 	vstream_fputs(translit(STR(buf), argv[1], argv[2]), VSTREAM_OUT);
83*41fbaed0Stron     vstream_fflush(VSTREAM_OUT);
84*41fbaed0Stron     vstring_free(buf);
85*41fbaed0Stron     return (0);
86*41fbaed0Stron }
87*41fbaed0Stron 
88*41fbaed0Stron #endif
89