00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #if !defined(TEXTSTRS_H_INCLUDED)
00012 #define TEXTSTRS_H_INCLUDED
00013
00014 #include <string>
00015 #include <vector>
00016
00036 class CTextStrs
00037 {
00038
00039 typedef std::vector<std::string> TStrings;
00040 TStrings m_Strings;
00041 unsigned int m_uiCharOnDisp;
00042 bool m_yWrap;
00043 unsigned int m_uiMaxDis;
00044 bool m_yIdent;
00045 void CalcMax()
00046 {
00047 m_uiMaxDis=0;
00048 for(TStrings::iterator pos=m_Strings.begin();pos!=m_Strings.end();++pos)
00049 m_uiMaxDis=std::max(m_uiMaxDis,pos->length());
00050 }
00051 public:
00053 void SetIdent(bool b=true){m_yIdent=b;}
00054 void EraseTop(unsigned int pos)
00055 {m_Strings.erase(m_Strings.begin(),m_Strings.begin()+pos);CalcMax();}
00061 void Erase(unsigned int pos,unsigned int where, unsigned int len)
00062 {m_Strings[pos].erase(where,len);CalcMax();}
00067 void Insert(unsigned int pos,unsigned int where, std::string& str)
00068 {
00069 m_Strings[pos].insert(where,str);
00070 if(m_uiMaxDis<m_Strings[pos].length() )
00071 m_uiMaxDis=m_Strings[pos].length();
00072 }
00074 unsigned int LenAt(unsigned int pos){return m_Strings[pos].length();}
00076 void clear(){m_Strings.clear();m_uiMaxDis=0;}
00078 unsigned int size(){return m_Strings.size();}
00080 const std::string& operator[](unsigned int i)const {return m_Strings[i];}
00082 void SetWrap(bool b){m_yWrap=b;}
00084 bool IsWrap(){return m_yWrap;}
00089 unsigned int GetCharOnDisp(){return m_uiCharOnDisp;}
00094 void SetCharOnDisp(unsigned int ui){m_uiCharOnDisp=ui;}
00095 CTextStrs():m_uiCharOnDisp(0),m_yWrap(false),m_uiMaxDis(0),m_yIdent(false){}
00099 unsigned int GetMaxDis(){return m_uiMaxDis;}
00108 void push_back(std::string str)
00109 {
00110 if(str=="")
00111 {
00112 m_Strings.push_back("");
00113 return;
00114 }
00115
00116
00117
00118
00119
00120 if(str.length()>0 && str[str.length()-1]=='\n')
00121 str.erase(str.length()-1);
00122
00123 if(str.length()>0 && str[str.length()-1]==0x0D)
00124 str.erase(str.length()-1);
00125
00126
00127 std::string::size_type tab=0;
00128 while((tab=str.find('\n'))!=std::string::npos)
00129 {
00130 push_back(str.substr(0,tab));
00131
00132 str.erase(0,tab+1);
00133 }
00134 if(str=="")
00135 {
00136 m_Strings.push_back("");
00137 return;
00138 }
00139
00140 char ch=0x09;
00141 tab=str.find(ch);
00142 while(tab!=std::string::npos)
00143 {
00144 str.replace(tab,1," ");
00145
00146 tab=str.find(ch);
00147 }
00148
00149 for(unsigned int ui=0;ui<str.length();ui++)
00150 {
00151 if(str[ui]<32 || str[ui]>127)str[ui]=' ';
00152 }
00153
00154 std::string strSeparators=" ,/.|?-#^*)$;%]\\!:";
00155 if(str.length()<=GetCharOnDisp() || !m_yWrap || GetCharOnDisp()==0)
00156 {
00157 m_Strings.push_back(str);
00158 m_uiMaxDis=std::max(m_uiMaxDis,str.length());
00159 }
00160 else
00161 {
00162 bool bf=true;
00163 while(str.length()!=0)
00164 {
00165 std::string sub;
00166 std::string::size_type iInd;
00167 if(GetCharOnDisp()<str.length())
00168 iInd=GetCharOnDisp();
00169 else
00170 iInd=str.length();
00171 if((str.length()<GetCharOnDisp() && !m_yIdent) ||
00172 (str.length()<(GetCharOnDisp()-3) && m_yIdent))
00173 {
00174 sub=str;
00175 iInd=str.length();
00176 }
00177 else
00178 {
00179 if(!bf && m_yIdent && iInd>3)iInd-=3;
00180 sub=str.substr(0,iInd);
00181 iInd=sub.find_last_of(strSeparators);
00182
00183 if(iInd==std::string::npos || iInd==str.length()-1)
00184 {
00185 if(GetCharOnDisp()<str.length())
00186 iInd=GetCharOnDisp();
00187 else
00188 iInd=str.length();
00189 }
00190 else
00191 iInd++;
00192 }
00193 if(!bf && m_yIdent)
00194 m_Strings.push_back(" "+sub.substr(0,iInd));
00195 else
00196 m_Strings.push_back(sub.substr(0,iInd));
00197 m_uiMaxDis=std::max(m_uiMaxDis,(unsigned int)iInd);
00198 str.erase(0,iInd);
00199 bf=false;
00200 }
00201 }
00202 }
00203 };
00204
00205 #endif // !defined(TEXTSTRS_H_INCLUDED)
00206