]> git.sur5r.net Git - openocd/commitdiff
fileio: refactor struct fileio to be an opaque structure
authorØyvind Harboe <oyvind.harboe@zylin.com>
Tue, 28 Sep 2010 13:37:56 +0000 (15:37 +0200)
committerØyvind Harboe <oyvind.harboe@zylin.com>
Wed, 29 Sep 2010 06:43:27 +0000 (08:43 +0200)
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
src/flash/mflash.c
src/flash/nand/fileio.c
src/flash/nand/tcl.c
src/flash/nor/tcl.c
src/helper/fileio.c
src/helper/fileio.h
src/target/etm.c
src/target/image.c
src/target/target.c

index 26b85b1fb3105357a732109b8c7d690d480dc677..272127be5746af38a1cd1a206f167557969fa2c6 100644 (file)
@@ -726,8 +726,8 @@ COMMAND_HANDLER(mg_write_cmd)
                return ERROR_FAIL;
        }
 
-       cnt = fileio.size / MG_FILEIO_CHUNK;
-       res = fileio.size % MG_FILEIO_CHUNK;
+       cnt = fileio_size(&fileio) / MG_FILEIO_CHUNK;
+       res = fileio_size(&fileio) % MG_FILEIO_CHUNK;
 
        struct duration bench;
        duration_start(&bench);
@@ -752,8 +752,8 @@ COMMAND_HANDLER(mg_write_cmd)
        if (duration_measure(&bench) == ERROR_OK)
        {
                command_print(CMD_CTX, "wrote %ld bytes from file %s "
-                               "in %fs (%0.3f kB/s)", (long)fileio.size, CMD_ARGV[1],
-                               duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
+                               "in %fs (%0.3f kB/s)", (long)fileio_size(&fileio), CMD_ARGV[1],
+                               duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
        }
 
        free(buffer);
index 3e397ebd6b70dd60f16fdfdef745f497cd06d354..0a006fc02b01646366f2639d8cc16f63b241ce3b 100644 (file)
@@ -180,7 +180,7 @@ COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
                return retval;
 
        if (!need_size)
-               state->size = state->fileio.size;
+               state->size = fileio_size(&state->fileio);
 
        *dev = nand;
 
index 57bbe00e5c379cffd550e87993cb39cd33e538f7..a54f8ea8fe6ddffdf5734f7cc6dc126a841b756e 100644 (file)
@@ -389,8 +389,8 @@ COMMAND_HANDLER(handle_nand_dump_command)
        if (nand_fileio_finish(&s) == ERROR_OK)
        {
                command_print(CMD_CTX, "dumped %ld bytes in %fs (%0.3f KiB/s)",
-                               (long)s.fileio.size, duration_elapsed(&s.bench),
-                               duration_kbps(&s.bench, s.fileio.size));
+                               (long)fileio_size(&s.fileio), duration_elapsed(&s.bench),
+                               duration_kbps(&s.bench, fileio_size(&s.fileio)));
        }
        return ERROR_OK;
 }
index 8604b4b33a66eb07d9d0e191dec78e0d827956c1..f36ab7da56a64cf7e05320ab8a0d5a1c10d2d2a1 100644 (file)
@@ -604,9 +604,9 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
                return ERROR_OK;
        }
 
-       buffer = malloc(fileio.size);
+       buffer = malloc(fileio_size(&fileio));
        size_t buf_cnt;
-       if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
+       if (fileio_read(&fileio, fileio_size(&fileio), buffer, &buf_cnt) != ERROR_OK)
        {
                free(buffer);
                fileio_close(&fileio);
@@ -622,8 +622,8 @@ COMMAND_HANDLER(handle_flash_write_bank_command)
        {
                command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u"
                                " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)",
-                               (long)fileio.size, CMD_ARGV[1], p->bank_number, offset,
-                               duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
+                               (long)fileio_size(&fileio), CMD_ARGV[1], p->bank_number, offset,
+                               duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
        }
 
        fileio_close(&fileio);
index ba62397cc781d975e27354d6628821258d27fb66..7c862e47135e6a8542f9c42905184e55c2ac1ff2 100644 (file)
 #include "configuration.h"
 #include "fileio.h"
 
-static inline int fileio_open_local(struct fileio *fileio)
+struct fileio_internal {
+       const char *url;
+       ssize_t size;
+       enum fileio_type type;
+       enum fileio_access access;
+       FILE *file;
+};
+
+static inline int fileio_close_local(struct fileio_internal *fileio);
+static inline int fileio_open_local(struct fileio_internal *fileio)
 {
        char file_access[4];
 
@@ -86,7 +95,7 @@ static inline int fileio_open_local(struct fileio *fileio)
 
                if ((fileio->size < 0)||(result < 0)||(result2 < 0))
                {
-                       fileio_close(fileio);
+                       fileio_close_local(fileio);
                        return ERROR_FILEIO_OPERATION_FAILED;
                }
        }
@@ -98,10 +107,13 @@ static inline int fileio_open_local(struct fileio *fileio)
        return ERROR_OK;
 }
 
-int fileio_open(struct fileio *fileio, const char *url, enum fileio_access access_type,        enum fileio_type type)
+int fileio_open(struct fileio *fileio_p, const char *url, enum fileio_access access_type,      enum fileio_type type)
 {
        int retval = ERROR_OK;
 
+       struct fileio_internal *fileio = malloc(sizeof(struct fileio_internal));
+       fileio_p->fp = fileio;
+
        fileio->type = type;
        fileio->access = access_type;
        fileio->url = strdup(url);
@@ -111,7 +123,7 @@ int fileio_open(struct fileio *fileio, const char *url, enum fileio_access acces
        return retval;
 }
 
-static inline int fileio_close_local(struct fileio *fileio)
+static inline int fileio_close_local(struct fileio_internal *fileio)
 {
        int retval;
        if ((retval = fclose(fileio->file)) != 0)
@@ -131,21 +143,26 @@ static inline int fileio_close_local(struct fileio *fileio)
        return ERROR_OK;
 }
 
-int fileio_close(struct fileio *fileio)
+int fileio_close(struct fileio *fileio_p)
 {
        int retval;
+       struct fileio_internal *fileio = fileio_p->fp;
 
        retval = fileio_close_local(fileio);
 
        free((void*)fileio->url);
        fileio->url = NULL;
 
+       free(fileio);
+       fileio_p->fp = NULL;
+
        return retval;
 }
 
-int fileio_seek(struct fileio *fileio, size_t position)
+int fileio_seek(struct fileio *fileio_p, size_t position)
 {
        int retval;
+       struct fileio_internal *fileio = fileio_p->fp;
        if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
        {
                LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
@@ -155,7 +172,7 @@ int fileio_seek(struct fileio *fileio, size_t position)
        return ERROR_OK;
 }
 
-static int fileio_local_read(struct fileio *fileio,
+static int fileio_local_read(struct fileio_internal *fileio,
                size_t size, void *buffer, size_t *size_read)
 {
        ssize_t retval = fread(buffer, 1, size, fileio->file);
@@ -163,16 +180,18 @@ static int fileio_local_read(struct fileio *fileio,
        return (retval < 0) ? retval : ERROR_OK;
 }
 
-int fileio_read(struct fileio *fileio, size_t size, void *buffer,
+int fileio_read(struct fileio *fileio_p, size_t size, void *buffer,
                size_t *size_read)
 {
+       struct fileio_internal *fileio = fileio_p->fp;
        return fileio_local_read(fileio, size, buffer, size_read);
 }
 
-int fileio_read_u32(struct fileio *fileio, uint32_t *data)
+int fileio_read_u32(struct fileio *fileio_p, uint32_t *data)
 {
        uint8_t buf[4];
        size_t size_read;
+       struct fileio_internal *fileio = fileio_p->fp;
        int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
        if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
                retval = -EIO;
@@ -181,7 +200,7 @@ int fileio_read_u32(struct fileio *fileio, uint32_t *data)
        return retval;
 }
 
-static int fileio_local_fgets(struct fileio *fileio,
+static int fileio_local_fgets(struct fileio_internal *fileio,
                size_t size, void *buffer)
 {
        if (fgets(buffer, size, fileio->file) == NULL)
@@ -190,12 +209,13 @@ static int fileio_local_fgets(struct fileio *fileio,
        return ERROR_OK;
 }
 
-int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
+int fileio_fgets(struct fileio *fileio_p, size_t size, void *buffer)
 {
+       struct fileio_internal *fileio = fileio_p->fp;
        return fileio_local_fgets(fileio, size, buffer);
 }
 
-static int fileio_local_write(struct fileio *fileio,
+static int fileio_local_write(struct fileio_internal *fileio,
                size_t size, const void *buffer, size_t *size_written)
 {
        ssize_t retval = fwrite(buffer, 1, size, fileio->file);
@@ -203,24 +223,30 @@ static int fileio_local_write(struct fileio *fileio,
        return (retval < 0) ? retval : ERROR_OK;
 }
 
-int fileio_write(struct fileio *fileio,
+int fileio_write(struct fileio *fileio_p,
                size_t size, const void *buffer, size_t *size_written)
 {
+       struct fileio_internal *fileio = fileio_p->fp;
        int retval = fileio_local_write(fileio, size, buffer, size_written);
        if (retval == ERROR_OK)
                fileio->size += *size_written;
        return retval;
 }
 
-int fileio_write_u32(struct fileio *fileio, uint32_t data)
+int fileio_write_u32(struct fileio *fileio_p, uint32_t data)
 {
        uint8_t buf[4];
        h_u32_to_be(buf, data);
-
        size_t size_written;
-       int retval = fileio_write(fileio, 4, buf, &size_written);
+       int retval = fileio_write(fileio_p, 4, buf, &size_written);
        if (ERROR_OK == retval && size_written != sizeof(uint32_t))
                retval = -EIO;
 
        return retval;
 }
+
+int fileio_size(struct fileio *fileio_p)
+{
+       struct fileio_internal *fileio = fileio_p->fp;
+       return fileio->size;
+}
index 597bafc0124ae5c53934bb9f866d61f9f6b57284..40c4ef0e6d78ba96a495134cb7aa53f33146b307 100644 (file)
@@ -46,12 +46,10 @@ enum fileio_access
        FILEIO_APPENDREAD,      /* open for writing, position at end, allow reading */
 };
 
-struct fileio {
-       const char *url;
-       ssize_t size;
-       enum fileio_type type;
-       enum fileio_access access;
-       FILE *file;
+struct fileio
+{
+       /* The structure is opaque */
+       struct fileio_internal *fp;
 };
 
 int fileio_open(struct fileio *fileio,
@@ -68,6 +66,7 @@ int fileio_write(struct fileio *fileio,
 
 int fileio_read_u32(struct fileio *fileio, uint32_t *data);
 int fileio_write_u32(struct fileio *fileio, uint32_t data);
+int fileio_size(struct fileio *fileio);
 
 #define ERROR_FILEIO_LOCATION_UNKNOWN  (-1200)
 #define ERROR_FILEIO_NOT_FOUND                 (-1201)
index 9da69550421df9b117ddb76c790dbdf2bee5dbda..c71c5d199fabbf1827b465e4267c14c0d55db835 100644 (file)
@@ -1897,7 +1897,7 @@ COMMAND_HANDLER(handle_etm_load_command)
                return ERROR_FAIL;
        }
 
-       if (file.size % 4)
+       if (fileio_size(&file) % 4)
        {
                command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data");
                fileio_close(&file);
index d36fbc34c83bb8eb543439bb2a3ebcc4c03be1a8..e77e190115ab6bbe6991e55d2998b03e663d79e9 100644 (file)
@@ -158,7 +158,7 @@ static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine,
        /* we can't determine the number of sections that we'll have to create ahead of time,
         * so we locally hold them until parsing is finished */
 
-       ihex->buffer = malloc(fileio->size >> 1);
+       ihex->buffer = malloc(fileio_size(fileio) >> 1);
        cooked_bytes = 0x0;
        image->num_sections = 0;
        section[image->num_sections].private = &ihex->buffer[cooked_bytes];
@@ -537,7 +537,7 @@ static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine,
        /* we can't determine the number of sections that we'll have to create ahead of time,
         * so we locally hold them until parsing is finished */
 
-       mot->buffer = malloc(fileio->size >> 1);
+       mot->buffer = malloc(fileio_size(fileio) >> 1);
        cooked_bytes = 0x0;
        image->num_sections = 0;
        section[image->num_sections].private = &mot->buffer[cooked_bytes];
@@ -747,7 +747,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
                image->num_sections = 1;
                image->sections = malloc(sizeof(struct imagesection));
                image->sections[0].base_address = 0x0;
-               image->sections[0].size = image_binary->fileio.size;
+               image->sections[0].size = fileio_size(&image_binary->fileio);
                image->sections[0].flags = 0;
        }
        else if (image->type == IMAGE_IHEX)
index c37432a30ca0ad7a6d6476250585b116e1a3cfe4..fcdcc3633489465dd6edccc5a296ba1ae6360de8 100644 (file)
@@ -2649,8 +2649,8 @@ COMMAND_HANDLER(handle_dump_image_command)
        if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
        {
                command_print(CMD_CTX,
-                               "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio.size,
-                               duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
+                               "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio_size(&fileio),
+                               duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio)));
        }
 
        return retval;