//reads in 3-4 length string int fromStringConstructorTest(void) { TestUtil testFramework("ObsID", "Constructor from String", __FILE__, __LINE__); std::string failMesg; //set with invalid length failMesg = "[testing] ObsID constructor from invalid string, [expected] exception gpstk::Exception, [actual] threw no exception"; try{gpstk::ObsID invalidID("G 10 "); testFramework.assert(false, failMesg, __LINE__);} catch (gpstk::Exception e) {testFramework.assert(true, failMesg, __LINE__);} try{gpstk::ObsID invalidID("G1"); testFramework.assert(false, failMesg, __LINE__);} catch (gpstk::Exception e) {testFramework.assert(true, failMesg, __LINE__);} //testing base assign w/out using any of the reused codes gpstk::ObsID obs1("GC1C"); // GPS L1 C/A PseudoRange failMesg = "Was the type value stored correctly?"; testFramework.assert(obs1.type == gpstk::ObsID::otRange, failMesg, __LINE__); failMesg = "Was the band value stored correctly?"; testFramework.assert(obs1.band == gpstk::ObsID::cbL1, failMesg, __LINE__); failMesg = "Was the code value stored correctly?"; testFramework.assert(obs1.code == gpstk::ObsID::tcCA, failMesg, __LINE__); //testing only case of reassinged codes for GPS gpstk::ObsID obs2("GD5X"); // GPS L5 IQ Doppler failMesg = "Was the type value stored correctly?"; testFramework.assert(obs2.type == gpstk::ObsID::otDoppler, failMesg, __LINE__); failMesg = "Was the band value stored correctly?"; testFramework.assert(obs2.band == gpstk::ObsID::cbL5, failMesg, __LINE__); failMesg = "Was the code value stored correctly?"; testFramework.assert(obs2.code == gpstk::ObsID::tcIQ5, failMesg, __LINE__); //testing completely random case gpstk::ObsID obs3("JL6L"); // QZSS E6 L Carrier Phase failMesg = "Was the type value stored correctly?"; testFramework.assert(obs3.type == gpstk::ObsID::otPhase, failMesg, __LINE__); failMesg = "Was the band value stored correctly?"; testFramework.assert(obs3.band == gpstk::ObsID::cbE6, failMesg, __LINE__); failMesg = "Was the code value stored correctly?"; testFramework.assert(obs3.code == gpstk::ObsID::tcJQ6, failMesg, __LINE__); return testFramework.countFails(); }
bool FractalConfiguration::save(string filename) { if(invalidID()) return true; // Check filename and append .xml if(!endsWith(filename, ".xml")) filename += ".xml"; // Create document XMLDocument doc; // Root element XMLElement *root = doc.NewElement("fractal"); root->SetAttribute("id", m_id.c_str()); doc.InsertEndChild(root); // Add properties iterate(); while(true) { if(!next()) break; XMLElement *prop = doc.NewElement("property"); prop->SetAttribute("name", getName().c_str()); XMLText *text = doc.NewText(""); if(isString()) { prop->SetAttribute("type", "string"); text->SetValue(getString().c_str()); } else if(isInt()) { prop->SetAttribute("type", "int"); stringstream ss; ss << getInt(); text->SetValue(ss.str().c_str()); } else if(isDouble()) { prop->SetAttribute("type", "double"); stringstream ss; ss << getDouble(); text->SetValue(ss.str().c_str()); } else if(isBool()) { prop->SetAttribute("type", "bool"); if(getBool()) text->SetValue("1"); else text->SetValue("0"); } prop->InsertEndChild(text); root->InsertEndChild(prop); } // Save to file if(doc.SaveFile(filename.c_str()) != XML_NO_ERROR) { m_last_error = doc.GetErrorStr1(); return true; } resetDirty(); return false; }
bool FractalConfiguration::open(string filename) { if(invalidID()) return true; m_dirty = false; XMLDocument doc; // Parse file doc.LoadFile(filename.c_str()); if(doc.Error()) { m_last_error = doc.GetErrorStr1(); return true; } // Get data XMLElement *root = doc.RootElement(); string name = string(root->Name()); if(name != "fractal") { m_last_error = "Configuration file is invalid!"; return true; } if(root->Attribute("id", m_id.c_str())) { XMLNode *node = root->FirstChild(); // Loop over all properties while(node != NULL) { XMLElement *element = node->ToElement(); if(element == NULL) { m_last_error = "Configuration file is invalid!"; return true; } // Get name and type (attributes) const char* tmp_name = element->Attribute("name"); const char* tmp_type = element->Attribute("type"); if(tmp_name == NULL || tmp_type == NULL) { m_last_error = "Configuration file is invalid!"; return true; } string name(tmp_name); string type(tmp_type); // Get text const char* tmp_text = element->GetText(); string text = ""; if(tmp_text != NULL) text = tmp_text; // Set property if(type == "string") { if(setStringProperty(name, text)) return true; } else if(type == "int") { int value; if(stringToInt(text, &value)) { m_last_error = "Error in configuration: " + text + " should be an integer"; return true; } if(setIntProperty(name, value)) return true; } else if(type == "double") { double value; if(stringToDouble(text, &value)) { m_last_error = "Error in configuration: " + text + " should be a double"; return true; } if(setDoubleProperty(name, value)) return true; } else if(type == "bool") { bool value; if(text == "1") value = true; else if(text == "0") value = false; else { m_last_error = "Error in configuration: " + text + " should be boolean (0 or 1)"; return true; } if(setBoolProperty(name, value)) return true; } else { m_last_error = "Error in configuration: " + type + " is not a valid type"; return true; } // Next node node = node->NextSibling(); } } else { m_last_error = "File contains not the configuration of a fractal with ID " + m_id; return true; } resetDirty(); return false; }