Skip to content

Commit

Permalink
pass struct decoded instead of some of its members
Browse files Browse the repository at this point in the history
bc_decode_*fields now accept a struct decoded* instead of the majority
of its fields.  Originally, only a couple of its fields were passed,
making the parameter list fairly short, but now the parameter list is
getting quite unwieldy.  The next commit would have pushed it too far so
we may as well make this change first.
  • Loading branch information
ossguy committed Dec 1, 2008
1 parent 2203266 commit 9dfe947
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions bitconvert.c
Expand Up @@ -129,9 +129,7 @@ int bc_decode_format(char* bits, char* result, size_t result_len, unsigned char
}

int bc_decode_track_fields(char* input, FILE* formats, int track,
char field_names[BC_NUM_FIELDS][BC_FIELD_SIZE],
char field_values[BC_NUM_FIELDS][BC_FIELD_SIZE],
int field_tracks[BC_NUM_FIELDS])
struct bc_decoded* d)
{
pcre* re;
const char* error;
Expand Down Expand Up @@ -187,7 +185,7 @@ int bc_decode_track_fields(char* input, FILE* formats, int track,
/* TODO: this could be eliminated by making j a parameter to
* bc_decode_track_fields
*/
for (j = 0; field_names[j][0] != '\0'; j++);
for (j = 0; d->field_names[j][0] != '\0'; j++);

/* read until we have read all the fields or we encounter end of file
* or an empty line
Expand Down Expand Up @@ -225,13 +223,13 @@ int bc_decode_track_fields(char* input, FILE* formats, int track,
field = &(buf[i + 2]);
field[strlen(field) - 1] = '\0'; /* remove '\n' */

strcpy(field_names[j], field);
strcpy(field_values[j], result);
field_tracks[j] = track;
strcpy(d->field_names[j], field);
strcpy(d->field_values[j], result);
d->field_tracks[j] = track;
j++;
}

field_names[j][0] = '\0';
d->field_names[j][0] = '\0';

return 0;
}
Expand All @@ -244,10 +242,7 @@ int bc_decode_track_fields(char* input, FILE* formats, int track,
* fixing the constants in the ovector and fields initialization
*/

int bc_decode_fields(char* t1_input, char* t2_input, char* t3_input,
char field_names[BC_NUM_FIELDS][BC_FIELD_SIZE],
char field_values[BC_NUM_FIELDS][BC_FIELD_SIZE],
int field_tracks[BC_NUM_FIELDS])
int bc_decode_fields(struct bc_decoded* d)
{
int err;
int rc;
Expand All @@ -266,31 +261,28 @@ int bc_decode_fields(char* t1_input, char* t2_input, char* t3_input,
while (fgets(name, FORMAT_LEN, formats)) {
/* TODO: check lengths before strcpy (in general, but here esp.)
*/
strcpy(field_names[0], "Type of card");
strcpy(d->field_names[0], "Type of card");

name[strlen(name) - 1] = '\0'; /* remove '\n' */
strcpy(field_values[0], name);
field_tracks[0] = 0;
strcpy(d->field_values[0], name);
d->field_tracks[0] = 0;

/* empty the rest of the fields list */
field_names[1][0] = '\0';
d->field_names[1][0] = '\0';


err = bc_decode_track_fields(t1_input, formats, BC_TRACK_1,
field_names, field_values, field_tracks);
err = bc_decode_track_fields(d->t1, formats, BC_TRACK_1, d);
rc = err;

err = bc_decode_track_fields(t2_input, formats, BC_TRACK_2,
field_names, field_values, field_tracks);
err = bc_decode_track_fields(d->t2, formats, BC_TRACK_2, d);
/* if previous tracks were ok but this one returned an error,
* update the overall return code accordingly
*/
if (0 == rc) {
rc = err;
}

rc = bc_decode_track_fields(t3_input, formats, BC_TRACK_3,
field_names, field_values, field_tracks);
err = bc_decode_track_fields(d->t3, formats, BC_TRACK_3, d);
/* if previous tracks were ok but this one returned an error,
* update the overall return code accordingly
*/
Expand All @@ -310,7 +302,7 @@ int bc_decode_fields(char* t1_input, char* t2_input, char* t3_input,
while (fgets(name, FORMAT_LEN, formats) && name[0] != '\n');

/* empty the fields list */
field_names[0][0] = '\0';
d->field_names[0][0] = '\0';
}

fclose(formats);
Expand Down Expand Up @@ -370,9 +362,7 @@ int bc_decode(struct bc_input* in, struct bc_decoded* result)

/* only do field lookup if there were no errors during parsing */
if (0 == rc) {
rc = bc_decode_fields(result->t1, result->t2, result->t3,
result->field_names, result->field_values,
result->field_tracks);
rc = bc_decode_fields(result);
}

return rc;
Expand Down

0 comments on commit 9dfe947

Please sign in to comment.