NAMD
MGridforceParams.C
Go to the documentation of this file.
1 /*
2  * MGridforceParams.C
3  *
4  *
5  * Created by Robert Brunner on 12/5/07.
6  * Copyright 2007 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 
11 #include "MGridforceParams.h"
12 
14 {
15  MGFElem* cur = head;
16  MGFElem* found = NULL;
17  MGridforceParams* result = NULL;
18 
19  while (found == NULL && cur != NULL) {
20  if (!strcasecmp((cur->elem).gridforceKey,key)) {
21  found = cur;
22  } else {
23  cur = cur->nxt;
24  }
25  }
26  if (found != NULL) {
27  result = &(found->elem);
28  }
29  return result;
30 }
31 
33 {
34  MGFElem* cur = head;
35  MGFElem* found = NULL;
36  int result = -1;
37 
38  int idx = 0;
39  while (found == NULL && cur != NULL) {
40  if (!strcasecmp((cur->elem).gridforceKey,key)) {
41  found = cur;
42  } else {
43  cur = cur->nxt;
44  idx++;
45  }
46  }
47  if (found != NULL) {
48  result = idx;
49  }
50  return result;
51 }
52 
54 {
55  MGFElem* cur = head;
56  MGFElem* found = NULL;
57  MGridforceParams* result = NULL;
58 
59  int counter = 0;
60  while (found == NULL && cur != NULL) {
61  if (counter == idx) {
62  found = cur;
63  } else {
64  cur = cur->nxt;
65  counter++;
66  }
67  }
68  if (found != NULL) {
69  result = &(found->elem);
70  }
71  return result;
72 }
73 
74 
76 {
77  // If the key is already in the list, we can't add it
78  if (find_key(key)!=NULL) {
79  return NULL;
80  }
81 
82  MGFElem* new_elem = new MGFElem();
83  int len = strlen(key);
84  MGridforceParams* elem = &(new_elem->elem);
85  elem->gridforceKey = new char[len+1];
86  strncpy(elem->gridforceKey,key,len+1);
87  elem->gridforceVfile = NULL;
88  elem->gridforceFile = NULL;
89  elem->gridforceCol = NULL;
90  elem->gridforceQcol = NULL;
91  elem->next = NULL;
92  new_elem->nxt = NULL;
93  if (head == NULL) {
94  head = new_elem;
95  }
96  if (tail != NULL) {
97  tail->nxt = new_elem;
98  tail->elem.next = elem;
99  }
100  tail = new_elem;
101  n_elements++;
102 
103  return elem;
104 }
105 
107 {
108  int i = n_elements;
109  msg->put(n_elements);
110  MGridforceParams *elem = get_first();
111  while (elem != NULL) {
112  int len;
113  len = strlen(elem->gridforceKey) + 1;
114  msg->put(len);
115  msg->put(len,elem->gridforceKey);
116 
117  len = strlen(elem->gridforceVfile) + 1;
118  msg->put(len);
119  msg->put(len,elem->gridforceVfile);
120 
121  Vector v = elem->gridforceScale;
122  msg->put(&v);
123 
124  len = strlen(elem->gridforceFile) + 1;
125  msg->put(len);
126  msg->put(len,elem->gridforceFile);
127 
128  len = strlen(elem->gridforceCol) + 1;
129  msg->put(len);
130  msg->put(len,elem->gridforceCol);
131 
132  if (elem->gridforceQcol == NULL)
133  msg->put(1); // Qcol_is_null = true
134  else {
135  msg->put(0); // Qcol_is_null = false
136  len = strlen(elem->gridforceQcol) + 1;
137  msg->put(len);
138  msg->put(len,elem->gridforceQcol);
139  }
140 
141  v = elem->gridforceVOffset;
142  msg->put(&v);
143 
144  short boolvals[6];
145  boolvals[0] = (elem->gridforceCont[0] ? 1 : 0);
146  boolvals[1] = (elem->gridforceCont[1] ? 1 : 0);
147  boolvals[2] = (elem->gridforceCont[2] ? 1 : 0);
148  boolvals[3] = (elem->gridforceVolts ? 1 : 0);
149  boolvals[4] = (elem->gridforceLite ? 1 : 0);
150  boolvals[5] = (elem->gridforceCheckSize ? 1 : 0);
151  msg->put(6,boolvals);
152 
153  i--;
154  elem = elem->next;
155  }
156  if (i != 0) {
157  NAMD_die("MGridforceParams message packing error\n");
158  }
159  return;
160 }
161 
163 {
164  int elements;
165  msg->get(elements);
166 
167  for(int i=0; i < elements; i++) {
168  // Get key
169  int len;
170  msg->get(len);
171  char *key = new char[len];
172  msg->get(len,key);
173  MGridforceParams *elem = add(key);
174 
175  msg->get(len);
176  char *str = new char[len];
177  msg->get(len,str);
178  elem->gridforceVfile = str;
179 
180  Vector v;
181  msg->get(&v);
182  elem->gridforceScale = v;
183 
184  msg->get(len);
185  str = new char[len];
186  msg->get(len,str);
187  elem->gridforceFile = str;
188 
189  msg->get(len);
190  str = new char[len];
191  msg->get(len,str);
192  elem->gridforceCol = str;
193 
194  int qcol_is_null;
195  msg->get(qcol_is_null);
196  if (qcol_is_null)
197  elem->gridforceQcol = NULL;
198  else {
199  msg->get(len);
200  str = new char[len];
201  msg->get(len,str);
202  elem->gridforceQcol = str;
203  }
204 
205  msg->get(&v);
206  elem->gridforceVOffset = v;
207 
208  short boolvals[6];
209  msg->get(6,boolvals);
210  elem->gridforceCont[0] = ( boolvals[0] != 0 );
211  elem->gridforceCont[1] = ( boolvals[1] != 0 );
212  elem->gridforceCont[2] = ( boolvals[2] != 0 );
213  elem->gridforceVolts = ( boolvals[3] != 0 );
214  elem->gridforceLite = ( boolvals[4] != 0 );
215  elem->gridforceCheckSize = ( boolvals[5] != 0 );
216 
217  delete [] key;
218  }
219 }
220 
Definition: Vector.h:64
MIStream * get(char &data)
Definition: MStream.h:29
MGridforceParams * get_first()
int index_for_key(const char *key)
MGridforceParams * find_key(const char *key)
void NAMD_die(const char *err_msg)
Definition: common.C:83
void unpack_data(MIStream *msg)
MGridforceParams * add(const char *key)
MGridforceParams * next
MOStream * put(char data)
Definition: MStream.h:112
void pack_data(MOStream *msg)
MGridforceParams * at_index(int idx)