ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/xyz2pov/src/atom_parser.c
Revision: 638
Committed: Fri Jul 18 15:17:15 2003 UTC (21 years ago) by mmeineke
Content type: text/plain
File size: 5029 byte(s)
Log Message:
fixed the AtomType file not found bug.

File Contents

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