moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / ktouch / extras / training-gen / c / ktouchgen.c
blobdbb2dcf2706d275ba8e4cb98ad4bd9d93c131c4e
1 /***************************************************************************
2 main.c - description
3 -------------------
4 begin : Wed Mar 21 21:39:53 EST 2001
5 copyright : (C) 2001 by Haavard Froeiland 2264228
6 email : havard@student.unsw.edu.au
8 ***************************************************************************/
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 #include <ctype.h>
19 #include <time.h>
21 #define max 50
23 typedef struct WordNode *WordList;
25 struct WordNode
27 char *word;
28 struct WordNode *next;
31 void *addWord(WordList first, char w[])
33 struct WordNode *ptr;
35 ptr=malloc(sizeof(struct WordNode));
36 ptr->word=strdup(w);
37 ptr->next=first;
38 return ptr;
41 void printWordList(WordList l)
43 struct WordNode *ptr=l;
44 /* loop throug the linked list */
45 while(ptr!=NULL)
47 printf("%s\n",ptr->word);
48 ptr=ptr->next;
52 void printLevel(FILE *f,char *l[])
54 int pos=0;
55 int line=0;
56 int arrayMax=0;
57 int arrayPos=0;
59 while(l[arrayMax]!=NULL)
61 arrayMax++;
64 if (arrayMax == 0) return;
65 /* loop throug the linked list */
66 while(line<30)
68 arrayPos=((float)rand()/RAND_MAX)*arrayMax;
69 pos=pos+strlen(l[arrayPos]);
70 fprintf(f,"%s",l[arrayPos]);
71 if(pos>40)
73 fprintf(f,"\n");
74 pos=0;
75 line++;
77 else
79 pos++;
80 fprintf(f," ");
86 void creatLevelList(WordList l,char *levelList[],char s_or[], char s_and[])
88 struct WordNode *ptr=l;
89 int i;
90 int j;
91 int pos=0;
92 bool or_failed;
93 bool and_failed;
95 /* loop throug the linked list */
96 while(ptr!=NULL)
98 /* loop through the word */
99 /* printf("%s\n",ptr->word); */
100 i=strlen(ptr->word)-1;
101 for(;i>=0;i--)
104 or_failed=true;
105 j=strlen(s_or)-1;
106 for(;j>=0;j--)
108 if(ptr->word[i]==s_or[j])
110 or_failed=false;
111 j=-1;
115 and_failed=true;
116 j=strlen(s_and)-1;
117 for(;j>=0;j--)
119 if(ptr->word[i]==s_and[j])
121 and_failed=false;
122 j=-1;
126 if(or_failed==true)
128 i=-1;
131 if(or_failed==false && and_failed==false)
133 levelList[pos]=ptr->word;
134 pos++;
135 /* printf("%d\n",pos); */
138 ptr=ptr->next;
140 levelList[pos]=NULL;
144 int main(int argc, char *argv[])
146 time_t lt;
147 FILE *file;
148 WordList list=NULL;
149 char *levelList[100000];
150 char word[max];
151 char test[100];
152 int i=0;
153 char *s[50];
155 if(argc<4)
157 printf("\nUsage: ktouchgen ConfigFile WordFile TrainingFile\n");
158 exit(0);
162 * Read in the configFile
165 if((file = fopen(argv[1],"r"))==NULL)
167 printf("can't open config_file:%s for reading",argv[2]);
169 i=0;
170 while(!feof(file))
172 fscanf(file,"%s",word);
174 s[i]=strdup(word);
175 printf("%s\n",s[i]);
176 i++;
178 s[i]=NULL;
179 fclose(file);
184 * Read in the wordFile and add each word to the list
187 printf("Starting reading words");
188 if((file = fopen(argv[2],"r"))==NULL)
190 printf("can't open word_file:%s for reading",argv[2]);
192 while(!feof(file))
194 fscanf(file,"%s",word);
195 list=addWord(list, word);
197 fclose(file);
200 if((file = fopen(argv[3],"w"))==NULL)
202 printf("Error when writing to file:%s",argv[3]);
205 lt = time(NULL);
206 fprintf(file,"#############################################################\n");
207 fprintf(file,"# Rrainingfile genereated %s",ctime(&lt));
208 fprintf(file,"# Program written by Håvard Frøiland\n");
209 fprintf(file,"#############################################################\n\n");
211 strcpy(test,"");
213 i=0;
214 while(s[i]!=NULL)
216 if (strlen(test) + strlen(s[i]) + 1 > sizeof(test))
218 printf("Buffer overflow.\n");
219 exit(1);
221 strcat(test,s[i]);
222 fprintf(file,"# Level %d\n",i+1);
223 fprintf(file,"%s\n", s[i]);
224 creatLevelList(list,levelList,test,s[i]);
225 printLevel(file,levelList);
226 fprintf(file,"\n");
227 i++;
229 fclose(file);
231 return EXIT_SUCCESS;