proj home

Files   Classes   Functions   Hierarchy  

html.cpp

Go to the documentation of this file.
00001 #include <html.h>
00002 #include <print.h>
00003 #include <stringserialization.h>
00004 #include <tokenizerlocal.h>
00005 
00006 
00007 string html::space = "&nbsp;";
00008 string html::br = "<br />";
00009 
00010 int html::insert(commandline& cmd)
00011 {
00012   string filename;
00013 
00014   cmd.mapvar(filename,"file");
00015   if (filename.empty())
00016   {
00017     cout << "error: file=\"\"" << endl;
00018     return 1;
00019   }
00020 
00021   string s0;
00022   if (filestring::serialize(s0,filename)==false)
00023   {
00024     cout << "error: unable to open " << filename << endl;
00025     return 1;
00026   };
00027 
00028   cout << "Reading " << filename << endl;
00029 
00030   tokenizer tk2(s0);
00031   tokenizerlocal tl2(tk2);
00032 
00033   string tag1;
00034   string tag2;
00035   string text;
00036   string file;
00037 
00038   int res=0;
00039   if (tl2.read(tag1,"tag1")==false)
00040   {
00041     res=1;
00042     cout << "error: no <tag1></tag1>" << endl;
00043   };
00044   if (tl2.read(tag2,"tag2")==false)
00045   {
00046     res=1;
00047     cout << "error: no <tag2></tag2>" << endl;
00048   };
00049   if (tl2.read(text,"text")==false)
00050   {
00051     res=1;
00052     cout << "error: no <text></text>" << endl;
00053   };
00054   if (tl2.read(file,"file")==false)
00055   {
00056     res=1;
00057     cout << "error: no <file></file>" << endl;
00058   };
00059   
00060   if (res==1)
00061     return 1;
00062 
00063   return insert(file,tag1,tag2,text);
00064 }
00065 
00066 int html::insert
00067 (
00068   stringc& file, 
00069   stringc& tag1,
00070   stringc& tag2,
00071   stringc& text
00072 )
00073 {
00074   string s1;
00075   if (filestring::serialize(s1,file)==false)
00076   {
00077     cout << "error: could not read " << file << endl;
00078     return 1;
00079   }
00080 
00081   tokenizer tk;
00082   tk.readaslines(s1);
00083 
00084   liststringi i1;
00085   liststringi i2;
00086 
00087   bool found1=false;
00088   bool found2=false;
00089   for (tk.reset(); !tk; ++tk)
00090   {
00091     if (found1==false)
00092     {
00093       if (tk()==tag1)
00094       {
00095         found1=true;
00096         i1=tk.current;
00097       }
00098       continue;
00099     }
00100   
00101 //cout << SHOW3(tk()) << endl;
00102 //cout << SHOW3(tag2) << endl;
00103     if (tk()==tag2)
00104     {
00105       found2=true;
00106       i2=tk.current;
00107     }
00108   }
00109 
00110   if (found1==false)
00111     return 1;
00112   if (found2==false)
00113     return 1;
00114 
00115   // See if there is data between the tags.
00116   liststringi i4=i1;
00117   ++i4;
00118   if (i4!=i2)
00119     tk.seq.erase(i4,i2); // [i4,i2)
00120 //cout << SHOW3( i4==i2 ) << endl;
00121 
00122 //  cout << "***" <<  print(tk.seq) << "***" << endl;
00123   
00124 //cout << SHOW3(*i1) << endl;
00125 //cout << SHOW3(*i2) << endl;
00126 
00127 //cout << SHOW3(*(i1+1)==*i2) << endl;
00128 //cout << "insert at i2 text" << endl;
00129   tk.seq.insert(i2,text);  // Insert before i2.
00130 
00131   // Add carriage return as tokenizer stripped these.
00132   for (tk.reset(); !tk; ++tk)
00133     *tk += "\n";
00134 
00135 //  cout << "***" <<  print(tk.seq) << "***" << endl;
00136   if (filestring::serializeInverse(file,(stringc)tk)==false)
00137     return 1;
00138 
00139   return 0;
00140 }
00141 
00142 

Generated on Fri Mar 4 00:49:28 2011 for Chelton Evans Source by  doxygen 1.5.8