summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWaLyong Cho <walyong.cho@samsung.com>2016-12-14 04:48:37 (GMT)
committerWaLyong Cho <walyong.cho@samsung.com>2016-12-14 04:48:37 (GMT)
commit0bf84e45ee6bb59bfab5dd37673f442db088062b (patch)
tree53241602d975260656ed00c4719cebf970a7a17c
parent3511ce781392b988e1554f8e8f642a635bedab61 (diff)
downloadlibsystem-0bf84e45ee6bb59bfab5dd37673f442db088062b.zip
libsystem-0bf84e45ee6bb59bfab5dd37673f442db088062b.tar.gz
libsystem-0bf84e45ee6bb59bfab5dd37673f442db088062b.tar.bz2
libsystem: add macro function for num read/write
Change-Id: I1ac6af736032f5b42017c94e5ef919a20886c5ad Signed-off-by: WaLyong Cho <walyong.cho@samsung.com>
-rw-r--r--src/libsystem/libsystem.c346
1 files changed, 85 insertions, 261 deletions
diff --git a/src/libsystem/libsystem.c b/src/libsystem/libsystem.c
index 6f87eef..a911745 100644
--- a/src/libsystem/libsystem.c
+++ b/src/libsystem/libsystem.c
@@ -590,154 +590,6 @@ int write_str_to_path(const char *path, const char *str, enum file_write_flags f
return write_str_to_file(f, str, flags);
}
-int write_int32_to_file(FILE *f, int32_t i, enum file_write_flags flags) {
- int r = 0;
-
- assert(f);
-
- STORE_RESET_ERRNO;
-
- (void) fprintf(f, "%d", i);
- if (flags & FILE_WRITE_NEWLINE_IF_NOT)
- (void) fputc('\n', f);
-
- if (flags & FILE_WRITE_WITH_FFLUSH)
- (void) fflush(f);
-
- if (ferror(f))
- r = errno ? -errno : -EIO;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int write_int32_to_path(const char *path, int32_t i, enum file_write_flags flags) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
-
- if (flags & FILE_WRITE_APPEND)
- f = fopen(path, "ae");
- else
- f = fopen(path, "we");
- if (!f)
- return -errno;
-
- return write_int32_to_file(f, i, flags);
-}
-
-int write_uint32_to_file(FILE *f, uint32_t u, enum file_write_flags flags) {
- int r = 0;
-
- assert(f);
-
- STORE_RESET_ERRNO;
-
- (void) fprintf(f, "%u", u);
- if (flags & FILE_WRITE_NEWLINE_IF_NOT)
- (void) fputc('\n', f);
-
- if (flags & FILE_WRITE_WITH_FFLUSH)
- (void) fflush(f);
-
- if (ferror(f))
- r = errno ? -errno : -EIO;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int write_uint32_to_path(const char *path, uint32_t u, enum file_write_flags flags) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
-
- if (flags & FILE_WRITE_APPEND)
- f = fopen(path, "ae");
- else
- f = fopen(path, "we");
- if (!f)
- return -errno;
-
- return write_uint32_to_file(f, u, flags);
-}
-
-int write_int64_to_file(FILE *f, int64_t i, enum file_write_flags flags) {
- int r = 0;
-
- assert(f);
-
- STORE_RESET_ERRNO;
-
- (void) fprintf(f, "%" PRId64, i);
- if (flags & FILE_WRITE_NEWLINE_IF_NOT)
- (void) fputc('\n', f);
-
- if (flags & FILE_WRITE_WITH_FFLUSH)
- (void) fflush(f);
-
- if (ferror(f))
- r = errno ? -errno : -EIO;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int write_int64_to_path(const char *path, int64_t i, enum file_write_flags flags) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
-
- if (flags & FILE_WRITE_APPEND)
- f = fopen(path, "ae");
- else
- f = fopen(path, "we");
- if (!f)
- return -errno;
-
- return write_int64_to_file(f, i, flags);
-}
-
-int write_uint64_to_file(FILE *f, uint64_t u, enum file_write_flags flags) {
- int r = 0;
-
- assert(f);
-
- STORE_RESET_ERRNO;
-
- (void) fprintf(f, "%" PRIu64, u);
- if (flags & FILE_WRITE_NEWLINE_IF_NOT)
- (void) fputc('\n', f);
-
- if (flags & FILE_WRITE_WITH_FFLUSH)
- (void) fflush(f);
-
- if (ferror(f))
- r = errno ? -errno : -EIO;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int write_uint64_to_path(const char *path, uint64_t u, enum file_write_flags flags) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
-
- if (flags & FILE_WRITE_APPEND)
- f = fopen(path, "ae");
- else
- f = fopen(path, "we");
- if (!f)
- return -errno;
-
- return write_uint64_to_file(f, u, flags);
-}
-
int read_one_line_from_file(FILE *f, char **line) {
char t[LINE_MAX], *c;
@@ -783,125 +635,97 @@ int read_one_line_from_path(const char *path, char **line) {
return read_one_line_from_file(f, line);
}
-int read_int32_from_file(FILE *f, int32_t *i) {
- int r = 0;
-
- assert(f);
- assert(i);
-
- STORE_RESET_ERRNO;
-
- r = fscanf(f, "%d", i);
- if (r == EOF && ferror(f))
- r = errno ? -errno : -EOF;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int read_int32_from_path(const char *path, int32_t *i) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
- assert(i);
-
- f = fopen(path, "re");
- if (!f)
- return -errno;
-
- return read_int32_from_file(f, i);
-}
-
-int read_uint32_from_file(FILE *f, uint32_t *u) {
- int r = 0;
-
- assert(f);
- assert(u);
-
- STORE_RESET_ERRNO;
-
- r = fscanf(f, "%u", u);
- if (r == EOF && ferror(f))
- r = errno ? -errno : -EOF;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int read_uint32_from_path(const char *path, uint32_t *u) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
- assert(u);
-
- f = fopen(path, "re");
- if (!f)
- return -errno;
-
- return read_uint32_from_file(f, u);
-}
-
-int read_int64_from_file(FILE *f, int64_t *i) {
- int r = 0;
-
- assert(f);
- assert(i);
-
- STORE_RESET_ERRNO;
-
- r = fscanf(f, "%" SCNd64, i);
- if (r == EOF && ferror(f))
- r = errno ? -errno : -EOF;
-
- RESTORE_ERRNO;
-
- return r;
-}
-
-int read_int64_from_path(const char *path, int64_t *i) {
- _cleanup_fclose_ FILE *f = NULL;
-
- assert(path);
- assert(i);
-
- f = fopen(path, "re");
- if (!f)
- return -errno;
-
- return read_int64_from_file(f, i);
-}
-
-int read_uint64_from_file(FILE *f, uint64_t *u) {
- int r = 0;
-
- assert(f);
- assert(u);
-
- STORE_RESET_ERRNO;
-
- r = fscanf(f, "%" SCNu64, u);
- if (r == EOF && ferror(f))
- r = errno ? -errno : -EOF;
+#define DEFINE_WRITE_NUM_TO_FILE(type, format) \
+ int write_##type##_to_file(FILE *f, \
+ type##_t u, \
+ enum file_write_flags flags) { \
+ int r = 0; \
+ \
+ assert(f); \
+ \
+ STORE_RESET_ERRNO; \
+ \
+ (void) fprintf(f, format, u); \
+ if (flags & FILE_WRITE_NEWLINE_IF_NOT) \
+ (void) fputc('\n', f); \
+ \
+ if (flags & FILE_WRITE_WITH_FFLUSH) \
+ (void) fflush(f); \
+ \
+ if (ferror(f)) \
+ r = errno ? -errno : -EIO; \
+ \
+ RESTORE_ERRNO; \
+ \
+ return r; \
+ }
- RESTORE_ERRNO;
+#define DEFINE_WRITE_NUM_TO_PATH(type) \
+ int write_##type##_to_path(const char *path, \
+ type##_t u, \
+ enum file_write_flags flags) { \
+ _cleanup_fclose_ FILE *f = NULL; \
+ \
+ assert(path); \
+ \
+ if (flags & FILE_WRITE_APPEND) \
+ f = fopen(path, "ae"); \
+ else \
+ f = fopen(path, "we"); \
+ if (!f) \
+ return -errno; \
+ \
+ return write_##type##_to_file(f, u, flags); \
+ }
- return r;
-}
+#define DEFINE_WRITE_NUM_DUAL(type, format) \
+ DEFINE_WRITE_NUM_TO_FILE(type, format); \
+ DEFINE_WRITE_NUM_TO_PATH(type)
+
+#define DEFINE_READ_NUM_FROM_FILE(type, format) \
+ int read_##type##_from_file(FILE *f, type##_t *num) { \
+ int r = 0; \
+ \
+ assert(f); \
+ assert(num); \
+ \
+ STORE_RESET_ERRNO; \
+ \
+ r = fscanf(f, format, num); \
+ if (r == EOF && ferror(f)) \
+ r = errno ? -errno : -EOF; \
+ \
+ RESTORE_ERRNO; \
+ \
+ return r; \
+ }
-int read_uint64_from_path(const char *path, uint64_t *u) {
- _cleanup_fclose_ FILE *f = NULL;
+#define DEFINE_READ_NUM_FROM_PATH(type) \
+ int read_##type##_from_path(const char *path, type##_t *num) { \
+ _cleanup_fclose_ FILE *f = NULL; \
+ \
+ assert(path); \
+ assert(num); \
+ \
+ f = fopen(path, "re"); \
+ if (!f) \
+ return -errno; \
+ \
+ return read_##type##_from_file(f, num); \
+ }
- assert(path);
- assert(u);
+#define DEFINE_READ_NUM_DUAL(type, format) \
+ DEFINE_READ_NUM_FROM_FILE(type, format); \
+ DEFINE_READ_NUM_FROM_PATH(type)
- f = fopen(path, "re");
- if (!f)
- return -errno;
+#define DEFINE_READ_WRITE_NUM_DUAL(type, r_format, w_format) \
+ DEFINE_READ_NUM_DUAL(type, r_format); \
+ DEFINE_WRITE_NUM_DUAL(type, w_format)
- return read_uint64_from_file(f, u);
-}
+DEFINE_READ_WRITE_NUM_DUAL(int32, "%d", "%d");
+DEFINE_READ_WRITE_NUM_DUAL(uint32, "%u", "%u");
+DEFINE_READ_WRITE_NUM_DUAL(int64, "%" SCNd64, "%" PRId64);
+DEFINE_READ_WRITE_NUM_DUAL(uint64, "%" SCNu64, "%" PRIu64);
int str_to_strv(const char *str, char ***strv, const char *separator) {
char *w, *state, *p;