Skip to content
Snippets Groups Projects
Commit 0aadc8a8 authored by James Graham's avatar James Graham
Browse files

Compile using ANSI standard

parent 3928e539
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ project(xtc-length) ...@@ -2,7 +2,7 @@ project(xtc-length)
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu90") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ansi -O2")
endif() endif()
add_executable(xtc-length xtc-length.c) add_executable(xtc-length xtc-length.c)
......
...@@ -11,4 +11,4 @@ If you with to install to a directory other than default use `cmake . -DCMAKE_IN ...@@ -11,4 +11,4 @@ If you with to install to a directory other than default use `cmake . -DCMAKE_IN
Since there is only one source file it is also possible to call the compiler directly if you prefer: Since there is only one source file it is also possible to call the compiler directly if you prefer:
`gcc -std=gnu90 -o xtc-length xtc-length.c` `gcc -o xtc-length xtc-length.c`
...@@ -30,34 +30,37 @@ void print_header(const uint8_t header[92]){ ...@@ -30,34 +30,37 @@ void print_header(const uint8_t header[92]){
} }
int get_xtc_num_frames(const char *filename, int *nframes, int *natoms, float *psec, bool quiet){ int get_xtc_num_frames(const char *filename, int *nframes, int *natoms, float *psec, bool quiet){
uint8_t header[92];
uint32_t frame_size;
uint32_t skip;
uint32_t ps_tmp;
long size = file_size(filename); long size = file_size(filename);
double avg_frame_size = 0.;
int est_nframes = 0;
FILE *xtc = fopen(filename, "rb"); FILE *xtc = fopen(filename, "rb");
if(!xtc) return -1; if(!xtc) return -1;
uint8_t header[92];
*nframes = 0; *nframes = 0;
double avg_frame_size = 0.; while(fread(header, 92, 1, xtc)){ /* Loop over frames */
int est_nframes = 0;
while(fread(header, 92, 1, xtc)){ // Loop over frames
(*nframes)++; (*nframes)++;
*natoms = u4_from_buffer(header+4); *natoms = u4_from_buffer(header+4);
uint32_t frame_size = u4_from_buffer(header+88); // Read frame size from header frame_size = u4_from_buffer(header+88); /* Read frame size from header */
uint32_t skip = (frame_size+3) & ~((uint32_t)3); // Round up to 4 bytes skip = (frame_size+3) & ~((uint32_t)3); /* Round up to 4 byte boundary */
avg_frame_size += (skip - avg_frame_size + 92) / *nframes; avg_frame_size += (skip - avg_frame_size + 92) / *nframes;
if(!quiet && *nframes % 10 == 0){ if(!quiet && *nframes % 10 == 0){
est_nframes = (int)(size / avg_frame_size); est_nframes = (int)(size / avg_frame_size);
uint32_t ps_tmp = u4_from_buffer(header+12); ps_tmp = u4_from_buffer(header+12);
memcpy(psec, &ps_tmp, 4); memcpy(psec, &ps_tmp, 4);
*psec *= (double)est_nframes / *nframes; *psec *= (double)est_nframes / *nframes;
printf("\rEstimated %'d frames (%'d ns) of %'d atoms. - %d%%", printf("\rEstimated %'d frames (%'d ns) of %'d atoms. - %d%%",
est_nframes, (int)(*psec/1000), *natoms, (int)(100. * *nframes) / est_nframes); est_nframes, (int)(*psec/1000), *natoms, (int)(100. * *nframes) / est_nframes);
fflush(stdout); fflush(stdout);
} }
fseeko(xtc, skip, SEEK_CUR); // Skip to next header fseeko(xtc, skip, SEEK_CUR); /* Skip to next header */
} }
uint32_t ps_tmp = u4_from_buffer(header+12); ps_tmp = u4_from_buffer(header+12);
memcpy(psec, &ps_tmp, 4); memcpy(psec, &ps_tmp, 4);
fclose(xtc); fclose(xtc);
...@@ -69,6 +72,10 @@ int main(const int argc, const char *argv[]){ ...@@ -69,6 +72,10 @@ int main(const int argc, const char *argv[]){
"Usage: xtc-length [-q] xtc [xtc]...\n\n" "Usage: xtc-length [-q] xtc [xtc]...\n\n"
"Default behaviour is to provide running estimate of file length\n" "Default behaviour is to provide running estimate of file length\n"
"This can be suppressed using the '-q' flag\n"; "This can be suppressed using the '-q' flag\n";
int i;
bool quiet = false;
int nframes, natoms;
float psec;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
...@@ -76,13 +83,9 @@ int main(const int argc, const char *argv[]){ ...@@ -76,13 +83,9 @@ int main(const int argc, const char *argv[]){
printf("%s", help_text); printf("%s", help_text);
return 0; return 0;
} }
bool quiet = false;
if(argc >= 2 && !strcmp(argv[1], "-q")) quiet = true; if(argc >= 2 && !strcmp(argv[1], "-q")) quiet = true;
int i;
for(i = quiet ? 2 : 1; i < argc; i++){ for(i = quiet ? 2 : 1; i < argc; i++){
int nframes, natoms;
float psec;
printf("%s\n", argv[i]); printf("%s\n", argv[i]);
if(get_xtc_num_frames(argv[i], &nframes, &natoms, &psec, quiet)){ if(get_xtc_num_frames(argv[i], &nframes, &natoms, &psec, quiet)){
printf("ERROR: Error reading XTC file\n"); printf("ERROR: Error reading XTC file\n");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment