1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/***************************************************************************
main.c - description
-------------------
begin : Wed Mar 21 21:39:53 EST 2001
copyright : (C) 2001 by Haavard Froeiland 2264228
email : havard@student.unsw.edu.au
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#define max 50
typedef struct WordNode *WordList;
struct WordNode
{
char *word;
struct WordNode *next;
};
void *addWord(WordList first, char w[])
{
struct WordNode *ptr;
ptr=malloc(sizeof(struct WordNode));
ptr->word=strdup(w);
ptr->next=first;
return ptr;
}
void printWordList(WordList l)
{
struct WordNode *ptr=l;
/* loop throug the linked list */
while(ptr!=NULL)
{
printf("%s\n",ptr->word);
ptr=ptr->next;
}
}
void printLevel(FILE *f,char *l[])
{
int pos=0;
int line=0;
int arrayMax=0;
int arrayPos=0;
while(l[arrayMax]!=NULL)
{
arrayMax++;
}
if (arrayMax == 0) return;
/* loop throug the linked list */
while(line<30)
{
arrayPos=((float)rand()/RAND_MAX)*arrayMax;
pos=pos+strlen(l[arrayPos]);
fprintf(f,"%s",l[arrayPos]);
if(pos>40)
{
fprintf(f,"\n");
pos=0;
line++;
}
else
{
pos++;
fprintf(f," ");
}
}
}
void creatLevelList(WordList l,char *levelList[],char s_or[], char s_and[])
{
struct WordNode *ptr=l;
int i;
int j;
int pos=0;
bool or_failed;
bool and_failed;
/* loop throug the linked list */
while(ptr!=NULL)
{
/* loop through the word */
/* printf("%s\n",ptr->word); */
i=strlen(ptr->word)-1;
for(;i>=0;i--)
{
or_failed=true;
j=strlen(s_or)-1;
for(;j>=0;j--)
{
if(ptr->word[i]==s_or[j])
{
or_failed=false;
j=-1;
}
}
and_failed=true;
j=strlen(s_and)-1;
for(;j>=0;j--)
{
if(ptr->word[i]==s_and[j])
{
and_failed=false;
j=-1;
}
}
if(or_failed==true)
{
i=-1;
}
}
if(or_failed==false && and_failed==false)
{
levelList[pos]=ptr->word;
pos++;
/* printf("%d\n",pos); */
}
ptr=ptr->next;
}
levelList[pos]=NULL;
}
int main(int argc, char *argv[])
{
time_t lt;
FILE *file;
WordList list=NULL;
char *levelList[100000];
char word[max];
char test[100];
int i=0;
char *s[50];
if(argc<4)
{
printf("\nUsage: ktouchgen ConfigFile WordFile TrainingFile\n");
exit(0);
}
/**
* Read in the configFile
*
*/
if((file = fopen(argv[1],"r"))==NULL)
{
printf("can't open config_file:%s for reading",argv[2]);
}
i=0;
while(!feof(file))
{
fscanf(file,"%s",word);
s[i]=strdup(word);
printf("%s\n",s[i]);
i++;
}
s[i]=NULL;
fclose(file);
/**
* Read in the wordFile and add each word to the list
*
*/
printf("Starting reading words");
if((file = fopen(argv[2],"r"))==NULL)
{
printf("can't open word_file:%s for reading",argv[2]);
}
while(!feof(file))
{
fscanf(file,"%s",word);
list=addWord(list, word);
}
fclose(file);
if((file = fopen(argv[3],"w"))==NULL)
{
printf("Error when writing to file:%s",argv[3]);
}
lt = time(NULL);
fprintf(file,"#############################################################\n");
fprintf(file,"# Rrainingfile genereated %s",ctime(<));
fprintf(file,"# Program written by Håvard Frøiland\n");
fprintf(file,"#############################################################\n\n");
strcpy(test,"");
i=0;
while(s[i]!=NULL)
{
if (strlen(test) + strlen(s[i]) + 1 > sizeof(test))
{
printf("Buffer overflow.\n");
exit(1);
}
strcat(test,s[i]);
fprintf(file,"# Level %d\n",i+1);
fprintf(file,"%s\n", s[i]);
creatLevelList(list,levelList,test,s[i]);
printLevel(file,levelList);
fprintf(file,"\n");
i++;
}
fclose(file);
return EXIT_SUCCESS;
}
|