/* Parse PGN tags; returns 0 for success or error number */ int ParsePGNTag (char *tag, GameInfo *gameInfo) { char *name, *value, *p, *oldTags; int len; int success; name = tag; while (!isalpha(*name) && !isdigit(*name)) { name++; } p = name; while (*p != ' ' && *p != '\t' && *p != '\n') { p++; } *p = NULLCHAR; value = strchr(p + 1, '"') + 1; p = strrchr(value, '"'); *p = NULLCHAR; if (StrCaseCmp(name, "Event") == 0) { success = StrSavePtr(value, &gameInfo->event) != NULL; } else if (StrCaseCmp(name, "Site") == 0) { success = StrSavePtr(value, &gameInfo->site) != NULL; } else if (StrCaseCmp(name, "Date") == 0) { success = StrSavePtr(value, &gameInfo->date) != NULL; } else if (StrCaseCmp(name, "Round") == 0) { success = StrSavePtr(value, &gameInfo->round) != NULL; } else if (StrCaseCmp(name, "White") == 0) { success = StrSavePtr(value, &gameInfo->white) != NULL; } else if (StrCaseCmp(name, "Black") == 0) { success = StrSavePtr(value, &gameInfo->black) != NULL; } /* Fold together the various ways of denoting White/Black rating */ else if ((StrCaseCmp(name, "WhiteElo")==0) || (StrCaseCmp(name, "WhiteUSCF")==0) ) { success = TRUE; gameInfo->whiteRating = atoi( value ); } else if ((StrCaseCmp(name, "BlackElo")==0) || (StrCaseCmp(name, "BlackUSCF")==0)) { success = TRUE; gameInfo->blackRating = atoi( value ); } else if (StrCaseCmp(name, "Result") == 0) { if (strcmp(value, "1-0") == 0) gameInfo->result = WhiteWins; else if (strcmp(value, "0-1") == 0) gameInfo->result = BlackWins; else if (strcmp(value, "1/2-1/2") == 0) gameInfo->result = GameIsDrawn; else gameInfo->result = GameUnfinished; success = TRUE; } else if (StrCaseCmp(name, "TimeControl") == 0) { // int tc, mps, inc = -1; // if(sscanf(value, "%d/%d", &mps, &tc) == 2 || ) success = StrSavePtr(value, &gameInfo->timeControl) != NULL; } else if (StrCaseCmp(name, "FEN") == 0) { success = StrSavePtr(value, &gameInfo->fen) != NULL; } else if (StrCaseCmp(name, "SetUp") == 0) { /* ignore on input; presence of FEN governs */ success = TRUE; } else if (StrCaseCmp(name, "Variant") == 0) { /* xboard-defined extension */ success = StrSavePtr(value, &gameInfo->variantName) != NULL; if(*value && strcmp(value, engineVariant)) // keep current engine-defined variant if it matches gameInfo->variant = StringToVariant(value); } else if (StrCaseCmp(name, "VariantMen") == 0) { /* for now ignore this tag, as we have no method yet */ /* for assigning the pieces to XBoard pictograms */ success = TRUE; } else if (StrCaseCmp(name, PGN_OUT_OF_BOOK) == 0) { /* [AS] Out of book annotation */ success = StrSavePtr(value, &gameInfo->outOfBook) != NULL; } else { if (gameInfo->extraTags == NULL) { oldTags = ""; } else { oldTags = gameInfo->extraTags; } /* Buffer size includes 7 bytes of space for [ ""]\n\0 */ len = strlen(oldTags) + strlen(value) + strlen(name) + 7; if ((p = (char *) malloc(len)) != NULL) { sprintf(p, "%s[%s \"%s\"]\n", oldTags, name, value); if (gameInfo->extraTags != NULL) free(gameInfo->extraTags); gameInfo->extraTags = p; success = TRUE; } else { success = FALSE; } } return(success ? 0 : ENOMEM); }
int main( int argc, char *argv[] ) { int opt, rc; char *exename; fstring opname; load_case_tables(); opt_debug = 0; /* todo set this from getopts */ lp_load( dyn_CONFIGFILE, True, False, False, True); exename = argv[0]; /* default */ fstrcpy( opname, "write" ); /* the default */ #if 0 /* TESTING CODE */ eventlog_add_source( "System", "TestSourceX", "SomeTestPathX" ); #endif while ( ( opt = getopt( argc, argv, "dho:" ) ) != EOF ) { switch ( opt ) { case 'o': fstrcpy( opname, optarg ); break; case 'h': usage( exename ); display_eventlog_names( ); exit( 0 ); break; case 'd': opt_debug = 1; break; } } argc -= optind; argv += optind; if ( argc < 1 ) { printf( "\nNot enough arguments!\n" ); usage( exename ); exit( 1 ); } /* note that the separate command types should call usage if they need to... */ while ( 1 ) { if ( !StrCaseCmp( opname, "addsource" ) ) { rc = DoAddSourceCommand( argc, argv, opt_debug, exename ); break; } if ( !StrCaseCmp( opname, "write" ) ) { rc = DoWriteCommand( argc, argv, opt_debug, exename ); break; } printf( "unknown command [%s]\n", opname ); usage( exename ); exit( 1 ); break; } return rc; }