context: use darray for include paths Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
diff --git a/src/context.c b/src/context.c
index 7112b47..1e28911 100644
--- a/src/context.c
+++ b/src/context.c
@@ -34,9 +34,7 @@
struct xkb_context {
int refcnt;
- char **include_paths;
- int num_include_paths;
- int size_include_paths;
+ darray(char *) includes;
/* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
int file_id;
@@ -52,20 +50,7 @@ xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
{
struct stat stat_buf;
int err;
-
- if (ctx->size_include_paths <= ctx->num_include_paths) {
- int new_size;
- char **new_paths;
- new_size = ctx->size_include_paths + 2;
- new_paths = uTypedRecalloc(ctx->include_paths,
- ctx->size_include_paths,
- new_size,
- char *);
- if (!new_paths)
- return 0;
- ctx->include_paths = new_paths;
- ctx->size_include_paths = new_size;
- }
+ char *tmp;
err = stat(path, &stat_buf);
if (err != 0)
@@ -81,11 +66,11 @@ xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
return 0;
#endif
- ctx->include_paths[ctx->num_include_paths] = strdup(path);
- if (!ctx->include_paths[ctx->num_include_paths])
+ tmp = strdup(path);
+ if (!tmp)
return 0;
- ctx->num_include_paths++;
+ darray_append(ctx->includes, tmp);
return 1;
}
@@ -119,15 +104,12 @@ xkb_context_include_path_append_default(struct xkb_context *ctx)
_X_EXPORT void
xkb_context_include_path_clear(struct xkb_context *ctx)
{
- int i;
+ char **path;
- for (i = 0; i < ctx->num_include_paths; i++) {
- free(ctx->include_paths[i]);
- ctx->include_paths[i] = NULL;
- }
- free(ctx->include_paths);
- ctx->include_paths = NULL;
- ctx->num_include_paths = 0;
+ darray_foreach(path, ctx->includes)
+ free(*path);
+
+ darray_free(ctx->includes);
}
/**
@@ -146,7 +128,7 @@ xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
_X_EXPORT unsigned int
xkb_context_num_include_paths(struct xkb_context *ctx)
{
- return ctx->num_include_paths;
+ return darray_size(ctx->includes);
}
/**
@@ -159,7 +141,7 @@ xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
if (idx >= xkb_context_num_include_paths(ctx))
return NULL;
- return ctx->include_paths[idx];
+ return darray_item(ctx->includes, idx);
}
int