completely finished the documentation of the EBUF module (!)
[neuro.git] / src / misc / chgen.c
blob5afa7a693825f32e1859113f6538b6246a98d532
2 /*
3 * libneuro, a light weight abstraction of high or lower libraries
4 * and toolkit for applications.
5 * Copyright (C) 2005-2006 Nicholas Niro, Robert Lemay
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 /* character generator */
24 #include <stdlib.h>
25 #include <stdio.h>
27 /* min is 33 because characters lower are control which doesn't have a gfx.*/
28 #define START_NUMBER 45 /* normal ascii starts at 0 but theres alot of event characters
29 that aren't graphical. To be safe we start at 45. 33 to 45 are
30 characters we prefer not to use(they are normally invalid anyway) */
31 /* max is 255 */
32 #define END_NUMBER 126 /* normal ascii finished at 126 */
33 /* obsolete */
34 #define SLEEP_TIME 5 /* in microseconds : 1 second == 10^6 microseconds */
36 static void
37 core_Uchar(unsigned char **buf, unsigned int *c_pass_count, unsigned int *cursor)
39 register unsigned char *c_pass = NULL;
41 if (*buf == NULL)
43 *buf = (unsigned char*)calloc(1, sizeof(unsigned char) + 1);
44 c_pass = *buf;
45 c_pass[0] = START_NUMBER;
46 *c_pass_count = 1;
47 c_pass[1] = '\0';
49 else
50 c_pass = *buf;
52 if (c_pass[*cursor] == END_NUMBER)
54 c_pass[*cursor] = START_NUMBER;
56 *cursor = *cursor + 1;
57 if (*c_pass_count < *cursor + 1)
59 *c_pass_count = *c_pass_count + 1;
60 *buf = (unsigned char*)realloc(*buf,
61 sizeof(unsigned char) * (1 + *c_pass_count));
62 c_pass = *buf;
63 c_pass[*c_pass_count] = '\0';
64 c_pass[*cursor] = START_NUMBER;
67 core_Uchar(buf, c_pass_count, cursor);
68 c_pass = *buf;
70 *cursor = 0;
72 c_pass[*cursor] = c_pass[*cursor] + 1;
76 static void
77 test_Uchar()
79 int total = 83;
80 unsigned char *buf = NULL;
82 while (total-- > 1)
84 Uchar(total, &buf);
85 printf("%s\n", buf);
86 free(buf);
87 buf = NULL;
92 void
93 Uchar(int amount, unsigned char **buf)
95 /* register unsigned char *c_pass; */
96 unsigned int c_pass_count = 0;
97 unsigned int cursor = 0;
98 register int total = amount;
100 if (amount <= 0 || *buf) /* to avoid mem leaks we leave if buf is not NULL */
101 return;
103 while (total-- > 0)
105 core_Uchar(buf, &c_pass_count, &cursor);