Example #1
0
int main(void)
{
    char line[10];
    unsigned paper = 0, ribbon = 0;
    struct box b;

    while(fgets(line, 10, stdin))
    {
        if (!parse_box(line, &b))
        {
            perror("Cannot parse box dimensions");
            return EXIT_FAILURE;
        }

        paper += paper_area(&b);
        ribbon += ribbon_len(&b);
    }

    if (ferror(stdin))
    {
        perror("Cannot read input");
        return EXIT_FAILURE;
    }

    printf("Total area of paper required: %u ft\n", paper);
    printf("Total length of ribbon required: %u ft\n", ribbon);

    return EXIT_SUCCESS;
}
Example #2
0
// core_load_level
// reads contents of file, adds corresponding cpShapes to cpSpace
// returns EXIT_SUCCESS if level loaded successfully
// returns EXIT_FAILURE if level loaded unsuccessfully (bad syntax)
static int core_load_level_parse ( cpSpace *space, FILE * fp ) { 
    
    int max_line_size = 40; // maximum length of line that we want to read in
    
    // array to hold contents of each line
    char line [max_line_size];
    char * line_place = line; // holds address of start of line
    
    fgets ( line, max_line_size, fp );
    
    // check to see that first line has correct content
    if( strncmp ( line, "gravity ", 8 ) != 0 ) return EXIT_FAILURE;
    
    line_place = &line[8]; // advance line to next chars to be read
    double gravity = atof ( line_place ); // saves gravity input into a double
    core_set_gravity ( space, gravity ); // set gravity
    
    // check that next line is blank
    fgets ( line, max_line_size, fp );
    
    int exit = 1; // keeps track of whether to continue reading lines
    
    while ( exit == 1 ) {
        
        if ( fgets ( line, max_line_size, fp ) == 0 ) return EXIT_SUCCESS; // get next line.  If EOF, return
    
        if ( strncmp ( line, "box STATIC", 10 ) == 0 ) { // static box information to follow
            Box * box = box_new();
            
            if ( parse_box ( fp, &(box->x), &(box->y), &(box->width), &(box->height), &(box->angle), (box->color), &(box->friction), &(box->elasticity), &(box->density)) == EXIT_FAILURE ) {
                
                fprintf ( stderr, "Parsing error in box STATIC\n" );
                box_destroy ( box );
                return EXIT_FAILURE;
            };
            
            // add static box to space and destroy temporary box data 
            core_add_static_box_shape ( space, box );
            box_destroy ( box );
            
        } else if ( strncmp ( line, "box NONSTATIC", 13 ) == 0 ) {
            
            Box * box = box_new();
            
            if( parse_box ( fp, &(box->x), &(box->y), &(box->width), &(box->height), &(box->angle), (box->color), &(box->friction), &(box->elasticity), &(box->density) ) == EXIT_FAILURE ) {
                
                fprintf ( stderr, "Parsing error in box NONSTATIC\n" );
                box_destroy ( box );
                return EXIT_FAILURE;
            };
            
            // need to add mass, inertia since body moves
            core_add_box_shape ( space, box, 0 );
            
            box_destroy ( box );
            
        } else if ( strncmp ( line, "ball NONSTATIC", 14 ) == 0 ) {
            
            Circle * circ = circle_new ();
            
            if ( parse_ball ( fp, &(circ->x), &(circ->y), &(circ->radius), &(circ->angle), circ->color, &(circ->friction), &(circ->elasticity), &(circ->density)) == EXIT_FAILURE ) {
                
                fprintf ( stderr, "Parsing error in ball NONSTATIC\n" );
                circle_destroy ( circ );
                return EXIT_FAILURE;
            }
            
            // need to add mass, inertia since body moves
            core_add_circle_shape ( space, circ, 0 );
            circle_destroy ( circ );
            
        } else if ( strncmp ( line, "ball STATIC", 11) == 0 ) {
            
            Circle * circ = circle_new();
            
            if ( parse_ball ( fp, &(circ->x), &(circ->y), &(circ->radius), &(circ->angle), circ->color, &(circ->friction), &(circ->elasticity), &(circ->density) ) == EXIT_FAILURE ) {
                
                fprintf ( stderr, "Parsing error in ball STATIC\n" );
                circle_destroy ( circ );
                return EXIT_FAILURE;
            }
            
            core_add_static_circle_shape ( space, circ );
            circle_destroy ( circ );
  
        } else  // invalid input
            return EXIT_FAILURE;
        
        
        if ( fgets ( line, max_line_size, fp ) == NULL ) exit = 0; // get next line.  If EOF, get out
    }
    
    cpSpaceEachBody ( space, &core_update_body, (void *) NULL );
    
    return EXIT_SUCCESS;
}