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