Skip to content

Commit 7030069

Browse files
committed
libs: Rework the C library and binding APIs
1 parent bddea9d commit 7030069

12 files changed

Lines changed: 536 additions & 168 deletions

File tree

.github/workflows/package-install-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ jobs:
3434
persist-credentials: false
3535
sparse-checkout: |
3636
packaging-examples/install_tests
37+
libs/c/example.c
3738
libs/bindings/python/example.py
3839
libs/bindings/perl/example.pl
3940
libs/bindings/ruby/example.rb
4041
libs/bindings/java/Example.java
4142
4243
- name: Collect test assets
4344
run: |
45+
cp libs/c/example.c \
46+
packaging-examples/install_tests/example.c
4447
cp libs/bindings/python/example.py \
4548
packaging-examples/install_tests/example.py
4649
cp libs/bindings/perl/example.pl \

libs/bindings/java/Example.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uk.co.terryburton.bwipp.BWIPP;
2+
import uk.co.terryburton.bwipp.InitOpts;
23

34
public class Example {
45

@@ -10,22 +11,23 @@ public static void main(final String[] args) {
1011
BWIPP bwipp;
1112

1213
if (args.length > 0) {
13-
bwipp = new BWIPP(args[0]);
14+
bwipp = new BWIPP(new InitOpts().filename(args[0]));
1415
if (bwipp.getVersion() == null) {
1516
System.err.println("Failed to load resource");
1617
System.exit(1);
1718
}
1819
System.out.println("Version: " + bwipp.getVersion());
1920
} else {
20-
BWIPP bwipp1 =
21-
new BWIPP("../../../build/monolithic_package/barcode.ps");
21+
BWIPP bwipp1 = new BWIPP(new InitOpts()
22+
.filename("../../../build/monolithic_package/barcode.ps"));
2223

2324
if (bwipp1.getVersion() == null) {
2425
System.err.println("Failed to load resource");
2526
System.exit(1);
2627
}
2728

28-
bwipp = new BWIPP("../../../build/monolithic/barcode.ps");
29+
bwipp = new BWIPP(new InitOpts()
30+
.filename("../../../build/monolithic/barcode.ps"));
2931

3032
if (bwipp.getVersion() == null) {
3133
System.err.println("Failed to load resource");

libs/bindings/perl/example.pl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
my $bwipp;
77

88
if (@ARGV) {
9-
$bwipp = postscriptbarcode::BWIPP->new($ARGV[0]) || die 'Failed to load resource\n';
9+
$bwipp = postscriptbarcode::BWIPP->new({filename => $ARGV[0]})
10+
|| die 'Failed to load resource\n';
1011
my $ver = $bwipp->get_version() || die 'Failed to get version\n';
1112
print "Version: $ver\n";
1213
} else {
13-
my $bwipp1 = postscriptbarcode::BWIPP->new("../../../build/monolithic_package/barcode.ps") || die 'Failed to load resource\n';
14-
$bwipp = postscriptbarcode::BWIPP->new("../../../build/monolithic/barcode.ps") || die 'Failed to load resource\n';
14+
my $bwipp1 = postscriptbarcode::BWIPP->new({
15+
filename => "../../../build/monolithic_package/barcode.ps",
16+
}) || die 'Failed to load resource\n';
17+
18+
$bwipp = postscriptbarcode::BWIPP->new({
19+
filename => "../../../build/monolithic/barcode.ps",
20+
}) || die 'Failed to load resource\n';
1521

1622
my $ver = $bwipp1->get_version() || die 'Failed to get version\n';
1723
print "Packaged version: $ver\n";

libs/bindings/postscriptbarcode.i

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636

3737
%{
3838
#include <postscriptbarcode.h>
39+
#include <stdlib.h>
40+
#include <string.h>
41+
42+
typedef struct BwippInitOpts {
43+
int flags;
44+
char *filename;
45+
} BwippInitOpts;
3946
%}
4047

4148
/*
@@ -115,8 +122,19 @@
115122
}
116123
#endif
117124

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+
118133
/* Java: rename snake_case methods to camelCase */
119134
#ifdef SWIGJAVA
135+
%rename(InitOpts) BwippInitOpts;
136+
%rename(setFilename) BwippInitOpts::set_filename;
137+
%rename(setLazyLoad) BwippInitOpts::set_lazy_load;
120138
%rename(getVersion) BWIPP::get_version;
121139
%rename(listEncoders) BWIPP::list_encoders;
122140
%rename(listProperties) BWIPP::list_properties;
@@ -128,14 +146,79 @@
128146
%rename(emitExec) BWIPP::emit_exec;
129147
#endif
130148

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+
131190
struct BWIPP { };
132191

133192
%extend BWIPP {
134193

135194
%typemap(newfree) char * "bwipp_free($1);";
136195

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);
139222
}
140223
BWIPP() {
141224
return bwipp_load();
@@ -175,3 +258,44 @@ struct BWIPP { };
175258
return bwipp_emit_exec($self,barcode,contents,options);
176259
}
177260
};
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

libs/bindings/python/example.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,29 @@
77
import postscriptbarcode
88

99
if len(sys.argv) > 1:
10-
path = sys.argv[1]
11-
bwipp = postscriptbarcode.BWIPP(path)
10+
bwipp = postscriptbarcode.BWIPP(filename=sys.argv[1])
1211
print("Version: " + bwipp.get_version())
1312
else:
14-
bwipp1 = postscriptbarcode.BWIPP("../../../build/monolithic_package/barcode.ps")
15-
bwipp2 = postscriptbarcode.BWIPP("../../../build/monolithic/barcode.ps")
13+
bwipp1 = postscriptbarcode.BWIPP(
14+
filename="../../../build/monolithic_package/barcode.ps")
15+
bwipp2 = postscriptbarcode.BWIPP(
16+
filename="../../../build/monolithic/barcode.ps")
17+
1618
print("Packaged version: " + bwipp1.get_version())
1719
print("Unpackaged version: " + bwipp2.get_version())
1820
bwipp = bwipp2
1921

2022
ps = bwipp1.emit_all_resources()
21-
print("Packaged lines: " + str(len(ps.split("\n"))))
23+
print("Packaged lines: " + str(ps.count("\n")))
2224

2325
ps = bwipp.emit_all_resources()
24-
print("Unpackaged lines: " + str(len(ps.split("\n"))))
26+
print("Unpackaged lines: " + str(ps.count("\n")))
2527

2628
ps = bwipp.emit_required_resources("qrcode")
27-
print("qrcode resource lines: " + str(len(ps.split("\n"))))
29+
print("qrcode resource lines: " + str(ps.count("\n")))
2830

2931
ps = bwipp.emit_exec("qrcode", "Hello World", "eclevel=M")
30-
print("emit_exec lines: " + str(len(ps.split("\n"))))
32+
print("emit_exec lines: " + str(ps.count("\n")))
3133

3234
encoders = bwipp.list_encoders()
3335
print("Encoders: " + str(len(encoders)))

libs/bindings/ruby/example.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
require "postscriptbarcode"
55

66
if ARGV.length > 0
7-
bwipp = Postscriptbarcode::BWIPP.new(ARGV[0])
7+
bwipp = Postscriptbarcode::BWIPP.new(filename: ARGV[0])
88
puts "Version: " + bwipp.get_version
99
else
10-
bwipp1 = Postscriptbarcode::BWIPP.new(File.dirname(__FILE__) + "/../../../build/monolithic_package/barcode.ps")
11-
bwipp = Postscriptbarcode::BWIPP.new(File.dirname(__FILE__) + "/../../../build/monolithic/barcode.ps")
10+
bwipp1 = Postscriptbarcode::BWIPP.new(
11+
filename: File.dirname(__FILE__) + "/../../../build/monolithic_package/barcode.ps")
12+
bwipp = Postscriptbarcode::BWIPP.new(
13+
filename: File.dirname(__FILE__) + "/../../../build/monolithic/barcode.ps")
1214

1315
puts "Packaged version: " + bwipp1.get_version
1416
puts "Unpackaged version: " + bwipp.get_version

0 commit comments

Comments
 (0)