Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 147 lines (126 sloc) 3.791 kb
ebe916f @ossguy add ISC licence in COPYING and file headers
authored
1 /*
2 * driver.c - test driver for libbitconvert
3 * This file is part of libbitconvert.
4 *
5 * Copyright (c) 2008-2009, Denver Gingerich <denver@ossguy.com>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
271330b @ossguy switch from manual version tracking to git
authored
20 #include "bitconvert.h"
21 #include <stdio.h> /* FILE, fgets, printf */
22 #include <string.h> /* strlen */
23
095061e @ossguy memory for struct bc_input now managed by user
authored
24 #define TRACK_INPUT_SIZE 4096
25
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
26
27 char* get_track(FILE* input, char* bits, int bits_len)
28 {
29 int bits_end;
30
82fbd62 @ossguy update code to use consistent bracing conventions
authored
31 if (NULL == fgets(bits, bits_len, input)) {
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
32 return NULL;
33 }
34 bits_end = strlen(bits);
35
36 /* strip trailing newline */
82fbd62 @ossguy update code to use consistent bracing conventions
authored
37 if ('\n' == bits[bits_end - 1]) {
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
38 bits[bits_end - 1] = '\0';
39 }
40
41 return bits;
42 }
271330b @ossguy switch from manual version tracking to git
authored
43
740ae24 @ossguy expose the encoding type through the driver
authored
44 char* encoding_to_str(int track)
45 {
46 switch (track) {
47 case BC_ENCODING_NONE: return "none";
48 case BC_ENCODING_BINARY: return "binary";
49 case BC_ENCODING_BCD: return "BCD";
50 case BC_ENCODING_ALPHA: return "ALPHA";
51 case BC_ENCODING_ASCII: return "ASCII";
52 default: return "unknown";
53 }
54 }
55
7b788a5 @ossguy add error callback function
authored
56 void print_error(const char* error)
57 {
58 printf("%s\n", error);
59 }
60
271330b @ossguy switch from manual version tracking to git
authored
61 int main(void)
62 {
63 FILE* input;
095061e @ossguy memory for struct bc_input now managed by user
authored
64 char t1[TRACK_INPUT_SIZE];
65 char t2[TRACK_INPUT_SIZE];
66 char t3[TRACK_INPUT_SIZE];
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
67 struct bc_input in;
68 struct bc_decoded result;
271330b @ossguy switch from manual version tracking to git
authored
69 int rv;
70 int i;
71
72 input = stdin;
095061e @ossguy memory for struct bc_input now managed by user
authored
73 bc_init(print_error);
271330b @ossguy switch from manual version tracking to git
authored
74
82fbd62 @ossguy update code to use consistent bracing conventions
authored
75 while (1) {
76 if (NULL == get_track(input, t1, sizeof(t1))) {
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
77 break;
82fbd62 @ossguy update code to use consistent bracing conventions
authored
78 }
79 if (NULL == get_track(input, t2, sizeof(t2))) {
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
80 break;
82fbd62 @ossguy update code to use consistent bracing conventions
authored
81 }
82 if (NULL == get_track(input, t3, sizeof(t3))) {
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
83 break;
82fbd62 @ossguy update code to use consistent bracing conventions
authored
84 }
271330b @ossguy switch from manual version tracking to git
authored
85
095061e @ossguy memory for struct bc_input now managed by user
authored
86 in.t1 = t1;
87 in.t2 = t2;
88 in.t3 = t3;
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
89 rv = bc_decode(&in, &result);
271330b @ossguy switch from manual version tracking to git
authored
90
301c2ed @ossguy add bc_strerror for printing nice error messages
authored
91 printf("Result: %d (%s)\n", rv, bc_strerror(rv));
06c3c5e @ossguy decoded track data is now dynamically allocated
authored
92 if (NULL == result.t1) {
93 printf("Track 1 - no data\n");
94 } else {
95 printf("Track 1 - len: %lu, encode: %s, data:\n`%s`\n",
96 (unsigned long)strlen(result.t1),
97 encoding_to_str(result.t1_encoding), result.t1);
98 }
99 if (NULL == result.t2) {
100 printf("Track 2 - no data\n");
101 } else {
102 printf("Track 2 - len: %lu, encode: %s, data:\n`%s`\n",
103 (unsigned long)strlen(result.t2),
104 encoding_to_str(result.t2_encoding), result.t2);
105 }
106 if (NULL == result.t3) {
107 printf("Track 3 - no data\n");
108 } else {
109 printf("Track 3 - len: %lu, encode: %s, data:\n`%s`\n",
110 (unsigned long)strlen(result.t3),
111 encoding_to_str(result.t3_encoding), result.t3);
112 }
271330b @ossguy switch from manual version tracking to git
authored
113
82fbd62 @ossguy update code to use consistent bracing conventions
authored
114 if (0 != rv) {
4f2fa88 @ossguy move field decoding into bc_find_fields
authored
115 /* if there was an error, bc_find_fields isn't useful */
116 continue;
82fbd62 @ossguy update code to use consistent bracing conventions
authored
117 }
4f2fa88 @ossguy move field decoding into bc_find_fields
authored
118
119 rv = bc_find_fields(&result);
82fbd62 @ossguy update code to use consistent bracing conventions
authored
120 if (0 != rv) {
301c2ed @ossguy add bc_strerror for printing nice error messages
authored
121 printf("Error %d (%s); no fields found for this card\n",
122 rv, bc_strerror(rv));
4f2fa88 @ossguy move field decoding into bc_find_fields
authored
123 continue;
124 }
125
7d77d2d @ossguy multi-track awareness, no malloc, fields change
authored
126 printf("\n=== Fields ===\n");
29f02fe @ossguy change name from special field to struct element
authored
127 printf("Card name: %s\n", result.name);
82fbd62 @ossguy update code to use consistent bracing conventions
authored
128 for (i = 0; result.field_names[i] != NULL; i++) {
2203266 @ossguy add warning about potential BC_TRACK_* changes
authored
129 /* NOTE: you should verify that the BC_TRACK_* constants
130 * in the version of libbitconvert that you are using
131 * map cleanly onto integers if you wish to print the
132 * tracks using the method below; the library may change
133 * to allow tracks such as BC_TRACK_JIS_II, which would
134 * not print correctly using this method
135 */
1ff61e9 @ossguy add track number to each field
authored
136 printf("Track %d - %s: %s\n", result.field_tracks[i],
137 result.field_names[i], result.field_values[i]);
271330b @ossguy switch from manual version tracking to git
authored
138 }
06c3c5e @ossguy decoded track data is now dynamically allocated
authored
139
140 bc_decoded_free(&result);
271330b @ossguy switch from manual version tracking to git
authored
141 }
142
143 fclose(input);
144
145 return 0;
146 }
Something went wrong with that request. Please try again.