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