ion_file.c
Go to the documentation of this file.
1 /******************************************************************************/
35 /******************************************************************************/
36 
37 #include "ion_file.h"
38 
41  char *name
42 ) {
43 #if defined(ARDUINO)
44  return (ion_boolean_t) SD_File_Exists(name);
45 #else
46  return -1 != access(name, F_OK);
47 #endif
48 }
49 
52  char *name
53 ) {
54 #if defined(ARDUINO)
55 
56  ion_file_handle_t toret;
57 
58  toret.file = fopen(name, "r+");
59 
60  if (NULL == toret.file) {
61  toret.file = fopen(name, "w+");
62  }
63 
64  return toret;
65 #else
66 
67  ion_file_handle_t file;
68 
69  file = fopen(name, "r+b");
70 
71  if (NULL == file) {
72  file = fopen(name, "w+b");
73  }
74 
75  return file;
76 #endif
77 }
78 
82 ) {
83 #if defined(ARDUINO)
84  fclose(file.file);
85  return err_ok;
86 #else
87  fclose(file);
88  return err_ok;
89 #endif
90 }
91 
94  char *name
95 ) {
96  int status;
97 
98  status = fremove(name);
99 
100  if (0 == status) {
101  return err_ok;
102  }
103  else {
104  return err_file_delete_error;
105  }
106 }
107 
108 ion_err_t
110  ion_file_handle_t file,
111  ion_file_offset_t seek_to,
112  int origin
113 ) {
114 #if defined(ARDUINO)
115 
116  if (0 != fseek(file.file, seek_to, origin)) {
117  return err_file_bad_seek;
118  }
119 
120  return err_ok;
121 #else
122 
123  if (0 != fseek(file, seek_to, origin)) {
124  return err_file_bad_seek;
125  }
126 
127  return err_ok;
128 #endif
129 }
130 
133  ion_file_handle_t file
134 ) {
135 #if defined(ARDUINO)
136  return ftell(file.file);
137 #else
138  return ftell(file);
139 #endif
140 }
141 
144  ion_file_handle_t file
145 ) {
146  ion_file_offset_t previous;
147  ion_file_offset_t to_return;
148 
149  previous = ion_ftell(file);
150  ion_fseek(file, 0, ION_FILE_END);
151  to_return = ion_ftell(file);
152  ion_fseek(file, previous, ION_FILE_START);
153 
154  return to_return;
155 }
156 
157 ion_err_t
159  ion_file_handle_t file,
160  unsigned int num_bytes,
161  ion_byte_t *to_write
162 ) {
163 #if defined(ARDUINO)
164 
165  if (num_bytes != (fwrite(to_write, num_bytes, 1, file.file) * num_bytes)) {
166  return err_file_write_error;
167  }
168 
169  return err_ok;
170 #else
171  fwrite(to_write, num_bytes, 1, file);
172  return err_ok;
173 #endif
174 }
175 
176 ion_err_t
178  ion_file_handle_t file,
179  ion_file_offset_t offset,
180  unsigned int num_bytes,
181  ion_byte_t *to_write
182 ) {
184 
185  error = ion_fseek(file, offset, ION_FILE_START);
186 
187  if (err_ok != error) {
188  return error;
189  }
190 
191  error = ion_fwrite(file, num_bytes, to_write);
192  return error;
193 }
194 
195 ion_err_t
197  ion_file_handle_t file,
198  unsigned int num_bytes,
199  ion_byte_t *to_write
200 ) {
202 
203  error = ion_fseek(file, 0, ION_FILE_END);
204 
205  if (err_ok != error) {
206  return error;
207  }
208 
209  error = ion_fwrite(file, num_bytes, to_write);
210  return error;
211 }
212 
213 ion_err_t
215  ion_file_handle_t file,
216  unsigned int num_bytes,
217  ion_byte_t *write_to
218 ) {
219 #if defined(ARDUINO)
220 
221  if (num_bytes != (fread(write_to, num_bytes, 1, file.file) * num_bytes)) {
222  return err_file_read_error;
223  }
224 
225  return err_ok;
226 #else
227 
228  if (1 != fread(write_to, num_bytes, 1, file)) {
229  return err_file_read_error;
230  }
231 
232  return err_ok;
233 #endif
234 }
235 
236 ion_err_t
238  ion_file_handle_t file,
239  ion_file_offset_t offset,
240  unsigned int num_bytes,
241  ion_byte_t *write_to
242 ) {
244 
245  error = ion_fseek(file, offset, ION_FILE_START);
246 
247  if (err_ok != error) {
248  return error;
249  }
250 
251  error = ion_fread(file, num_bytes, write_to);
252  return error;
253 }
A file API for the ionDB.
unsigned char ion_byte_t
A byte type.
Definition: kv_system.h:232
ion_err_t ion_fappend(ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *to_write)
Definition: ion_file.c:196
ion_boolean_t ion_fexists(char *name)
Definition: ion_file.c:40
int SD_File_Exists(char *filepath)
#define ION_FILE_END
Definition: ion_file.h:50
#define fremove(x)
Definition: kv_system.h:56
char ion_err_t
The error type used to store error codes.
Definition: kv_system.h:226
FILE * ion_file_handle_t
Definition: ion_file.h:69
ion_err_t ion_fseek(ion_file_handle_t file, ion_file_offset_t seek_to, int origin)
Definition: ion_file.c:109
ion_file_handle_t ion_fopen(char *name)
Definition: ion_file.c:51
ion_err_t ion_fwrite(ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *to_write)
Definition: ion_file.c:158
#define ION_FILE_START
Definition: ion_file.h:49
ion_err_t ion_fremove(char *name)
Definition: ion_file.c:93
long ion_file_offset_t
Definition: ion_file.h:47
ion_err_t ion_fclose(ion_file_handle_t file)
Definition: ion_file.c:80
#define error(rc)
Definition: bpp_tree.c:139
ion_err_t ion_fwrite_at(ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *to_write)
Definition: ion_file.c:177
ion_err_t ion_fread(ion_file_handle_t file, unsigned int num_bytes, ion_byte_t *write_to)
Definition: ion_file.c:214
ion_file_offset_t ion_fend(ion_file_handle_t file)
Definition: ion_file.c:143
char ion_boolean_t
A boolean type.
Definition: kv_system.h:269
ion_file_offset_t ion_ftell(ion_file_handle_t file)
Definition: ion_file.c:132
ion_err_t ion_fread_at(ion_file_handle_t file, ion_file_offset_t offset, unsigned int num_bytes, ion_byte_t *write_to)
Definition: ion_file.c:237