int main() { int i; int d; int *p; int (*fp)(); chk_p = &d; chk_fp = &f; for( i = 0; i < 16; ++i ) { p = ( i & 0x01 ) ? &d : 0; check_p( p, __LINE__ ); p = ( i & 0x01 ) ? 0 : &d; check_p( p, __LINE__ ); fp = ( i & 0x01 ) ? &f : 0; check_fp( fp, __LINE__ ); fp = ( i & 0x01 ) ? 0 : &f; check_fp( fp, __LINE__ ); } _PASS; }
static void read_using_writer_schema(const char *filename) { avro_file_reader_t file; avro_schema_t writer_schema; avro_value_iface_t *writer_iface; avro_value_t writer_value; // Open an Avro file and grab the writer schema that was used to create the // file. check_i(avro_file_reader(filename, &file)); writer_schema = avro_file_reader_get_writer_schema(file); // Then create a value that is an instance of the writer schema. As above, // we use the built-in "generic" value implementation for the value instance // that will actually store the data. check_p(writer_iface = avro_generic_class_from_schema(writer_schema)); check_i(avro_generic_value_new(writer_iface, &writer_value)); // Read values from the file until we run out, printing the contents of each // one. Here, we can read directly into `writer_value` since we know that // it's an instance of the schema that was used to create the file. while (avro_file_reader_read_value(file, &writer_value) == 0) { avro_value_t field; int32_t a; int32_t b; check_i(avro_value_get_by_name(&writer_value, "a", &field, NULL)); check_i(avro_value_get_int(&field, &a)); check_i(avro_value_get_by_name(&writer_value, "b", &field, NULL)); check_i(avro_value_get_int(&field, &b)); printf(" a: %" PRId32 ", b: %" PRId32 "\n", a, b); } // Close the file and clean up after ourselves. avro_file_reader_close(file); avro_value_decref(&writer_value); avro_value_iface_decref(writer_iface); avro_schema_decref(writer_schema); }
static void write_data(const char *filename) { avro_file_writer_t file; avro_schema_t writer_schema; avro_schema_error_t error; avro_value_iface_t *writer_iface; avro_value_t writer_value; avro_value_t field; // First parse the JSON schema into the C API's internal schema // representation. check_i(avro_schema_from_json(WRITER_SCHEMA, 0, &writer_schema, &error)); // Then create a value that is an instance of that schema. We use the // built-in "generic" value implementation, which is what you'll usually use // to create value instances that can actually store data. We only need to // create one instance, since we can re-use it for all of the values that // we're going to write into the file. check_p(writer_iface = avro_generic_class_from_schema(writer_schema)); check_i(avro_generic_value_new(writer_iface, &writer_value)); // Open a new data file for writing, and then write a slew of records into // it. check_i(avro_file_writer_create(filename, writer_schema, &file)); /* record 1 */ check_i(avro_value_get_by_name(&writer_value, "a", &field, NULL)); check_i(avro_value_set_int(&field, 10)); check_i(avro_value_get_by_name(&writer_value, "b", &field, NULL)); check_i(avro_value_set_int(&field, 11)); check_i(avro_file_writer_append_value(file, &writer_value)); /* record 2 */ check_i(avro_value_get_by_name(&writer_value, "a", &field, NULL)); check_i(avro_value_set_int(&field, 20)); check_i(avro_value_get_by_name(&writer_value, "b", &field, NULL)); check_i(avro_value_set_int(&field, 21)); check_i(avro_file_writer_append_value(file, &writer_value)); /* record 3 */ check_i(avro_value_get_by_name(&writer_value, "a", &field, NULL)); check_i(avro_value_set_int(&field, 30)); check_i(avro_value_get_by_name(&writer_value, "b", &field, NULL)); check_i(avro_value_set_int(&field, 31)); check_i(avro_file_writer_append_value(file, &writer_value)); /* record 4 */ check_i(avro_value_get_by_name(&writer_value, "a", &field, NULL)); check_i(avro_value_set_int(&field, 40)); check_i(avro_value_get_by_name(&writer_value, "b", &field, NULL)); check_i(avro_value_set_int(&field, 41)); check_i(avro_file_writer_append_value(file, &writer_value)); /* record 5 */ check_i(avro_value_get_by_name(&writer_value, "a", &field, NULL)); check_i(avro_value_set_int(&field, 50)); check_i(avro_value_get_by_name(&writer_value, "b", &field, NULL)); check_i(avro_value_set_int(&field, 51)); check_i(avro_file_writer_append_value(file, &writer_value)); // Close the file and clean up after ourselves. avro_file_writer_close(file); avro_value_decref(&writer_value); avro_value_iface_decref(writer_iface); avro_schema_decref(writer_schema); }
static void read_with_schema_resolution(const char *filename, const char *reader_schema_json, const char *field_name) { avro_file_reader_t file; avro_schema_error_t error; avro_schema_t reader_schema; avro_schema_t writer_schema; avro_value_iface_t *writer_iface; avro_value_iface_t *reader_iface; avro_value_t writer_value; avro_value_t reader_value; // Open an Avro file and grab the writer schema that was used to create the // file. check_i(avro_file_reader(filename, &file)); writer_schema = avro_file_reader_get_writer_schema(file); // Create a value instance that we want to read the data into. Note that // this is *not* the writer schema! check_i(avro_schema_from_json (reader_schema_json, 0, &reader_schema, &error)); check_p(reader_iface = avro_generic_class_from_schema(reader_schema)); check_i(avro_generic_value_new(reader_iface, &reader_value)); // Create a resolved writer that will perform the schema resolution for us. // If the two schemas aren't compatible, this function will return an error, // and the error text should describe which parts of the schemas are // incompatible. check_p(writer_iface = avro_resolved_writer_new(writer_schema, reader_schema)); // Create an instance of the resolved writer, and tell it to wrap our reader // value instance. check_i(avro_resolved_writer_new_value(writer_iface, &writer_value)); avro_resolved_writer_set_dest(&writer_value, &reader_value); // Now we've got the same basic loop as above. But we've got two value // instances floating around! Which do we use? We have the file reader // fill in `writer_value`, since that's the value that is an instance of the // file's writer schema. Since it's an instance of a resolved writer, // though, it doesn't actually store any data itself. Instead, it will // perform schema resolution on the data read from the file, and fill in its // wrapped value (which in our case is `reader_value`). That means that // once the data has been read, we can get its (schema-resolved) contents // via `reader_value`. while (avro_file_reader_read_value(file, &writer_value) == 0) { avro_value_t field; int32_t value; check_i(avro_value_get_by_name(&reader_value, field_name, &field, NULL)); check_i(avro_value_get_int(&field, &value)); printf(" %s: %" PRId32 "\n", field_name, value); } // Close the file and clean up after ourselves. avro_file_reader_close(file); avro_value_decref(&writer_value); avro_value_iface_decref(writer_iface); avro_schema_decref(writer_schema); avro_value_decref(&reader_value); avro_value_iface_decref(reader_iface); avro_schema_decref(reader_schema); }
// Check the integrity of tablefile bool tablecheck(FILE *table) { // Check IP vals if(!check_ip(table)) { // IP vals are invalid fprintf(stderr, "ERROR: tablecheck failed for IP (Initial Permutation) values.\n"); return false; } // Check E vals if(!check_e(table)) { // E vals are invalid fprintf(stderr, "ERROR: tablecheck failed for E (Expansion) values.\n"); return false; } // Check P vals if(!check_p(table)) { // P vals are invalid fprintf(stderr, "ERROR: tablecheck failed for P (Permutation) values.\n"); return false; } // Check S# vals char header[3]; for(int i=1; i<=8; i++) { sprintf(header, "S%d=", i); if(!check_s(table, header)) { // S# vals are invalid fprintf(stderr, "ERROR: tablecheck failed for S%d (Substitution) values.\n", i); return false; } } // Check V vals if(!check_v(table)) { // V vals are invalid fprintf(stderr, "ERROR: tablecheck failed for V (Circular Rotation) values.\n"); return false; } // Check PC1 vals if(!check_pc1(table)) { // PC1 vals are invalid fprintf(stderr, "ERROR: tablecheck failed for PC1 (Permutated Choice 1) values.\n"); return false; } // Check PC2 vals if(!check_pc2(table)) { // PC2 vals are invalid fprintf(stderr, "ERROR: tablecheck failed for PC2 (Permutated Choice 2) values.\n"); return false; } return true; // PSEUDO-CODE // - Use read_line_vals method to search through file until finds a line starting with header // - Read each item in the line into an array of ints, using comma delimiters to separate // - Run whatever test is needed on the array to ensure that all the values are valid // - Usually something like counting the instances of each number to ensure correct permutation // - Repeat for all headers }