qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs hex.c unihex.c image.c video.c qe.h qe.c


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs hex.c unihex.c image.c video.c qe.h qe.c
Date: Thu, 16 Jan 2014 14:00:29 +0000

CVSROOT:        /sources/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        14/01/16 14:00:29

Modified files:
        .              : hex.c unihex.c image.c video.c qe.h qe.c 

Log message:
        use buf_t in get_mode_line functions, add compute_percent
        
        * add compute_percent to compute percentages safely and correctly
        * changed get_mode_line API
        * simplify code

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/hex.c?cvsroot=qemacs&r1=1.34&r2=1.35
http://cvs.savannah.gnu.org/viewcvs/qemacs/unihex.c?cvsroot=qemacs&r1=1.19&r2=1.20
http://cvs.savannah.gnu.org/viewcvs/qemacs/image.c?cvsroot=qemacs&r1=1.24&r2=1.25
http://cvs.savannah.gnu.org/viewcvs/qemacs/video.c?cvsroot=qemacs&r1=1.15&r2=1.16
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.h?cvsroot=qemacs&r1=1.121&r2=1.122
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.c?cvsroot=qemacs&r1=1.123&r2=1.124

Patches:
Index: hex.c
===================================================================
RCS file: /sources/qemacs/qemacs/hex.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -b -r1.34 -r1.35
--- hex.c       15 Jan 2014 15:54:27 -0000      1.34
+++ hex.c       16 Jan 2014 14:00:28 -0000      1.35
@@ -304,18 +304,11 @@
     }
 }
 
-static int hex_mode_line(EditState *s, char *buf, int buf_size)
+static void hex_mode_line(EditState *s, buf_t *out)
 {
-    int percent, pos;
-
-    pos = basic_mode_line(s, buf, buf_size, '-');
-    pos += snprintf(buf + pos, buf_size - pos, "0x%x--0x%x",
-                    s->offset, s->b->total_size);
-    percent = 0;
-    if (s->b->total_size > 0)
-        percent = (s->offset * 100) / s->b->total_size;
-    pos += snprintf(buf + pos, buf_size - pos, "--%d%%", percent);
-    return pos;
+    basic_mode_line(s, out, '-');
+    buf_printf(out, "0x%x--0x%x", s->offset, s->b->total_size);
+    buf_printf(out, "--%d%%", compute_percent(s->offset, s->b->total_size));
 }
 
 static ModeDef binary_mode = {

Index: unihex.c
===================================================================
RCS file: /sources/qemacs/qemacs/unihex.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -b -r1.19 -r1.20
--- unihex.c    15 Jan 2014 15:54:27 -0000      1.19
+++ unihex.c    16 Jan 2014 14:00:28 -0000      1.20
@@ -195,20 +195,13 @@
     s->offset = eb_goto_char(s->b, pos);
 }
 
-static int unihex_mode_line(EditState *s, char *buf, int buf_size)
+static void unihex_mode_line(EditState *s, buf_t *out)
 {
-    int percent, pos, cpos;
-
-    cpos = eb_get_char_offset(s->b, s->offset);
-
-    pos = basic_mode_line(s, buf, buf_size, '-');
-    pos += snprintf(buf + pos, buf_size - pos, "0x%x--0x%x--%s",
-                    cpos, s->offset, s->b->charset->name);
-    percent = 0;
-    if (s->b->total_size > 0)
-        percent = (s->offset * 100) / s->b->total_size;
-    pos += snprintf(buf + pos, buf_size - pos, "--%d%%", percent);
-    return pos;
+    basic_mode_line(s, out, '-');
+    buf_printf(out, "0x%x--0x%x--%s",
+               eb_get_char_offset(s->b, s->offset),
+               s->offset, s->b->charset->name);
+    buf_printf(out, "--%d%%", compute_percent(s->offset, s->b->total_size));
 }
 
 static ModeDef unihex_mode = {

Index: image.c
===================================================================
RCS file: /sources/qemacs/qemacs/image.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -b -r1.24 -r1.25
--- image.c     14 Jan 2014 03:29:37 -0000      1.24
+++ image.c     16 Jan 2014 14:00:28 -0000      1.25
@@ -758,14 +758,14 @@
     update_bmp(e);
 }
 
-int image_mode_line(EditState *s, char *buf, int buf_size)
+void image_mode_line(EditState *s, buf_t *out)
 {
-    int pos;
     EditBuffer *b = s->b;
     ImageBuffer *ib = b->data;
     char alpha_mode;
 
-    pos = basic_mode_line(s, buf, buf_size, '-');
+    basic_mode_line(s, out, '-');
+
     if (ib->alpha_info & FF_ALPHA_SEMI_TRANSP)
         alpha_mode = 'A';
     else if (ib->alpha_info & FF_ALPHA_TRANSP)
@@ -773,12 +773,11 @@
     else
         alpha_mode = ' ';
 
-    pos += snprintf(buf + pos, buf_size - pos, "%dx%d %s %c%c",
+    buf_printf(out, "%dx%d %s %c%c",
                     ib->width, ib->height,
                     avcodec_get_pix_fmt_name(ib->pix_fmt),
                     alpha_mode,
                     ib->interleaved ? 'I' : ' ');
-    return pos;
 }
 
 static void pixel_format_completion(CompleteState *cp)

Index: video.c
===================================================================
RCS file: /sources/qemacs/qemacs/video.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -b -r1.15 -r1.16
--- video.c     4 Jan 2014 17:28:37 -0000       1.15
+++ video.c     16 Jan 2014 14:00:28 -0000      1.16
@@ -860,30 +860,29 @@
     return buf;
 }
 
-static int video_mode_line(EditState *s, char *buf, int buf_size)
+static void video_mode_line(EditState *s, buf_t *out)
 {
-    int pos;
     const char *name;
     VideoState *is = s->mode_data;
     AVCodec *codec;
     AVCodecContext *dec;
     char buf1[32];
 
-    pos = basic_mode_line(s, buf, buf_size, '-');
-    if (is->paused) {
-        pos += snprintf(buf + pos, buf_size - pos, "[paused]--");
-    }
-    if (is->ic) {
-        pos += snprintf(buf + pos, buf_size - pos, "%s",
-                        is->ic->iformat->name);
-    }
+    basic_mode_line(s, out, '-');
+
+    if (is->paused)
+        buf_printf(out, "[paused]--");
+
+    if (is->ic)
+        buf_printf(out, "%s", is->ic->iformat->name);
+
     if (is->video_st) {
         name = "???";
         dec = &is->video_st->codec;
         codec = dec->codec;
         if (codec)
             name = codec->name;
-        pos += snprintf(buf + pos, buf_size - pos, "--%s/address@hidden",
+        buf_printf(out, "--%s/address@hidden",
                         name, get_stream_id(is->ic, is->video_st, buf1, 
sizeof(buf1)),
                         dec->width, dec->height,
                         (float)dec->frame_rate / dec->frame_rate_base);
@@ -894,11 +893,10 @@
         codec = dec->codec;
         if (codec)
             name = codec->name;
-        pos += snprintf(buf + pos, buf_size - pos, "--%s/%s[%dHz:%dch]",
+        buf_printf(out, "--%s/%s[%dHz:%dch]",
                         name, get_stream_id(is->ic, is->audio_st, buf1, 
sizeof(buf1)),
                         dec->sample_rate, dec->channels);
     }
-    return pos;
 }
 
 static void av_cycle_stream(EditState *s, int codec_type)

Index: qe.h
===================================================================
RCS file: /sources/qemacs/qemacs/qe.h,v
retrieving revision 1.121
retrieving revision 1.122
diff -u -b -r1.121 -r1.122
--- qe.h        15 Jan 2014 19:44:25 -0000      1.121
+++ qe.h        16 Jan 2014 14:00:28 -0000      1.122
@@ -445,6 +445,10 @@
         return a;
 }
 
+static inline int compute_percent(int a, int b) {
+    return b <= 0 ? 0 : (int)((long long)a * 100 / b);
+}
+
 /* charset.c */
 
 /* maximum number of bytes for a character in all the supported charsets */
@@ -1137,7 +1141,7 @@
 #define MODEF_NOCMD 0x0001 /* do not register xxx-mode command automatically */
 
     EditBufferDataType *data_type; /* native buffer data type (NULL = raw) */
-    int (*get_mode_line)(EditState *s, char *buf, int buf_size); /* return 
mode line */
+    void (*get_mode_line)(EditState *s, buf_t *out);
 
     /* mode specific key bindings */
     struct KeyDef *first_key;
@@ -1665,8 +1669,8 @@
 void do_up_down(EditState *s, int dir);
 void do_left_right(EditState *s, int dir);
 void text_mouse_goto(EditState *s, int x, int y);
-int basic_mode_line(EditState *s, char *buf, int buf_size, int c1);
-int text_mode_line(EditState *s, char *buf, int buf_size);
+void basic_mode_line(EditState *s, buf_t *out, int c1);
+void text_mode_line(EditState *s, buf_t *out);
 void do_toggle_full_screen(EditState *s);
 void do_toggle_control_h(EditState *s, int set);
 

Index: qe.c
===================================================================
RCS file: /sources/qemacs/qemacs/qe.c,v
retrieving revision 1.123
retrieving revision 1.124
diff -u -b -r1.123 -r1.124
--- qe.c        15 Jan 2014 19:44:25 -0000      1.123
+++ qe.c        16 Jan 2014 14:00:28 -0000      1.124
@@ -1945,7 +1945,7 @@
         pos = pos * (long long)s->b->total_size / 100;
         if (rel)
             pos += s->offset;
-        eb_get_pos(s->b, &line, &col, max(pos, 0));
+        eb_get_pos(s->b, &line, &col, clamp(pos, 0, s->b->total_size));
         line += (col > 0);
         goto getcol;
 
@@ -2051,11 +2051,10 @@
 
 /* compute string for the first part of the mode line (flags,
    filename, modename) */
-int basic_mode_line(EditState *s, char *buf, int buf_size, int c1)
+void basic_mode_line(EditState *s, buf_t *out, int c1)
 {
-    int mod, state, pos;
+    int mod, state;
 
-    pos = 0;
     mod = s->b->modified ? '*' : '-';
     if (s->b->flags & BF_LOADING)
         state = 'L';
@@ -2066,26 +2065,19 @@
     else
         state = '-';
 
-    pos += snprintf(buf + pos, buf_size - pos, "%c%c:%c%c  %-20s  (%s",
-                    c1,
-                    state,
-                    s->b->flags & BF_READONLY ? '%' : mod,
-                    mod,
-                    s->b->name,
-                    s->mode->name);
+    buf_printf(out, "%c%c:%c%c  %-20s  (%s",
+               c1, state, s->b->flags & BF_READONLY ? '%' : mod,
+               mod, s->b->name, s->mode->name);
     if (!s->insert)
-        pos += snprintf(buf + pos, buf_size - pos, " Ovwrt");
+        buf_printf(out, " Ovwrt");
     if (s->interactive)
-        pos += snprintf(buf + pos, buf_size - pos, " Interactive");
-    pos += snprintf(buf + pos, buf_size - pos, ")--");
-
-    return pos;
+        buf_printf(out, " Interactive");
+    buf_printf(out, ")--");
 }
 
-int text_mode_line(EditState *s, char *buf, int buf_size)
+void text_mode_line(EditState *s, buf_t *out)
 {
     int line_num, col_num, wrap_mode;
-    int percent, pos;
 
     wrap_mode = '-';
     if (!s->hex_mode) {
@@ -2094,42 +2086,33 @@
         else if (s->wrap == WRAP_WORD)
             wrap_mode = 'W';
     }
-    pos = basic_mode_line(s, buf, buf_size, wrap_mode);
+    basic_mode_line(s, out, wrap_mode);
 
     eb_get_pos(s->b, &line_num, &col_num, s->offset);
-    pos += snprintf(buf + pos, buf_size - pos, "L%d--C%d--%s",
+    buf_printf(out, "L%d--C%d--%s",
                     line_num + 1, col_num, s->b->charset->name);
-    if (s->bidir) {
-        pos += snprintf(buf + pos, buf_size - pos, "--%s",
-                        s->cur_rtl ? "RTL" : "LTR");
-    }
-    if (s->input_method) {
-        pos += snprintf(buf + pos, buf_size - pos, "--%s",
-                        s->input_method->name);
-    }
+    if (s->bidir)
+        buf_printf(out, "--%s", s->cur_rtl ? "RTL" : "LTR");
+
+    if (s->input_method)
+        buf_printf(out, "--%s", s->input_method->name);
 #if 0
-    pos += snprintf(buf + pos, buf_size - pos, "--[%d,%d]-[%d]",
-                    s->x_disp[0], s->x_disp[1], s->y_disp);
+    buf_printf(out, "--[%d,%d]-[%d]", s->x_disp[0], s->x_disp[1], s->y_disp);
 #endif
-    percent = 0;
-    if (s->b->total_size > 0)
-        percent = (s->offset * 100) / s->b->total_size;
-    pos += snprintf(buf + pos, buf_size - pos, "--%d%%", percent);
-    return pos;
+    buf_printf(out, "--%d%%", compute_percent(s->offset, s->b->total_size));
 }
 
 void display_mode_line(EditState *s)
 {
     char buf[MAX_SCREEN_WIDTH];
+    buf_t outbuf, *out;
     int y = s->ytop + s->height;
 
     if (s->flags & WF_MODELINE) {
-        s->mode->get_mode_line(s, buf, sizeof(buf));
+        out = buf_init(&outbuf, buf, sizeof(buf));
+        s->mode->get_mode_line(s, out);
         if (!strequal(buf, s->modeline_shadow)) {
-            print_at_byte(s->screen,
-                          s->xleft,
-                          y,
-                          s->width,
+            print_at_byte(s->screen, s->xleft, y, s->width,
                           s->qe_state->mode_line_height,
                           buf, QE_STYLE_MODE_LINE);
             pstrcpy(s->modeline_shadow, sizeof(s->modeline_shadow), buf);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]