aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/undo.c
diff options
context:
space:
mode:
authorEliseo Martínez <eliseomarmol@gmail.com>2014-12-21 12:36:16 +0100
committerEliseo Martínez <eliseomarmol@gmail.com>2014-12-24 20:00:04 +0100
commitd2745a59f830b1671c8951ba2a52c2f153191932 (patch)
tree8140acb941ead1c3de227e637a186f4213ef21bb /src/nvim/undo.c
parent59985523b851fe61b62afcef69f4564f064cbc0a (diff)
downloadrneovim-d2745a59f830b1671c8951ba2a52c2f153191932.tar.gz
rneovim-d2745a59f830b1671c8951ba2a52c2f153191932.tar.bz2
rneovim-d2745a59f830b1671c8951ba2a52c2f153191932.zip
Remove long_u: put_bytes(): Refactor.
Remove all long_u instances due to put_bytes() function. First, function signature is changed this way: - nr : long_u --> uintmax_t uintmax_t is chosen so that invocations can use any unsigned integer type (including size_t) without needing to cast. - len : int --> unsigned int This is to pass the size in bytes of the previous param, thus an unsigned int is enough. All invocations use positive integer literals, so change is safe without the need for casts. Then, function implementation is adapted accordingly. Last, all invocation points are refactored this way: - Refactor types to minimize casts. - Inline declarations (C99 style) in containing function. All this changes were done with -Wconversion temporarily activated for spell.c and undo.c, so that we can assert changes are type-safe and do not introduce any warnings to that respect.
Diffstat (limited to 'src/nvim/undo.c')
-rw-r--r--src/nvim/undo.c78
1 files changed, 35 insertions, 43 deletions
diff --git a/src/nvim/undo.c b/src/nvim/undo.c
index 9a3da5bcdb..0dddfe703e 100644
--- a/src/nvim/undo.c
+++ b/src/nvim/undo.c
@@ -715,42 +715,40 @@ static void u_free_uhp(u_header_T *uhp)
static int serialize_header(FILE *fp, buf_T *buf, char_u *hash)
{
- int len;
-
/* Start writing, first the magic marker and undo info version. */
- if (fwrite(UF_START_MAGIC, (size_t)UF_START_MAGIC_LEN, (size_t)1, fp) != 1)
+ if (fwrite(UF_START_MAGIC, UF_START_MAGIC_LEN, 1, fp) != 1)
return FAIL;
- put_bytes(fp, (long_u)UF_VERSION, 2);
+ put_bytes(fp, UF_VERSION, 2);
/* Write a hash of the buffer text, so that we can verify it is still the
* same when reading the buffer text. */
- if (fwrite(hash, (size_t)UNDO_HASH_SIZE, (size_t)1, fp) != 1)
+ if (fwrite(hash, UNDO_HASH_SIZE, 1, fp) != 1)
return FAIL;
/* buffer-specific data */
- put_bytes(fp, (long_u)buf->b_ml.ml_line_count, 4);
- len = buf->b_u_line_ptr != NULL ? (int)STRLEN(buf->b_u_line_ptr) : 0;
- put_bytes(fp, (long_u)len, 4);
+ put_bytes(fp, (uintmax_t)buf->b_ml.ml_line_count, 4);
+ size_t len = buf->b_u_line_ptr ? STRLEN(buf->b_u_line_ptr) : 0;
+ put_bytes(fp, len, 4);
if (len > 0 && fwrite(buf->b_u_line_ptr, len, 1, fp) != 1)
return FAIL;
- put_bytes(fp, (long_u)buf->b_u_line_lnum, 4);
- put_bytes(fp, (long_u)buf->b_u_line_colnr, 4);
+ put_bytes(fp, (uintmax_t)buf->b_u_line_lnum, 4);
+ put_bytes(fp, (uintmax_t)buf->b_u_line_colnr, 4);
/* Undo structures header data */
put_header_ptr(fp, buf->b_u_oldhead);
put_header_ptr(fp, buf->b_u_newhead);
put_header_ptr(fp, buf->b_u_curhead);
- put_bytes(fp, (long_u)buf->b_u_numhead, 4);
- put_bytes(fp, (long_u)buf->b_u_seq_last, 4);
- put_bytes(fp, (long_u)buf->b_u_seq_cur, 4);
+ put_bytes(fp, (uintmax_t)buf->b_u_numhead, 4);
+ put_bytes(fp, (uintmax_t)buf->b_u_seq_last, 4);
+ put_bytes(fp, (uintmax_t)buf->b_u_seq_cur, 4);
put_time(fp, buf->b_u_time_cur);
/* Optional fields. */
putc(4, fp);
putc(UF_LAST_SAVE_NR, fp);
- put_bytes(fp, (long_u)buf->b_u_save_nr_last, 4);
+ put_bytes(fp, (uintmax_t)buf->b_u_save_nr_last, 4);
putc(0, fp); /* end marker */
@@ -759,22 +757,19 @@ static int serialize_header(FILE *fp, buf_T *buf, char_u *hash)
static int serialize_uhp(FILE *fp, buf_T *buf, u_header_T *uhp)
{
- int i;
- u_entry_T *uep;
-
- if (put_bytes(fp, (long_u)UF_HEADER_MAGIC, 2) == FAIL)
+ if (put_bytes(fp, UF_HEADER_MAGIC, 2) == FAIL)
return FAIL;
put_header_ptr(fp, uhp->uh_next.ptr);
put_header_ptr(fp, uhp->uh_prev.ptr);
put_header_ptr(fp, uhp->uh_alt_next.ptr);
put_header_ptr(fp, uhp->uh_alt_prev.ptr);
- put_bytes(fp, uhp->uh_seq, 4);
+ put_bytes(fp, (uintmax_t)uhp->uh_seq, 4);
serialize_pos(uhp->uh_cursor, fp);
- put_bytes(fp, (long_u)uhp->uh_cursor_vcol, 4);
- put_bytes(fp, (long_u)uhp->uh_flags, 2);
+ put_bytes(fp, (uintmax_t)uhp->uh_cursor_vcol, 4);
+ put_bytes(fp, (uintmax_t)uhp->uh_flags, 2);
/* Assume NMARKS will stay the same. */
- for (i = 0; i < NMARKS; ++i)
+ for (size_t i = 0; i < NMARKS; ++i)
serialize_pos(uhp->uh_namedm[i], fp);
serialize_visualinfo(&uhp->uh_visual, fp);
put_time(fp, uhp->uh_time);
@@ -782,17 +777,17 @@ static int serialize_uhp(FILE *fp, buf_T *buf, u_header_T *uhp)
/* Optional fields. */
putc(4, fp);
putc(UHP_SAVE_NR, fp);
- put_bytes(fp, (long_u)uhp->uh_save_nr, 4);
+ put_bytes(fp, (uintmax_t)uhp->uh_save_nr, 4);
putc(0, fp); /* end marker */
/* Write all the entries. */
- for (uep = uhp->uh_entry; uep != NULL; uep = uep->ue_next) {
- put_bytes(fp, (long_u)UF_ENTRY_MAGIC, 2);
+ for (u_entry_T *uep = uhp->uh_entry; uep; uep = uep->ue_next) {
+ put_bytes(fp, UF_ENTRY_MAGIC, 2);
if (serialize_uep(fp, buf, uep) == FAIL)
return FAIL;
}
- put_bytes(fp, (long_u)UF_ENTRY_END_MAGIC, 2);
+ put_bytes(fp, UF_ENTRY_END_MAGIC, 2);
return OK;
}
@@ -875,16 +870,13 @@ static u_header_T *unserialize_uhp(FILE *fp, char_u *file_name)
*/
static int serialize_uep(FILE *fp, buf_T *buf, u_entry_T *uep)
{
- int i;
- size_t len;
-
- put_bytes(fp, (long_u)uep->ue_top, 4);
- put_bytes(fp, (long_u)uep->ue_bot, 4);
- put_bytes(fp, (long_u)uep->ue_lcount, 4);
- put_bytes(fp, (long_u)uep->ue_size, 4);
- for (i = 0; i < uep->ue_size; ++i) {
- len = STRLEN(uep->ue_array[i]);
- if (put_bytes(fp, (long_u)len, 4) == FAIL)
+ put_bytes(fp, (uintmax_t)uep->ue_top, 4);
+ put_bytes(fp, (uintmax_t)uep->ue_bot, 4);
+ put_bytes(fp, (uintmax_t)uep->ue_lcount, 4);
+ put_bytes(fp, (uintmax_t)uep->ue_size, 4);
+ for (size_t i = 0; i < (size_t)uep->ue_size; ++i) {
+ size_t len = STRLEN(uep->ue_array[i]);
+ if (put_bytes(fp, len, 4) == FAIL)
return FAIL;
if (len > 0 && fwrite(uep->ue_array[i], len, 1, fp) != 1)
return FAIL;
@@ -938,9 +930,9 @@ static u_entry_T *unserialize_uep(FILE *fp, int *error, char_u *file_name)
*/
static void serialize_pos(pos_T pos, FILE *fp)
{
- put_bytes(fp, (long_u)pos.lnum, 4);
- put_bytes(fp, (long_u)pos.col, 4);
- put_bytes(fp, (long_u)pos.coladd, 4);
+ put_bytes(fp, (uintmax_t)pos.lnum, 4);
+ put_bytes(fp, (uintmax_t)pos.col, 4);
+ put_bytes(fp, (uintmax_t)pos.coladd, 4);
}
/*
@@ -966,8 +958,8 @@ static void serialize_visualinfo(visualinfo_T *info, FILE *fp)
{
serialize_pos(info->vi_start, fp);
serialize_pos(info->vi_end, fp);
- put_bytes(fp, (long_u)info->vi_mode, 4);
- put_bytes(fp, (long_u)info->vi_curswant, 4);
+ put_bytes(fp, (uintmax_t)info->vi_mode, 4);
+ put_bytes(fp, (uintmax_t)info->vi_curswant, 4);
}
/*
@@ -987,7 +979,7 @@ static void unserialize_visualinfo(visualinfo_T *info, FILE *fp)
* pointers when reading. */
static void put_header_ptr(FILE *fp, u_header_T *uhp)
{
- put_bytes(fp, (long_u)(uhp != NULL ? uhp->uh_seq : 0), 4);
+ put_bytes(fp, (uintmax_t)(uhp != NULL ? uhp->uh_seq : 0), 4);
}
/*
@@ -1178,7 +1170,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash)
uhp = uhp->uh_next.ptr;
}
- if (put_bytes(fp, (long_u)UF_HEADER_END_MAGIC, 2) == OK)
+ if (put_bytes(fp, UF_HEADER_END_MAGIC, 2) == OK)
write_ok = true;
#ifdef U_DEBUG
if (headers_written != buf->b_u_numhead) {