src/route.c
// Read the config and init the global routes, return the number of runs
Read the config and init the global routes, return the number of runs
Read the config and init the global routes return the number of runs
void init_global_routes(const char* routes_fn) {
    char line[MAX_ROUTE_CHARS]; 
    char stops[MAX_NUM_STOPS][MAX_NUM_STOP_CHARS]; 
    int times[MAX_NUM_STOPS]; 
    int start_time = 0;
    int end_time = 0;
    char* curr;
    char* newline;
    int route_steps = 0;
    bool start_line = false;
    bool count_line = false;
    int total_routes = 0;
    int curr_route = 0;
    int stop_counter;
    char *line_holder, *end, *token;
    int i;
    FILE* dat_file = fopen(routes_fn, "r");
    if (dat_file == NULL) {
        perror("Failed to open routes file!");
        exit(-1);
    }
    while (fgets(line, sizeof(line), dat_file)) {
        if (start_line == false) {
            start_line = true;
            g_start_time = atoi(line);
            continue;
        }
        if (count_line == false) {
            count_line = true;
            total_routes = atoi(line);
            route_list = (route_t* )calloc(total_routes, sizeof(route_t));
            continue;
        }
        newline = strchr(line, '\n');
        if (newline != NULL) {
            *newline = 0;
        }
        stop_counter = 0;
        curr = strtok(line, " ");
        while (curr != NULL) {
            line_holder = end = strdup(curr);
            token = strsep(&end, ",");
            strcpy(stops[stop_counter], token);
            token = strsep(&end, ",");
            times[stop_counter] = atoi(token);
            if (start_time == 0) {
                start_time = atoi(token);
            }
            end_time = atoi(token);
            free(line_holder);
            stop_counter++;
            curr = strtok(NULL, " ");
        }
        stop_counter--;
        (route_list[curr_route]).start_time = start_time;
        (route_list[curr_route]).end_time = end_time;
        (route_list[curr_route]).length = stop_counter + 1;
        (route_list[curr_route]).stops = (char**)calloc(stop_counter +1, sizeof(char*));
        (route_list[curr_route]).route = (int*)calloc(stop_counter +1, sizeof(int));
        (route_list[curr_route]).stop_time= (int*)calloc(stop_counter +1, sizeof(int));
        for(i=0; i < stop_counter+1; i++) {
            (route_list[curr_route]).stops[i] = (char *)calloc(strlen(stops[i]) + 1, sizeof(char)); 
            strcpy((route_list[curr_route]).stops[i], stops[i]);
            (route_list[curr_route]).stop_time[i] = times[i];
            (route_list[curr_route]).route[i] = sta_id_lookup(stops[i]);
        }
        (route_list[curr_route]).origin = (route_list[curr_route]).route[0];
        (route_list[curr_route]).start_dir = (route_list[curr_route]).route[1] - (route_list[curr_route]).route[0];
        (route_list[curr_route]).terminal = (route_list[curr_route]).route[stop_counter];
        (route_list[curr_route]).prev_route = NULL;
        (route_list[curr_route]).next_route = NULL;
        start_time = 0;
        curr_route++;
    }
    g_total_routes = total_routes;
    g_total_transit_units = allocate_transit_units();
    return;
}
void init_global_routes ( const char * routes_fn ) { char line [ MAX_ROUTE_CHARS ] ; char stops [ MAX_NUM_STOPS ] [ MAX_NUM_STOP_CHARS ] ; int times [ MAX_NUM_STOPS ] ; int start_time = 0 ; int end_time = 0 ; char * curr ; char * newline ; int route_steps = 0 ; bool start_line = false ; bool count_line = false ; int total_routes = 0 ; int curr_route = 0 ; int stop_counter ; char * line_holder , * end , * token ; int i ; FILE * dat_file = fopen ( routes_fn , " " ) ; if ( dat_file == NULL ) { perror ( " " ) ; exit ( -1 ) ; } while ( fgets ( line , sizeof ( line ) , dat_file ) ) { if ( start_line == false ) { start_line = true ; g_start_time = atoi ( line ) ; continue ; } if ( count_line == false ) { count_line = true ; total_routes = atoi ( line ) ; route_list = ( route_t * ) calloc ( total_routes , sizeof ( route_t ) ) ; continue ; } newline = strchr ( line , ' \n ' ) ; if ( newline != NULL ) { * newline = 0 ; } stop_counter = 0 ; curr = strtok ( line , " " ) ; while ( curr != NULL ) { line_holder = end = strdup ( curr ) ; token = strsep ( & end , " " ) ; strcpy ( stops [ stop_counter ] , token ) ; token = strsep ( & end , " " ) ; times [ stop_counter ] = atoi ( token ) ; if ( start_time == 0 ) { start_time = atoi ( token ) ; } end_time = atoi ( token ) ; free ( line_holder ) ; stop_counter ++ ; curr = strtok ( NULL , " " ) ; } stop_counter -- ; ( route_list [ curr_route ] ) . start_time = start_time ; ( route_list [ curr_route ] ) . end_time = end_time ; ( route_list [ curr_route ] ) . length = stop_counter + 1 ; ( route_list [ curr_route ] ) . stops = ( char * * ) calloc ( stop_counter + 1 , sizeof ( char * ) ) ; ( route_list [ curr_route ] ) . route = ( int * ) calloc ( stop_counter + 1 , sizeof ( int ) ) ; ( route_list [ curr_route ] ) . stop_time = ( int * ) calloc ( stop_counter + 1 , sizeof ( int ) ) ; for ( i = 0 ; i < stop_counter + 1 ; i ++ ) { ( route_list [ curr_route ] ) . stops [ i ] = ( char * ) calloc ( strlen ( stops [ i ] ) + 1 , sizeof ( char ) ) ; strcpy ( ( route_list [ curr_route ] ) . stops [ i ] , stops [ i ] ) ; ( route_list [ curr_route ] ) . stop_time [ i ] = times [ i ] ; ( route_list [ curr_route ] ) . route [ i ] = sta_id_lookup ( stops [ i ] ) ; } ( route_list [ curr_route ] ) . origin = ( route_list [ curr_route ] ) . route [ 0 ] ; ( route_list [ curr_route ] ) . start_dir = ( route_list [ curr_route ] ) . route [ 1 ] - ( route_list [ curr_route ] ) . route [ 0 ] ; ( route_list [ curr_route ] ) . terminal = ( route_list [ curr_route ] ) . route [ stop_counter ] ; ( route_list [ curr_route ] ) . prev_route = NULL ; ( route_list [ curr_route ] ) . next_route = NULL ; start_time = 0 ; curr_route ++ ; } g_total_routes = total_routes ; g_total_transit_units = allocate_transit_units ( ) ; return ; }
Read the config and init the global routes, return the number of runs
Read the config and init the global routes return the number of runs
//XXX DANGEROUS, assumes shortish lines // XXX less but STILL DANGEROUS // XXX less but STILL DANGEROUS // TODO: Add the path as a proper arg // Let's read the file! // If its the first line, it's the global start time, grab that // Get the number of routes we'll have // Go ahead and allocate space for those bad boys // Ok now, we are parsing the regular lines // Strip the newline // 0 out the stop counter // Now tokenize and get the path // Process the current line // That first one is the station // That second one is the time // Clean up our strsep mess // Bump the counter // Get the next one // On the last interation, we dont move to next stop // Ok, we've copied all the stops into a temp array, now go ahead and // put together a global list for init // Make some space for that name // Copy it // Stash the times too // Make an array of the ID mapped versions // Set the origin and terminal based on what we were given // Clear out start time for the next one // Bump the index for the current route