hex2chars.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sun, 19 Jul 2009 19:44:48 +0200
changeset 101 176448af80fa
parent 77 49e0babccb23
permissions -rw-r--r--
Ignore files from the javadb project Do not nerve with class- and derby files committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * 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;
}