Pristine Ack-5.5
[Ack-5.5.git] / modules / src / string / str2bts.c
1 /* $Id: str2bts.c,v 1.6 1994/06/24 11:22:44 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /* str2bts -- (1985, EHB)
7 */
8
9 #include "ack_string.h"
10
11 static
12 is_oct(c)
13         char c;
14 {
15         return ((unsigned int)(c-'0') <= ('7'-'0'));
16 }
17
18 /*      str2bts() strips the escaped characters of a
19         string and replaces them by the ascii characters they stand for.
20         The ascii length of the resulting string is returned, including the
21         terminating null-character.
22 */
23 char *
24 str2bts(str, bts, pn)
25         register char *str;
26         char *bts;
27         int *pn;
28 {
29         register char *t = bts;
30
31         while (*str) {
32                 if (*str == '\\') {
33                         switch (*++str) {
34                         case 'b':
35                                 *t++ = '\b';
36                                 str++;
37                                 break;
38                         case 'f':
39                                 *t++ = '\f';
40                                 str++;
41                                 break;
42                         case 'n':
43                                 *t++ = '\n';
44                                 str++;
45                                 break;
46                         case 'r':
47                                 *t++ = '\r';
48                                 str++;
49                                 break;
50                         case 't':
51                                 *t++ = '\t';
52                                 str++;
53                                 break;
54                         default:
55                                 if (is_oct(*str)) {
56                                         register cnt = 0, oct = 0;
57
58                                         do
59                                                 oct = oct * 8 + *str - '0';
60                                         while (is_oct(*++str) && ++cnt < 3);
61                                         *t++ = (char) oct;
62                                         break;
63                                 }
64                                 *t++ = *str++;
65                                 break;
66                         }
67                 }
68                 else
69                         *t++ = *str++;
70         }
71         *t = '\0';      /* don't forget this one !!!    */
72         *pn = t - bts + 1;
73         return bts;
74 }