📄 refere~1.c
字号:
// if (!r.isFolder()){// output << "<A HREF=\"" << r.url << "\" ";// if (r.aliasID != "")// output << ALIASID << "=\"" << r.aliasID << "\" ";// if (r.aliasOf != "")// output << ALIASOF << "=\"" << r.aliasOf << "\" ";// output << addDate << "=\"" << r.creationTime << "\" "// << lastVisit << "=\"" << r.visitTime << "\" "// << lastModified << "=\"" << r.modifiedTime << "\">"// << r.title << "</A>" << endl;// if (r.comment != ""){// string comt(r.comment);// replaceAll(comt,"<","<");// output << "<DD>" << comt << endl;// };// }// else {// output << "<H3 FOLDED " << addDate << "=\"" << r.creationTime << "\">"// << r.title << "</H3>" << endl;// if (r.comment != ""){// string comt = r.comment;// replaceAll(comt,"<","<");// output << "<DD>" << comt << endl;// };// output << "<DL><p>" << endl// << *(r.children)// << "</DL><p>" << endl;// }// }// return output;// };//outputs in flat list formatvoid reference::writeAsFlatFile(ostream& output) const // It assumes that all \n's have been removed from the strings (for >> to work){ if (!isPrivate() && !isAliasof()){ if (children == 0) output << "LEAF" << endl; else output << "FOLDER" << endl; string ptitle = title; output << navBar << endl << url << endl << title << endl << comment << endl << creationTime << endl << modifiedTime << endl << visitTime << endl << hits << endl; if (children != 0) children->writeAsFlatFile(output); }}//read a reference from a flat file.iwebstream &operator>>(iwebstream& is, reference & r){ string s1; // getline (is, s1); is.getline(s1); string s; s = trim(s1); if (s == "") return is; //done. r.children = 0; if (s == "FOLDER"){ r.folder = true; } else if (s == "LEAF"){ r.folder = false; } else { cout << "Could not parse input on symbol===" << s << "===" << endl; return is; }; // getline(is,r.navBar); is.getline(r.navBar); r.navBar = trim(r.navBar); // getline (is, r.url); is.getline(r.url); r.url = trim(r.url); // getline (is, r.title); is.getline(r.title); r.title = trim(r.title); // getline (is, r.comment); is.getline(r.comment); is >> r.creationTime >> r.modifiedTime >> r.visitTime >> r.hits; // getline (is, s); //get the last /n....not needed with iwebstream return is;}/**set the contents of this reference based on the next <item> in is return true if an item was read, false otherwise*/bool reference::readXml(iwebstream & is) { string s = is.getData(); //easier to do with the whole string const string itemT = "<item>"; const string itemET = "</item>"; const string titleT = "<title>"; const string titleET = "</title>"; const string linkT = "<link>"; const string linkET = "</link>"; const string descriptionT = "<description>"; const string descriptionET = "</description>"; string temp; // cout << "readXML:"; if (!is.find(itemT)) return false; title = is.findTag(titleT, titleET, itemT, itemET); title = trim(title); if (title == "" ) return false; url = is.findTag(linkT, linkET, itemT, itemET); url = trim(url); if (url == "" ) return false; comment = is.findTag(descriptionT, descriptionET, itemT, itemET); comment = trim(comment); is.findAndPass(itemET); // cout << title << url << comment << endl; return true;}string referenceTree::getToken(iwebstream & is){ string token; char c,c1,c2, c3; while (!is.eof()) { c = is.get(); if (c != '<') continue; c1 = is.get(); if (c1 == 0) return ""; c2 = is.get(); if (c2 == 0) return ""; if (c1 == 'A') { token += c; token += c1; token += c2; while (!is.eof()) { c = is.get(); token +=c; if (c == '/'){ c = is.get(); token += c; if (c == 'A'){ c = is.get(); token += c; if (c == '>'){ token +=c; return token; // <A HREF=.... </A> } } } } return ""; //if we exited its because the file ended } else if ((c1 == 'H') && (c2 == '3')) { token += c; token += c1; token += c2; while (!is.eof()) { c = is.get(); if (is.eof()) return ""; token +=c; if (c == '/'){ c = is.get(); token +=c; if (c == 'H'){ c = is.get(); token +=c; if (c == '3'){ c = is.get(); token +=c; if (c == '>'){ token +=c; return token; // <H3> ... </H3> } } } } } return ""; } else if ((c1 == 'D') && (c2 == 'L')) { token += c; token += c1; token += c2; while ((c = is.get()) != '>'){ token +=c; if (is.eof()) return ""; }; token +=c; return token; // <DL> } else if ((c1 == 'D') && (c2 == 'D')) { token += c; token += c1; token += c2; c1 = is.get(); c2 = is.get(); while ((!((c1 == '<') && (c2 == 'D'))) && (!((c1 == '<') && (c2 == '/')))) { //end only when find <D or </ ..this ignores <BR> // cout << c1; //eliminate <BR>s if (is.eof()) return ""; if ((c1 == '<') && (c2 == 'B')) { //assume this is a <BR> token += c1; token += c2; c3 = is.get(); // R token += c3; c3 = is.get(); // > token += c3; c1 = is.get(); //' '; // insert a space instead c2 = is.get(); continue; } if ((c1 == '&') && (c2 == 'l')) { //assume this is < replace with < c3 = is.get(); //t c3 = is.get(); //; token += '<'; c1 = is.get(); c2 = is.get(); continue; } token +=c1; c1 = c2; c2 = is.get(); } is.putback(c2); is.putback(c1); return token; // <DD> comment... (up to next '<') } else if ((c1 == '/') && (c2 == 'D') && ((c3 = is.get()) == 'L')) { token += c; token += c1; token += c2; token += c3; while ((c = is.get()) != '>'){ token +=c; if (is.eof()) return ""; }; token +=c; return token; // </DL> } } return "";}//read a referenceTree from a bookmarks file.iwebstream &operator>>(iwebstream& is, referenceTree & rt){ string token; // cout << "Calling >> " << endl; // cout << "Calling >> numl=" << rt.numLeafs << endl; while ((token = rt.getToken(is)) != ""){ // cout << token << endl; if (token.substr(0,2) == "<A") { reference r(token); (rt.contents).push_back(r); rt.numLeafs++; } else if (token.substr(0,4) == "<DD>") { int len = token.size(); if (rt.contents.empty()) { //if its a comment to the folder, add it. string temp = token.substr(4,len-4); rt.comment = trim(temp); } else {//if a comment to a leaf then get the leaf and add a comment to it. reference & last = rt.contents.back(); string temp = token.substr(4,len-4); last.comment = trim(temp); if (last.comment == PRIVATE){ last.priv = true; rt.numLeafs--; //do not count this one } } } else if (token.substr(0,2) == "<H") { //a new (sub)folder reference r(token); referenceTree * rtc = new referenceTree; is >> (*rtc); if (rtc->comment == PRIVATE){ r.priv = true; rt.numLeafs -= rtc->numLeafs; //do not count these } if (rtc->comment.substr(0,5) == ALIAS) { r.aliasof = true; string::size_type endDirName = rtc->comment.find("\n", 0); if (endDirName == string::npos) endDirName = rtc->comment.length(); r.aliasDir = rtc->comment.substr(5,endDirName - 5); r.aliasDir = trim(r.aliasDir); } r.children = rtc; // rtc->doSort(); //sort, if needed (as per comment) if (cmpNoCase(rtc->comment.substr(0,7), INCLUDE) == 0){ //INCLUDE found, read file and merge with rtc. string::size_type endFileName = rtc->comment.find("\n", 0); if (endFileName == string::npos) endFileName = rtc->comment.length(); string fileName = rtc->comment.substr(7,endFileName - 7); fileName = trim(fileName); iwebstream istream(fileName.c_str()); cout << "Found INCLUDE " << fileName << " in folder " << r.title << endl; if (istream == 0){ cout << "Could not open " << fileName << endl; } else { cout << "Reading " << fileName << endl; referenceTree rtc2; istream >> rtc2; rtc->addTree(rtc2); } } if (cmpNoCase(rtc->comment.substr(0,7), DIRNAME) == 0){ //DIRNAME found, set to dirname. string::size_type beginDirName = rtc->comment.find_first_not_of(" tn", 7); if (beginDirName == string::npos) beginDirName = 7; string::size_type endDirName = rtc->comment.find_first_of(" tn", beginDirName); if (endDirName == string::npos) endDirName = rtc->comment.length(); string dirName = rtc->comment.substr(beginDirName, endDirName - beginDirName); rtc->comment.erase(0, endDirName); dirName = trim(dirName); r.dirname = dirName; cout << "Found DIRNAME \"" << r.dirname << "\" in folder " << r.title << endl; } else { r.dirname = r.title; } r.comment = rtc->comment; //make a copy, waste memory. r.folder = true; rt.contents.push_back(r); rt.numLeafs += rtc->numLeafs; } else if (token == "</DL>"){ // cout << "Returning from >> numl=" << rt.numLeafs << endl; return is; } }; cerr << "Bookmark file is not well formed. Trying anyway...(crash may ensue)." << endl; // rt.contents.erase(rt.contents.begin(), rt.contents.end()); // rt.numLeafs = 0; // rt.comment = ""; return is; //this should never happen with a well formed bookmark file}// //write as bookmarkfile ---does not add indenting spaces// ostream &operator<<(ostream& out, const referenceTree & rt) // {// for (vector<reference>::const_iterator i = rt.contents.begin(); i != rt.contents.end(); ++i){// const reference& r = *i;// out << r;// }// return out;// }/** write as bookmarkfile */void referenceTree::writeAsBookHelper(ostream& out, string & prepend) const{ for (vector<reference>::const_iterator i = contents.begin(); i != contents.end(); ++i){ const reference& r = *i; // out << prepend << "<DT>"; r.writeAsBookHelper(out, prepend); }}/** write as bookmarkfile */void referenceTree::writeAsBookmarkFile(ostream & out, string title) const{ out << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl << "<!--This file was generated by bk2site-->" << endl << "<TITLE>" << title << "</TITLE>" << endl << "<H1>" << title << "</H1>" << endl << endl << "<DL><p>" << endl; // << *this string prepend = ""; writeAsBookHelper(out, prepend); out << "</DL><p>" << endl;}/** write as xbel */void referenceTree::writeAsXBELHelper(ostream& out, string & prepend) const{ for (vector<reference>::const_iterator i = contents.begin(); i != contents.end(); ++i){ const reference& r = *i; // out << prepend << "<DT>"; r.writeAsXBELHelper(out, prepend); }}/** write as xbel */void referenceTree::writeAsXBELFile(ostream & out, string title) const{ out << "<?xml version=\"1.0\"?>" << endl << "<!DOCTYPE xbel PUBLIC \"+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML\" \"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd\">" << endl << "<xbel>" << endl << "<info>File generated by bk2site</info>" << endl << "<title>" << title << "</title>" << endl; string prepend = " "; writeAsXBELHelper(out, prepend); out << "</xbel>" << endl;}/** Outputs in flat list format */void referenceTree::writeAsFlatFile(ostream& out) const{ for (vector<reference>::const_iterator i = contents.begin(); i != contents.end(); ++i){ const reference& r = *i; r.writeAsFlatFile(out); };}/** Returns the path to a directory named "title" whose comment is * NOT ALIAS. The result is prepended by "dir". */string referenceTree::getPath(const string & dirname, const string & dir, const string & subdirsep) const{ for (vector<reference>::const_iterator i = contents.begin(); i != contents.end(); ++i){ const reference& r = *i; if ((r.dirname == dirname) && (r.children != 0) && !(r.isAliasof())) { return dir + name2filename(r.dirname) + subdirsep; } else if (r.children){ string newDir = dir + name2filename(r.dirname) + subdirsep; string res = (r.children)->getPath(dirname, newDir, subdirsep); if (res != "") return res; } } return "";}/** Increase the number of hits for url by 1. */void referenceTree::increaseHits(string & url, int x){ for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.url == url) r.hits+=x; if (r.children) r.children->increaseHits(url, x); }}/** Set the creationtime of all folders to be the max of all descendants. Returns the max Creationtime of this tree */time_t referenceTree::setFolderCreationToMaxDescendant() { time_t maxTime =0; for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.isFolder()) r.creationTime = r.children->setFolderCreationToMaxDescendant(); if ((r.creationTime > maxTime) && !r.isAliasof()) maxTime = r.creationTime; } return maxTime;}/** Set the creationtime of all folders to be the max of all its children. */time_t referenceTree::setFolderCreationToMaxChildren() { time_t maxTime =0; for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.isFolder()) r.creationTime = r.children->setFolderCreationToMaxDescendant(); else if (r.creationTime > maxTime) maxTime = r.creationTime; } return maxTime;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -