int tabtoh::convert(){ bool err = 1; QString instr_mnemonic; QString num_of_instr_str; QString temp_line; QStringList instr_description; QStringList converted_instr_description; QStringList instr_names_list; int num_of_instr = 0; int instr_num_of_lines = 0; qDebug()<<"(tabtoh) Counting instructions ..."; num_of_instr = count_instructions(); num_of_instr_str = QString::number(num_of_instr, 10); qDebug()<<"(tabtoh) Instructions: "<<num_of_instr; qDebug()<<"(tabtoh) Extracting instructions ..."; for(int i = 0; i < input_file.size(); i++){ if(input_file.at(i).contains("INSTR")){ err = 0; instr_mnemonic = input_file.at(i).section('\t', 1, 1); instr_mnemonic.prepend("&"); instr_names_list << instr_mnemonic; for(int j = i; j < input_file.size(); j++){ instr_description << input_file.at(j); instr_num_of_lines++; if(is_empty_line(input_file.at(j))){ break; } } if(instr_num_of_lines){ converted_instr_description << parse_and_convert_instr(instr_description); instr_num_of_lines = 0; instr_description.clear(); } } } insert_comment("/* Automatically generated by tabtoh */", output_file); insert_newline(output_file); insert_ifndef("ISA_H_", output_file); insert_include("common.h", output_file); insert_newline(output_file); insert_define("NUM_OF_INSTRUCTIONS", num_of_instr_str.toLatin1().data(), output_file); insert_newline(output_file); output_file += converted_instr_description; insert_newline(output_file); insert_array("const instruction_t*", model_name_str.toLatin1().data(), "NUM_OF_INSTRUCTIONS", instr_names_list, output_file); insert_endif(output_file); if(!err){ write_entire_file(output_file_str, output_file); } return err; }
/* * Name: combine_wrap_spill * Purpose: combine word wrap lines so we don't push each word onto a * separate line. * Date: November 27, 1991 * Passed: window: pointer to current window * wrap_col: col to combine next line * lm: left margin * rm: right margin * side: left or right margin to insert spaces * new_line: boolean, should we insert a new line? */ void combine_wrap_spill( WINDOW *window, int wrap_col, int lm, int rm, int side, int new_line ) { line_list_ptr p; /* line we wrapped */ line_list_ptr pp; /* pointer to next line after wrapped line */ int p_len; /* length of line we just word wrapped */ int non_blank; /* first non-blank column on next line */ int control_t; /* number of times to call word_delete */ int next_line_len; /* length of next line counting from 1st word */ WINDOW w; /* scratch window */ dup_window_info( &w, window ); g_status.command = WordWrap; w.rcol = wrap_col; if (new_line) { insert_newline( &w ); if (mode.right_justify == TRUE) justify_right_margin( &w, w.ll->prev, mode.word_wrap == FIXED_WRAP ? find_left_margin( w.ll->prev, mode.word_wrap ) : lm, rm, side ); p = window->ll->next; } else p = window->ll; if (p != NULL) { p_len = find_end( p->line, p->len ); pp = p->next; if (pp != NULL) { non_blank = first_non_blank( pp->line, pp->len ); next_line_len = find_end( pp->line, pp->len ) - non_blank; if (!is_line_blank( pp->line, pp->len ) && p_len + next_line_len <= rm) { control_t = 1; if (mode.inflate_tabs) { if (*pp->line == ' ' || *pp->line == '\t') ++control_t; } else if (*pp->line == ' ') ++control_t; w.ll = p; w.rcol = p_len + 1; if (*(p->line+p_len-1) == '.') ++w.rcol; while (control_t--) word_delete( &w ); remove_spaces( lm ); un_copy_line( w.ll, &w, TRUE ); } window->file_info->dirty = GLOBAL; } } }
int main(int argc, char **argv) { if (argc != 2) { printf("usage: %s %s\n", argv[0], "filename"); return 0; } FILE *file = fopen(argv[1], "r+"); if (!file) { perror("Could not open file.\n"); return 1; } insert_newline(file); char line[200] = ""; while (fgets(line, 200, file) && !feof(file)) { longest_word(line); } fclose(file); return 0; }
/* * Print the list of completions in a set of columns. */ static void completion_print (gl_list_t l, size_t size) { size_t i, j, col, max, numcols; max = calculate_max_length (l, size) + 5; numcols = (get_window_ewidth (cur_wp) - 1) / max; bprintf ("Possible completions are:\n"); for (i = col = 0; i < MIN (size, gl_list_size (l)); i++) { char *s = (char *) gl_list_get_at (l, i); size_t len = strlen (s); if (col >= numcols) { col = 0; insert_newline (); } insert_nstring (s, len); for (j = max - len; j > 0; --j) insert_char_in_insert_mode (' '); ++col; } }
int main(int argc, char **argv) { if (argc != 2) { printf("usage: %s %s\n", argv[0], "filename"); return 0; } FILE *file = fopen(argv[1], "r+"); if (!file) { perror("Could not open file.\n"); return 1; } insert_newline(file); static const unsigned MAX_N = 100; /* maximum NxN size is 10x10 */ char matrix[MAX_N]; unsigned ncount = 0; char c; while ((c = fgetc(file)) && !feof(file)) { if (c == '\n') { unsigned n = sqrt(ncount); /* row/column length */ char rotate[MAX_N]; unsigned iter, k; /* offset */ for (iter = 0, k = 0; iter < n; iter++) { unsigned place, l; /* rotation */ for (l = 0, place = ncount + iter; l < n; l++) { place -= n; rotate[k++] = matrix[place]; } } unsigned j; /* output */ for (j = 0; j <= ncount - 1; j++) { printf("%c", rotate[j]); if (j != ncount - 1) printf(" "); else printf("\n"); } unsigned i; /* wiping persistent data for next run */ for (i = 0; i <= ncount; i++) { matrix[i] = '\0'; } ncount = 0; } else { if (c != ' ') { matrix[ncount] = c; ncount++; } } } fclose(file); return 0; }
/* * Revert an action. Return the next undo entry. */ static undop revert_action(undop up) { int i; doing_undo = TRUE; if (up->type == UNDO_END_SEQUENCE) { undo_save(UNDO_START_SEQUENCE, up->pointn, up->pointo, 0, 0); up = up->next; while (up->type != UNDO_START_SEQUENCE) { revert_action(up); up = up->next; } undo_save(UNDO_END_SEQUENCE, up->pointn, up->pointo, 0, 0); goto_point(up->pointn, up->pointo); return up->next; } goto_point(up->pointn, up->pointo); switch (up->type) { case UNDO_INSERT_CHAR: if (up->delta.c == '\n') insert_newline(); else insert_char_in_insert_mode(up->delta.c); break; case UNDO_INTERCALATE_CHAR: if (up->delta.c == '\n') intercalate_newline(); else intercalate_char(up->delta.c); break; case UNDO_INSERT_BLOCK: undo_save(UNDO_REMOVE_BLOCK, up->pointn, up->pointo, up->delta.block.size, 0); undo_nosave = TRUE; for (i = 0; i < up->delta.block.size; i++) if (up->delta.block.text[i] != '\n') insert_char(up->delta.block.text[i]); else insert_newline(); undo_nosave = FALSE; break; case UNDO_REMOVE_CHAR: FUNCALL(delete_char); break; case UNDO_REMOVE_BLOCK: undo_save(UNDO_INSERT_BLOCK, up->pointn, up->pointo, up->delta.block.size, 0); undo_nosave = TRUE; for (i = 0; i < up->delta.block.size; i++) FUNCALL(delete_char); undo_nosave = FALSE; break; case UNDO_REPLACE_CHAR: undo_save(UNDO_REPLACE_CHAR, up->pointn, up->pointo, cur_wp->pointp->text[up->pointo], 0); cur_wp->pointp->text[up->pointo] = up->delta.c; cur_bp->flags |= BFLAG_MODIFIED; if (cur_bp->flags & BFLAG_FONTLOCK) font_lock_reset_anchors(cur_bp, cur_wp->pointp); break; case UNDO_REPLACE_BLOCK: undo_save(UNDO_REPLACE_BLOCK, up->pointn, up->pointo, up->delta.block.size, up->delta.block.osize); undo_nosave = TRUE; for (i = 0; i < up->delta.block.size; i++) FUNCALL(delete_char); for (i = 0; i < up->delta.block.osize; i++) if (up->delta.block.text[i] != '\n') insert_char(up->delta.block.text[i]); else insert_newline(); undo_nosave = FALSE; break; } doing_undo = FALSE; return up->next; }
/* * Name: word_wrap * Purpose: make sure lines don't get longer than right margin * Date: November 27, 1991 * Passed: window: pointer to current window * Notes: rcol, lm, rm, pm all start counting at zero. * len (line length) starts counting at 1. * * when we compare margins and line lengths, we either have to * add one to the margins or subtract one from the len. I add * one to the margins. */ void word_wrap( WINDOW *window ) { int c; /* character the user just entered. */ register int len; /* length of current line */ int i; /* padding spaces required */ line_list_ptr p; /* line above wrapped line */ int rcol; int lm; int rm; int side; register WINDOW *win; /* put window pointer in a register */ win = window; /* * set up a few local variables. */ c = g_status.key_pressed; rcol = win->rcol; copy_line( win->ll ); detab_linebuff( ); /* * always start the right margin justification on the right side * at the beginning of paragraphs. then, alternate with left margin. */ side = 1; p = win->ll->prev; while (p != NULL && !is_line_blank( p->line, p->len )) { ++side; p = p->prev; } side = (side & 1) ? RIGHT : LEFT; /* * when we wrap, we need know where the left margin is. * let's look at the line above to see if this is the first line * in a paragraph. */ p = win->ll->prev; lm = find_left_margin( win->ll, mode.word_wrap ); rm = mode.right_margin; /* * there two ways that words are pushed onto next line. * 1. if the word being typed goes over the right margin * 2. typing a word in the middle of the line pushes words at end of * line to next line * * if the user enters spaces past the right margin then we don't * word wrap spaces. */ len = g_status.line_buff_len; if (rcol > rm+1 && c != ' ') { /* * if this is the first line in a paragraph then set left margin * to paragraph margin. */ if ((p == NULL || is_line_blank( p->line, p->len )) && first_non_blank( (text_ptr)g_status.line_buff, g_status.line_buff_len ) > rm && mode.word_wrap == FIXED_WRAP) lm = mode.parg_margin; /* * simple word wrap. the cursor goes past the right margin. * find the beginning of the word and put it on a new line. * * Special case - if the word begins at the left margin then * don't wrap it. */ for (i=rcol-1; i > lm && g_status.line_buff[i] != ' '; ) i--; if (i > lm) { i++; win->rcol = i; g_status.command = WordWrap; insert_newline( win ); if (mode.right_justify == TRUE) justify_right_margin( win, win->ll->prev, mode.word_wrap == FIXED_WRAP ? find_left_margin( win->ll->prev, mode.word_wrap ) : lm, rm, side ); /* * find out where to place the cursor on the new line. */ win->rcol = lm + rcol - i; check_virtual_col( win, win->rcol, win->rcol ); /* * we just wrapped the word at the eol. now, let's see if * we can combine it with the line below. since just added * a line, set new_line to false - don't add another line. */ len = find_end( win->ll->line, win->ll->len ); if (len < rm+1) combine_wrap_spill( win, len, lm, rm, side, FALSE ); } } else if (len > rm+1) { /* * this is the second word wrap case. we are pushing words onto * next line. we need to now what character is in the right margin. * * 1) if the character is not a space, then we need to search backwards * to find the start of the word that is on the right margin. * 2) if the character is a space, then we need to search forward to * find the word that is over the right margin. */ /* * don't wrap spaces past right margin */ if (c == ' ' && rcol > rm) { for (i=rcol; i<len && g_status.line_buff[i] == ' ';) i++; /* * if i == len then all that's left on line is blanks - don't wrap. */ if (i < len) combine_wrap_spill( win, i, lm, rm, side, TRUE ); } else if (g_status.line_buff[rm+1] != ' ') { /* * search backwards for the word to put on next line. */ for (i=rm+1; i > lm && g_status.line_buff[i] != ' '; ) i--; /* * if we search all the way back to left margin then test for * a special case - see the matching else for more info. */ if (i > lm) { i++; /* * if i > rcol then cursor stays on same line. */ if (i > rcol) { combine_wrap_spill( win, i, lm, rm, side, TRUE ); /* * split the line at or behind the cursor. almost the * same as when the cursor goes over the right margin. */ } else if (i <= rcol) { win->rcol = i; g_status.command = WordWrap; insert_newline( win ); if (mode.right_justify == TRUE) justify_right_margin( win, win->ll->prev, mode.word_wrap == FIXED_WRAP ? find_left_margin( win->ll->prev, mode.word_wrap ) : lm, rm, side ); win->rcol = lm + rcol - i; check_virtual_col( win, win->rcol, win->rcol ); len = find_end( win->ll->line, win->ll->len ); if (len < rm+1) combine_wrap_spill( win, len, lm, rm, side, FALSE ); } } /* * if the user changed margins or for some reason there's a long * text line, let's see if there are any words past the right * margin. if we get to this else, we know the current word * begins at least at the left margin. * * now search forwards for a break */ } else { /* * go to the right margin and see if there are any words past * right margin. */ for (i=rm+1; i<len && g_status.line_buff[i] == ' '; ) i++; /* * we either found a space or the eol. test for eol. * if i == len then this is one big word - don't wrap it. */ if (i != len) combine_wrap_spill( win, i, lm, rm, side, TRUE ); } } }