00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include "molfile_plugin.h"
00029
00030
00031 static molfile_plugin_t *splugin = 0;
00032 static molfile_plugin_t *cplugin = 0;
00033 static const char *sfiletype = NULL;
00034 static const char *cfiletype = NULL;
00035
00036 static int register_cb(void *v, vmdplugin_t *p) {
00037 if (!strcmp(p->type, MOLFILE_PLUGIN_TYPE)) {
00038 if (!strcmp(p->name, sfiletype))
00039 splugin = (molfile_plugin_t *)p;
00040 if (!strcmp(p->name, cfiletype))
00041 cplugin = (molfile_plugin_t *)p;
00042 }
00043 return VMDPLUGIN_SUCCESS;
00044 }
00045
00046 int main(int argc, char *argv[]) {
00047 const char *sfilename;
00048 const char *cfilename;
00049 int rc, natoms;
00050 void *handle;
00051 void *chandle;
00052 molfile_timestep_t timestep;
00053
00054 if (argc != 3 && argc != 5) {
00055 fprintf(stderr, "Usage: %s <filetype> <filename> [<filetype> <filename>]\n", argv[0]);
00056 return 1;
00057 }
00058
00059 if (argc == 3) {
00060 sfiletype = argv[1];
00061 sfilename = argv[2];
00062 cfiletype = sfiletype;
00063 cfilename = sfilename;
00064 } else {
00065 sfiletype = argv[1];
00066 sfilename = argv[2];
00067 cfiletype = argv[3];
00068 cfilename = argv[4];
00069 }
00070
00071 vmdplugin_init();
00072 vmdplugin_register(NULL, register_cb);
00073
00074 if (!splugin) {
00075 fprintf(stderr, "No plugin for filetype %s was linked in!\n", sfiletype);
00076 return 1;
00077 }
00078 if (!cplugin) {
00079 fprintf(stderr, "No plugin for filetype %s was linked in!\n", cfiletype);
00080 return 1;
00081 }
00082
00083
00084 if (!splugin->open_file_read) {
00085 fprintf(stdout, "FAILED: No open_file_read found in structure plugin.\n");
00086 return 1;
00087 }
00088 handle = splugin->open_file_read(sfilename, sfiletype, &natoms);
00089 if (!handle) {
00090 fprintf(stderr, "FAILED: open_file_read returned NULL in structure plugin.\n");
00091 return 1;
00092 }
00093 printf("Opened file %s; structure plugin found %d atoms\n", sfilename, natoms);
00094 if (splugin->read_structure) {
00095 int optflags;
00096 molfile_atom_t *atoms;
00097 atoms = (molfile_atom_t *)malloc(natoms * sizeof(molfile_atom_t));
00098 rc = splugin->read_structure(handle, &optflags, atoms);
00099 free(atoms);
00100 if (rc) {
00101 fprintf(stderr, "FAILED: read_structure returned %d\n", rc);
00102 splugin->close_file_read(handle);
00103 return 1;
00104 } else {
00105 printf("Successfully read atom structure information.\n");
00106 }
00107 if (splugin->read_bonds) {
00108 int nbonds, *from, *to, *bondtype, nbondtypes;
00109 float *bondorder;
00110 char **bondtypename;
00111 if ((rc = splugin->read_bonds(handle, &nbonds, &from, &to,
00112 &bondorder, &bondtype, &nbondtypes, &bondtypename))) {
00113 fprintf(stderr, "FAILED: read_bonds returned %d\n", rc);
00114 } else {
00115 printf("read_bonds read %d bonds\n", nbonds);
00116 }
00117 } else {
00118 printf("Structure file contains no bond information\n");
00119 }
00120 } else {
00121 fprintf(stderr, "FAILED: File contains no structure information!\n");
00122 return 1;
00123 }
00124
00125
00126 if (splugin != cplugin) {
00127 splugin->close_file_read(handle);
00128 int cnatoms;
00129 chandle = cplugin->open_file_read(cfilename, cfiletype, &cnatoms);
00130 printf("Opened coordinates file %s\n", cfilename);
00131 if (cnatoms != MOLFILE_NUMATOMS_UNKNOWN && cnatoms != natoms) {
00132 fprintf(stderr, "FAILED: Different number of atoms in structure file (%d) than in coordinates file (%d)!",
00133 natoms, cnatoms);
00134 cplugin->close_file_read(chandle);
00135 exit(1);
00136 }
00137 } else {
00138 chandle = handle;
00139 }
00140
00141
00142 if (cplugin->read_next_timestep) {
00143 timestep.velocities = NULL;
00144 int nsteps = 0;
00145 timestep.coords = (float *)malloc(3*natoms*sizeof(float));
00146 while (!(rc = cplugin->read_next_timestep(chandle, natoms, ×tep)))
00147 nsteps++;
00148 free(timestep.coords);
00149 if (rc != MOLFILE_SUCCESS) {
00150 fprintf(stderr, "FAILED: read_next_timestep returned %d\n", rc);
00151 } else {
00152 printf("Successfully read %d timesteps\n", nsteps);
00153 }
00154 }
00155
00156
00157 cplugin->close_file_read(chandle);
00158
00159 vmdplugin_fini();
00160 printf("Tests finished.\n");
00161 return 0;
00162 }
00163