| 1 |
#define _FILE_OFFSET_BITS 64 |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "atom_parser.h" |
| 8 |
|
| 9 |
#define MK_STR(s) # s |
| 10 |
#define STR_DEFINE(t, s) t = MK_STR(s) |
| 11 |
|
| 12 |
|
| 13 |
struct linked_atom *head_main; |
| 14 |
struct linked_atom *head_inUse; |
| 15 |
int n_inUse; |
| 16 |
|
| 17 |
int findAtomType_internal(char *, struct atom *); |
| 18 |
|
| 19 |
void initializeParser(){ |
| 20 |
char *token; |
| 21 |
char *delim = "\t ,;"; |
| 22 |
struct linked_atom *temp_linked; |
| 23 |
const int buffer_size = 300; |
| 24 |
char lineBuffer[buffer_size]; |
| 25 |
|
| 26 |
char in_filenameStd[500]; |
| 27 |
char tempString[500]; |
| 28 |
char *atPath; |
| 29 |
char *in_filenameSet; |
| 30 |
char *in_file_env = "_xyz2pov_AtomTypes_"; |
| 31 |
FILE *in_file; |
| 32 |
|
| 33 |
head_main = (struct linked_atom *)malloc(sizeof(struct linked_atom)); |
| 34 |
head_main->next = NULL; |
| 35 |
|
| 36 |
head_inUse = (struct linked_atom *)malloc(sizeof(struct linked_atom)); |
| 37 |
head_inUse->next = NULL; |
| 38 |
|
| 39 |
n_inUse = 0; |
| 40 |
|
| 41 |
// generate the AtomTypes Name |
| 42 |
|
| 43 |
strcpy( in_filenameStd, "AtomTypes" ); |
| 44 |
|
| 45 |
// attempt to open the file in the current directory first. |
| 46 |
|
| 47 |
in_file = fopen( in_filenameStd, "r" ); |
| 48 |
|
| 49 |
if( in_file == NULL ){ |
| 50 |
|
| 51 |
// next see if the force path enviorment variable is set |
| 52 |
|
| 53 |
in_filenameSet = getenv( in_file_env ); |
| 54 |
if( in_filenameSet != NULL ) { |
| 55 |
|
| 56 |
in_file = fopen(in_filenameSet, "r"); |
| 57 |
|
| 58 |
if(in_file == NULL){ |
| 59 |
|
| 60 |
fprintf(stderr, |
| 61 |
"Error reading AtomTypes file. The _xyz2pov_AtomTypes_\n" |
| 62 |
"enviorment variable was set incorrectly.\n"); |
| 63 |
exit(8); |
| 64 |
} |
| 65 |
} |
| 66 |
else{ |
| 67 |
STR_DEFINE(atPath, TYPES_PATH ); |
| 68 |
|
| 69 |
strcpy( tempString, atPath ); |
| 70 |
strcat( tempString, "/" ); |
| 71 |
strcat( tempString, in_filenameStd ); |
| 72 |
strcpy( in_filenameStd, tempString ); |
| 73 |
|
| 74 |
in_file = fopen( in_filenameStd, "r" ); |
| 75 |
|
| 76 |
if( in_file == NULL ){ |
| 77 |
|
| 78 |
fprintf(stderr, |
| 79 |
"Error opening the AtomTypes definition file: %s\n" |
| 80 |
"Have you tried setting the _xyz2pov_AtomTypes_ environment " |
| 81 |
"vairable?\n", |
| 82 |
in_filenameStd ); |
| 83 |
exit(8); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
while(fgets(lineBuffer, sizeof(lineBuffer), in_file) != NULL){ |
| 90 |
|
| 91 |
if(lineBuffer[0] == '#' || lineBuffer[0] == '\n'){ |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
token = strtok(lineBuffer, delim); |
| 96 |
sscanf(token, "%s", head_main->myAtom.name); |
| 97 |
strtok(NULL, delim); |
| 98 |
strtok(NULL, delim); |
| 99 |
strtok(NULL, delim); |
| 100 |
token = strtok(NULL, delim); |
| 101 |
sscanf(token, "%lf", &head_main->myAtom.vanDerWallRadii); |
| 102 |
token = strtok(NULL, delim); |
| 103 |
sscanf(token, "%lf", &head_main->myAtom.covalentRadii); |
| 104 |
token = strtok(NULL, delim); |
| 105 |
sscanf(token, "%d", &head_main->myAtom.red); |
| 106 |
token = strtok(NULL, delim); |
| 107 |
sscanf(token, "%d", &head_main->myAtom.green); |
| 108 |
token = strtok(NULL, delim); |
| 109 |
sscanf(token, "%d", &head_main->myAtom.blue); |
| 110 |
|
| 111 |
temp_linked = (struct linked_atom *)malloc(sizeof(struct linked_atom)); |
| 112 |
temp_linked->next = head_main; |
| 113 |
head_main = temp_linked; |
| 114 |
} |
| 115 |
|
| 116 |
fclose(in_file); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
int findAtomType_internal(char *typeKey, struct atom *dummy_plug){ |
| 122 |
|
| 123 |
struct linked_atom *link; |
| 124 |
|
| 125 |
link = head_main->next; |
| 126 |
|
| 127 |
while(link != NULL){ |
| 128 |
|
| 129 |
if(!strcmp(link->myAtom.name, typeKey)){ |
| 130 |
strcpy(dummy_plug->name, link->myAtom.name); |
| 131 |
dummy_plug->vanDerWallRadii = link->myAtom.vanDerWallRadii; |
| 132 |
dummy_plug->covalentRadii = link->myAtom.covalentRadii; |
| 133 |
dummy_plug->red = link->myAtom.red; |
| 134 |
dummy_plug->green = link->myAtom.green; |
| 135 |
dummy_plug->blue = link->myAtom.blue; |
| 136 |
|
| 137 |
return 1; |
| 138 |
} |
| 139 |
link = link->next; |
| 140 |
} |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
void update_types(char *new_key){ |
| 146 |
|
| 147 |
int found = 0; |
| 148 |
|
| 149 |
struct linked_atom *link; |
| 150 |
|
| 151 |
link = head_inUse->next; |
| 152 |
|
| 153 |
while(link != NULL){ |
| 154 |
|
| 155 |
if(!strcmp(link->myAtom.name, new_key)){ |
| 156 |
found = 1; |
| 157 |
} |
| 158 |
|
| 159 |
link = link->next; |
| 160 |
} |
| 161 |
|
| 162 |
if(!found){ |
| 163 |
found = findAtomType_internal(new_key, &head_inUse->myAtom); |
| 164 |
if(!found){ |
| 165 |
fprintf(stderr, "Atom Type %s, not found!\n", |
| 166 |
new_key); |
| 167 |
exit(8); |
| 168 |
} |
| 169 |
|
| 170 |
link = (struct linked_atom *)malloc(sizeof(struct linked_atom)); |
| 171 |
|
| 172 |
link->next = head_inUse; |
| 173 |
head_inUse = link; |
| 174 |
|
| 175 |
n_inUse++; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
int get_n_inUse(void){ |
| 182 |
return n_inUse; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
int findAtomType(char *typeKey, struct atom *dummy_plug){ |
| 187 |
|
| 188 |
struct linked_atom *link; |
| 189 |
|
| 190 |
link = head_inUse->next; |
| 191 |
|
| 192 |
while(link != NULL){ |
| 193 |
|
| 194 |
if(!strcmp(link->myAtom.name, typeKey)){ |
| 195 |
strcpy(dummy_plug->name, link->myAtom.name); |
| 196 |
dummy_plug->vanDerWallRadii = link->myAtom.vanDerWallRadii; |
| 197 |
dummy_plug->covalentRadii = link->myAtom.covalentRadii; |
| 198 |
dummy_plug->red = link->myAtom.red; |
| 199 |
dummy_plug->green = link->myAtom.green; |
| 200 |
dummy_plug->blue = link->myAtom.blue; |
| 201 |
|
| 202 |
return 1; |
| 203 |
} |
| 204 |
link = link->next; |
| 205 |
} |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
struct linked_atom *get_type_list(){ |
| 211 |
|
| 212 |
return head_inUse; |
| 213 |
} |
| 214 |
|
| 215 |
void clean_type_list(){ |
| 216 |
|
| 217 |
struct linked_atom *current_atom; |
| 218 |
struct linked_atom *next_atom; /* place holders */ |
| 219 |
|
| 220 |
|
| 221 |
current_atom = head_inUse->next; |
| 222 |
|
| 223 |
while(current_atom != NULL){ |
| 224 |
|
| 225 |
next_atom = current_atom->next; |
| 226 |
free(current_atom); |
| 227 |
current_atom = next_atom; |
| 228 |
} |
| 229 |
|
| 230 |
head_inUse->next = NULL; |
| 231 |
} |