diff options
| -rw-r--r-- | common/utils.c | 88 | ||||
| -rw-r--r-- | common/utils.h | 18 |
2 files changed, 106 insertions, 0 deletions
diff --git a/common/utils.c b/common/utils.c index bd2bf1f..5248c2c 100644 --- a/common/utils.c +++ b/common/utils.c | |||
| @@ -106,3 +106,91 @@ char *string_concat(const char *str, ...) | |||
| 106 | 106 | ||
| 107 | return result; | 107 | return result; |
| 108 | } | 108 | } |
| 109 | |||
| 110 | void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) | ||
| 111 | { | ||
| 112 | FILE *f; | ||
| 113 | uint64_t size; | ||
| 114 | |||
| 115 | *length = 0; | ||
| 116 | |||
| 117 | f = fopen(filename, "rb"); | ||
| 118 | if (!f) { | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | fseek(f, 0, SEEK_END); | ||
| 123 | size = ftell(f); | ||
| 124 | rewind(f); | ||
| 125 | |||
| 126 | if (size == 0) { | ||
| 127 | fclose(f); | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | *buffer = (char*)malloc(sizeof(char)*(size+1)); | ||
| 132 | fread(*buffer, sizeof(char), size, f); | ||
| 133 | fclose(f); | ||
| 134 | |||
| 135 | *length = size; | ||
| 136 | } | ||
| 137 | |||
| 138 | void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) | ||
| 139 | { | ||
| 140 | FILE *f; | ||
| 141 | |||
| 142 | f = fopen(filename, "ab"); | ||
| 143 | if (!f) | ||
| 144 | f = fopen(filename, "wb"); | ||
| 145 | if (f) { | ||
| 146 | fwrite(buffer, sizeof(char), length, f); | ||
| 147 | fclose(f); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | int plist_read_from_filename(plist_t *plist, const char *filename) | ||
| 152 | { | ||
| 153 | char *buffer = NULL; | ||
| 154 | uint64_t length; | ||
| 155 | |||
| 156 | if (!filename) | ||
| 157 | return 0; | ||
| 158 | |||
| 159 | buffer_read_from_filename(filename, &buffer, &length); | ||
| 160 | |||
| 161 | if (!buffer) { | ||
| 162 | return 0; | ||
| 163 | } | ||
| 164 | |||
| 165 | if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) { | ||
| 166 | plist_from_bin(buffer, length, plist); | ||
| 167 | } else { | ||
| 168 | plist_from_xml(buffer, length, plist); | ||
| 169 | } | ||
| 170 | |||
| 171 | free(buffer); | ||
| 172 | |||
| 173 | return 1; | ||
| 174 | } | ||
| 175 | |||
| 176 | int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) | ||
| 177 | { | ||
| 178 | char *buffer = NULL; | ||
| 179 | uint32_t length; | ||
| 180 | |||
| 181 | if (!plist || !filename) | ||
| 182 | return 0; | ||
| 183 | |||
| 184 | if (format == PLIST_FORMAT_XML) | ||
| 185 | plist_to_xml(plist, &buffer, &length); | ||
| 186 | else if (format == PLIST_FORMAT_BINARY) | ||
| 187 | plist_to_bin(plist, &buffer, &length); | ||
| 188 | else | ||
| 189 | return 0; | ||
| 190 | |||
| 191 | buffer_write_to_filename(filename, buffer, length); | ||
| 192 | |||
| 193 | free(buffer); | ||
| 194 | |||
| 195 | return 1; | ||
| 196 | } | ||
diff --git a/common/utils.h b/common/utils.h index 441d188..8394272 100644 --- a/common/utils.h +++ b/common/utils.h | |||
| @@ -22,9 +22,27 @@ | |||
| 22 | #ifndef __UTILS_H | 22 | #ifndef __UTILS_H |
| 23 | #define __UTILS_H | 23 | #define __UTILS_H |
| 24 | 24 | ||
| 25 | #ifdef WIN32 | ||
| 26 | #include <windows.h> | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #include <stdio.h> | ||
| 30 | #include <plist/plist.h> | ||
| 31 | |||
| 25 | #ifndef HAVE_STPCPY | 32 | #ifndef HAVE_STPCPY |
| 26 | char *stpcpy(char * s1, const char * s2); | 33 | char *stpcpy(char * s1, const char * s2); |
| 27 | #endif | 34 | #endif |
| 28 | char *string_concat(const char *str, ...); | 35 | char *string_concat(const char *str, ...); |
| 29 | 36 | ||
| 37 | void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length); | ||
| 38 | void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length); | ||
| 39 | |||
| 40 | enum plist_format_t { | ||
| 41 | PLIST_FORMAT_XML, | ||
| 42 | PLIST_FORMAT_BINARY | ||
| 43 | }; | ||
| 44 | |||
| 45 | int plist_read_from_filename(plist_t *plist, const char *filename); | ||
| 46 | int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); | ||
| 47 | |||
| 30 | #endif | 48 | #endif |
