/* =========================================================================== * Initialize the "longest match" routines for a new file */ void lm_init (int pack_level, /* 1: best speed, 9: best compression */ ush *flags) /* general purpose bit flag */ { register unsigned j; if (pack_level < 1 || pack_level > 9) gzip_error ("bad pack level"); /* Initialize the hash table. */ memzero((char*)head, HASH_SIZE*sizeof(*head)); /* prev will be initialized on the fly */ /* rsync params */ rsync_chunk_end = 0xFFFFFFFFUL; rsync_sum = 0; /* Set the default configuration parameters: */ max_lazy_match = configuration_table[pack_level].max_lazy; good_match = configuration_table[pack_level].good_length; #ifndef FULL_SEARCH nice_match = configuration_table[pack_level].nice_length; #endif max_chain_length = configuration_table[pack_level].max_chain; if (pack_level == 1) { *flags |= FAST; } else if (pack_level == 9) { *flags |= SLOW; } /* ??? reduce max_chain_length for binary files */ strstart = 0; block_start = 0L; lookahead = read_buf((char*)window, sizeof(int) <= 2 ? (unsigned)WSIZE : 2*WSIZE); if (lookahead == 0 || lookahead == (unsigned)EOF) { eofile = 1, lookahead = 0; return; } eofile = 0; /* Make sure that we always have enough lookahead. This is important * if input comes from a device such as a tty. */ while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window(); ins_h = 0; for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]); /* If lookahead < MIN_MATCH, ins_h is garbage, but this is * not important since only literal bytes will be emitted. */ }
static void init_status(WINDOW *win, STATUS * sp) { memset(sp, 0, sizeof(*sp)); sp->c = 99; sp->v = 99; sp->ch = ' '; keypad(win, TRUE); fill_window(win); getbegyx(win, sp->y_beg, sp->x_beg); getmaxyx(win, sp->y_max, sp->x_max); }
static WINDOW * create_my_derwin(WINDOW *parent) { PAIR ul, lr; WINDOW *result = 0; if (getwindow(parent, &ul, &lr)) { result = derwin(parent, lines_of(ul, lr), cols_of(ul, lr), pair_of(ul)); if (result != 0) { fill_window(result, 'd'); add_window(parent, result); } } if (result == 0) result = parent; return result; }
static WINDOW * create_my_window(WINDOW *current) { PAIR ul, lr; WINDOW *result = 0; if (getwindow(stdscr, &ul, &lr)) { result = newwin(lines_of(ul, lr), cols_of(ul, lr), pair_of(ul)); if (result != 0) { fill_window(result, 'c'); add_window(stdscr, result); } } if (result == 0) result = current; return result; }
static WINDOW * create_my_subwin(WINDOW *parent) { PAIR ul, lr; WINDOW *result = 0; if (getwindow(parent, &ul, &lr)) { result = subwin(parent, lines_of(ul, lr), cols_of(ul, lr), ul.y + getbegy(parent), ul.x + getbegx(parent)); if (result != 0) { fill_window(result, 's'); add_window(parent, result); } } if (result == 0) result = parent; return result; }
static void test_insdelln(void) { STATUS st; int n; init_status(stdscr, &st); do { (void) attrset(st.attr | COLOR_PAIR(st.pair)); switch (st.ch) { case 'i': for (n = 0; n < st.count; ++n) insertln(); break; case 'd': for (n = 0; n < st.count; ++n) deleteln(); break; case 'I': insdelln(st.count); break; case 'D': insdelln(-st.count); break; case 'f': fill_window(stdscr); reshow_status(stdscr, &st); break; case 'w': do_subwindow(stdscr, &st, test_winsdelln); break; case 'q': return; default: update_status(stdscr, &st); break; } } while ((st.ch = getch()) != ERR); }
static void test_winsdelln(WINDOW *win) { STATUS st; int n; init_status(win, &st); do { (void) wattrset(win, st.attr | COLOR_PAIR(st.pair)); switch (st.ch) { case 'i': for (n = 0; n < st.count; ++n) winsertln(win); break; case 'd': for (n = 0; n < st.count; ++n) wdeleteln(win); break; case 'I': winsdelln(win, st.count); break; case 'D': winsdelln(win, -st.count); break; case 'f': fill_window(win); reshow_status(win, &st); break; case 'w': do_subwindow(win, &st, test_winsdelln); break; case 'q': return; default: update_status(win, &st); break; } } while ((st.ch = wgetch(win)) != ERR); }
int dialog_inputbox(WINDOW *main_window, const char *title, const char *prompt, const char *init, char **resultp, int *result_len) { int prompt_lines = 0; int prompt_width = 0; WINDOW *win; WINDOW *prompt_win; WINDOW *form_win; PANEL *panel; int i, x, y; int res = -1; int cursor_position = strlen(init); int cursor_form_win; char *result = *resultp; if (strlen(init)+1 > *result_len) { *result_len = strlen(init)+1; *resultp = result = realloc(result, *result_len); } prompt_lines = get_line_no(prompt); for (i = 0; i < prompt_lines; i++) { const char *line = get_line(prompt, i); int len = get_line_length(line); prompt_width = max(prompt_width, len); } if (title) prompt_width = max(prompt_width, strlen(title)); y = (LINES-(prompt_lines+4))/2; x = (COLS-(prompt_width+4))/2; strncpy(result, init, *result_len); win = newwin(prompt_lines+6, prompt_width+7, y, x); prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); keypad(form_win, TRUE); (void) wattrset(form_win, attributes[INPUT_FIELD]); (void) wattrset(win, attributes[INPUT_BOX]); box(win, 0, 0); (void) wattrset(win, attributes[INPUT_HEADING]); if (title) mvwprintw(win, 0, 3, "%s", title); (void) wattrset(prompt_win, attributes[INPUT_TEXT]); fill_window(prompt_win, prompt); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); cursor_form_win = min(cursor_position, prompt_width-1); mvwprintw(form_win, 0, 0, "%s", result + cursor_position-cursor_form_win); panel = new_panel(win); curs_set(1); touchwin(win); refresh_all_windows(main_window); while ((res = wgetch(form_win))) { int len = strlen(result); switch (res) { case 10: case 27: case KEY_F(F_HELP): case KEY_F(F_EXIT): case KEY_F(F_BACK): break; case 127: case KEY_BACKSPACE: if (cursor_position > 0) { memmove(&result[cursor_position-1], &result[cursor_position], len-cursor_position+1); cursor_position--; cursor_form_win--; len--; } break; case KEY_DC: if (cursor_position >= 0 && cursor_position < len) { memmove(&result[cursor_position], &result[cursor_position+1], len-cursor_position+1); len--; } break; case KEY_UP: case KEY_RIGHT: if (cursor_position < len) { cursor_position++; cursor_form_win++; } break; case KEY_DOWN: case KEY_LEFT: if (cursor_position > 0) { cursor_position--; cursor_form_win--; } break; case KEY_HOME: cursor_position = 0; cursor_form_win = 0; break; case KEY_END: cursor_position = len; cursor_form_win = min(cursor_position, prompt_width-1); break; default: if ((isgraph(res) || isspace(res))) { if (len+2 > *result_len) { *result_len = len+2; *resultp = result = realloc(result, *result_len); } memmove(&result[cursor_position+1], &result[cursor_position], len-cursor_position+1); result[cursor_position] = res; cursor_position++; cursor_form_win++; len++; } else { mvprintw(0, 0, "unknown key: %d\n", res); } break; } if (cursor_form_win < 0) cursor_form_win = 0; else if (cursor_form_win > prompt_width-1) cursor_form_win = prompt_width-1; wmove(form_win, 0, 0); wclrtoeol(form_win); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%s", result + cursor_position-cursor_form_win); wmove(form_win, 0, cursor_form_win); touchwin(win); refresh_all_windows(main_window); if (res == 10) { res = 0; break; } else if (res == 27 || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { res = KEY_EXIT; break; } else if (res == KEY_F(F_HELP)) { res = 1; break; } } curs_set(0); del_panel(panel); delwin(prompt_win); delwin(form_win); delwin(win); return res; }
static void reshow_status(WINDOW *win, STATUS * sp) { fill_window(win); show_status(win, sp); }
static void draw_edges(void) { /* * render the offset depth image */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); #ifdef GL_POLYGON_OFFSET_FILL glPolygonOffset(4.0, 1.0); #else /* XXX should change to use glPolygonOffset in OpenGL 1.1 */ glPolygonOffsetEXT(4.0, (1.0 / (1 << 22))); #endif glEnable(GL_POLYGON_OFFSET_FILL); /* glColorMask(0,0,0,0); */ glCallList(1); /* glColorMask(1,1,1,1); */ glDisable(GL_POLYGON_OFFSET_FILL); /* * make no further changes to the depth image */ glDepthMask(0); glDisable(GL_DEPTH_TEST); /* XXX */ glEnable(GL_DEPTH_TEST); /* XXX */ /* * cull all facets of one (arbitrary) orientation. render the * remaining facets in outline mode, toggling the stencil bit * at each pixel. */ glDisable(GL_LIGHTING); glColor3f(0.f, 0.f, 0.f); glLineWidth(2.0); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 0, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); glEnable(GL_CULL_FACE); glColorMask(0, 0, 0, 0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glCallList(1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glColorMask(1, 1, 1, 1); glDisable(GL_CULL_FACE); /* * color all pixels in the framebuffer with stencil value 1 */ glStencilFunc(GL_EQUAL, 1, 1); glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); glColor3f(0, 0, 0); fill_window(); glDisable(GL_STENCIL_TEST); /* * draw all true edges, testing against the depth image */ glColor3f(0, 0, 0); glCallList(2); /* * return state to default values */ glDepthMask(1); glDisable(GL_DEPTH_TEST); /* XXX */ glEnable(GL_DEPTH_TEST); /* XXX */ if (lighting) glEnable(GL_LIGHTING); glLineWidth(1.0); glColor3f(1.f, 1.f, 1.f); }
int dialog_inputbox(WINDOW *main_window, const char *title, const char *prompt, const char *init, char *result, int result_len) { int prompt_lines = 0; int prompt_width = 0; WINDOW *win; WINDOW *prompt_win; WINDOW *form_win; PANEL *panel; int i, x, y; int res = -1; int cursor_position = strlen(init); /* find the widest line of msg: */ prompt_lines = get_line_no(prompt); for (i = 0; i < prompt_lines; i++) { const char *line = get_line(prompt, i); int len = get_line_length(line); prompt_width = max(prompt_width, len); } if (title) prompt_width = max(prompt_width, strlen(title)); /* place dialog in middle of screen */ y = (LINES-(prompt_lines+4))/2; x = (COLS-(prompt_width+4))/2; strncpy(result, init, result_len); /* create the windows */ win = newwin(prompt_lines+6, prompt_width+7, y, x); prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); keypad(form_win, TRUE); wattrset(form_win, attributes[INPUT_FIELD]); wattrset(win, attributes[INPUT_BOX]); box(win, 0, 0); wattrset(win, attributes[INPUT_HEADING]); if (title) mvwprintw(win, 0, 3, "%s", title); /* print message */ wattrset(prompt_win, attributes[INPUT_TEXT]); fill_window(prompt_win, prompt); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%s", result); /* create panels */ panel = new_panel(win); /* show the cursor */ curs_set(1); touchwin(win); refresh_all_windows(main_window); while ((res = wgetch(form_win))) { int len = strlen(result); switch (res) { case 10: /* ENTER */ case 27: /* ESCAPE */ case KEY_F(F_HELP): case KEY_F(F_EXIT): case KEY_F(F_BACK): break; case 127: case KEY_BACKSPACE: if (cursor_position > 0) { memmove(&result[cursor_position-1], &result[cursor_position], len-cursor_position+1); cursor_position--; } break; case KEY_DC: if (cursor_position >= 0 && cursor_position < len) { memmove(&result[cursor_position], &result[cursor_position+1], len-cursor_position+1); } break; case KEY_UP: case KEY_RIGHT: if (cursor_position < len && cursor_position < min(result_len, prompt_width)) cursor_position++; break; case KEY_DOWN: case KEY_LEFT: if (cursor_position > 0) cursor_position--; break; default: if ((isgraph(res) || isspace(res)) && len-2 < result_len) { /* insert the char at the proper position */ memmove(&result[cursor_position+1], &result[cursor_position], len+1); result[cursor_position] = res; cursor_position++; } else { mvprintw(0, 0, "unknow key: %d\n", res); } break; } wmove(form_win, 0, 0); wclrtoeol(form_win); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%s", result); wmove(form_win, 0, cursor_position); touchwin(win); refresh_all_windows(main_window); if (res == 10) { res = 0; break; } else if (res == 27 || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { res = KEY_EXIT; break; } else if (res == KEY_F(F_HELP)) { res = 1; break; } } /* hide the cursor */ curs_set(0); del_panel(panel); delwin(prompt_win); delwin(form_win); delwin(win); return res; }
block_state deflate_medium(deflate_state *s, int flush) { struct match current_match, next_match; memset(¤t_match, 0, sizeof(struct match)); memset(&next_match, 0, sizeof(struct match)); for (;;) { IPos hash_head = 0; /* head of the hash chain */ int bflush; /* set if current block must be flushed */ /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes * for the next match, plus MIN_MATCH bytes to insert the * string following the next current_match. */ if (s->lookahead < MIN_LOOKAHEAD) { fill_window(s); if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { return need_more; } if (s->lookahead == 0) break; /* flush the current block */ next_match.match_length = 0; } s->prev_length = 2; /* Insert the string window[strstart .. strstart+2] in the * dictionary, and set hash_head to the head of the hash chain: */ /* If we already have a future match from a previous round, just use that */ if (next_match.match_length > 0) { current_match = next_match; next_match.match_length = 0; } else { hash_head = 0; if (s->lookahead >= MIN_MATCH) { hash_head = insert_string(s, s->strstart); } /* set up the initial match to be a 1 byte literal */ current_match.match_start = 0; current_match.match_length = 1; current_match.strstart = s->strstart; current_match.orgstart = current_match.strstart; /* Find the longest match, discarding those <= prev_length. * At this point we have always match_length < MIN_MATCH */ if (hash_head != 0 && s->strstart - hash_head <= MAX_DIST2) { /* To simplify the code, we prevent matches with the string * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ current_match.match_length = longest_match (s, hash_head); current_match.match_start = s->match_start; if (current_match.match_length < MIN_MATCH) current_match.match_length = 1; if (current_match.match_start >= current_match.strstart) { /* this can happen due to some restarts */ current_match.match_length = 1; } } } insert_match(s, current_match); /* now, look ahead one */ if (s->lookahead > MIN_LOOKAHEAD) { s->strstart = current_match.strstart + current_match.match_length; hash_head = insert_string(s, s->strstart); /* set up the initial match to be a 1 byte literal */ next_match.match_start = 0; next_match.match_length = 1; next_match.strstart = s->strstart; next_match.orgstart = next_match.strstart; /* Find the longest match, discarding those <= prev_length. * At this point we have always match_length < MIN_MATCH */ if (hash_head != 0 && s->strstart - hash_head <= MAX_DIST2) { /* To simplify the code, we prevent matches with the string * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ next_match.match_length = longest_match (s, hash_head); next_match.match_start = s->match_start; if (next_match.match_start >= next_match.strstart) /* this can happen due to some restarts */ next_match.match_length = 1; if (next_match.match_length < MIN_MATCH) next_match.match_length = 1; else fizzle_matches(s, ¤t_match, &next_match); } /* short matches with a very long distance are rarely a good idea encoding wise */ if (next_match.match_length == 3 && (next_match.strstart - next_match.match_start) > 12000) next_match.match_length = 1; s->strstart = current_match.strstart; } else { next_match.match_length = 0; } /* now emit the current match */ bflush = emit_match(s, current_match, hash_head); /* move the "cursor" forward */ s->strstart += current_match.match_length; if (bflush) FLUSH_BLOCK(s, 0); } s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; if (flush == Z_FINISH) { FLUSH_BLOCK(s, 1); return finish_done; } if (s->last_lit) FLUSH_BLOCK(s, 0); return block_done; }
/* * Begin the file transfer process */ void initiate_transfer(int last_ackno) { // Init list of timers timer_list = (struct packet_timer**)calloc(MAX_TIMERS, sizeof(struct packet_timer*)); // Start thread to poll list of timers and check if any have timed out pthread_t pthread; pthread_create(&pthread, NULL, poll_timers, NULL); // Create and add all DAT timers, don't start them until they're sent //printf("Creating and adding DAT timers...\n"); pthread_mutex_lock(&timers_mutex); int j; for(j=0; data_packets[j] != 0; j++) { //if(j){printf("Attempting to create timer with data_packets[%d]...\n",j);} struct packet_timer* DAT_timer = create_timer(data_packets[j]->header.ackno, -1); add_timer(timer_list, DAT_timer); } //print_stoppedTimers(timer_list); pthread_mutex_unlock(&timers_mutex); // send SYN //printf("creating SYN packet...\n"); struct packet* SYN_pckt = create_packet(NULL, 0, SYN, first_seqno); //printf("created SYN packet\n"); // update statistics send_packet(SYN_pckt, socketfd, adr_receiver); // sent, unique, SYN, seqno, length) print_log(true, true, SYN, SYN_pckt->header.seqno, SYN_pckt->payload_length); bytes_sent += SYN_pckt->payload_length; unique_bytes_sent += SYN_pckt->payload_length; SYN_packets_sent++; // start SYN's timer pthread_mutex_lock(&timers_mutex); //printf("Creating SYN timer...\n"); struct packet_timer* SYN_timer = create_timer(SYN_pckt->header.ackno, clock()); add_timer(timer_list, SYN_timer); start_timer(SYN_pckt->header.ackno, timer_list); //printf("Created, added, and started SYN timer\n"); pthread_mutex_unlock(&timers_mutex); // wait on SYN's ACK while(1) { struct sockaddr_in adr_src; struct packet* rec_pckt = receive_packet(socketfd, &adr_src); if(rec_pckt && rec_pckt->header.type == ACK) { print_log(false, true, ACK, rec_pckt->header.seqno, rec_pckt->payload_length); // update statistics ACK_packets_received++; printf("sender: SYN acknowledged\n"); pthread_mutex_lock(&timers_mutex); stop_timer(timer_list, SYN_pckt->header.ackno); pthread_mutex_unlock(&timers_mutex); break; } // resend the SYN packet send_packet(SYN_pckt, socketfd, adr_receiver); print_log(true, false, SYN, SYN_pckt->header.seqno, SYN_pckt->payload_length); // update statistics bytes_sent += SYN_pckt->payload_length; SYN_packets_sent++; if(clock()/CLOCKS_PER_SEC >= SENDER_TIMEOUT_S) { terminate(-2); } } // Init window struct window* window = (struct window*)malloc(sizeof(*window)); window->size = WINDOW_SIZE; window->occupied = 0; window->base_seqno = data_packets[0]->header.seqno; // Send initial wave of data packets to fill the window //printf("sender: initial wave of data...\n"); fill_window(window); //printf("Window:\n size->%d\n base_seqno->%d\n occupied->%d\n",window->size,window->base_seqno,window->occupied); // Receive ACKs, send data to fill the window, and check for timeouts while(1) { /* pthread_mutex_lock(&timers_mutex); print_runningTimers(timer_list); pthread_mutex_unlock(&timers_mutex); */ // wait for packet struct sockaddr_in adr_src; struct packet* rec_pckt; if((rec_pckt = receive_packet(socketfd, &adr_src)) != NULL) { print_log(false, true, ACK, rec_pckt->header.seqno, rec_pckt->payload_length); // handle received ACKs handle_packet(rec_pckt); } // Update the window's base_seqno as ACKs are received //printf("Updating window...\n"); update_windowBase(window); /* send new data packets if window permits */ //printf("Filling window...\n"); fill_window(window); //printf("Resending timedout packets...\n"); resend_timedoutPackets(); // check if all the data packets have been successfully acknowledged if(allPacketsAcknowledged() == true) { printf("ALL PACKETS ACKNOWLEDGED\n"); // send FIN int FIN_seqno = last_ackno; struct packet* FIN_pckt = create_packet(NULL, 0, FIN, FIN_seqno); send_packet(FIN_pckt, socketfd, adr_receiver); print_log(true, true, FIN, FIN_pckt->header.seqno, FIN_pckt->payload_length); // Update statistics bytes_sent += FIN_pckt->payload_length; unique_bytes_sent += FIN_pckt->payload_length; FIN_packets_sent++; // Start its timer pthread_mutex_lock(&timers_mutex); struct packet_timer* FIN_timer = create_timer(FIN_pckt->header.ackno, clock()); add_timer(timer_list, FIN_timer); start_timer(FIN_timer->pckt_ackno, timer_list); pthread_mutex_unlock(&timers_mutex); // wait for FIN's ACK if((rec_pckt = receive_packet(socketfd, &adr_src)) != NULL) { print_log(false, true, ACK, rec_pckt->header.seqno, rec_pckt->payload_length); handle_packet(rec_pckt); if(rec_pckt->header.seqno == FIN_pckt->header.ackno) { printf("FIN ACK RECEIVED\n"); print_statistics(); terminate(0); } } } // Global timeout if(clock()/CLOCKS_PER_SEC >= SENDER_TIMEOUT_S) { terminate(-2); } } }
/* =========================================================================== * Same as deflate_medium, but achieves better compression. We use a lazy * evaluation for matches: a match is finally adopted only if there is * no better match at the next window position. */ block_state deflate_slow(deflate_state *s, int flush) { IPos hash_head; /* head of hash chain */ int bflush; /* set if current block must be flushed */ /* Process the input block. */ for (;;) { /* Make sure that we always have enough lookahead, except * at the end of the input file. We need MAX_MATCH bytes * for the next match, plus MIN_MATCH bytes to insert the * string following the next match. */ if (s->lookahead < MIN_LOOKAHEAD) { fill_window(s); if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { return need_more; } if (s->lookahead == 0) break; /* flush the current block */ } /* Insert the string window[strstart .. strstart+2] in the * dictionary, and set hash_head to the head of the hash chain: */ hash_head = NIL; if (s->lookahead >= MIN_MATCH) { hash_head = insert_string(s, s->strstart); } /* Find the longest match, discarding those <= prev_length. */ s->prev_length = s->match_length, s->prev_match = s->match_start; s->match_length = MIN_MATCH-1; if (hash_head != NIL && s->prev_length < s->max_lazy_match && s->strstart - hash_head <= MAX_DIST(s)) { /* To simplify the code, we prevent matches with the string * of window index 0 (in particular we have to avoid a match * of the string with itself at the start of the input file). */ s->match_length = longest_match(s, hash_head); /* longest_match() sets match_start */ if (s->match_length <= 5 && (s->strategy == Z_FILTERED #if TOO_FAR <= 32767 || (s->match_length == MIN_MATCH && s->strstart - s->match_start > TOO_FAR) #endif )) { /* If prev_match is also MIN_MATCH, match_start is garbage * but we will ignore the current match anyway. */ s->match_length = MIN_MATCH-1; } } /* If there was a match at the previous step and the current * match is not better, output the previous match: */ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; /* Do not insert strings in hash table beyond this. */ check_match(s, s->strstart-1, s->prev_match, s->prev_length); _tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - MIN_MATCH, bflush); /* Insert in hash table all strings up to the end of the match. * strstart-1 and strstart are already inserted. If there is not * enough lookahead, the last two strings are not inserted in * the hash table. */ s->lookahead -= s->prev_length-1; #ifdef NOT_TWEAK_COMPILER s->prev_length -= 2; do { if (++s->strstart <= max_insert) { insert_string(s, s->strstart); } } while (--s->prev_length != 0); s->match_available = 0; s->match_length = MIN_MATCH-1; s->strstart++; #else { uInt mov_fwd = s->prev_length - 2; uInt insert_cnt = mov_fwd; if (unlikely(insert_cnt > max_insert - s->strstart)) insert_cnt = max_insert - s->strstart; bulk_insert_str(s, s->strstart + 1, insert_cnt); s->prev_length = 0; s->match_available = 0; s->match_length = MIN_MATCH-1; s->strstart += mov_fwd + 1; } #endif /*NOT_TWEAK_COMPILER*/ if (bflush) FLUSH_BLOCK(s, 0); } else if (s->match_available) { /* If there was no match at the previous position, output a * single literal. If there was a match but the current match * is longer, truncate the previous match to a single literal. */ Tracevv((stderr, "%c", s->window[s->strstart-1])); _tr_tally_lit(s, s->window[s->strstart-1], bflush); if (bflush) { FLUSH_BLOCK_ONLY(s, 0); } s->strstart++; s->lookahead--; if (s->strm->avail_out == 0) return need_more; } else { /* There is no previous match to compare with, wait for * the next step to decide. */ s->match_available = 1; s->strstart++; s->lookahead--; } } Assert(flush != Z_NO_FLUSH, "no flush?"); if (s->match_available) { Tracevv((stderr, "%c", s->window[s->strstart-1])); _tr_tally_lit(s, s->window[s->strstart-1], bflush); s->match_available = 0; } s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; if (flush == Z_FINISH) { FLUSH_BLOCK(s, 1); return finish_done; } if (s->last_lit) FLUSH_BLOCK(s, 0); return block_done; }
/* =========================================================================== * Initialize the "longest match" routines for a new file * * IN assertion: window_size is > 0 if the input file is already read or * mmap'ed in the window[] array, 0 otherwise. In the first case, * window_size is sufficient to contain the whole input file plus * MIN_LOOKAHEAD bytes (to avoid referencing memory beyond the end * of window[] when looking for matches towards the end). */ void IZDeflate::lm_init (int pack_level, ush *flags) //int pack_level; /* 0: store, 1: best speed, 9: best compression */ //ush *flags; /* general purpose bit flag */ { unsigned j; if (pack_level < 1 || pack_level > 9) error("bad pack level"); /* Do not slide the window if the whole input is already in memory * (window_size > 0) */ sliding = 0; if (window_size == 0L) { sliding = 1; window_size = (ulg)2L*WSIZE; } /* Use dynamic allocation if compiler does not like big static arrays: */ #ifdef DYN_ALLOC if (window == NULL) { window = (uch *) zcalloc(WSIZE, 2*sizeof(uch)); if (window == NULL) ziperr(ZE_MEM, "window allocation"); } if (prev == NULL) { prev = (Pos *) zcalloc(WSIZE, sizeof(Pos)); head = (Pos *) zcalloc(HASH_SIZE, sizeof(Pos)); if (prev == NULL || head == NULL) { ziperr(ZE_MEM, "hash table allocation"); } } #endif /* DYN_ALLOC */ /* Initialize the hash table (avoiding 64K overflow for 16 bit systems). * prev[] will be initialized on the fly. */ head[HASH_SIZE-1] = NIL; memset((char*)head, NIL, (unsigned)(HASH_SIZE-1)*sizeof(*head)); /* Set the default configuration parameters: */ max_lazy_match = configuration_table[pack_level].max_lazy; good_match = configuration_table[pack_level].good_length; #ifndef FULL_SEARCH nice_match = configuration_table[pack_level].nice_length; #endif max_chain_length = configuration_table[pack_level].max_chain; if (pack_level <= 2) { *flags |= FAST; } else if (pack_level >= 8) { *flags |= SLOW; } /* ??? reduce max_chain_length for binary files */ strstart = 0; block_start = 0L; #if defined(ASMV) && !defined(RISCOS) match_init(); /* initialize the asm code */ #endif j = WSIZE; #ifndef MAXSEG_64K if (sizeof(int) > 2) j <<= 1; /* Can read 64K in one step */ #endif lookahead = (*read_buf)(read_handle, (char*)window, j); if (lookahead == 0 || lookahead == (unsigned)EOF) { eofile = 1, lookahead = 0; return; } eofile = 0; /* Make sure that we always have enough lookahead. This is important * if input comes from a device such as a tty. */ if (lookahead < MIN_LOOKAHEAD) fill_window(); ins_h = 0; for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]); /* If lookahead < MIN_MATCH, ins_h is garbage, but this is * not important since only literal bytes will be emitted. */ }
int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...) { va_list ap; char *btn; int btns_width = 0; int msg_lines = 0; int msg_width = 0; int total_width; int win_rows = 0; WINDOW *win; WINDOW *msg_win; WINDOW *menu_win; MENU *menu; ITEM *btns[btn_num+1]; int i, x, y; int res = -1; va_start(ap, btn_num); for (i = 0; i < btn_num; i++) { btn = va_arg(ap, char *); btns[i] = new_item(btn, ""); btns_width += strlen(btn)+1; } va_end(ap); btns[btn_num] = NULL; /* find the widest line of msg: */ msg_lines = get_line_no(msg); for (i = 0; i < msg_lines; i++) { const char *line = get_line(msg, i); int len = get_line_length(line); if (msg_width < len) msg_width = len; } total_width = max(msg_width, btns_width); /* place dialog in middle of screen */ y = (LINES-(msg_lines+4))/2; x = (COLS-(total_width+4))/2; /* create the windows */ if (btn_num > 0) win_rows = msg_lines+4; else win_rows = msg_lines+2; win = newwin(win_rows, total_width+4, y, x); keypad(win, TRUE); menu_win = derwin(win, 1, btns_width, win_rows-2, 1+(total_width+2-btns_width)/2); menu = new_menu(btns); msg_win = derwin(win, win_rows-2, msg_width, 1, 1+(total_width+2-msg_width)/2); set_menu_fore(menu, attributes[DIALOG_MENU_FORE]); set_menu_back(menu, attributes[DIALOG_MENU_BACK]); wattrset(win, attributes[DIALOG_BOX]); box(win, 0, 0); /* print message */ wattrset(msg_win, attributes[DIALOG_TEXT]); fill_window(msg_win, msg); set_menu_win(menu, win); set_menu_sub(menu, menu_win); set_menu_format(menu, 1, btn_num); menu_opts_off(menu, O_SHOWDESC); menu_opts_off(menu, O_SHOWMATCH); menu_opts_on(menu, O_ONEVALUE); menu_opts_on(menu, O_NONCYCLIC); set_menu_mark(menu, ""); post_menu(menu); touchwin(win); refresh_all_windows(main_window); while ((res = wgetch(win))) { switch (res) { case KEY_LEFT: menu_driver(menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(menu, REQ_RIGHT_ITEM); break; case 10: /* ENTER */ case 27: /* ESCAPE */ case ' ': case KEY_F(F_BACK): case KEY_F(F_EXIT): break; } touchwin(win); refresh_all_windows(main_window); if (res == 10 || res == ' ') { res = item_index(current_item(menu)); break; } else if (res == 27 || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { res = KEY_EXIT; break; } } unpost_menu(menu); free_menu(menu); for (i = 0; i < btn_num; i++) free_item(btns[i]); delwin(win); return res; }
int dialog_inputbox(WINDOW *main_window, const char *title, const char *prompt, const char *init, char **resultp, int *result_len) { int prompt_lines = 0; int prompt_width = 0; WINDOW *win; WINDOW *prompt_win; WINDOW *form_win; PANEL *panel; int i, x, y, lines, columns, win_lines, win_cols; int res = -1; int cursor_position = strlen(init); int cursor_form_win; char *result = *resultp; getmaxyx(stdscr, lines, columns); if (strlen(init)+1 > *result_len) { *result_len = strlen(init)+1; *resultp = result = realloc(result, *result_len); } /* find the widest line of msg: */ prompt_lines = get_line_no(prompt); for (i = 0; i < prompt_lines; i++) { const char *line = get_line(prompt, i); int len = get_line_length(line); prompt_width = max(prompt_width, len); } if (title) prompt_width = max(prompt_width, strlen(title)); win_lines = min(prompt_lines+6, lines-2); win_cols = min(prompt_width+7, columns-2); prompt_lines = max(win_lines-6, 0); prompt_width = max(win_cols-7, 0); /* place dialog in middle of screen */ y = (lines-win_lines)/2; x = (columns-win_cols)/2; strncpy(result, init, *result_len); /* create the windows */ win = newwin(win_lines, win_cols, y, x); prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); keypad(form_win, TRUE); (void) wattrset(form_win, attributes[INPUT_FIELD]); (void) wattrset(win, attributes[INPUT_BOX]); box(win, 0, 0); (void) wattrset(win, attributes[INPUT_HEADING]); if (title) mvwprintw(win, 0, 3, "%s", title); /* print message */ (void) wattrset(prompt_win, attributes[INPUT_TEXT]); fill_window(prompt_win, prompt); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); cursor_form_win = min(cursor_position, prompt_width-1); mvwprintw(form_win, 0, 0, "%s", result + cursor_position-cursor_form_win); /* create panels */ panel = new_panel(win); /* show the cursor */ curs_set(1); touchwin(win); refresh_all_windows(main_window); while ((res = wgetch(form_win))) { int len = strlen(result); switch (res) { case 10: /* ENTER */ case 27: /* ESCAPE */ case KEY_F(F_HELP): case KEY_F(F_EXIT): case KEY_F(F_BACK): break; case 127: case KEY_BACKSPACE: if (cursor_position > 0) { memmove(&result[cursor_position-1], &result[cursor_position], len-cursor_position+1); cursor_position--; cursor_form_win--; len--; } break; case KEY_DC: if (cursor_position >= 0 && cursor_position < len) { memmove(&result[cursor_position], &result[cursor_position+1], len-cursor_position+1); len--; } break; case KEY_UP: case KEY_RIGHT: if (cursor_position < len) { cursor_position++; cursor_form_win++; } break; case KEY_DOWN: case KEY_LEFT: if (cursor_position > 0) { cursor_position--; cursor_form_win--; } break; case KEY_HOME: cursor_position = 0; cursor_form_win = 0; break; case KEY_END: cursor_position = len; cursor_form_win = min(cursor_position, prompt_width-1); break; default: if ((isgraph(res) || isspace(res))) { /* one for new char, one for '\0' */ if (len+2 > *result_len) { *result_len = len+2; *resultp = result = realloc(result, *result_len); } /* insert the char at the proper position */ memmove(&result[cursor_position+1], &result[cursor_position], len-cursor_position+1); result[cursor_position] = res; cursor_position++; cursor_form_win++; len++; } else { mvprintw(0, 0, "unknown key: %d\n", res); } break; } if (cursor_form_win < 0) cursor_form_win = 0; else if (cursor_form_win > prompt_width-1) cursor_form_win = prompt_width-1; wmove(form_win, 0, 0); wclrtoeol(form_win); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%s", result + cursor_position-cursor_form_win); wmove(form_win, 0, cursor_form_win); touchwin(win); refresh_all_windows(main_window); if (res == 10) { res = 0; break; } else if (res == 27 || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { res = KEY_EXIT; break; } else if (res == KEY_F(F_HELP)) { res = 1; break; } } /* hide the cursor */ curs_set(0); del_panel(panel); delwin(prompt_win); delwin(form_win); delwin(win); return res; }
/* layman's scrollable window... */ void show_scroll_win(WINDOW *main_window, const char *title, const char *text) { int res; int total_lines = get_line_no(text); int x, y; int start_x = 0, start_y = 0; int text_lines = 0, text_cols = 0; int total_cols = 0; int win_cols = 0; int win_lines = 0; int i = 0; WINDOW *win; WINDOW *pad; PANEL *panel; /* find the widest line of msg: */ total_lines = get_line_no(text); for (i = 0; i < total_lines; i++) { const char *line = get_line(text, i); int len = get_line_length(line); total_cols = max(total_cols, len+2); } /* create the pad */ pad = newpad(total_lines+10, total_cols+10); wattrset(pad, attributes[SCROLLWIN_TEXT]); fill_window(pad, text); win_lines = min(total_lines+4, LINES-2); win_cols = min(total_cols+2, COLS-2); text_lines = max(win_lines-4, 0); text_cols = max(win_cols-2, 0); /* place window in middle of screen */ y = (LINES-win_lines)/2; x = (COLS-win_cols)/2; win = newwin(win_lines, win_cols, y, x); keypad(win, TRUE); /* show the help in the help window, and show the help panel */ wattrset(win, attributes[SCROLLWIN_BOX]); box(win, 0, 0); wattrset(win, attributes[SCROLLWIN_HEADING]); mvwprintw(win, 0, 3, " %s ", title); panel = new_panel(win); /* handle scrolling */ do { copywin(pad, win, start_y, start_x, 2, 2, text_lines, text_cols, 0); print_in_middle(win, text_lines+2, 0, text_cols, "<OK>", attributes[DIALOG_MENU_FORE]); wrefresh(win); res = wgetch(win); switch (res) { case KEY_NPAGE: case ' ': start_y += text_lines-2; break; case KEY_PPAGE: start_y -= text_lines+2; break; case KEY_HOME: start_y = 0; break; case KEY_END: start_y = total_lines-text_lines; break; case KEY_DOWN: case 'j': start_y++; break; case KEY_UP: case 'k': start_y--; break; case KEY_LEFT: case 'h': start_x--; break; case KEY_RIGHT: case 'l': start_x++; break; } if (res == 10 || res == 27 || res == 'q' || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { break; } if (start_y < 0) start_y = 0; if (start_y >= total_lines-text_lines) start_y = total_lines-text_lines; if (start_x < 0) start_x = 0; if (start_x >= total_cols-text_cols) start_x = total_cols-text_cols; } while (res); del_panel(panel); delwin(win); refresh_all_windows(main_window); }
void analyzer(int argc, char *argv[]) { char input[BUF_SIZE] = { 0, }; int length = 0; int threshold = 0; int range = 0; // FILE *output dd= NULL; char org_chr[8] = { 0, }; char prev_chr[8] = { 0, }; int org_pos; int prev_pos = 1; int is_new_pos = 0; // HERE char fnd_chr[8] = { 0, }; int fnd_pos; char base; uint32_t flag; int qual; int ret; char *ARGV[ARGC_MAX]; int ARGC = 0; int i = 0; if( argc == 0 ) // HADOOP { ARGV[0] = getenv("mapred_job_arg0"); // e.g. 50 ARGV[1] = "analyzer"; ARGV[2] = getenv("mapred_job_arg2"); // e.g. 50 ARGV[3] = getenv("mapred_job_arg3"); // e.g. -e ARGV[4] = getenv("mapred_job_arg4"); // e.g. 0.02 ARGV[5] = getenv("mapred_job_arg5"); // e.g. -q ARGV[6] = getenv("mapred_job_arg6"); // e.g. A ARGV[7] = getenv("mapred_job_arg7"); // e.g. -r ARGV[8] = getenv("mapred_job_arg8"); // e.g. 200 ARGV[9] = getenv("mapred_job_arg9"); // e.g. -t ARGV[10] = getenv("mapred_job_arg10"); // e.g. 20 ARGV[11] = getenv("mapred_job_arg11"); // e.g. -f ARGV[12] = getenv("mapred_job_arg12"); ARGV[13] = getenv("mapred_job_arg13"); ARGV[14] = getenv("mapred_job_arg14"); ARGV[15] = getenv("mapred_job_arg15"); ARGV[16] = getenv("mapred_job_arg16"); ARGV[17] = getenv("mapred_job_arg17"); ARGV[18] = getenv("mapred_job_arg18"); ARGV[19] = getenv("mapred_job_arg19"); ARGV[20] = getenv("mapred_job_arg20"); ARGV[21] = getenv("mapred_job_arg21"); ARGV[22] = getenv("mapred_job_arg22"); ARGV[23] = getenv("mapred_job_arg23"); fprintf( stderr, "0. %s\n", ARGV[0] ); fprintf( stderr, "1. %s\n", ARGV[1] ); fprintf( stderr, "2. %s\n", ARGV[2] ); fprintf( stderr, "3. %s\n", ARGV[3] ); fprintf( stderr, "4. %s\n", ARGV[4] ); fprintf( stderr, "5. %s\n", ARGV[5] ); fprintf( stderr, "6. %s\n", ARGV[6] ); fprintf( stderr, "7. %s\n", ARGV[7] ); fprintf( stderr, "8. %s\n", ARGV[8] ); fprintf( stderr, "9. %s\n", ARGV[9] ); fprintf( stderr, "10. %s\n", ARGV[10] ); fprintf( stderr, "11. %s\n", ARGV[11] ); fprintf( stderr, "12. %s\n", ARGV[12] ); fprintf( stderr, "13. %s\n", ARGV[13] ); fprintf( stderr, "14. %s\n", ARGV[14] ); fprintf( stderr, "15. %s\n", ARGV[15] ); fprintf( stderr, "16. %s\n", ARGV[16] ); fprintf( stderr, "17. %s\n", ARGV[17] ); fprintf( stderr, "18. %s\n", ARGV[18] ); fprintf( stderr, "19. %s\n", ARGV[19] ); fprintf( stderr, "20. %s\n", ARGV[20] ); fprintf( stderr, "21. %s\n", ARGV[21] ); fprintf( stderr, "22. %s\n", ARGV[22] ); fprintf( stderr, "23. %s\n", ARGV[23] ); ARGC = atoi( ARGV[0] ); parse_args( ARGC, ARGV, &length, &threshold, &range ); } else { parse_args( argc, argv, &length, &threshold, &range ); } fprintf( stderr, "==================================================\n" ); fprintf( stderr, "length : %d\n", length ); fprintf( stderr, "threshold : %d\n", threshold ); fprintf( stderr, "library distance : %d\n", range ); fprintf( stderr, "==================================================\n" ); fprintf( stdout, "#chr\tpos\t\tbase\tmaq\ttotal = >= + <(um)\tGMS\n"); init_prob_window( length ); // output = fopen( "sort.gma", "w+" ); while( fgets( input, BUF_SIZE-1, stdin ) ) { get_info( input, org_chr, &org_pos, fnd_chr, &fnd_pos, &base, &flag, &qual, &ret ); /* fprintf( stderr, "%s", input ); */ /* fprintf( stderr, "%s:", org_chr ); fprintf( stderr, "%d-", org_pos ); fprintf( stderr, "%s:", fnd_chr ); fprintf( stderr, "%d\t", fnd_pos ); fprintf( stderr, "%c:", base ); fprintf( stderr, "%d(0x%x):", flag, flag ); fprintf( stderr, "%d:", qual ); fprintf( stderr, "%d\n", ret ); */ if( strcmp( prev_chr, "" ) == 0 ) { //fprintf( stderr, "none -> %s +\n", org_chr ); strcpy( prev_chr, org_chr ); } else if( strcmp( prev_chr, org_chr ) == 0 ) { // in the same chromosome // fprintf( stderr, "org_pos: %d, prev_pos: %d\n", org_pos, prev_pos ); if( prev_pos != org_pos ) { for( i = 1; i < ( org_pos - prev_pos ) ; i++ ) { //fprintf( stdout, "prev_pos + i : %d\n", prev_pos + i ); move_window( NULL, 0, // range org_chr, prev_pos+i, //org_pos, '*', //base, 0, //flag, -1, //qual, threshold ); } prev_pos = org_pos; is_new_pos = 1; } else { is_new_pos = 0; } } else // new chromosome { fprintf( stderr, "clear windows: prv_chr(%s), prv_pos(%d+1)\n", prev_chr, prev_pos ); fprintf( stderr, "clear windows: org_chr(%s), org_pos(%d)\n", org_chr, org_pos ); clean_windows( NULL, prev_chr, prev_pos, length, threshold); destroy_prob_window(); init_prob_window( length ); fprintf( stderr, "%s -> %s *\n", prev_chr, org_chr ); is_new_pos = 0; strcpy( prev_chr, org_chr ); } // prg_chr starts with "chr", e.g. chrX, chr1 // fnd_chr depends on how BWA and SAM tools work if( org_pos == fnd_pos ) { //fprintf( stderr, "%9d:%9d:%d\n", org_pos, fnd_pos, qual ); } else { qual = 0; } //fprintf( stdout, "%9d:%09d:%09d:%d\n", prev_pos, org_pos, fnd_pos, qual ); if( ( strcmp( &org_chr[3], fnd_chr ) == 0 ) || ( strcmp( &org_chr[3], &fnd_chr[3] ) == 0 ) || ( strcmp( org_chr , fnd_chr ) == 0 ) || ( strcmp( org_chr , &fnd_chr[3] ) == 0 ) ) { //fprintf( stderr, "same %s == %s\n", org_chr, fnd_chr ); if( is_new_pos == 0 ) { fill_window( NULL, range, org_chr, org_pos, base, flag, qual, threshold ); } else { // fprintf( stderr, "\n" ); move_window( NULL, range, org_chr, org_pos, base, flag, qual, threshold ); } } else { //fprintf( stderr, "diff %s != %s\n", org_chr, fnd_chr ); if( is_new_pos == 0 ) { fill_window( NULL, range, org_chr, org_pos, base, flag, -1, threshold ); } else { // fprintf( stderr, "\n" ); move_window( NULL, range, org_chr, org_pos, base, flag, -1, threshold ); } } } // while //clean_windows( NULL, org_chr, org_pos+1, length); clean_windows( NULL, org_chr, org_pos, length, threshold); destroy_prob_window(); }