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