Skip to content

Commit

Permalink
add team::verify_pos, enforce valid positions
Browse files Browse the repository at this point in the history
  • Loading branch information
seansweda committed Mar 28, 2017
1 parent 507e3ff commit 63e54e1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
66 changes: 62 additions & 4 deletions team.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,23 @@ team::pos_change( int spot, char **comment )
while ( oldpl->next && oldpl->next->ord <= spot )
oldpl = oldpl->next;

fprintf( output, "Enter new position for %d: ", spot );
memset( pos, '\0', MAX_INPUT );
fgets( pos, MAX_INPUT, input );
sanitize( &pos, POSLEN );
int loop = 1;
while ( loop ) {
fprintf( output, "Enter new position for %d: ", spot );
memset( pos, '\0', MAX_INPUT );
fgets( pos, MAX_INPUT, input );
sanitize( &pos, POSLEN );

if ( verify_pos( pos ) == 0 ) {
fprintf( stderr, "invalid position: %s\n", pos );
if ( input != stdin )
exit(1);
else
continue;
}
// if we get here it's all good
break;
}

#ifdef DEBUG
fprintf( stderr, "pos_change %d: %s\n", spot, pos );
Expand Down Expand Up @@ -180,6 +193,13 @@ team::insert( int spot, char **comment, const char *def, const char *str )
else
continue;
}
if ( verify_pos( pos ) == 0 ) {
fprintf( stderr, "invalid position: %s\n", pos );
if ( input != stdin )
exit(1);
else
continue;
}
// if we get here it's all good
fprintf( output, "\n" );
break;
Expand Down Expand Up @@ -298,6 +318,13 @@ team::make_lineups()
else
continue;
}
if ( verify_pos( pos ) == 0 ) {
fprintf( stderr, "invalid position: %s\n", pos );
if ( input != stdin )
exit(1);
else
continue;
}
// if we get here it's all good
break;
}
Expand Down Expand Up @@ -674,6 +701,37 @@ team::check_defense()
fprintf(output,"defense missing:%s\n", missing);
}

}

int
team::verify_pos( const char *str )
{
// return 0 if invalid
// return 1 if valid

// valid positions
const char* valid_pos[] = {
"p", "P",
"c", "C",
"1b", "1B",
"2b", "2B",
"3b", "3B",
"ss", "SS",
"lf", "LF",
"cf", "CF",
"rf", "RF",
"dh", "DH",
"ph", "PH",
"pr", "PR"
};

int i, tot;
tot = sizeof valid_pos / sizeof *valid_pos;
for( i = 0; i != tot; i++ )
if ( strcmp( str, valid_pos[i] ) == 0 )
return( 1 );

return( 0 );
}

int
Expand Down
1 change: 1 addition & 0 deletions team.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public :

void print_lineup(void);
void check_defense(void);
int verify_pos(const char*);
char *posout(int);
char *nout();
player *up();
Expand Down

0 comments on commit 63e54e1

Please sign in to comment.