00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00037 #include <string.h>
00038 #include <malloc.h>
00039 #include <stdlib.h>
00040 #include "xmlutils.hpp"
00041 #include "book1.hpp"
00042 #include "dice.hpp"
00043
00044 static char *opts[]={
00045 "--random",
00046 "--army",
00047 "--navy",
00048 "--marines",
00049 "--scouts",
00050 "--merchants",
00051 "--other",
00052 "--all",
00053 "--xml-out",
00054 "--count",
00055 (char*)0
00056 };
00057
00058 static int enabled[6]={0,0,0,0,0,0};
00059 static int randomGen=0;
00060 static char* xmlFileName = (char*)0;
00061 static int xmlOut=0;
00062 int *enabledServices;
00063 int enabledCount;
00064 int genCount=10;
00065 int setFilename=0;
00066 int setCount=0;
00067
00068
00069 int matchOption(char *p){
00070 int ix=0;
00071 while(opts[ix] != (char*)0){
00072 if (strcmp(p,opts[ix])== 0) return ix;
00073 ix++;
00074 }
00075 return -1;
00076 }
00077
00078
00079 void processCommandLine(int argc,char** argv){
00080 #ifdef DEBUG_ON
00081 fprintf(stderr,"Entering ::processCommandLine(int,char**)\n");
00082 fprintf(stderr,"\t\tARGC: %d\n",argc);
00083 #endif
00084
00085 int ix;
00086 for(ix=1;ix<argc;ix++){
00087 if ( ! ( (setCount == 1 ) || ( setFilename==1)) ){
00088 #ifdef DEBUG_ON
00089 fprintf(stderr,"Processing argument %d [\"%s\"]\n",ix,argv[ix]);
00090 #endif
00091 int opt_num=matchOption(argv[ix]);
00092 #ifdef DEBUG_ON
00093 fprintf(stderr,"Matched option #%d\n",opt_num);
00094 #endif
00095 if ( opt_num < 0 ) {
00096 fprintf(stderr,"could not match \"%s\" as an option\n",argv[ix]);
00097 exit(-1);
00098 }
00099
00100 switch(opt_num){
00101 case 0:
00102 randomGen=1;
00103 break;
00104 case 1:
00105 case 2:
00106 case 3:
00107 case 4:
00108 case 5:
00109 case 6:
00110 enabled[opt_num-1]=1;
00111 break;
00112 case 7:
00113 int iy;
00114 for(iy=0;iy<6;iy++)
00115 enabled[iy]=1;
00116 break;
00117 case 8:
00118 fprintf(stderr,"Setting XMLout\n");
00119 xmlOut=1;
00121 setFilename=1;
00122 break;
00123 case 9:
00124 setCount=1;
00125 break;
00126 };
00127
00128
00129 } else {
00130 if ( setFilename == 1 ) {
00131 setFilename=0;
00132 xmlFileName=(char*) malloc(strlen(argv[ix])+1);
00133 strcpy(xmlFileName,argv[ix]);
00134 }
00135 if ( setCount== 1) {
00136 genCount=atoi(argv[ix]);
00137 setCount =0;
00138
00139 }
00140
00141
00142 }
00143 }
00144 #ifdef DEBUG_ON
00145 fprintf(stderr,"Exiting ::processCommandLine(int,char**)\n");
00146 #endif
00147
00148 }
00149
00150 int getEnabledCount(){
00151 int ix;
00152 int count=0;
00153
00154 for(ix=0;ix<6;ix++)
00155 if ( enabled[ix] == 1 )
00156 count++;
00157
00158 return count;
00159 }
00160
00161 void makeFlagList(){
00162 int ix;
00163 int iy=0;
00164
00165 #ifdef DEBUG_ON
00166 fprintf(stderr,"Entering ::makeFlagList()\n");
00167 #endif
00168
00169 enabledServices = (int*)malloc(enabledCount);
00170 for(ix=0;ix<6;ix++){
00171 if ( enabled[ix] == 1 ) {
00172 enabledServices[iy++]=ix;
00173 }
00174
00175 }
00176 #ifdef DEBUG_ON
00177 fprintf(stderr,"Exiting ::makeFlagList()\n");
00178 #endif
00179
00180 }
00181
00182 book1Person* getNewPerson(){
00183 book1Person *p;
00184
00185 if ( (randomGen == 1) || (enabledCount == 6 ) ) {
00186 switch(d1d6()){
00187 case 1:
00188 p = new b1NavyPerson();
00189 break;
00190 case 2:
00191 p = new b1MarinePerson();
00192 break;
00193 case 3:
00194 p = new b1ArmyPerson();
00195 break;
00196 case 4:
00197 p= new b1ScoutPerson();
00198 break;
00199 case 5:
00200 p = new b1MerchantPerson();
00201 break;
00202 case 6:
00203 p = new b1OtherPerson();
00204 break;
00205 };
00206
00207 } else {
00208
00209 int iy = randxInRange(0,enabledCount-1);
00210
00211
00212
00213
00214
00215
00216
00217 switch(enabledServices[iy]){
00218 case 0:
00219 p=new b1ArmyPerson();
00220 break;
00221 case 1:
00222 p=new b1NavyPerson();
00223 break;
00224 case 2:
00225 p=new b1MarinePerson();
00226 break;
00227 case 3:
00228 p=new b1ScoutPerson();
00229 break;
00230 case 4:
00231 p=new b1MerchantPerson();
00232 break;
00233 case 5:
00234 p=new b1OtherPerson();
00235 break;
00236 };
00237
00238 }
00239
00240 if ( p->getAccepted() && p->getAlive() )
00241 return p;
00242 else
00243 return getNewPerson();
00244 }
00245
00246 int
00247 main(int argc,char** argv){
00248
00249 int ix;
00250 FILE* fout;
00251
00252 processCommandLine(argc,argv);
00253
00254 enabledCount=getEnabledCount();
00255 if ( enabledCount < 6 ) {
00256 makeFlagList();
00257 }
00258
00259 if ( xmlOut == 1 ) {
00260 fprintf(stderr,"We are going to output to an XML file.\n");
00264 if ( xmlFileName != (char*)0 ) {
00265 fout = fopen(xmlFileName,"w");
00266 if ( fout == NULL ) {
00267 fprintf(stderr,"Could not open %s for writing.\n",xmlFileName);
00268 exit(-1);
00269 }
00270 } else {
00271 fout=stdout;
00272 }
00273 fprintf(fout,"%s\n","<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
00274 fprintf(fout,"<!DOCTYPE person-list \"http://frpuniverse.sourceforge.net/xml/person-list.dtd\">\n");
00275
00276
00277 fprintf(fout,"%s\n",createStartElement("person-list"));
00278
00279 }
00280
00281
00282
00283 for(ix=0;ix<genCount;ix++){
00284 book1Person* p=getNewPerson();
00285 if ( xmlOut == 1 ) {
00286 p->XMLout(fout);
00287 } else {
00288 p->toStdOut();
00289 }
00290
00291
00292 }
00293
00294 if ( xmlOut == 1 ) {
00295 fprintf(fout,"%s\n",createEndElement("person-list"));
00296 }
00297
00298 return 0;
00299 }