file_util.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file file_util.c
9 * @date Oct 5, 2016
10 * @brief File utility module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "file_util.h"
15 #include "log_util.h"
16 #include "definitions.h"
17 
18 #if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
19 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
20 #endif
21 
22 E_MCL_ERROR_CODE file_util_fopen(const char *file_name, const char *mode, void **file_descriptor)
23 {
24  DEBUG_ENTRY("const char *file_name = <%s>, const char *mode = <%s>, void **file_descriptor = <%p>", file_name, mode, file_descriptor)
25 
26  E_MCL_ERROR_CODE return_code = file_util_fopen_without_log(file_name, mode, file_descriptor);
27 
28  DEBUG_LEAVE("retVal = <%d>", return_code);
29  return return_code;
30 }
31 
32 E_MCL_ERROR_CODE file_util_fopen_without_log(const char *file_name, const char *mode, void **file_descriptor)
33 {
34  E_MCL_ERROR_CODE return_code = MCL_OK;
35 
36  FILE *file;
37 
38 #if defined(WIN32) || defined(WIN64)
39  fopen_s(&file, file_name, mode);
40 #else
41  file = fopen(file_name, mode);
42 #endif
43  *file_descriptor = file;
44  if (MCL_NULL == file)
45  {
46  return_code = MCL_FAIL;
47  }
48 
49  return return_code;
50 }
51 
52 E_MCL_ERROR_CODE file_util_fclose(void *file_descriptor)
53 {
54  DEBUG_ENTRY("void *file_descriptor = <%p>", file_descriptor)
55 
56  E_MCL_ERROR_CODE return_code = file_util_fclose_without_log(file_descriptor);
57 
58  DEBUG_LEAVE("retVal = <%d>", return_code);
59  return return_code;
60 }
61 
63 {
64  E_MCL_ERROR_CODE return_code = MCL_OK;
65 
66  if (MCL_NULL != file_descriptor)
67  {
68  mcl_int32_t result = fclose((FILE *)file_descriptor);
69  if (0 != result)
70  {
71  return_code = MCL_FAIL;
72  }
73  }
74 
75  return return_code;
76 }
77 
78 E_MCL_ERROR_CODE file_util_fwrite(const void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor)
79 {
80  DEBUG_ENTRY("const void *data = <%p>, mcl_size_t size = <%u>, mcl_size_t count = <%u>, void *file_descriptor = <%p>", data, size, count, file_descriptor)
81 
82  E_MCL_ERROR_CODE return_code = file_util_fwrite_without_log(data, size, count, file_descriptor);
83 
84  DEBUG_LEAVE("retVal = <%d>", return_code);
85  return return_code;
86 }
87 
88 E_MCL_ERROR_CODE file_util_fwrite_without_log(const void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor)
89 {
90  E_MCL_ERROR_CODE return_code = MCL_FAIL;
91 
92  mcl_size_t actual_count = fwrite(data, size, count, (FILE *)file_descriptor);
93  if (count == actual_count)
94  {
95  return_code = MCL_OK;
96  }
97  else
98  {
99  MCL_ERROR("Data can not be written to file.");
100  }
101 
102  return return_code;
103 }
104 
105 void file_util_fread(void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor, mcl_size_t *actual_count)
106 {
107  DEBUG_ENTRY("void *data = <%p>, mcl_size_t size = <%u>, mcl_size_t count = <%u>, void *file_descriptor = <%p>, mcl_size_t *actual_count = <%p>", data, size, count,
108  file_descriptor, actual_count)
109 
110  *actual_count = fread(data, size, count, (FILE *)file_descriptor);
111 
112  DEBUG_LEAVE("retVal = void");
113 }
114 
115 E_MCL_ERROR_CODE file_util_fputs(const char *data, void *file_descriptor)
116 {
117  DEBUG_ENTRY("const char *data = <%s>, void *file_descriptor = <%p>", data, file_descriptor)
118 
119  E_MCL_ERROR_CODE return_code = MCL_FAIL;
120 
121  mcl_int32_t result = fputs(data, (FILE *)file_descriptor);
122  if (0 <= result)
123  {
124  return_code = MCL_OK;
125  }
126  else
127  {
128  MCL_ERROR("Data can not be written to file.");
129  }
130 
131  DEBUG_LEAVE("retVal = <%d>", return_code);
132  return return_code;
133 }
134 
135 E_MCL_ERROR_CODE file_util_fgets(char *data, mcl_size_t data_size, void *file_descriptor)
136 {
137  DEBUG_ENTRY("char *data = <%p>, mcl_size_t data_size = <%u>, void *file_descriptor = <%p>", data, data_size, file_descriptor)
138 
139  E_MCL_ERROR_CODE return_code = MCL_FAIL;
140 
141  char *output = fgets(data, (int)data_size, (FILE *)file_descriptor);
142  if (MCL_NULL != output)
143  {
144  return_code = MCL_OK;
145  }
146  else
147  {
148  MCL_DEBUG("No data is read from file.");
149  }
150 
151  DEBUG_LEAVE("retVal = <%d>", return_code);
152  return return_code;
153 }
154 
155 E_MCL_ERROR_CODE file_util_fstat(void *file_descriptor, struct stat *file_attributes)
156 {
157  DEBUG_ENTRY("void *file_descriptor = <%p>, struct stat *file_attributes = <%p>", file_descriptor, file_attributes)
158 
159  E_MCL_ERROR_CODE return_code = MCL_FAIL;
160 
161  int result;
162 
163 #if defined(WIN32) || defined(WIN64)
164  result = fstat(_fileno((FILE *)file_descriptor), file_attributes);
165 #else
166  result = fstat(fileno((FILE *)file_descriptor), file_attributes);
167 #endif
168 
169  if (0 == result)
170  {
171  return_code = MCL_OK;
172  }
173  else
174  {
175  MCL_ERROR("Error in retrieving file attributes.");
176  }
177 
178  DEBUG_LEAVE("retVal = <%d>", return_code);
179  return return_code;
180 }
181 
182 E_MCL_ERROR_CODE file_util_fflush(void *file_descriptor)
183 {
184  DEBUG_ENTRY("void *file_descriptor = <%p>", file_descriptor)
185 
186  E_MCL_ERROR_CODE return_code = file_util_fflush_without_log(file_descriptor);
187 
188  DEBUG_LEAVE("retVal = <%d>", return_code);
189  return return_code;
190 }
191 
193 {
194  E_MCL_ERROR_CODE return_code = MCL_FAIL;
195 
196  int result;
197 
198 #if defined(WIN32) || defined(WIN64)
199  result = fflush((FILE *)file_descriptor);
200 #else
201  result = fflush((FILE *)file_descriptor);
202 #endif
203 
204  if (0 == result)
205  {
206  return_code = MCL_OK;
207  }
208  else
209  {
210  MCL_ERROR("Error in flush.");
211  }
212 
213  return return_code;
214 }
215 
217 {
218  DEBUG_ENTRY("const mcl_stat_t *file_attributes = <%p>", file_attributes)
219  mcl_bool_t is_regular_file = MCL_FALSE;
220 
221 #ifdef S_ISREG
222  is_regular_file = S_ISREG(file_attributes->st_mode);
223 #endif
224 
225  DEBUG_LEAVE("retVal = <%d>", is_regular_file);
226  return is_regular_file;
227 }
E_MCL_ERROR_CODE file_util_fflush_without_log(void *file_descriptor)
Definition: file_util.c:192
Internal failure in MCL.
Definition: mcl_common.h:141
E_MCL_ERROR_CODE file_util_fwrite(const void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor)
Definition: file_util.c:78
E_MCL_ERROR_CODE file_util_fflush(void *file_descriptor)
Definition: file_util.c:182
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
E_MCL_ERROR_CODE file_util_fgets(char *data, mcl_size_t data_size, void *file_descriptor)
Definition: file_util.c:135
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
E_MCL_ERROR_CODE file_util_fopen_without_log(const char *file_name, const char *mode, void **file_descriptor)
Definition: file_util.c:32
#define MCL_DEBUG(...)
Definition: log_util.h:70
struct stat mcl_stat_t
Definition: file_util.h:25
#define MCL_FALSE
MCL bool type.
Definition: mcl_common.h:53
E_MCL_ERROR_CODE file_util_fputs(const char *data, void *file_descriptor)
Definition: file_util.c:115
Log utility module header file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
E_MCL_ERROR_CODE file_util_fopen(const char *file_name, const char *mode, void **file_descriptor)
Definition: file_util.c:22
E_MCL_ERROR_CODE file_util_fwrite_without_log(const void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor)
Definition: file_util.c:88
Definitions module header file.
File utility module header file.
void file_util_fread(void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor, mcl_size_t *actual_count)
Definition: file_util.c:105
E_MCL_ERROR_CODE file_util_fclose(void *file_descriptor)
Definition: file_util.c:52
size_t mcl_size_t
Definition: mcl_common.h:38
mcl_uint8_t mcl_bool_t
Definition: mcl_common.h:47
Success.
Definition: mcl_common.h:140
E_MCL_ERROR_CODE file_util_fstat(void *file_descriptor, struct stat *file_attributes)
Definition: file_util.c:155
#define MCL_ERROR(...)
Definition: log_util.h:97
mcl_bool_t file_util_check_if_regular_file(const mcl_stat_t *file_attributes)
Definition: file_util.c:216
#define MCL_NULL
Definition: definitions.h:24
E_MCL_ERROR_CODE file_util_fclose_without_log(void *file_descriptor)
Definition: file_util.c:62
int32_t mcl_int32_t
Definition: mcl_common.h:41