MGridforceParams.C

Go to the documentation of this file.
00001 /*
00002  *  MGridforceParams.C
00003  *  
00004  *
00005  *  Created by Robert Brunner on 12/5/07.
00006  *  Copyright 2007 __MyCompanyName__. All rights reserved.
00007  *
00008  */
00009  
00010 
00011 #include "MGridforceParams.h"
00012 
00013 MGridforceParams* MGridforceParamsList::find_key(const char* key)
00014 {
00015     MGFElem* cur = head;
00016     MGFElem* found = NULL;
00017     MGridforceParams* result = NULL;
00018     
00019     while (found == NULL && cur != NULL) {
00020        if (!strcasecmp((cur->elem).gridforceKey,key)) {
00021         found = cur;
00022       } else {
00023         cur = cur->nxt;
00024       }
00025     }
00026     if (found != NULL) {
00027       result = &(found->elem);
00028     }
00029     return result;
00030 }
00031   
00032 int MGridforceParamsList::index_for_key(const char* key)
00033 {
00034     MGFElem* cur = head;
00035     MGFElem* found = NULL;
00036     int result = -1;
00037     
00038     int idx = 0;
00039     while (found == NULL && cur != NULL) {
00040        if (!strcasecmp((cur->elem).gridforceKey,key)) {
00041         found = cur;
00042       } else {
00043         cur = cur->nxt;
00044         idx++;
00045       }
00046     }
00047     if (found != NULL) {
00048         result = idx;
00049     }
00050     return result;
00051 }
00052 
00053 MGridforceParams* MGridforceParamsList::at_index(int idx)
00054 {
00055     MGFElem* cur = head;
00056     MGFElem* found = NULL;
00057     MGridforceParams* result = NULL;
00058     
00059     int counter = 0;
00060     while (found == NULL && cur != NULL) {
00061       if (counter == idx) {
00062         found = cur;
00063       } else {
00064         cur = cur->nxt;
00065         counter++;
00066       }
00067     }
00068     if (found != NULL) {
00069         result = &(found->elem);
00070     }
00071     return result;
00072 }
00073 
00074  
00075 MGridforceParams* MGridforceParamsList::add(const char* key) 
00076 {
00077     // If the key is already in the list, we can't add it
00078     if (find_key(key)!=NULL) {
00079       return NULL;
00080     }
00081     
00082     MGFElem* new_elem = new MGFElem();
00083     int len = strlen(key);
00084     MGridforceParams* elem = &(new_elem->elem);
00085     elem->gridforceKey = new char[len+1];
00086     strncpy(elem->gridforceKey,key,len+1);
00087     elem->gridforceVfile = NULL;
00088     elem->gridforceFile = NULL;
00089     elem->gridforceCol = NULL;
00090     elem->gridforceQcol = NULL;
00091     elem->next = NULL;
00092     new_elem->nxt = NULL;
00093     if (head == NULL) {
00094       head = new_elem;
00095     }
00096     if (tail != NULL) {
00097       tail->nxt = new_elem;
00098       tail->elem.next = elem;
00099     }
00100     tail = new_elem;
00101     n_elements++;
00102     
00103     return elem;
00104 }
00105   
00106 void MGridforceParamsList::pack_data(MOStream *msg) 
00107 {
00108     int i = n_elements;
00109     msg->put(n_elements);
00110     MGridforceParams *elem = get_first();
00111     while (elem != NULL) {
00112       int len;
00113       len = strlen(elem->gridforceKey) + 1;
00114       msg->put(len);
00115       msg->put(len,elem->gridforceKey);
00116 
00117       len = strlen(elem->gridforceVfile) + 1;
00118       msg->put(len);
00119       msg->put(len,elem->gridforceVfile);
00120 
00121       Vector v = elem->gridforceScale;
00122       msg->put(&v);
00123       
00124       len = strlen(elem->gridforceFile) + 1;
00125       msg->put(len);
00126       msg->put(len,elem->gridforceFile);
00127       
00128       len = strlen(elem->gridforceCol) + 1;
00129       msg->put(len);
00130       msg->put(len,elem->gridforceCol);
00131       
00132       if (elem->gridforceQcol == NULL) 
00133         msg->put(1); // Qcol_is_null = true
00134       else {
00135         msg->put(0); // Qcol_is_null = false
00136         len = strlen(elem->gridforceQcol) + 1;
00137         msg->put(len);
00138         msg->put(len,elem->gridforceQcol);
00139       }
00140       
00141       v = elem->gridforceVOffset;
00142       msg->put(&v);
00143       
00144       short boolvals[6];
00145       boolvals[0] = (elem->gridforceCont[0] ? 1 : 0);
00146       boolvals[1] = (elem->gridforceCont[1] ? 1 : 0);
00147       boolvals[2] = (elem->gridforceCont[2] ? 1 : 0);
00148       boolvals[3] = (elem->gridforceVolts ? 1 : 0);
00149       boolvals[4] = (elem->gridforceLite ? 1 : 0);
00150       boolvals[5] = (elem->gridforceCheckSize ? 1 : 0);
00151       msg->put(6,boolvals);
00152       
00153       i--;
00154       elem = elem->next;
00155     }
00156     if (i != 0) {
00157       NAMD_die("MGridforceParams message packing error\n");
00158     }
00159     return;
00160 }
00161   
00162 void MGridforceParamsList::unpack_data(MIStream *msg)
00163 {
00164     int elements;
00165     msg->get(elements);
00166     
00167     for(int i=0; i < elements; i++) {
00168       // Get key
00169       int len;
00170       msg->get(len);
00171       char *key = new char[len];
00172       msg->get(len,key);
00173       MGridforceParams *elem = add(key);
00174       
00175       msg->get(len);
00176       char *str = new char[len];
00177       msg->get(len,str);
00178       elem->gridforceVfile = str;
00179 
00180       Vector v;
00181       msg->get(&v);
00182       elem->gridforceScale = v;
00183       
00184       msg->get(len);
00185       str = new char[len];
00186       msg->get(len,str);
00187       elem->gridforceFile = str;
00188       
00189       msg->get(len);
00190       str = new char[len];
00191       msg->get(len,str);
00192       elem->gridforceCol = str;
00193       
00194       int qcol_is_null;
00195       msg->get(qcol_is_null);
00196       if (qcol_is_null)
00197         elem->gridforceQcol = NULL;
00198       else {
00199         msg->get(len);
00200         str = new char[len];
00201         msg->get(len,str);
00202         elem->gridforceQcol = str;
00203       }
00204       
00205       msg->get(&v);
00206       elem->gridforceVOffset = v;
00207       
00208       short boolvals[6];
00209       msg->get(6,boolvals);
00210       elem->gridforceCont[0] = ( boolvals[0] != 0 );
00211       elem->gridforceCont[1] = ( boolvals[1] != 0 );
00212       elem->gridforceCont[2] = ( boolvals[2] != 0 );
00213       elem->gridforceVolts = ( boolvals[3] != 0 );
00214       elem->gridforceLite = ( boolvals[4] != 0 );
00215       elem->gridforceCheckSize = ( boolvals[5] != 0 );
00216       
00217       delete [] key;
00218     }
00219 }
00220   

Generated on Thu Nov 23 01:17:12 2017 for NAMD by  doxygen 1.4.7