WIP, it compiles
[imager.git] / context.c
CommitLineData
74315ca9
TC
1#include "imageri.h"
2
3/*
4=item im_context_new()
5
6Create a new Imager context object.
7
8=cut
9*/
10
11im_context_t
12im_context_new(void) {
13 im_context_t ctx = mymalloc(sizeof(im_context_struct));
14 int i;
15
16 ctx->error_sp = IM_ERROR_COUNT-1;
17 for (i = 0; i < IM_ERROR_COUNT; ++i) {
18 ctx->error_alloc[i] = 0;
19 ctx->error_stack[i].msg = NULL;
20 ctx->error_stack[i].code = 0;
21 }
22#ifdef IMAGER_LOG
23 ctx->log_level = 0;
24 ctx->lg_file = NULL;
25#endif
44d86483
TC
26 ctx->max_width = 0;
27 ctx->max_height = 0;
28 ctx->max_bytes = 0;
74315ca9
TC
29
30 return ctx;
31}
32
33/*
34=item im_context_delete(ctx)
35
36Release memory used by an Imager context object.
37
38=cut
39*/
40
41void
42im_context_delete(im_context_t ctx) {
43 int i;
44
45 for (i = 0; i < IM_ERROR_COUNT; ++i) {
46 if (ctx->error_stack[i].msg)
47 myfree(ctx->error_stack[i].msg);
48 }
49#ifdef IMAGER_LOG
50 if (ctx->lg_file)
51 fclose(ctx->lg_file);
52#endif
53}
54
55/*
56=item im_context_clone(ctx)
57
58Clone an Imager context object, returning the result.
59
60=cut
61*/
62
63im_context_t
64im_context_clone(im_context_t ctx) {
65 im_context_t nctx = mymalloc(sizeof(im_context_struct));
66 int i;
67
68 nctx->error_sp = ctx->error_sp;
69 for (i = 0; i < IM_ERROR_COUNT; ++i) {
70 if (ctx->error_stack[i].msg) {
71 size_t sz = ctx->error_alloc[i];
72 nctx->error_alloc[i] = sz;
73 nctx->error_stack[i].msg = mymalloc(sz);
74 memcpy(nctx->error_stack[i].msg, ctx->error_stack[i].msg, sz);
75 }
76 else {
77 nctx->error_alloc[i] = 0;
78 nctx->error_stack[i].msg = NULL;
79 }
80 nctx->error_stack[i].code = ctx->error_stack[i].code;
81 }
82#ifdef IMAGER_LOG
83 nctx->log_level = ctx->log_level;
84 if (ctx->lg_file) {
85 /* disable buffering, this isn't perfect */
86 setvbuf(ctx->lg_file, NULL, _IONBF, 0);
87
88 /* clone that and disable buffering some more */
89 nctx->lg_file = fdopen(fileno(ctx->lg_file), "a");
90 if (nctx->lg_file)
91 setvbuf(nctx->lg_file, NULL, _IONBF, 0);
92 }
93 else {
44d86483 94 nctx->lg_file = NULL;
74315ca9
TC
95 }
96#endif
44d86483
TC
97 nctx->max_width = ctx->max_width;
98 nctx->max_height = ctx->max_height;
99 nctx->max_bytes = ctx->max_bytes;
74315ca9
TC
100
101 return ctx;
102}