bool XMLInOut::SaveScript(const char* filename) { int rc; ofstream out; // First check to see that we can open the filename before saving/killing the document. assert(filename); out.open(filename); if (out.fail()) return false; rc = xmlTextWriterEndDocument(writer); if (rc < 0) { printf("testXmlwriterMemory: Error at xmlTextWriterEndDocument\n"); return false; } // Now print it or write it... cout << "XML Command being saved:" << endl << "'" << (const char *) buf->content << "'" << endl; out << buf->content; out.close(); // Reset back to zero. New document. SetupDoc(); return true; }
XMLInOut::XMLInOut() { writer = NULL; buf = NULL; SetupDoc(); }
NS_IMETHODIMP mozSpellChecker::NextMisspelledWord(nsAString &aWord, nsTArray<nsString> *aSuggestions) { if(!aSuggestions||!mConverter) return NS_ERROR_NULL_POINTER; int32_t selOffset; int32_t begin,end; nsresult result; result = SetupDoc(&selOffset); bool isMisspelled,done; if (NS_FAILED(result)) return result; while( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ) { nsString str; result = mTsDoc->GetCurrentTextBlock(&str); if (NS_FAILED(result)) return result; do{ result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end); if(NS_SUCCEEDED(result)&&(begin != -1)){ const nsAString &currWord = Substring(str, begin, end - begin); result = CheckWord(currWord, &isMisspelled, aSuggestions); if(isMisspelled){ aWord = currWord; mTsDoc->SetSelection(begin, end-begin); // After ScrollSelectionIntoView(), the pending notifications might // be flushed and PresShell/PresContext/Frames may be dead. // See bug 418470. mTsDoc->ScrollSelectionIntoView(); return NS_OK; } } selOffset = end; }while(end != -1); mTsDoc->NextBlock(); selOffset=0; } return NS_OK; }
NS_IMETHODIMP mozSpellChecker::NextMisspelledWord(nsAString &aWord, nsStringArray *aSuggestions) { if(!aSuggestions||!mConverter) return NS_ERROR_NULL_POINTER; PRUint32 selOffset; PRInt32 begin,end; nsresult result; result = SetupDoc(&selOffset); PRBool isMisspelled,done; if (NS_FAILED(result)) return result; while( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ) { nsString str; result = mTsDoc->GetCurrentTextBlock(&str); if (NS_FAILED(result)) return result; do{ result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end); if(NS_SUCCEEDED(result)&&(begin != -1)){ const nsAString &currWord = Substring(str, begin, end - begin); result = CheckWord(currWord, &isMisspelled, aSuggestions); if(isMisspelled){ aWord = currWord; mTsDoc->SetSelection(begin, end-begin); mTsDoc->ScrollSelectionIntoView(); return NS_OK; } } selOffset = end; }while(end != -1); mTsDoc->NextBlock(); selOffset=0; } return NS_OK; }
NS_IMETHODIMP mozSpellChecker::Replace(const nsAString &aOldWord, const nsAString &aNewWord, bool aAllOccurrences) { if(!mConverter) return NS_ERROR_NULL_POINTER; nsAutoString newWord(aNewWord); // sigh if(aAllOccurrences){ int32_t selOffset; int32_t startBlock,currentBlock,currOffset; int32_t begin,end; bool done; nsresult result; nsAutoString str; // find out where we are result = SetupDoc(&selOffset); if(NS_FAILED(result)) return result; result = GetCurrentBlockIndex(mTsDoc,&startBlock); if(NS_FAILED(result)) return result; //start at the beginning result = mTsDoc->FirstBlock(); currOffset=0; currentBlock = 0; while( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ) { result = mTsDoc->GetCurrentTextBlock(&str); do{ result = mConverter->FindNextWord(str.get(),str.Length(),currOffset,&begin,&end); if(NS_SUCCEEDED(result)&&(begin != -1)){ if (aOldWord.Equals(Substring(str, begin, end-begin))) { // if we are before the current selection point but in the same block // move the selection point forwards if((currentBlock == startBlock)&&(begin < selOffset)){ selOffset += int32_t(aNewWord.Length()) - int32_t(aOldWord.Length()); if(selOffset < begin) selOffset=begin; } mTsDoc->SetSelection(begin, end-begin); mTsDoc->InsertText(&newWord); mTsDoc->GetCurrentTextBlock(&str); end += (aNewWord.Length() - aOldWord.Length()); // recursion was cute in GEB, not here. } } currOffset = end; }while(currOffset != -1); mTsDoc->NextBlock(); currentBlock++; currOffset=0; } // We are done replacing. Put the selection point back where we found it (or equivalent); result = mTsDoc->FirstBlock(); currentBlock = 0; while(( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ) &&(currentBlock < startBlock)){ mTsDoc->NextBlock(); } //After we have moved to the block where the first occurrence of replace was done, put the //selection to the next word following it. In case there is no word following it i.e if it happens //to be the last word in that block, then move to the next block and put the selection to the //first word in that block, otherwise when the Setupdoc() is called, it queries the LastSelectedBlock() //and the selection offset of the last occurrence of the replaced word is taken instead of the first //occurrence and things get messed up as reported in the bug 244969 if( NS_SUCCEEDED(mTsDoc->IsDone(&done)) && !done ){ nsString str; result = mTsDoc->GetCurrentTextBlock(&str); result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end); if(end == -1) { mTsDoc->NextBlock(); selOffset=0; result = mTsDoc->GetCurrentTextBlock(&str); result = mConverter->FindNextWord(str.get(),str.Length(),selOffset,&begin,&end); mTsDoc->SetSelection(begin, 0); } else mTsDoc->SetSelection(begin, 0); } } else{ mTsDoc->InsertText(&newWord); } return NS_OK; }