[vlc-commits] modules: factor common loading from macros to functions
Rémi Denis-Courmont
git at videolan.org
Tue Oct 25 21:55:08 CEST 2016
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Oct 25 19:55:32 2016 +0300| [8c6435bd21e23b225d75701ce87ef4b5d316f084] | committer: Rémi Denis-Courmont
modules: factor common loading from macros to functions
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=8c6435bd21e23b225d75701ce87ef4b5d316f084
---
src/modules/cache.c | 74 ++++++++++++++++++++++++++++++++---------------------
1 file changed, 45 insertions(+), 29 deletions(-)
diff --git a/src/modules/cache.c b/src/modules/cache.c
index 54a1bba..a12d43b 100644
--- a/src/modules/cache.c
+++ b/src/modules/cache.c
@@ -76,48 +76,64 @@ void CacheDelete( vlc_object_t *obj, const char *dir )
free( path );
}
-#define LOAD_IMMEDIATE(a) \
- if (fread (&(a), sizeof (char), sizeof (a), file) != sizeof (a)) \
- goto error
-#define LOAD_FLAG(a) \
- do { \
- unsigned char b; \
- LOAD_IMMEDIATE(b); \
- if (b > 1) \
- goto error; \
- (a) = b; \
- } while (0)
+static int vlc_cache_load_immediate(void *out, FILE *in, size_t size)
+{
+ return fread(out, sizeof (char), size, in) == size ? 0 : -1;
+}
-static int CacheLoadString (char **p, FILE *file)
+static int vlc_cache_load_bool(bool *out, FILE *in)
+{
+ unsigned char b;
+
+ if (vlc_cache_load_immediate(&b, in, 1) || b > 1)
+ return -1;
+
+ *out = b;
+ return 0;
+}
+
+static int vlc_cache_load_string(char **restrict p, FILE *file)
{
- char *psz = NULL;
uint16_t size;
- LOAD_IMMEDIATE (size);
- if (size > 16384)
- {
-error:
+ if (vlc_cache_load_immediate(&size, file, sizeof (size)) || size > 16384)
return -1;
+
+ if (size == 0)
+ {
+ *p = NULL;
+ return 0;
}
- if (size > 0)
+ char *str = malloc(size + 1);
+ if (unlikely(str == NULL))
+ return -1;
+
+ if (vlc_cache_load_immediate(str, file, size))
{
- psz = malloc (size+1);
- if (unlikely(psz == NULL))
- goto error;
- if (fread (psz, 1, size, file) != size)
- {
- free (psz);
- goto error;
- }
- psz[size] = '\0';
+ free(str);
+ return -1;
}
- *p = psz;
+
+ str[size] = '\0';
+ *p = str;
return 0;
}
+#define LOAD_IMMEDIATE(a) \
+ if (vlc_cache_load_immediate(&(a), file, sizeof (a))) \
+ goto error
+#define LOAD_FLAG(a) \
+ do \
+ { \
+ bool b; \
+ if (vlc_cache_load_bool(&b, file)) \
+ goto error; \
+ (a) = b; \
+ } while (0)
#define LOAD_STRING(a) \
- if (CacheLoadString (&(a), file)) goto error
+ if (vlc_cache_load_string(&(a), file)) \
+ goto error
static int CacheLoadConfig (module_config_t *cfg, FILE *file)
{
More information about the vlc-commits
mailing list