From: Nathael Pajani Date: Mon, 21 May 2012 17:13:05 +0000 (+0200) Subject: Adding isp_file_to_buff to isp_utils library (regression tests not done) X-Git-Tag: v1.0~17 X-Git-Url: http://git.techno-innov.fr/?a=commitdiff_plain;h=aa87bf7ed0d0a1e9acb3b7f62e4a5a1b1ae96fb5;p=soft%2Ftools%2Flpctools Adding isp_file_to_buff to isp_utils library (regression tests not done) --- diff --git a/isp_utils.c b/isp_utils.c index 2004752..6409375 100644 --- a/isp_utils.c +++ b/isp_utils.c @@ -330,3 +330,49 @@ int isp_buff_to_file(char* data, unsigned int len, char* filename) return ret; } + +int isp_file_to_buff(char* data, unsigned int len, char* filename) +{ + int in_fd = -1; + unsigned int bytes_read = 0; + + /* Open file for reading if input is not stdin */ + if (strncmp(filename, "-", strlen(filename)) == 0) { + /* FIXME : set as non blocking ? */ + in_fd = STDIN_FILENO; + } else { + in_fd = open(filename, O_RDONLY | O_NONBLOCK); + if (in_fd <= 0) { + perror("Unable to open file for reading"); + printf("Tried to open \"%s\".\n", filename); + return -13; + } + } + + /* Read file content */ + do { + int nb = read(in_fd, &data[bytes_read], (len - bytes_read)); + if (nb < 0) { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { + usleep( 50 ); + continue; + } + perror("Input file read error"); + if (in_fd != STDIN_FILENO) { + close(in_fd); + } + return -12; + } else if (nb == 0) { + /* End of file */ + break; + } + bytes_read += nb; + } while (bytes_read < len); + + if (in_fd != STDIN_FILENO) { + close(in_fd); + } + + return bytes_read; +} + diff --git a/isp_utils.h b/isp_utils.h index b3edbc6..5a59186 100644 --- a/isp_utils.h +++ b/isp_utils.h @@ -45,4 +45,6 @@ int isp_uu_decode(char* dest, char* src, unsigned int orig_size); /* ---- File utility functions ----------------------------------------------*/ int isp_buff_to_file(char* data, unsigned int len, char* filename); +int isp_file_to_buff(char* data, unsigned int len, char* filename); + #endif /* ISP_UTILS_H */ diff --git a/isp_wrapper.c b/isp_wrapper.c index dca4c51..e916652 100644 --- a/isp_wrapper.c +++ b/isp_wrapper.c @@ -99,7 +99,6 @@ int isp_cmd_write_to_ram(int arg_count, char** args) /* Arguments */ unsigned long int addr = 0; char* in_file_name = NULL; - int in_fd = -1; char file_buff[RAM_MAX_SIZE]; unsigned int bytes_read = 0; int ret = 0; @@ -119,40 +118,12 @@ int isp_cmd_write_to_ram(int arg_count, char** args) } in_file_name = args[1]; - /* Open file for reading if input is not stdin */ - if (strncmp(in_file_name, "-", strlen(in_file_name)) == 0) { - /* FIXME : set as non blocking ? */ - in_fd = STDIN_FILENO; - } else { - in_fd = open(in_file_name, O_RDONLY | O_NONBLOCK); - if (in_fd <= 0) { - perror("Unable to open file for reading"); - printf("Tried to open \"%s\".\n", in_file_name); - return -13; - } - } - /* Read file content */ - do { - int nb = read(in_fd, &file_buff[bytes_read], (RAM_MAX_SIZE - bytes_read)); - if (nb < 0) { - if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { - usleep( 50 ); - continue; - } - perror("Input file read error"); - if (in_fd != STDIN_FILENO) { - close(in_fd); - } - return -12; - } else if (nb == 0) { - /* End of file */ - break; - } - bytes_read += nb; - } while (bytes_read < RAM_MAX_SIZE); - if (in_fd != STDIN_FILENO) { - close(in_fd); + /* Read data */ + bytes_read = isp_file_to_buff(file_buff, RAM_MAX_SIZE, in_file_name); + if (bytes_read <= 0){ + return -13; } + if (trace_on) { printf("Read %d octet(s) from input file\n", bytes_read); }