remove trailing blank lines in write_file - change check_bound - gramscii - A simple editor for ASCII box-and-arrow charts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Tags
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 82a7c12e8338288ccb40d213fcc9e15d102aa968
 (DIR) parent 62713aaf65264950aecad9237d50e911d7dad893
 (HTM) Author: KatolaZ <katolaz@freaknet.org>
       Date:   Thu,  1 Aug 2019 16:42:36 +0100
       
       remove trailing blank lines in write_file - change check_bound
       
       Diffstat:
         M draw.c                              |      33 ++++++++++++++++---------------
         M files.c                             |      14 +++++++++++---
         M gramscii.h                          |       6 ++++--
         M lineset.c                           |       9 +++++++--
         M main.c                              |       2 +-
         M screen.c                            |      14 +++++++-------
       
       6 files changed, 47 insertions(+), 31 deletions(-)
       ---
 (DIR) diff --git a/draw.c b/draw.c
       @@ -94,7 +94,7 @@ void get_text(FILE *fc){
                                if (x >= WIDTH)
                                        x = orig_x;
                        }
       -                check_bound();
       +                check_bound(&x, &y);
                        status_bar();
                        show_cursor();
                }
       @@ -150,7 +150,7 @@ void get_box(FILE *fc){
                                goto update_box;
                        if (!move_around(c, fc)) 
                                continue;
       -                check_bound();
       +                check_bound(&x, &y);
                        redraw();
                        step = 1;
        update_box:
       @@ -166,19 +166,19 @@ update_box:
                mode = MOVE;
        }
        
       -void draw_arrow(int x, int y, char *a, int a_len, int fix){
       +void draw_arrow(int xl, int yl, char *a, int a_len, int fix){
        
                int i, j, cur_dir;
                char line;
                void (*f)(int, int, char);
        
       -        a_miny = a_maxy = y;
       +        a_miny = a_maxy = yl;
                if (fix == FIX)
                        f = set_xy;
                else
                        f = draw_xy;
        
       -        f(x,y,mark_st);
       +        f(xl, yl, mark_st);
                if (!a_len){
                        show_cursor();
                        return;
       @@ -189,27 +189,28 @@ void draw_arrow(int x, int y, char *a, int a_len, int fix){
                                /* If we are switching between horizontal and vertical, put a "corner" */
                                if (((cur_dir & DIR_HOR) && (a[i] & DIR_VER)) ||
                                    ((cur_dir & DIR_VER) && (a[i] & DIR_HOR))){
       -                                f(x,y,corner);
       +                                f(xl, yl, corner);
                                        show_cursor();
                                }
                        }
                        for(j=0; j<a[i+1]; j++){
                                line = (a[i] & DIR_L) || (a[i] & DIR_R) ? line_h : line_v;
       -                        x += progr_x(a[i]);
       -                        y += progr_y(a[i]);
       -                        if (y < a_miny) a_miny = y;
       -                        if (y > a_maxy) a_maxy = y;
       -                        f(x, y, line);
       +                        xl += progr_x(a[i]);
       +                        yl += progr_y(a[i]);
       +                        check_bound(&xl, &yl);
       +                        if (yl < a_miny) a_miny = yl;
       +                        if (yl > a_maxy) a_maxy = yl;
       +                        f(xl, yl, line);
                        }
                        /* f(x,y,mark_end);*/
                        cur_dir = a[i];
                }
                if (autoend){
                        if (cur_dir != DIR_N)
       -                        f(x,y, get_mark(cur_dir));
       +                        f(xl,yl, get_mark(cur_dir));
                }
                else 
       -                f(x,y,mark_end);
       +                f(xl,yl,mark_end);
                show_cursor();
        }
        
       @@ -235,7 +236,7 @@ void get_arrow(FILE *fc){
                                goto update_arrow;
                        if (!move_around(c, fc))
                                continue;
       -                check_bound();
       +                check_bound(&x, &y);
                        /* FIXME: if we are out of bound, do nothing? */
                        if (arrow_len == arrow_sz){
                                arrow_sz *=2;
       @@ -289,7 +290,7 @@ void erase(FILE *fc){
                show_cursor();
                while((c=fgetc(fc))!=EOF && c!=27 && c!= 'x' && c != '\n'){
                        if (!move_around(c, fc)) continue;
       -                check_bound();
       +                check_bound(&x, &y);
                        if (first || 
                            (y != orig_y && ! opened) ||
                            (y == orig_y && x != orig_x && !opened) ){
       @@ -354,7 +355,7 @@ void visual_box(FILE *fc){
                                        goto vis_exit;
                                        break;
                        } 
       -                check_bound();
       +                check_bound(&x, &y);
                        set_video(VIDEO_NRM);
                        redraw();
                        step = 1;
 (DIR) diff --git a/files.c b/files.c
       @@ -7,7 +7,7 @@
        
        void write_file(FILE *fc){
                FILE *fout;
       -        int i;
       +        int i, ne;
        
                if (!fname[0] || force_new){
                        get_string(fc, "Write to: ", fname, 255);
       @@ -23,8 +23,16 @@ void write_file(FILE *fc){
                        get_key(fc, "Error opening file.");
                        return;
                }
       +        ne = 0;
                for (i=0; i<HEIGHT; i++){
       -                fprintf(fout, "%s\n", screen.l[i].s);
       +                if (strlen(screen.l[i].s)){/* remove trailing blank lines */
       +                        /* put the empty lines preceeding the current non-empty one */
       +                        while (ne--)
       +                                fprintf(fout, "\n");
       +                        fprintf(fout, "%s\n", screen.l[i].s);
       +                        ne = 0;
       +                }
       +                else ne++;
                }
                fclose(fout);
                modified = 0;
       @@ -51,7 +59,7 @@ void load_file(FILE *fc){
                if ((fin=fopen(newfname, "r")) != NULL){
                        i = 0;
                        while((fgets(screen.l[i].s, WIDTH+1, fin)) != NULL && i<HEIGHT){
       -                        screen.l[i].lst = strlen(screen.l[i].s) - 1;
       +                        screen.l[i].lst = strlen(screen.l[i].s) - 2;
                                screen.l[i].n = i;
                                screen.l[i].s[strlen(screen.l[i].s)-1]='\0';
                                i++;
 (DIR) diff --git a/gramscii.h b/gramscii.h
       @@ -82,7 +82,9 @@ typedef struct{
        #define progr_x(d) ((d) == DIR_L ? -1 : (d) == DIR_R ? 1 : 0)
        #define progr_y(d) ((d) == DIR_U ? -1 : (d) == DIR_D ? 1 : 0)
        
       -#define DEBUG 1
       +/*
       + * #define DEBUG 1
       + */
        
        /** global variables **/ 
        
       @@ -139,7 +141,7 @@ struct termios t1, t2, t3;
        void reset_styles();
        void redraw();
        int move_around(char c, FILE *fc);
       -void check_bound();
       +void check_bound(int *x, int *y);
        void status_bar();
        void show_cursor();
        void set_cur(char c);
 (DIR) diff --git a/lineset.c b/lineset.c
       @@ -144,13 +144,18 @@ void copy_lines_to_ring(int y1, int y2, int which){
                else
                        idx = undo_cur + 1;
                if (idx >= undo_sz - 1){
       -                undo_sz += 10;
       -                tmp = realloc(undo, undo_sz * sizeof(lineset_t));
       +                tmp = realloc(undo, (undo_sz + 10) * sizeof(lineset_t));
                        if (tmp == NULL){
                                fprintf(stderr, "Error allocating undo buffer");
                                exit(1);
                        }
                        undo = tmp;
       +                for (i=0; i<10; i++){
       +                        undo[undo_sz + i].sz = 0;
       +                        undo[undo_sz + i].l = NULL;
       +                        undo[undo_sz + i].num = 0;
       +                }
       +                undo_sz += 10;
                }
                ensure_num_lines(&(undo[idx]), y2 - y1 + 1);
                for(i=y1; i<=y2; i++){
 (DIR) diff --git a/main.c b/main.c
       @@ -135,7 +135,7 @@ void commands(FILE *fc){
                                                break;
                                }
                        }
       -                check_bound();
       +                check_bound(&x, &y);
                        status_bar();
                        show_cursor();
                        step = 1;
 (DIR) diff --git a/screen.c b/screen.c
       @@ -196,11 +196,11 @@ void erase_screen(){
                        erase_line(i);
        }
        
       -void check_bound(){
       -        if (x<0) x=0;
       -        else if (x>=WIDTH) x = WIDTH-1;
       -        if (y<0) y=0;
       -        else if (y>=HEIGHT) y = HEIGHT -1;
       +void check_bound(int *x, int *y){
       +        if (*x<0) *x=0;
       +        else if (*x>=WIDTH) *x = WIDTH-1;
       +        if (*y<0) *y=0;
       +        else if (*y>=HEIGHT) *y = HEIGHT -1;
        }
        
        void reset_styles(){
       @@ -242,7 +242,7 @@ void go_to(int where){
                                y = HEIGHT/2;
                                break;
                }
       -        check_bound();
       +        check_bound(&x, &y);
                show_cursor();
        }
        
       @@ -279,7 +279,7 @@ void handle_goto(){
                                go_to(MIDDLE);
                                break;
                }
       -        check_bound();
       +        check_bound(&x, &y);
                show_cursor();
        }