From df072c3b2b20fb7d3d9d50b5ab0df92827aa628f Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 11 Sep 2016 04:01:41 +0200 Subject: refactor: eliminate misc2.c move `call_shell` to misc1.c Move some fns to state.c Move some fns to option.c Move some fns to memline.c Move `vim_chdir*` fns to file_search.c Move some fns to new module, bytes.c Move some fns to fileio.c --- src/nvim/fileio.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 6 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 89e5fdb776..4226f2e8d2 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -32,7 +32,6 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/misc2.h" #include "nvim/garray.h" #include "nvim/move.h" #include "nvim/normal.h" @@ -44,6 +43,7 @@ #include "nvim/screen.h" #include "nvim/search.h" #include "nvim/sha256.h" +#include "nvim/state.h" #include "nvim/strings.h" #include "nvim/ui.h" #include "nvim/types.h" @@ -4447,11 +4447,95 @@ bool vim_fgets(char_u *buf, int size, FILE *fp) FUNC_ATTR_NONNULL_ALL return eof == NULL; } -/* - * os_rename() only works if both files are on the same file system, this - * function will (attempts to?) copy the file across if rename fails -- webb - * Return -1 for failure, 0 for success. - */ +/// Read 2 bytes from "fd" and turn them into an int, MSB first. +int get2c(FILE *fd) +{ + int n; + + n = getc(fd); + n = (n << 8) + getc(fd); + return n; +} + +/// Read 3 bytes from "fd" and turn them into an int, MSB first. +int get3c(FILE *fd) +{ + int n; + + n = getc(fd); + n = (n << 8) + getc(fd); + n = (n << 8) + getc(fd); + return n; +} + +/// Read 4 bytes from "fd" and turn them into an int, MSB first. +int get4c(FILE *fd) +{ + // Use unsigned rather than int otherwise result is undefined + // when left-shift sets the MSB. + unsigned n; + + n = (unsigned)getc(fd); + n = (n << 8) + (unsigned)getc(fd); + n = (n << 8) + (unsigned)getc(fd); + n = (n << 8) + (unsigned)getc(fd); + return (int)n; +} + +/// Read 8 bytes from `fd` and turn them into a time_t, MSB first. +time_t get8ctime(FILE *fd) +{ + time_t n = 0; + int i; + + for (i = 0; i < 8; i++) { + n = (n << 8) + getc(fd); + } + return n; +} + +/// Reads a string of length "cnt" from "fd" into allocated memory. +/// @return pointer to the string or NULL when unable to read that many bytes. +char *read_string(FILE *fd, size_t cnt) +{ + uint8_t *str = xmallocz(cnt); + for (size_t i = 0; i < cnt; i++) { + int c = getc(fd); + if (c == EOF) { + xfree(str); + return NULL; + } + str[i] = (uint8_t)c; + } + return (char *)str; +} + +/// 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) +{ + assert(len > 0); + for (size_t i = len - 1; i < len; i--) { + if (putc((int)(number >> (i * 8)), fd) == EOF) { + return false; + } + } + return true; +} + +/// Writes time_t to file "fd" in 8 bytes. +/// @returns FAIL when the write failed. +int put_time(FILE *fd, time_t time_) +{ + uint8_t buf[8]; + time_to_bytes(time_, buf); + return fwrite(buf, sizeof(uint8_t), ARRAY_SIZE(buf), fd) == 1 ? OK : FAIL; +} + +/// os_rename() only works if both files are on the same file system, this +/// function will (attempts to?) copy the file across if rename fails -- webb +// +/// @return -1 for failure, 0 for success int vim_rename(char_u *from, char_u *to) { int fd_in; -- cgit From ca93b4a299feaf2ab9344c026b89e5fba360372e Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 12 Sep 2016 02:05:42 +0200 Subject: read_string: Use char if you mean char. --- src/nvim/fileio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/fileio.c') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 4226f2e8d2..c0d4a71b35 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -4498,16 +4498,16 @@ time_t get8ctime(FILE *fd) /// @return pointer to the string or NULL when unable to read that many bytes. char *read_string(FILE *fd, size_t cnt) { - uint8_t *str = xmallocz(cnt); + char *str = xmallocz(cnt); for (size_t i = 0; i < cnt; i++) { int c = getc(fd); if (c == EOF) { xfree(str); return NULL; } - str[i] = (uint8_t)c; + str[i] = (char)c; } - return (char *)str; + return str; } /// Writes a number to file "fd", most significant bit first, in "len" bytes. -- cgit