|
36 | 36 |
|
37 | 37 | %{ |
38 | 38 | #include <postscriptbarcode.h> |
| 39 | +#include <stdlib.h> |
| 40 | +#include <string.h> |
| 41 | + |
| 42 | +typedef struct BwippInitOpts { |
| 43 | + int flags; |
| 44 | + char *filename; |
| 45 | +} BwippInitOpts; |
39 | 46 | %} |
40 | 47 |
|
41 | 48 | /* |
|
115 | 122 | } |
116 | 123 | #endif |
117 | 124 |
|
| 125 | +/* Ruby: attribute writer style */ |
| 126 | +#ifdef SWIGRUBY |
| 127 | +%rename("filename=") BwippInitOpts::set_filename; |
| 128 | +%rename("lazy_load=") BwippInitOpts::set_lazy_load; |
| 129 | +#endif |
| 130 | + |
| 131 | +/* Python: property wrappers added via %pythoncode below */ |
| 132 | + |
118 | 133 | /* Java: rename snake_case methods to camelCase */ |
119 | 134 | #ifdef SWIGJAVA |
| 135 | +%rename(InitOpts) BwippInitOpts; |
| 136 | +%rename(setFilename) BwippInitOpts::set_filename; |
| 137 | +%rename(setLazyLoad) BwippInitOpts::set_lazy_load; |
120 | 138 | %rename(getVersion) BWIPP::get_version; |
121 | 139 | %rename(listEncoders) BWIPP::list_encoders; |
122 | 140 | %rename(listProperties) BWIPP::list_properties; |
|
128 | 146 | %rename(emitExec) BWIPP::emit_exec; |
129 | 147 | #endif |
130 | 148 |
|
| 149 | +struct BwippInitOpts { }; |
| 150 | + |
| 151 | +%extend BwippInitOpts { |
| 152 | + BwippInitOpts() { |
| 153 | + return (BwippInitOpts *)calloc(1, sizeof(BwippInitOpts)); |
| 154 | + } |
| 155 | + ~BwippInitOpts() { |
| 156 | + free($self->filename); |
| 157 | + free($self); |
| 158 | + } |
| 159 | + void set_filename(const char *filename) { |
| 160 | + free($self->filename); |
| 161 | + $self->filename = filename ? strdup(filename) : NULL; |
| 162 | + } |
| 163 | + void set_lazy_load(int enable) { |
| 164 | + if (enable) |
| 165 | + $self->flags |= bwipp_iLAZY_LOAD; |
| 166 | + else |
| 167 | + $self->flags &= ~bwipp_iLAZY_LOAD; |
| 168 | + } |
| 169 | +#ifdef SWIGPYTHON |
| 170 | + %pythoncode %{ |
| 171 | + filename = property(fset=set_filename) |
| 172 | + lazy_load = property(fset=set_lazy_load) |
| 173 | + %} |
| 174 | +#endif |
| 175 | +#ifdef SWIGJAVA |
| 176 | + /* Java: fluent setter wrappers returning this */ |
| 177 | + %proxycode %{ |
| 178 | + public InitOpts filename(String filename) { |
| 179 | + setFilename(filename); |
| 180 | + return this; |
| 181 | + } |
| 182 | + public InitOpts lazyLoad(boolean enable) { |
| 183 | + setLazyLoad(enable ? 1 : 0); |
| 184 | + return this; |
| 185 | + } |
| 186 | + %} |
| 187 | +#endif |
| 188 | +}; |
| 189 | + |
131 | 190 | struct BWIPP { }; |
132 | 191 |
|
133 | 192 | %extend BWIPP { |
134 | 193 |
|
135 | 194 | %typemap(newfree) char * "bwipp_free($1);"; |
136 | 195 |
|
137 | | - BWIPP(char* filename) { |
138 | | - return bwipp_load_from_file(filename); |
| 196 | +#ifdef SWIGPERL |
| 197 | + /* Perl: accept a hashref as init options */ |
| 198 | + %perlcode %{ |
| 199 | + package postscriptbarcode::BWIPP; |
| 200 | + my $_new_orig = \&new; |
| 201 | + no warnings 'redefine'; |
| 202 | + *new = sub { |
| 203 | + my $pkg = shift; |
| 204 | + if (@_ == 1 && ref $_[0] eq 'HASH') { |
| 205 | + my $h = $_[0]; |
| 206 | + my $opts = postscriptbarcode::BwippInitOpts->new(); |
| 207 | + $opts->set_filename($h->{filename}) if exists $h->{filename}; |
| 208 | + $opts->set_lazy_load($h->{lazy_load} ? 1 : 0) if exists $h->{lazy_load}; |
| 209 | + return $_new_orig->($pkg, $opts); |
| 210 | + } |
| 211 | + return $_new_orig->($pkg, @_); |
| 212 | + }; |
| 213 | + %} |
| 214 | +#endif |
| 215 | + BWIPP(BwippInitOpts *opts) { |
| 216 | + bwipp_load_init_opts_t c_opts = { |
| 217 | + .struct_size = sizeof(c_opts), |
| 218 | + .filename = opts ? opts->filename : NULL, |
| 219 | + .flags = opts ? opts->flags : bwipp_iDEFAULT, |
| 220 | + }; |
| 221 | + return bwipp_load_ex(&c_opts); |
139 | 222 | } |
140 | 223 | BWIPP() { |
141 | 224 | return bwipp_load(); |
@@ -175,3 +258,44 @@ struct BWIPP { }; |
175 | 258 | return bwipp_emit_exec($self,barcode,contents,options); |
176 | 259 | } |
177 | 260 | }; |
| 261 | + |
| 262 | +#ifdef SWIGRUBY |
| 263 | +/* Ruby: accept keyword arguments to BWIPP.new */ |
| 264 | +%init %{ |
| 265 | + rb_eval_string( |
| 266 | + "class Postscriptbarcode::BWIPP\n" |
| 267 | + " class << self\n" |
| 268 | + " alias_method :_new_orig, :new\n" |
| 269 | + " def new(**kwargs)\n" |
| 270 | + " if kwargs.empty?\n" |
| 271 | + " _new_orig\n" |
| 272 | + " else\n" |
| 273 | + " opts = Postscriptbarcode::BwippInitOpts.new\n" |
| 274 | + " opts.filename = kwargs[:filename] if kwargs.key?(:filename)\n" |
| 275 | + " opts.lazy_load = kwargs[:lazy_load] ? 1 : 0 if kwargs.key?(:lazy_load)\n" |
| 276 | + " _new_orig(opts)\n" |
| 277 | + " end\n" |
| 278 | + " end\n" |
| 279 | + " end\n" |
| 280 | + "end\n" |
| 281 | + ); |
| 282 | +%} |
| 283 | +#endif |
| 284 | + |
| 285 | +#ifdef SWIGPYTHON |
| 286 | +/* Python: accept keyword arguments to BWIPP() */ |
| 287 | +%pythoncode %{ |
| 288 | +_BWIPP_init_orig = BWIPP.__init__ |
| 289 | +def _BWIPP_init_kwargs(self, *args, filename=None, lazy_load=None): |
| 290 | + if args or (filename is None and lazy_load is None): |
| 291 | + _BWIPP_init_orig(self, *args) |
| 292 | + else: |
| 293 | + opts = BwippInitOpts() |
| 294 | + if filename is not None: |
| 295 | + opts.filename = filename |
| 296 | + if lazy_load is not None: |
| 297 | + opts.lazy_load = lazy_load |
| 298 | + _BWIPP_init_orig(self, opts) |
| 299 | +BWIPP.__init__ = _BWIPP_init_kwargs |
| 300 | +%} |
| 301 | +#endif |
0 commit comments