-Wshadow fixes
[openocd/cortex.git] / src / helper / fileio.c
blob71e807ff7870a7aa4e992de1c7d42180acbb28b5
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "log.h"
31 #include "configuration.h"
32 #include "fileio.h"
34 static inline int fileio_open_local(struct fileio *fileio)
36 char file_access[4];
38 switch (fileio->access)
40 case FILEIO_READ:
41 strcpy(file_access, "r");
42 break;
43 case FILEIO_WRITE:
44 strcpy(file_access, "w");
45 break;
46 case FILEIO_READWRITE:
47 strcpy(file_access, "w+");
48 break;
49 case FILEIO_APPEND:
50 strcpy(file_access, "a");
51 break;
52 case FILEIO_APPENDREAD:
53 strcpy(file_access, "a+");
54 break;
55 default:
56 LOG_ERROR("BUG: access neither read, write nor readwrite");
57 return ERROR_INVALID_ARGUMENTS;
60 /* win32 always opens in binary mode */
61 #ifndef _WIN32
62 if (fileio->type == FILEIO_BINARY)
63 #endif
65 strcat(file_access, "b");
68 if (!(fileio->file = open_file_from_path (fileio->url, file_access)))
70 LOG_ERROR("couldn't open %s", fileio->url);
71 return ERROR_FILEIO_OPERATION_FAILED;
74 if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
76 /* NB! Here we use fseek() instead of stat(), since stat is a
77 * more advanced operation that might not apply to e.g. a disk path
78 * that refers to e.g. a tftp client */
79 int result, result2;
81 result = fseek(fileio->file, 0, SEEK_END);
83 fileio->size = ftell(fileio->file);
85 result2 = fseek(fileio->file, 0, SEEK_SET);
87 if ((fileio->size < 0)||(result < 0)||(result2 < 0))
89 fileio_close(fileio);
90 return ERROR_FILEIO_OPERATION_FAILED;
93 else
95 fileio->size = 0x0;
98 return ERROR_OK;
101 int fileio_open(struct fileio *fileio, const char *url, enum fileio_access access, enum fileio_type type)
103 int retval = ERROR_OK;
105 fileio->type = type;
106 fileio->access = access;
107 fileio->url = strdup(url);
109 retval = fileio_open_local(fileio);
111 return retval;
114 static inline int fileio_close_local(struct fileio *fileio)
116 int retval;
117 if ((retval = fclose(fileio->file)) != 0)
119 if (retval == EBADF)
121 LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
123 else
125 LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
128 return ERROR_FILEIO_OPERATION_FAILED;
131 return ERROR_OK;
134 int fileio_close(struct fileio *fileio)
136 int retval;
138 retval = fileio_close_local(fileio);
140 free((void*)fileio->url);
141 fileio->url = NULL;
143 return retval;
146 int fileio_seek(struct fileio *fileio, size_t position)
148 int retval;
149 if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
151 LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
152 return ERROR_FILEIO_OPERATION_FAILED;
155 return ERROR_OK;
158 static int fileio_local_read(struct fileio *fileio,
159 size_t size, void *buffer, size_t *size_read)
161 ssize_t retval = fread(buffer, 1, size, fileio->file);
162 *size_read = (retval >= 0) ? retval : 0;
163 return (retval < 0) ? retval : ERROR_OK;
166 int fileio_read(struct fileio *fileio, size_t size, void *buffer,
167 size_t *size_read)
169 return fileio_local_read(fileio, size, buffer, size_read);
172 int fileio_read_u32(struct fileio *fileio, uint32_t *data)
174 uint8_t buf[4];
175 size_t size_read;
176 int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
177 if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
178 retval = -EIO;
179 if (ERROR_OK == retval)
180 *data = be_to_h_u32(buf);
181 return retval;
184 static int fileio_local_fgets(struct fileio *fileio,
185 size_t size, void *buffer)
187 if (fgets(buffer, size, fileio->file) == NULL)
188 return ERROR_FILEIO_OPERATION_FAILED;
190 return ERROR_OK;
193 int fileio_fgets(struct fileio *fileio, size_t size, void *buffer)
195 return fileio_local_fgets(fileio, size, buffer);
198 static int fileio_local_write(struct fileio *fileio,
199 size_t size, const void *buffer, size_t *size_written)
201 ssize_t retval = fwrite(buffer, 1, size, fileio->file);
202 *size_written = (retval >= 0) ? retval : 0;
203 return (retval < 0) ? retval : ERROR_OK;
206 int fileio_write(struct fileio *fileio,
207 size_t size, const void *buffer, size_t *size_written)
209 int retval = fileio_local_write(fileio, size, buffer, size_written);
210 if (retval == ERROR_OK)
211 fileio->size += *size_written;
212 return retval;
215 int fileio_write_u32(struct fileio *fileio, uint32_t data)
217 uint8_t buf[4];
218 h_u32_to_be(buf, data);
220 size_t size_written;
221 int retval = fileio_write(fileio, 4, buf, &size_written);
222 if (ERROR_OK == retval && size_written != sizeof(uint32_t))
223 retval = -EIO;
225 return retval;