int read_seq_write_rand(command_list *cl, DCB_registered_src *r_src, unsigned char is_overlay, cfile *out_cfh, unsigned long buf_size) { unsigned char *buf; unsigned char *p; unsigned long x, start=0, end=0, len=0; unsigned long max_pos = 0, pos = 0; unsigned long offset; signed long tmp_len; dcb_src_read_func read_func; cfile_window *cfw; u_dcb_src u_src; #define END_POS(x) ((x).src_pos + (x).len) pos = 0; max_pos = 0; if(is_overlay) { read_func = r_src->mask_read_func; } else { read_func = r_src->read_func; } assert(read_func != NULL); u_src = r_src->src_ptr; if(0 != cseek(u_src.cfh, 0, CSEEK_FSTART)) { ap_printf("cseeked failed: bailing, io_error 0\n"); return IO_ERROR; } if((buf = (unsigned char *)malloc(buf_size)) == NULL) { return MEM_ERROR; } // we should *never* go backwards u_src.cfh->state_flags |= CFILE_FLAG_BACKWARD_SEEKS; while(start < cl->com_count) { if(pos < cl->full_command[start].src_pos) { pos = cl->full_command[start].src_pos; max_pos = END_POS(cl->full_command[start]); } else { while(start < cl->com_count && pos > cl->full_command[start].src_pos) { start++; } if(start == cl->com_count) continue; pos = cl->full_command[start].src_pos; max_pos = MAX(max_pos, END_POS(cl->full_command[start])); } if(end < start) { end = start; } while(end < cl->com_count && cl->full_command[end].src_pos < max_pos) { max_pos = MAX(max_pos, END_POS(cl->full_command[end])); end++; } if(pos == max_pos) { continue; } while(pos < max_pos) { len = MIN(max_pos - pos, buf_size); x = read_func(u_src, pos, buf, len); // if(len < max_pos - pos) // v0printf("buffered %lu, max was %lu\n", len, max_pos - pos); if(len != x){ ap_printf("x=%lu, pos=%lu, len=%lu\n", x, pos, len); ap_printf("bailing, io_error 2\n"); free(buf); return IO_ERROR; } for(x=start; x < end; x++) { offset = MAX(cl->full_command[x].src_pos, pos); tmp_len = MIN(END_POS(cl->full_command[x]), pos + len) - offset; if(tmp_len > 0) { if(cl->full_command[x].ver_pos + (offset - cl->full_command[x].src_pos) != cseek(out_cfh, cl->full_command[x].ver_pos + (offset - cl->full_command[x].src_pos), CSEEK_FSTART)) { ap_printf("bailing, io_error 3\n"); free(buf); return IO_ERROR; } if(is_overlay) { p = buf + offset - pos; cfw = expose_page(out_cfh); if(cfw->write_end == 0) { cfw->write_start = cfw->pos; } while(buf + offset - pos + tmp_len > p) { if(cfw->pos == cfw->end) { cfw->write_end = cfw->end; cfw = next_page(out_cfh); if(cfw->end == 0) { ap_printf("bailing from applying overlay mask in read_seq_writ_rand\n"); free(buf); return IO_ERROR; } } cfw->buff[cfw->pos] += *p; p++; cfw->pos++; } cfw->write_end = cfw->pos; } else { if(tmp_len != cwrite(out_cfh, buf + offset - pos, tmp_len)) { ap_printf("bailing, io_error 4\n"); free(buf); return IO_ERROR; } } } } pos += len; } } u_src.cfh->state_flags &= ~CFILE_FLAG_BACKWARD_SEEKS; free(buf); return 0; }
//Display information about a route to the screen void display_route(struct track_node * track, struct train_route * route) { struct print_buffer pbuff; int i = 0, j = 0, s = 0; int distance; ap_init_buff(&pbuff); ap_printf(&pbuff, "PRIMARY "); int psdist = 0, sensor = 0; for (i = 0; i < route->primary_size && i < 20; i++) { ap_printf(&pbuff, "%s (%d:%d:%d:%d) ", track[route->primary[i].node].name, route->primary[i].distance, route->primary[i].edge, route->primary[i].secondary_route_index, route->primary[i].length_reserved); } if (pbuff.mem[0] != 0) { CommandOutput(pbuff.mem); ap_init_buff(&pbuff); } ap_printf(&pbuff, "SECONDARY "); for (i = 0; i < route->primary_size && i < 20; i++) { s = route->primary[i].secondary_route_index; if (s != -1) { ap_printf(&pbuff, "(%s): ", track[route->primary[i].node].name); distance = first_secondary_sensor_distance(track, route, route->primary[i].secondary_route_index, &sensor); ap_printf(&pbuff, "[%s:%d]: ", track[sensor].name,distance); for (j = 0; j < route->secondary_size[s]; j++) { ap_printf(&pbuff, "%s (%d:%d:%d) ", track[route->secondary[s][j].node].name, route->secondary[s][j].distance, route->secondary[s][j].edge,route->secondary[s][j].length_reserved); } ap_printf(&pbuff, "} "); } } if (pbuff.mem[0] != 0) { CommandOutput(pbuff.mem); ap_init_buff(&pbuff); } //Delay(500); struct position pos; pos.node = route->primary[0].node; pos.offset = 0; int sensors[10], distances[10], primary[10], numsensors = 0; ap_printf(&pbuff, "Guaranteed length: %d, Sensors: ", route_guaranteed_length(track, &pos, route, -1)); numsensors = predict_sensors_quantum(track, &pos, route, sensors, distances, primary, -1); for (i = 0; i < numsensors; i++) { ap_printf(&pbuff, " %s(%d) ", track[sensors[i]].name, distances[i]); } /* ap_printf(&pbuff, "SENSORS "); for (i = 0; i < route->primary_size && i < 20; i++) { pos.node = route->primary[i].node; pos.offset = 0; psdist = next_primary_sensor_distance(track, &pos, route, &sensor); CommandOutput("%d %s %d %d", i, track[route->primary[i].node].name, psdist, sensor); Delay(100); } */ CommandOutput(pbuff.mem); ap_init_buff(&pbuff); //set_upcoming_switches(track, route, &pos, 600); }