tizen 2.3 release
[kernel/api/system-resource.git] / src / common / file-helper.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20
21 /*
22  * @file file-helper.c
23  * @desc Helper functions for working with files
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include "file-helper.h"
32 #include "trace.h"
33 #include "macro.h"
34
35 #define BUF_MAX         (BUFSIZ)
36 #define BUF_INC_SIZE    (512 * 1024)
37 #define KB(bytes)       ((bytes)/1024)
38
39 resourced_ret_c fwrite_str(const char *path, const char *str)
40 {
41         FILE *f;
42         int ret;
43
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");
48
49         f = fopen(path, "w");
50         ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, "Fail to file open");
51
52         ret = fputs(str, f);
53         fclose(f);
54         ret_value_errno_msg_if(ret == EOF, RESOURCED_ERROR_FAIL,
55                                "Fail to write file\n");
56
57         return RESOURCED_ERROR_NONE;
58 }
59
60 resourced_ret_c fwrite_int(const char *path, const int number)
61 {
62         char digit_buf[MAX_DEC_SIZE(int)];
63         int ret;
64
65         ret = sprintf(digit_buf, "%d", number);
66         ret_value_errno_msg_if(ret < 0, RESOURCED_ERROR_FAIL,
67                                "sprintf failed\n");
68
69         return fwrite_str(path, digit_buf);
70 }
71
72 resourced_ret_c fwrite_uint(const char *path, const u_int32_t number)
73 {
74         char digit_buf[MAX_DEC_SIZE(u_int32_t)];
75         int ret;
76
77         ret = sprintf(digit_buf, "%u", number);
78         ret_value_errno_msg_if(ret < 0, RESOURCED_ERROR_FAIL,
79                                "sprintf failed\n");
80
81         return fwrite_str(path, digit_buf);
82 }
83
84 resourced_ret_c fread_int(const char *path, u_int32_t *number)
85 {
86         FILE *f;
87         int ret;
88
89         f = fopen(path, "r");
90
91         ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, "Fail to open file");
92
93         ret = fscanf(f, "%u", number);
94         fclose(f);
95         ret_value_errno_msg_if(ret == EOF, RESOURCED_ERROR_FAIL,
96                                "Fail to read file\n");
97
98         return RESOURCED_ERROR_NONE;
99 }
100
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)
104 {
105         FILE *f;
106         int ret;
107
108         ret_value_msg_if(!array, RESOURCED_ERROR_INVALID_PARAMETER,
109                          "please provide valid array of elements!\n");
110
111         f = fopen(path, "w");
112
113         ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL,
114                                "Failed open %s file\n", path);
115
116         ret = fwrite(array, size_of_elem, numb_of_elem, f);
117         fclose(f);
118         ret_value_errno_msg_if(ret != numb_of_elem, RESOURCED_ERROR_FAIL,
119                                "Failed write array into %s file\n");
120
121         return RESOURCED_ERROR_NONE;
122 }
123
124 /* reads file contents into memory */
125 char* cread(const char* path)
126 {
127         char*   text = NULL;
128         size_t  size = 0;
129
130         ssize_t ret;
131         char*   ptr = text;
132         size_t  cap = size;
133         int     fd  = open(path, O_RDONLY);
134
135         if (fd < 0) {
136                 _E("%s open error", path);
137                 return NULL;
138         }
139
140         do {
141                 /* ensure we have enough space */
142                 if (cap == 0) {
143                         ptr = (char*)realloc(text, size + BUF_INC_SIZE);
144                         if (ptr == NULL) {
145                                 ret = -1;
146                                 break;
147                         }
148
149                         text  = ptr;
150                         ptr   = text + size;
151                         cap   = BUF_INC_SIZE;
152                         size += BUF_INC_SIZE;
153                 }
154                 ret = read(fd, ptr, cap);
155                 if (ret == 0) {
156                         *ptr = 0;
157                 } else if (ret > 0) {
158                         cap -= ret;
159                         ptr += ret;
160                 }
161         } while (ret > 0);
162         close(fd);
163
164         return (ret < 0 ? NULL : text);
165 }
166
167 /* like fgets/gets but adjusting contents pointer */
168 char* cgets(char** contents)
169 {
170         if (contents && *contents && **contents) {
171                 char* bos = *contents;          /* begin of string */
172                 char* eos = strchr(bos, '\n');  /* end of string   */
173
174                 if (eos) {
175                         *contents = eos + 1;
176                         *eos      = 0;
177                 } else {
178                         *contents = NULL;
179                 }
180
181                 return bos;
182         }
183
184         return NULL;
185 }