author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Sat, 13 Dec 2008 17:58:00 +0100 | |
changeset 8 | 96d16dfe787a |
parent 0 | af501b0c1716 |
child 9 | c3fecc82ade6 |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* $Id: hex2chars.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ |
|
3 |
* $Source: /development/c/demos/hex2chars.c,v $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
10 |
#include <math.h> |
|
11 |
||
12 |
#ifndef RUNS |
|
13 |
#define RUNS 16 |
|
14 |
#endif |
|
15 |
||
16 |
/* |
|
17 |
* =708== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) |
|
18 |
* ==708== malloc/free: in use at exit: 352 bytes in 32 blocks. |
|
19 |
* ==708== malloc/free: 48 allocs, 16 frees, 3,680 bytes allocated. |
|
20 |
* ==708== For counts of detected errors, rerun with: -v |
|
21 |
* ==708== searching for pointers to 32 not-freed blocks. |
|
22 |
* ==708== checked 68,684 bytes. |
|
23 |
*/ |
|
24 |
||
25 |
const char *characters = "xuzicjklmneopbdaqfrstvgwhy"; |
|
26 |
||
27 |
char *remap_works (char *str, int slen) |
|
28 |
{ |
|
29 |
int i; |
|
30 |
int pos; |
|
31 |
static char *s; |
|
32 |
||
33 |
if ((s = malloc (slen + 1)) == NULL) |
|
34 |
return NULL; |
|
35 |
||
36 |
for (i = 0; i < slen; i++) { |
|
37 |
if (str[i] >= 'a') { |
|
38 |
pos = str[i] - 'a'; |
|
39 |
s[i] = characters[pos]; |
|
40 |
} else { |
|
41 |
/* |
|
42 |
* Let numbers asis |
|
43 |
*/ |
|
44 |
s[i] = str[i]; |
|
45 |
} |
|
46 |
} |
|
47 |
||
48 |
s[i] = 0; |
|
49 |
return s; |
|
50 |
} |
|
51 |
||
52 |
char *remap_works_not (char *str, int slen) |
|
53 |
{ |
|
54 |
int i, j; |
|
55 |
double value; |
|
56 |
double *table; |
|
57 |
static char *s; |
|
58 |
||
59 |
int clen = strlen (characters); |
|
60 |
||
61 |
if ((s = malloc (slen + 1)) == NULL) |
|
62 |
return NULL; |
|
63 |
||
64 |
if ((table = calloc (clen, sizeof (double))) == NULL) |
|
65 |
return NULL; |
|
66 |
||
67 |
for (i = 0; i < clen; i++) |
|
68 |
table[i] = cos ('a' + i); |
|
69 |
||
70 |
for (i = 0; i < slen; i++) { |
|
71 |
if (str[i] >= 'a') { |
|
72 |
value = cos (str[i]); |
|
73 |
for (j = 0; j < clen; j++) { |
|
74 |
/* |
|
75 |
* flip and expand characters |
|
76 |
*/ |
|
77 |
s[i] = '*'; |
|
78 |
if (value == table[j]) { |
|
79 |
s[i] = characters[j]; |
|
80 |
break; |
|
81 |
} |
|
82 |
||
83 |
if ((fabs (value - table[j])) < 0.001) { |
|
84 |
s[i] = characters[j]; |
|
85 |
break; |
|
86 |
} |
|
87 |
} |
|
88 |
} else { |
|
89 |
/* |
|
90 |
* Let numbers asis |
|
91 |
*/ |
|
92 |
s[i] = str[i]; |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
s[i] = 0; |
|
97 |
||
98 |
if (table != NULL) |
|
99 |
free (table); |
|
100 |
||
101 |
return s; |
|
102 |
} |
|
103 |
||
104 |
int main (int argc, char **argv) |
|
105 |
{ |
|
106 |
int i; |
|
107 |
int len; |
|
108 |
char *start; |
|
109 |
char *end; |
|
110 |
||
111 |
if (argc != 2) { |
|
112 |
printf ("Usage: %s <string>\n", argv[0]); |
|
113 |
exit (0); |
|
114 |
} |
|
115 |
||
116 |
start = argv[1]; |
|
117 |
len = strlen (start); |
|
118 |
||
119 |
printf ("\n --- REMAP_WORKS FINE ---\n\n"); |
|
120 |
for (i = 0; i < RUNS; i++) { |
|
121 |
if ((end = remap_works (start, len)) != NULL) |
|
122 |
printf ("%s\t<==>\t%s\n", start, end); |
|
123 |
start = end; |
|
124 |
} |
|
125 |
||
126 |
/* |
|
127 |
* reset state |
|
128 |
*/ |
|
129 |
start = argv[1]; |
|
130 |
||
131 |
printf ("\n --- REMAP_WORKS_NOT because floating-point comparison is evil at all ---\n\n"); |
|
132 |
for (i = 0; i < RUNS; i++) { |
|
133 |
if ((end = remap_works_not (start, len)) != NULL) |
|
134 |
printf ("%s\t<==>\t%s\n", start, end); |
|
135 |
start = end; |
|
136 |
} |
|
137 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
138 |
return EXIT_SUCCESS; |
0 | 139 |
} |