Category : C Source Code
Archive   : ECSTR.ZIP
Filename : _STR2MAP.C

 
Output of file : _STR2MAP.C contained in archive : ECSTR.ZIP
/* File : _str2map.c
Author : Richard A. O'Keefe.
Updated: 20 April 1984
Defines: _map_vec[], _str2map().

_str2map(option, from, to) constructs a translation table. If from
or to is NullS, the same string is used as last time, so if you want
to translate a whole lot of strings using the same mapping you don't
have to reconstruct it each time. The options are

0: initialise the map to the identity function,
then map each from[i] to the corresponding to[i].
If to[] is shorter than from[], its last character is
repeated as often as needed.

1: as 0, but don't initialise the map.

2: initialise the map to send every character to to[0],
then map each from[i] to itself.

For example, to build a map which forces letters to lower case but
sends everything else to blank, call

_str2map(2, "abcdefghijklmnopqrstuvwxyz", " ");
_str2map(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz");

Only strtrans() and strntrans() in this package call _str2map; if
you want to build your own maps this way you can "fool" them into
using it, as when the two strings are NullS they don't change the
map. As an extra-special dubious *hack*, _map_vec has an extra NUL
character at the end, so after calling _str2map(0, "", ""), you can
use _map_vec+1 as a string of the 127 non-NUL characters (or if the
_AlphabetSize is 256, of the 255 non-NUL characters).
*/

#include "strings.h"
#include "_str2map.h"

static _char_ *oldFrom = "?";
static char *oldTo = "?";

char _map_vec[_AlphabetSize+1];

void _str2map(option, from, to)
int option;
register _char_ *from;
register char *to;
{
register int i, c;

if (from == NullS && to == NullS) return;
if (from == NullS) from = oldFrom; else oldFrom = from;
if (to == NullS) to = oldTo; else oldTo = to;
switch (option) {
case 0:
for (i = _AlphabetSize; --i >= 0; _map_vec[i] = i) ;
case 1:
while (i = *from++) {
_map_vec[i] = *to++;
if (!*to) {
c = *--to;
while (i = *from++) _map_vec[i] = c;
return;
}
}
return;
case 2:
c = *to;
for (i = _AlphabetSize; --i >= 0; _map_vec[i] = c) ;
while (c = *from++) _map_vec[c] = c;
return;
}
}



  3 Responses to “Category : C Source Code
Archive   : ECSTR.ZIP
Filename : _STR2MAP.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/