00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "SeExprEdEditable.h"
00011 #include "SeExprEdEditableExpression.h"
00012 #include <sstream>
00013
00014 bool SeExprParse(std::vector<SeExprEdEditable*>& literals,std::vector<std::string>& variables,
00015 std::vector<std::pair<int,int> >& comments,const char* str);
00016
00017
00018 SeExprEdEditableExpression::SeExprEdEditableExpression()
00019 {}
00020
00021 SeExprEdEditableExpression::~SeExprEdEditableExpression()
00022 {
00023 cleanup();
00024 }
00025
00026 void SeExprEdEditableExpression::
00027 setExpr(const std::string& expr)
00028 {
00029
00030 cleanup();
00031
00032
00033 _expr=expr;
00034 std::vector<std::pair<int,int> > comments;
00035 SeExprParse(_editables,_variables,comments,_expr.c_str());
00036
00037 for(Editables::iterator it=_editables.begin();it!=_editables.end();){
00038 SeExprEdEditable& literal=**it;
00039 int endPos=literal.endPos;
00040 std::string comment="";
00041 for(size_t ci=0;ci<comments.size();ci++){
00042 if(comments[ci].first>=endPos){
00043
00044 size_t pos=_expr.find('\n',endPos);
00045 if(pos==std::string::npos || pos>=(size_t)comments[ci].second){
00046 comment=_expr.substr(comments[ci].first,comments[ci].second-comments[ci].first);
00047 break;
00048 }
00049 }
00050 }
00051 bool keepEditable=literal.parseComment(comment);
00052 if(!keepEditable){
00053 delete &literal;
00054 it=_editables.erase(it);
00055 }else{
00056 ++it;
00057 }
00058 }
00059 }
00060
00061 void SeExprEdEditableExpression::cleanup()
00062 {
00063 for(size_t i=0;i<_editables.size();i++) delete _editables[i];
00064 _editables.clear();
00065 _variables.clear();
00066
00067 }
00068
00069 std::string SeExprEdEditableExpression::getEditedExpr() const
00070 {
00071 int offset=0;
00072 std::stringstream stream;
00073 for(size_t i=0,sz=_editables.size();i<sz;i++){
00074 SeExprEdEditable& literal=*_editables[i];
00075 stream<<_expr.substr(offset,literal.startPos-offset);
00076 literal.appendString(stream);
00077 offset=literal.endPos;
00078 }
00079 stream<<_expr.substr(offset,_expr.size()-offset);
00080 return stream.str();
00081 }
00082
00083 void SeExprEdEditableExpression::updateString(const SeExprEdEditableExpression& other)
00084 {
00085
00086 _variables=other._variables;
00087 _expr=other._expr;
00088 _variables=other._variables;
00089 for(size_t i=0,sz=_editables.size();i<sz;i++){
00090 SeExprEdEditable& literal=*_editables[i];
00091 SeExprEdEditable& otherLiteral=*other._editables[i];
00092 assert(literal.controlsMatch(otherLiteral));
00093 literal.updatePositions(otherLiteral);
00094 }
00095 }
00096
00097 bool SeExprEdEditableExpression::controlsMatch(const SeExprEdEditableExpression& other) const
00098 {
00099 if(_editables.size() != other._editables.size()) return false;
00100
00101 for(size_t i=0,sz=_editables.size();i<sz;i++){
00102 const SeExprEdEditable& literal=*_editables[i];
00103 const SeExprEdEditable& otherLiteral=*other._editables[i];
00104 if(!literal.controlsMatch(otherLiteral)) return false;
00105 }
00106 return true;
00107 }