Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

textstrs.h

00001 // $Id: textstrs.h,v 1.1.1.1 2003/06/24 15:33:14 sjcomp Exp $
00002 // Copyright (c) 2003, Alexander Shyrokov aka SJ
00003 // E-mail: sjcomp[@]users.sourceforge.com
00004 // Web:    http://sjgui.sourceforge.net
00005 //
00006 // This code is under BSD license agreement.
00007 //
00008 // Alexander Shyrokov makes no representations about the suitability of this software 
00009 // for any purpose. It is provided "AS IS" without express or implied warranty.
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     // typedef for texts
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         // I want to display only characters in range of 33 through 126 inclusive
00116         // the rest should be substituted with spaces with some exclusions
00117         // tab should be replaced with 4 spaces
00118         // 0x0A and 0x0D should be removed at all if they are at the end
00119         // check if it has end of line character if yes remove it
00120         if(str.length()>0 && str[str.length()-1]=='\n')
00121             str.erase(str.length()-1);
00122         // for dos version I also could need to remove 0x0D
00123         if(str.length()>0 && str[str.length()-1]==0x0D)
00124             str.erase(str.length()-1);
00125         // Now if I have end of line characters, I would like
00126         // to add them as different lines
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             // not to forget remove new line character itself
00132             str.erase(0,tab+1);
00133         }
00134         if(str=="")
00135         {
00136             m_Strings.push_back("");
00137             return;
00138         }
00139         // replace tabs
00140         char ch=0x09;
00141         tab=str.find(ch);
00142         while(tab!=std::string::npos)
00143         {
00144             str.replace(tab,1,"    ");
00145             // find next tab
00146             tab=str.find(ch);
00147         }
00148         // now replace all other characters
00149         for(unsigned int ui=0;ui<str.length();ui++)
00150         {
00151             if(str[ui]<32 || str[ui]>127)str[ui]=' ';
00152         }
00153         // Also I want to wrap the data if needed
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                     // if I did not find anything just cut it as it is
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 
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:04 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo