sbgInterfaceFile.c
Go to the documentation of this file.
1 #include "sbgInterfaceFile.h"
2 
3 //----------------------------------------------------------------------//
4 //- Internal methods declarations -//
5 //----------------------------------------------------------------------//
6 
7 //----------------------------------------------------------------------//
8 //- Operations methods declarations -//
9 //----------------------------------------------------------------------//
10 
18 {
19  SbgErrorCode errorCode = SBG_NO_ERROR;
20  FILE *pInputFile;
21 
22  assert(pHandle);
23  assert(filePath);
24 
25  //
26  // Always call the underlying zero init method to make sure we can correctly handle SbgInterface evolutions
27  //
28  sbgInterfaceZeroInit(pHandle);
29 
30  //
31  // Try to open the file
32  //
33  pInputFile = fopen(filePath, "rb");
34 
35  //
36  // Test if the input file has been opened
37  //
38  if (pInputFile)
39  {
40  //
41  // Define base interface members
42  //
43  pHandle->handle = pInputFile;
44  pHandle->type = SBG_IF_TYPE_FILE;
45 
46  //
47  // Define the interface name
48  //
49  sbgInterfaceNameSet(pHandle, filePath);
50 
51  //
52  // Define all overloaded members
53  //
57  }
58  else
59  {
60  //
61  // Unable to open the input file
62  //
63  errorCode = SBG_INVALID_PARAMETER;
64  }
65 
66  return errorCode;
67 }
68 
76 {
77  SbgErrorCode errorCode = SBG_NO_ERROR;
78  FILE *pInputFile;
79 
80  //
81  // Test input arguments
82  //
83  assert(pHandle);
84  assert(filePath);
85 
86  //
87  // Try to open the file
88  //
89  pInputFile = fopen(filePath, "wb");
90 
91  //
92  // Test if the input file has been opened
93  //
94  if (pInputFile)
95  {
96  //
97  // Define the interface members
98  //
99  pHandle->handle = pInputFile;
100  pHandle->type = SBG_IF_TYPE_FILE;
104  }
105  else
106  {
107  //
108  // Unable to open the input file
109  //
110  errorCode = SBG_INVALID_PARAMETER;
111  }
112 
113  return errorCode;
114 }
115 
122 {
123  FILE *pInputFile;
124 
125  //
126  // Test input arguments
127  //
128  assert(pHandle);
129 
130  //
131  // Get the internal FILE handle
132  //
133  pInputFile = (FILE*)(pHandle->handle);
134 
135  //
136  // Close the input file only if opened
137  //
138  if (pInputFile)
139  {
140  fclose(pInputFile);
141  pHandle->handle = NULL;
142  }
143 
144  return SBG_NO_ERROR;
145 }
146 
153 {
154  FILE *pInputFile;
155  long cursorPos;
156  long fileSize;
157 
158  //
159  // Test input arguments
160  //
161  assert(pHandle);
162 
163  //
164  // Initialize the file size to 0 in case of error
165  //
166  fileSize = 0;
167 
168  //
169  // Get the internal FILE handle
170  //
171  pInputFile = (FILE*)(pHandle->handle);
172 
173  //
174  // Test if the file handle is valid
175  //
176  if (pInputFile)
177  {
178  //
179  // Compute the file size
180  //
181  cursorPos = ftell(pInputFile);
182  fseek(pInputFile, 0, SEEK_END);
183  fileSize = ftell(pInputFile);
184  fseek(pInputFile, cursorPos, SEEK_SET);
185  }
186 
187  return (size_t)fileSize;
188 }
189 
196 {
197  FILE *pInputFile;
198 
199  //
200  // Test input argument
201  //
202  assert(pHandle);
203 
204  //
205  // Get the internal FILE handle
206  //
207  pInputFile = (FILE*)(pHandle->handle);
208 
209  //
210  // Test if the file handle is valid
211  //
212  if (pInputFile)
213  {
214  //
215  // Return the current cursor position
216  //
217  return (size_t)ftell(pInputFile);
218  }
219 
220  return 0;
221 }
222 
223 
224 //----------------------------------------------------------------------//
225 //- Internal interfaces write/read implementations -//
226 //----------------------------------------------------------------------//
227 
235 SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileWrite(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
236 {
237  FILE *pOutputFile;
238  size_t bytesWritten;
239 
240  //
241  // Test input argument
242  //
243  assert(pHandle);
244  assert(pBuffer);
245 
246  //
247  // Get the internal FILE handle
248  //
249  pOutputFile = (FILE*)(pHandle->handle);
250 
251  //
252  // Write as much bytes as we can
253  //
254  bytesWritten = fwrite(pBuffer, sizeof(uint8_t), bytesToWrite, pOutputFile);
255 
256  //
257  // Check if we could successfuly write our bytes
258  //
259  if (bytesWritten == bytesToWrite)
260  {
261 
262  return SBG_NO_ERROR;
263  }
264  else
265  {
266  return SBG_WRITE_ERROR;
267  }
268 }
269 
278 SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileRead(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
279 {
280  FILE *pInputFile;
281 
282  //
283  // Test input argument
284  //
285  assert(pHandle);
286  assert(pBuffer);
287  assert(pReadBytes);
288 
289  //
290  // Get the internal FILE handle
291  //
292  pInputFile = (FILE*)(pHandle->handle);
293 
294  //
295  // Read some bytes from the file
296  //
297  *pReadBytes = fread(pBuffer, sizeof(uint8_t), bytesToRead, pInputFile);
298 
299  return SBG_NO_ERROR;
300 }
301 
308 {
309  SbgErrorCode errorCode;
310  FILE *pInputFile;
311  int ret;
312 
313  //
314  // Test input argument
315  //
316  assert(pHandle);
317 
318  //
319  // Get the internal FILE handle
320  //
321  pInputFile = (FILE*)(pHandle->handle);
322 
323  //
324  // Flush all pending data
325  //
326  ret = fflush(pInputFile);
327 
328  if (ret == 0)
329  {
330  errorCode = SBG_NO_ERROR;
331  }
332  else
333  {
334  errorCode = SBG_ERROR;
335  }
336 
337  return errorCode;
338 }
339 
347 SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileWriteFake(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
348 {
349  //
350  // Simply avoid warnings and return an error
351  //
352  SBG_UNUSED_PARAMETER(pHandle);
353  SBG_UNUSED_PARAMETER(pBuffer);
354  SBG_UNUSED_PARAMETER(bytesToWrite);
355 
356  return SBG_ERROR;
357 }
358 
367 SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileReadFake(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
368 {
369  //
370  // Simply avoid warnings and return an error
371  //
372  SBG_UNUSED_PARAMETER(pHandle);
373  SBG_UNUSED_PARAMETER(pBuffer);
374  SBG_UNUSED_PARAMETER(pReadBytes);
375  SBG_UNUSED_PARAMETER(bytesToRead);
376 
377  return SBG_ERROR;
378 }
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileWriteFake(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
#define SBG_COMMON_LIB_API
Definition: sbgDefines.h:58
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileReadFake(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileWriteOpen(SbgInterface *pHandle, const char *filePath)
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileFlush(SbgInterface *pHandle)
#define SBG_IF_TYPE_FILE
Definition: sbgInterface.h:50
uint32_t type
Definition: sbgInterface.h:134
void sbgInterfaceZeroInit(SbgInterface *pHandle)
Definition: sbgInterface.c:24
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileClose(SbgInterface *pHandle)
SbgInterfaceHandle handle
Definition: sbgInterface.h:133
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileOpen(SbgInterface *pHandle, const char *filePath)
SBG_COMMON_LIB_API size_t sbgInterfaceFileGetSize(SbgInterface *pHandle)
SbgInterfaceReadFunc pReadFunc
Definition: sbgInterface.h:138
void sbgInterfaceNameSet(SbgInterface *pInterface, const char *pName)
Definition: sbgInterface.c:61
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileWrite(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
#define NULL
Definition: sbgDefines.h:81
SBG_COMMON_LIB_API size_t sbgInterfaceFileGetCursor(const SbgInterface *pHandle)
#define SBG_UNUSED_PARAMETER(x)
Definition: sbgDefines.h:194
SbgInterfaceFlushFunc pFlushFunc
Definition: sbgInterface.h:139
SBG_COMMON_LIB_API SbgErrorCode sbgInterfaceFileRead(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
SbgInterfaceWriteFunc pWriteFunc
Definition: sbgInterface.h:137
enum _SbgErrorCode SbgErrorCode
This file implements a file interface for read only operations.


sbg_driver
Author(s): SBG Systems
autogenerated on Thu Oct 22 2020 03:47:22