diff options
author | Florian Walch <fwalch@users.noreply.github.com> | 2015-04-09 00:02:09 +0300 |
---|---|---|
committer | Florian Walch <fwalch@users.noreply.github.com> | 2015-04-09 00:02:09 +0300 |
commit | 04e09ba74279d286985333486c43f14a935a13c2 (patch) | |
tree | 0f6dbc6ca1ddcb79f53d7c42b6adc18ba65f6727 /src/nvim/misc2.c | |
parent | e1bac3b84090d07afa669a8b79816c28813c605e (diff) | |
parent | dea2731184f23521f5eae03ab46cbe811b8825b7 (diff) | |
download | rneovim-04e09ba74279d286985333486c43f14a935a13c2.tar.gz rneovim-04e09ba74279d286985333486c43f14a935a13c2.tar.bz2 rneovim-04e09ba74279d286985333486c43f14a935a13c2.zip |
Merge pull request #1805 from fwalch/vim-7.4.399
vim-patch: Port parts of Vim 7.4.399.
Diffstat (limited to 'src/nvim/misc2.c')
-rw-r--r-- | src/nvim/misc2.c | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/nvim/misc2.c b/src/nvim/misc2.c index fafce66c5f..79b42cabf0 100644 --- a/src/nvim/misc2.c +++ b/src/nvim/misc2.c @@ -9,6 +9,7 @@ /* * misc2.c: Various functions. */ +#include <assert.h> #include <errno.h> #include <inttypes.h> #include <string.h> @@ -29,6 +30,7 @@ #include "nvim/fileio.h" #include "nvim/fold.h" #include "nvim/getchar.h" +#include "nvim/macros.h" #include "nvim/mark.h" #include "nvim/mbyte.h" #include "nvim/memfile.h" @@ -459,22 +461,33 @@ char *read_string(FILE *fd, size_t cnt) return (char *)str; } -/// Write a number to file "fd", MSB first, in "len" bytes. -/// @return OK/FAIL. -int put_bytes(FILE *fd, uintmax_t number, unsigned int len) +/// Writes a number to file "fd", most significant bit first, in "len" bytes. +/// @returns false in case of an error. +bool put_bytes(FILE *fd, uintmax_t number, size_t len) { - for (unsigned int i = len - 1; i < len; --i) - if (putc((int)(number >> (i * 8)), fd) == EOF) - return FAIL; - return OK; + assert(len > 0); + for (size_t i = len - 1; i < len; i--) { + if (putc((int)(number >> (i * 8)), fd) == EOF) { + return false; + } + } + return true; } -/// Write time_t to file "fd" in 8 bytes. +/// Writes time_t to file "fd" in 8 bytes. void put_time(FILE *fd, time_t time_) { + uint8_t buf[8]; + time_to_bytes(time_, buf); + fwrite(buf, sizeof(uint8_t), ARRAY_SIZE(buf), fd); +} + +/// Writes time_t to "buf[8]". +void time_to_bytes(time_t time_, uint8_t buf[8]) +{ // time_t can be up to 8 bytes in size, more than uintmax_t in 32 bits // systems, thus we can't use put_bytes() here. - for (unsigned int i = 7; i < 8; --i) { - putc((int)((uint64_t)time_ >> (i * 8)), fd); + for (size_t i = 7, bufi = 0; bufi < 8; i--, bufi++) { + buf[bufi] = (uint8_t)((uint64_t)time_ >> (i * 8)); } } |