4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 * @desc Helper functions for working with files
31 #include "file-helper.h"
35 #define BUF_MAX (BUFSIZ)
36 #define BUF_INC_SIZE (512 * 1024)
37 #define KB(bytes) ((bytes)/1024)
39 resourced_ret_c fwrite_str(const char *path, const char *str)
44 ret_value_msg_if(!path, RESOURCED_ERROR_INVALID_PARAMETER,
45 "please provide valid name file!\n");
46 ret_value_msg_if(!str, RESOURCED_ERROR_INVALID_PARAMETER,
47 "please provide valid string!\n");
50 ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, "Fail to file open");
54 ret_value_errno_msg_if(ret == EOF, RESOURCED_ERROR_FAIL,
55 "Fail to write file\n");
57 return RESOURCED_ERROR_NONE;
60 resourced_ret_c fwrite_int(const char *path, const int number)
62 char digit_buf[MAX_DEC_SIZE(int)];
65 ret = sprintf(digit_buf, "%d", number);
66 ret_value_errno_msg_if(ret < 0, RESOURCED_ERROR_FAIL,
69 return fwrite_str(path, digit_buf);
72 resourced_ret_c fwrite_uint(const char *path, const u_int32_t number)
74 char digit_buf[MAX_DEC_SIZE(u_int32_t)];
77 ret = sprintf(digit_buf, "%u", number);
78 ret_value_errno_msg_if(ret < 0, RESOURCED_ERROR_FAIL,
81 return fwrite_str(path, digit_buf);
84 resourced_ret_c fread_int(const char *path, u_int32_t *number)
91 ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, "Fail to open file");
93 ret = fscanf(f, "%u", number);
95 ret_value_errno_msg_if(ret == EOF, RESOURCED_ERROR_FAIL,
96 "Fail to read file\n");
98 return RESOURCED_ERROR_NONE;
101 resourced_ret_c fwrite_array(const char *path, const void *array,
102 const size_t size_of_elem,
103 const size_t numb_of_elem)
108 ret_value_msg_if(!array, RESOURCED_ERROR_INVALID_PARAMETER,
109 "please provide valid array of elements!\n");
111 f = fopen(path, "w");
113 ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL,
114 "Failed open %s file\n", path);
116 ret = fwrite(array, size_of_elem, numb_of_elem, f);
118 ret_value_errno_msg_if(ret != numb_of_elem, RESOURCED_ERROR_FAIL,
119 "Failed write array into %s file\n");
121 return RESOURCED_ERROR_NONE;
124 /* reads file contents into memory */
125 char* cread(const char* path)
133 int fd = open(path, O_RDONLY);
136 _E("%s open error", path);
141 /* ensure we have enough space */
143 ptr = (char*)realloc(text, size + BUF_INC_SIZE);
152 size += BUF_INC_SIZE;
154 ret = read(fd, ptr, cap);
157 } else if (ret > 0) {
164 return (ret < 0 ? NULL : text);
167 /* like fgets/gets but adjusting contents pointer */
168 char* cgets(char** contents)
170 if (contents && *contents && **contents) {
171 char* bos = *contents; /* begin of string */
172 char* eos = strchr(bos, '\n'); /* end of string */