diff options
-rw-r--r-- | include/tng_io.h | 7 | ||||
-rw-r--r-- | src/lib/tng_io.c | 94 |
2 files changed, 99 insertions, 2 deletions
diff --git a/include/tng_io.h b/include/tng_io.h index d7ebfb1..832379b 100644 --- a/include/tng_io.h +++ b/include/tng_io.h @@ -269,6 +269,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <assert.h> #ifdef USE_STD_INTTYPES_H @@ -342,6 +343,12 @@ typedef unsigned __int64 uint64_t; /** The maximum allowed length of a string */ #define TNG_MAX_STR_LEN 1024 +#ifndef NDEBUG +#define TNG_ASSERT(cnd, msg) if(!cnd) {printf("%s\n", msg); assert(cnd);} +#else +#define TNG_ASSERT(cnd, msg) ((void) 0) +#endif + /** Flag to specify the endianness of a TNG file */ typedef enum {TNG_BIG_ENDIAN, TNG_LITTLE_ENDIAN} tng_file_endianness; diff --git a/src/lib/tng_io.c b/src/lib/tng_io.c index 875e093..d4f8e03 100644 --- a/src/lib/tng_io.c +++ b/src/lib/tng_io.c @@ -485,6 +485,7 @@ static tng_function_status tng_swap_byte_order_big_endian_32 static tng_function_status tng_swap_byte_order_big_endian_64 (const tng_trajectory_t tng_data, int64_t *v) { + TNG_ASSERT(v != 0, "NULL pointer when swapping byte order."); switch(tng_data->endianness_64) { case TNG_LITTLE_ENDIAN_64: /* Byte order is reversed. */ @@ -537,6 +538,7 @@ static tng_function_status tng_swap_byte_order_big_endian_64 static tng_function_status tng_swap_byte_order_little_endian_32 (const tng_trajectory_t tng_data, int32_t *v) { + TNG_ASSERT(v != 0, "NULL pointer when swapping byte order."); switch(tng_data->endianness_32) { case TNG_LITTLE_ENDIAN_32: /* Already correct */ @@ -575,6 +577,7 @@ static tng_function_status tng_swap_byte_order_little_endian_32 static tng_function_status tng_swap_byte_order_little_endian_64 (const tng_trajectory_t tng_data, int64_t *v) { + TNG_ASSERT(v != 0, "NULL pointer when swapping byte order."); switch(tng_data->endianness_64) { case TNG_LITTLE_ENDIAN_64: /* Already correct */ @@ -652,6 +655,13 @@ static tng_function_status tng_md5_hash_match_verify(tng_gen_block_t block, md5_state_t md5_state; char hash[TNG_MD5_HASH_LEN]; + TNG_ASSERT(block != 0, "Block not set (NULL) when verifying MD5 hash."); + if(block->block_contents_size == 0) + { + return(TNG_FAILURE); + } + TNG_ASSERT(block->block_contents != 0, "Block contents not set (NULL) when generating MD5 hash."); + if(strncmp(block->md5_hash, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0) { *results = TNG_TRUE; @@ -1049,6 +1059,8 @@ static tng_function_status tng_block_header_write { int name_len, offset = 0; + TNG_ASSERT(block != 0, "Trying to write uninitialized block (NULL pointer)."); + if(tng_output_file_init(tng_data) != TNG_SUCCESS) { printf("TNG library: Cannot initialise destination file. %s: %d\n", @@ -1056,7 +1068,6 @@ static tng_function_status tng_block_header_write return(TNG_CRITICAL); } - if(!block->name) { block->name = malloc(1); @@ -1187,6 +1198,8 @@ static tng_function_status tng_general_info_block_read void *temp; + TNG_ASSERT(block != 0, "Trying to read data to an uninitialized block (NULL pointer)"); + if(tng_input_file_init(tng_data) != TNG_SUCCESS) { return(TNG_CRITICAL); @@ -1828,6 +1841,8 @@ static tng_function_status tng_chain_data_read(tng_trajectory_t tng_data, { int len; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + memcpy(&chain->id, block->block_contents+*offset, sizeof(chain->id)); if(tng_data->input_endianness_swap_func_64) @@ -1880,6 +1895,8 @@ static tng_function_status tng_chain_data_write(tng_trajectory_t tng_data, { int len; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + memcpy(block->block_contents+*offset, &chain->id, sizeof(chain->id)); if(tng_data->output_endianness_swap_func_64) { @@ -1928,6 +1945,8 @@ static tng_function_status tng_residue_data_read(tng_trajectory_t tng_data, { int len; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + memcpy(&residue->id, block->block_contents+*offset, sizeof(residue->id)); if(tng_data->input_endianness_swap_func_64) @@ -1980,6 +1999,8 @@ static tng_function_status tng_residue_data_write(tng_trajectory_t tng_data, { int len; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + memcpy(block->block_contents+*offset, &residue->id, sizeof(residue->id)); if(tng_data->output_endianness_swap_func_64) { @@ -2028,6 +2049,8 @@ static tng_function_status tng_atom_data_read(tng_trajectory_t tng_data, { int len; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + memcpy(&atom->id, block->block_contents+*offset, sizeof(atom->id)); if(tng_data->input_endianness_swap_func_64) @@ -2073,6 +2096,8 @@ static tng_function_status tng_atom_data_write(tng_trajectory_t tng_data, { int len; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + memcpy(block->block_contents+*offset, &atom->id, sizeof(atom->id)); if(tng_data->output_endianness_swap_func_64) @@ -4523,6 +4548,8 @@ static tng_function_status tng_particle_data_read &tng_data->current_trajectory_frame_set; char block_type_flag; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + switch(datatype) { case TNG_CHAR_DATA: @@ -5456,6 +5483,8 @@ static tng_function_status tng_data_read(tng_trajectory_t tng_data, &tng_data->current_trajectory_frame_set; char block_type_flag; + TNG_ASSERT(offset != 0, "Offset must not be a NULL pointer."); + /* printf("TNG library: %s\n", block->name);*/ switch(datatype) @@ -8333,6 +8362,8 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init(tng_trajectory_t *tng_ tng_trajectory_frame_set_t frame_set; tng_trajectory_t tng_data; + TNG_ASSERT(*tng_data_p == 0, "Pointer to tng_trajectory_t must be NULL to initialise the trajectory"); + *tng_data_p = malloc(sizeof(struct tng_trajectory)); if(!*tng_data_p) { @@ -8863,6 +8894,9 @@ tng_function_status DECLSPECDLLEXPORT tng_trajectory_init_from_src(tng_trajector tng_trajectory_frame_set_t frame_set; tng_trajectory_t dest; + TNG_ASSERT(src != 0, "Source trajectory must not be NULL."); + TNG_ASSERT(*dest_p == 0, "Pointer to tng_trajectory_t must be NULL to initialise the trajectory"); + *dest_p = malloc(sizeof(struct tng_trajectory)); if(!*dest_p) { @@ -8980,6 +9014,8 @@ tng_function_status DECLSPECDLLEXPORT tng_input_file_get(const tng_trajectory_t { tng_function_status stat; + TNG_ASSERT(file_name, "file_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9005,6 +9041,8 @@ tng_function_status DECLSPECDLLEXPORT tng_input_file_set(tng_trajectory_t tng_da char *temp; tng_function_status stat; + TNG_ASSERT(file_name, "file_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9046,6 +9084,8 @@ tng_function_status tng_output_file_get(const tng_trajectory_t tng_data, { tng_function_status stat; + TNG_ASSERT(file_name, "file_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9071,6 +9111,8 @@ tng_function_status DECLSPECDLLEXPORT tng_output_file_set(tng_trajectory_t tng_d char *temp; tng_function_status stat; + TNG_ASSERT(file_name, "file_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9114,6 +9156,8 @@ tng_function_status DECLSPECDLLEXPORT tng_output_file_endianness_get tng_endianness_64 end_64; tng_function_status stat; + TNG_ASSERT(endianness, "endianness must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9267,6 +9311,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_program_name_get { tng_function_status stat; + TNG_ASSERT(name, "name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9291,6 +9337,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_program_name_set(tng_trajectory_ unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9328,6 +9376,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_program_name_get { tng_function_status stat; + TNG_ASSERT(name, "name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9353,6 +9403,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_program_name_set unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9390,6 +9442,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_user_name_get { tng_function_status stat; + TNG_ASSERT(name, "name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9415,6 +9469,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_user_name_set unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9454,6 +9510,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_user_name_get { tng_function_status stat; + TNG_ASSERT(name, "name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9479,6 +9537,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_user_name_set unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9518,6 +9578,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_get { tng_function_status stat; + TNG_ASSERT(name, "name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9543,6 +9605,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_computer_name_set unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9580,6 +9644,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_get (const tng_trajectory_t tng_data, char *name, const int max_len) { + TNG_ASSERT(name, "name must not be a NULL pointer"); + tng_function_status stat; stat = tng_check_trajectory_container(tng_data); @@ -9607,6 +9673,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_computer_name_set unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9647,6 +9715,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_signature_get { tng_function_status stat; + TNG_ASSERT(signature, "signature must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9672,6 +9742,8 @@ tng_function_status DECLSPECDLLEXPORT tng_first_signature_set unsigned int len; tng_function_status stat; + TNG_ASSERT(signature, "signature must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9712,6 +9784,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_signature_get { tng_function_status stat; + TNG_ASSERT(signature, "signature must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9737,6 +9811,8 @@ tng_function_status DECLSPECDLLEXPORT tng_last_signature_set unsigned int len; tng_function_status stat; + TNG_ASSERT(signature, "signature must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9777,6 +9853,8 @@ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_get { tng_function_status stat; + TNG_ASSERT(name, "name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9802,6 +9880,8 @@ tng_function_status DECLSPECDLLEXPORT tng_forcefield_name_set unsigned int len; tng_function_status stat; + TNG_ASSERT(new_name, "new_name must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9841,6 +9921,8 @@ tng_function_status DECLSPECDLLEXPORT tng_medium_stride_length_get { tng_function_status stat; + TNG_ASSERT(len, "len must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9883,6 +9965,8 @@ tng_function_status DECLSPECDLLEXPORT tng_long_stride_length_get { tng_function_status stat; + TNG_ASSERT(len, "len must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9925,6 +10009,8 @@ tng_function_status DECLSPECDLLEXPORT tng_time_per_frame_get { tng_function_status stat; + TNG_ASSERT(time, "time must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9974,6 +10060,8 @@ tng_function_status DECLSPECDLLEXPORT tng_input_file_len_get { tng_function_status stat; + TNG_ASSERT(len, "len must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -9995,6 +10083,8 @@ tng_function_status DECLSPECDLLEXPORT tng_num_frames_get tng_function_status stat; int64_t file_pos; + TNG_ASSERT(n, "n must not be a NULL pointer"); + stat = tng_check_trajectory_container(tng_data); if(stat != TNG_SUCCESS) { @@ -10044,7 +10134,7 @@ tng_function_status DECLSPECDLLEXPORT tng_implicit_num_particles_set (tng_trajectory_t tng_data, const int64_t n) { - tng_data->n_molecules = n; + tng_data->n_particles = n; return(TNG_SUCCESS); } |