00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #include "weDiffLib.h"
00022 #include <boost/lexical_cast.hpp>
00023 #include "externals/dtl.hpp"
00024 
00025 WeDiffLibWordList* WeDiffLibTextToList(string txt, weCmpMode mode)
00026 {
00027     WeDiffLibWordList*  lst = new WeDiffLibWordList;
00028     WeDiffLibWord *wObj;
00029     bool currSpace;
00030     unsigned idx, last;
00031     string word;
00032 
00033     last = idx = 0;
00034     currSpace = std::isspace(txt[idx++], std::locale());
00035     word = "";
00036     while(idx < txt.length()) {
00037         while (currSpace == std::isspace(txt[idx++], std::locale()) && idx < txt.length());
00038         if (idx == txt.length()) {
00039             idx++; 
00040         }
00041         if (currSpace) {
00042             
00043             if (mode & weCmpCollapseSpace) {
00044                 word += " ";    
00045             }
00046             else {
00047                 word += txt.substr(last, idx - 1 - last);
00048             }
00049             wObj = new WeDiffLibWord;
00050             wObj->mode = mode;
00051             wObj->word = word;
00052             lst->push_back(*wObj);
00053             word = "";
00054             last = idx - 1;
00055             currSpace = std::isspace(txt[last], std::locale());
00056         }
00057         else {
00058             
00059             word += txt.substr(last, idx - 1 - last);
00060             last = idx - 1;
00061             currSpace = std::isspace(txt[last], std::locale());
00062         }
00063     }
00064     if (!word.empty()) {
00065         wObj = new WeDiffLibWord;
00066         wObj->mode = mode;
00067         wObj->word = word;
00068         lst->push_back(*wObj);
00069     }
00070     return lst;    
00071 }
00072 
00073 WeCmpResults* WeTextDiff(string txt1, string txt2, weCmpMode mode)
00074 {
00075     WeCmpResults* retval;
00076     string        txt;
00077     unsigned      idx;
00078     WeDiffLibWordList* lst1;
00079     WeDiffLibWordList* lst2;
00080 
00081     
00082     lst1 = WeDiffLibTextToList(txt1, mode);
00083     lst2 = WeDiffLibTextToList(txt2, mode);
00084 
00085     dtl::Diff<WeDiffLibWord, WeDiffLibWordList> d(*lst1, *lst2);
00086     d.compose();
00087     dtl::Ses<WeDiffLibWord> ses = d.getSes();
00088     vector< pair<WeDiffLibWord, dtl::elemInfo> > ses_v = ses.getSequence();
00089 
00090     
00091     retval = new WeCmpResults;
00092     for (idx = 0; idx < ses_v.size(); idx++) {
00093         if (idx == 0) {
00094             retval->push_back(new WeCmpBlock());
00095             retval->back()->state = (weCmpState)ses_v[idx].second.type;
00096             txt = ses_v[idx].first.word;
00097         }
00098         else {
00099             if(retval->back()->state == ses_v[idx].second.type) {
00100                 txt += ses_v[idx].first.word;
00101             }
00102             else {
00103                 retval->back()->entity = txt;
00104                 retval->push_back(new WeCmpBlock());
00105                 retval->back()->state = (weCmpState)ses_v[idx].second.type;
00106                 txt = ses_v[idx].first.word;
00107             }
00108         }
00109     }
00110     if (!txt.empty()) {
00111         retval->back()->entity = txt;
00112     }
00113 
00114     
00115     delete lst1;
00116     delete lst2;
00117 
00118     return retval;
00119 }