// searches a new source for the atom node head. // If a new source is found the function returns true. // Otherwise the function returns false and unfounded_ contains head // as well as atoms with no source that circularly depend on head. bool DefaultUnfoundedCheck::findSource(NodeId head) { assert(unfounded_.empty()); enqueueUnfounded(head); // unfounded, unless we find a new source VarVec noSourceYet; bool changed = false; const NodeId* bodyIt, *bodyEnd; while (!unfounded_.empty()) { head = unfounded_.front(); if (!atoms_[head].hasSource()) { // no source unfounded_.pop_front(); // note: current atom is still marked AtomNodeP headNode(graph_->getAtom(head)); for (bodyIt = headNode.node->bodies(), bodyEnd = headNode.node->bodies_end(); bodyIt != bodyEnd; ++bodyIt) { BodyNodeP bodyNode(graph_->getBody(*bodyIt)); if (!solver_->isFalse(bodyNode.node->lit)) { if (bodyNode.node->scc != headNode.node->scc || isValidSource(bodyNode)) { atoms_[head].ufs = 0; // found a new source, setSource(headNode, bodyNode); // set the new source propagateSource(); // and propagate it forward changed = true; // may source atoms in noSourceYet! break; } else { addUnsourced(bodyNode); } } } if (!atoms_[head].hasSource()) { noSourceYet.push_back(head);// no source found } } else { // head has a source dequeueUnfounded(); } } // while unfounded_.emtpy() == false unfounded_.clear(); if (changed) { // remove all atoms that have a source as they are not unfounded VarVec::iterator it; for (it = noSourceYet.begin(); it != noSourceYet.end(); ++it) { if ( atoms_[*it].hasSource() ) { atoms_[*it].ufs = 0; } else { unfounded_.push_back(*it); } } } else { // all atoms in noSourceYet are unfounded! noSourceYet.swap(unfounded_.vec_); } return unfounded_.empty(); }
ListNode* partition(ListNode* head, int x) { ListNode headNode(0); headNode.next = head; head = &headNode; ListNode *cur = head, *pre = head; while (cur->next) { if (cur->next->val < x) { ListNode *movePtr = cur->next; cur->next = cur->next->next; ListNode *nonvisited = cur->next; movePtr->next = pre->next; pre->next = movePtr; pre = pre->next; if (nonvisited != cur->next) cur = cur->next; } else cur = cur->next; } return head->next; }
/** * The parser uses a code sandwich to wrap the parsing process. Before * the process begins, WillBuildModel() is called. Afterwards the parser * calls DidBuildModel(). * @update gess5/18/98 * @param aFilename is the name of the file being parsed. * @return error code (almost always 0) */ NS_IMETHODIMP CViewSourceHTML::BuildModel(nsITokenizer* aTokenizer, PRBool aCanInterrupt, PRBool aCountLines, const nsCString* aCharsetPtr) { nsresult result=NS_OK; if(aTokenizer) { nsITokenizer* oldTokenizer=mTokenizer; mTokenizer=aTokenizer; nsTokenAllocator* theAllocator=mTokenizer->GetTokenAllocator(); if(!mHasOpenRoot) { // For the stack-allocated tokens below, it's safe to pass a null // token allocator, because there are no attributes on the tokens. CStartToken htmlToken(NS_LITERAL_STRING("HTML"), eHTMLTag_html); nsCParserNode htmlNode(&htmlToken, 0/*stack token*/); mSink->OpenContainer(htmlNode); CStartToken headToken(NS_LITERAL_STRING("HEAD"), eHTMLTag_head); nsCParserNode headNode(&headToken, 0/*stack token*/); mSink->OpenContainer(headNode); CStartToken titleToken(NS_LITERAL_STRING("TITLE"), eHTMLTag_title); nsCParserNode titleNode(&titleToken, 0/*stack token*/); mSink->OpenContainer(titleNode); // Note that XUL will automatically add the prefix "Source of: " if (StringBeginsWith(mFilename, NS_LITERAL_STRING("data:")) && mFilename.Length() > 50) { nsAutoString dataFilename(Substring(mFilename, 0, 50)); dataFilename.AppendLiteral("..."); CTextToken titleText(dataFilename); nsCParserNode titleTextNode(&titleText, 0/*stack token*/); mSink->AddLeaf(titleTextNode); } else { CTextToken titleText(mFilename); nsCParserNode titleTextNode(&titleText, 0/*stack token*/); mSink->AddLeaf(titleTextNode); } mSink->CloseContainer(eHTMLTag_title); if (theAllocator) { CStartToken* theToken= static_cast<CStartToken*> (theAllocator->CreateTokenOfType(eToken_start, eHTMLTag_link, NS_LITERAL_STRING("LINK"))); if (theToken) { nsCParserStartNode theNode(theToken, theAllocator); AddAttrToNode(theNode, theAllocator, NS_LITERAL_STRING("rel"), NS_LITERAL_STRING("stylesheet")); AddAttrToNode(theNode, theAllocator, NS_LITERAL_STRING("type"), NS_LITERAL_STRING("text/css")); AddAttrToNode(theNode, theAllocator, NS_LITERAL_STRING("href"), NS_LITERAL_STRING("resource://gre/res/viewsource.css")); mSink->AddLeaf(theNode); } IF_FREE(theToken, theAllocator); } result = mSink->CloseContainer(eHTMLTag_head); if(NS_SUCCEEDED(result)) { mHasOpenRoot = PR_TRUE; } } if (NS_SUCCEEDED(result) && !mHasOpenBody) { if (theAllocator) { CStartToken* bodyToken= static_cast<CStartToken*> (theAllocator->CreateTokenOfType(eToken_start, eHTMLTag_body, NS_LITERAL_STRING("BODY"))); if (bodyToken) { nsCParserStartNode bodyNode(bodyToken, theAllocator); AddAttrToNode(bodyNode, theAllocator, NS_LITERAL_STRING("id"), NS_ConvertASCIItoUTF16(kBodyId)); if (mWrapLongLines) { AddAttrToNode(bodyNode, theAllocator, NS_LITERAL_STRING("class"), NS_ConvertASCIItoUTF16(kBodyClassWrap)); } result = mSink->OpenContainer(bodyNode); if(NS_SUCCEEDED(result)) mHasOpenBody=PR_TRUE; } IF_FREE(bodyToken, theAllocator); if (NS_SUCCEEDED(result)) { CStartToken* preToken = static_cast<CStartToken*> (theAllocator->CreateTokenOfType(eToken_start, eHTMLTag_pre, NS_LITERAL_STRING("PRE"))); if (preToken) { nsCParserStartNode preNode(preToken, theAllocator); AddAttrToNode(preNode, theAllocator, NS_LITERAL_STRING("id"), NS_LITERAL_STRING("line1")); result = mSink->OpenContainer(preNode); } else { result = NS_ERROR_OUT_OF_MEMORY; } IF_FREE(preToken, theAllocator); } } } NS_ASSERTION(aCharsetPtr, "CViewSourceHTML::BuildModel expects a charset!"); mCharset = *aCharsetPtr; NS_ASSERTION(aCanInterrupt, "CViewSourceHTML can't run scripts, so " "document.write should not forbid interruptions. Why is " "the parser telling us not to interrupt?"); while(NS_SUCCEEDED(result)){ CToken* theToken=mTokenizer->PopToken(); if(theToken) { result=HandleToken(theToken); if(NS_SUCCEEDED(result)) { IF_FREE(theToken, mTokenizer->GetTokenAllocator()); if (mSink->DidProcessAToken() == NS_ERROR_HTMLPARSER_INTERRUPTED) { result = NS_ERROR_HTMLPARSER_INTERRUPTED; break; } } else { mTokenizer->PushTokenFront(theToken); } } else break; }//while mTokenizer=oldTokenizer; } else result=NS_ERROR_HTMLPARSER_BADTOKENIZER; return result; }
void Project::CopyTo(const wxString& new_path, const wxString& new_name, const wxString& description) { // first save the xml document to the destination folder wxString newFile = new_path + new_name + wxT(".project"); if ( !m_doc.Save( newFile ) ) { return; } // load the new xml and modify it wxXmlDocument doc; if ( !doc.Load( newFile ) ) { return; } // update the 'Name' property XmlUtils::UpdateProperty(doc.GetRoot(), wxT("Name"), new_name); // set description wxXmlNode *descNode(NULL); // update the description descNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("Description")); if (!descNode) { descNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Description")); doc.GetRoot()->AddChild(descNode); } XmlUtils::SetNodeContent(descNode, description); // Remove the 'Dependencies' wxXmlNode *deps = doc.GetRoot()->GetChildren(); while(deps) { if(deps->GetName() == wxT("Dependencies")) { doc.GetRoot()->RemoveChild(deps); delete deps; // restart the search from the begining deps = doc.GetRoot()->GetChildren(); } else { // try next child deps = deps->GetNext(); } } // add an empty deps node deps = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies")); doc.GetRoot()->AddChild(deps); // Remove virtual folders wxXmlNode *vd = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("VirtualDirectory")); while (vd) { doc.GetRoot()->RemoveChild( vd ); delete vd; vd = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("VirtualDirectory")); } // add all files under this path std::vector<wxFileName> files; GetFiles(files, true); wxXmlNode *srcNode(NULL); wxXmlNode *headNode(NULL); wxXmlNode *rcNode(NULL); // copy the files to their new location for (size_t i=0; i<files.size(); i++) { wxFileName fn = files.at(i); wxCopyFile(fn.GetFullPath(), new_path + wxT("/") + fn.GetFullName()); wxXmlNode *file_node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("File")); file_node->AddProperty(wxT("Name"), fn.GetFullName()); switch ( FileExtManager::GetType( fn.GetFullName() ) ) { case FileExtManager::TypeSourceC: case FileExtManager::TypeSourceCpp: // source file if ( !srcNode ) { srcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory")); srcNode->AddProperty(wxT("Name"), wxT("src")); doc.GetRoot()->AddChild(srcNode); } srcNode->AddChild(file_node); break; case FileExtManager::TypeHeader: // header file if (!headNode) { headNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory")); headNode->AddProperty(wxT("Name"), wxT("include")); doc.GetRoot()->AddChild(headNode); } headNode->AddChild(file_node); break; default: // resource file if ( !rcNode ) { rcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory")); rcNode->AddProperty(wxT("Name"), wxT("resources")); doc.GetRoot()->AddChild(rcNode); } rcNode->AddChild(file_node); break; } } doc.Save(newFile); }