hex2chars.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 53 6b3d7e3418c1
parent 48 b94d657a9acb
child 77 49e0babccb23
permissions -rw-r--r--
getch.c renamed to cross_getch.c committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/hex2chars.c
 * Copyright (C) 2008 Markus Broeker
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#ifndef RUNS
#define RUNS 16
#endif

/*
 * =708== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1)
 * ==708== malloc/free: in use at exit: 352 bytes in 32 blocks.
 * ==708== malloc/free: 48 allocs, 16 frees, 3,680 bytes allocated.
 * ==708== For counts of detected errors, rerun with: -v
 * ==708== searching for pointers to 32 not-freed blocks.
 * ==708== checked 68,684 bytes.
 */

const char *characters = "xuzicjklmneopbdaqfrstvgwhy";

char *remap_works (char *str, int slen)
{
    int i;
    int pos;

    static char *s;

    if ((s = malloc (slen + 1)) == NULL)
        return NULL;

    for (i = 0; i < slen; i++) {
        if (str[i] >= 'a') {
            pos = str[i] - 'a';
            s[i] = characters[pos];
        } else {
            /*
             * Let numbers asis
             */
            s[i] = str[i];
        }
    }

    s[i] = 0;
    return s;
}

char *remap_works_not (char *str, int slen)
{
    int i, j;

    double value;
    double *table;

    static char *s;

    int clen = strlen (characters);

    if ((s = malloc (slen + 1)) == NULL)
        return NULL;

    if ((table = calloc (clen, sizeof (double))) == NULL)
        return NULL;

    for (i = 0; i < clen; i++)
        table[i] = cos ('a' + i);

    for (i = 0; i < slen; i++) {
        if (str[i] >= 'a') {
            value = cos (str[i]);
            for (j = 0; j < clen; j++) {
                /*
                 * flip and expand characters
                 */
                s[i] = '*';
                if (value == table[j]) {
                    s[i] = characters[j];
                    break;
                }

                if ((fabs (value - table[j])) < 0.001) {
                    s[i] = characters[j];
                    break;
                }
            }
        } else {
            /*
             * Let numbers asis
             */
            s[i] = str[i];
        }
    }

    s[i] = 0;

    if (table != NULL)
        free (table);

    return s;
}

int main (int argc, char **argv)
{
    int i;

    int len;

    char *start;

    char *end;

    if (argc != 2) {
        printf ("Usage: %s <string>\n", argv[0]);
        return EXIT_FAILURE;
    }

    start = argv[1];
    len = strlen (start);

    printf ("\n  --- REMAP_WORKS FINE ---\n\n");
    for (i = 0; i < RUNS; i++) {
        if ((end = remap_works (start, len)) != NULL)
            printf ("%s\t<==>\t%s\n", start, end);
        start = end;
    }

    /*
     * reset state
     */
    start = argv[1];

    printf ("\n  --- REMAP_WORKS_NOT because floating-point comparison is evil at all ---\n\n");
    for (i = 0; i < RUNS; i++) {
        if ((end = remap_works_not (start, len)) != NULL)
            printf ("%s\t<==>\t%s\n", start, end);
        start = end;
    }

    return EXIT_SUCCESS;
}