Kontakt  Anfahrtplan   Datenschutzerklärung  Impressum Englisch 

Service: Code-Beispiele: CStringCH: Quellcode

Header:

/* * StringCH.h : Header-File to CStringCH class * Extends CString. 1st implementation contains generic 'Split()'. * 1st Extension: 'Fill()'. * Hints: * Derived from CString * Also add StringCH.cpp to your project file. * You may ask yourself "What is this about the weired 'CH' name suffix?" * I didn't want to name my CString class extension 'CStringExt' because * almost every extended of every programmer might be named 'CStringExt'. * CH means CodeHelper. I think, that there is a good chance, my CStringCH * will never have a name confict with another CString class extension. */ #if !defined(STRINGCH_H__E5DB739E_213D_468D_9371_BD8E4FC99309__INCLUDED_) #define STRINGCH_H__E5DB739E_213D_468D_9371_BD8E4FC99309__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <afx.h> #include <afxwin.h> // MFC Core- and Standardcomponents #include <afxext.h> // MFC Extensions #include <afxtempl.h> class CStringCH: public CString { public: /* Overwritten construtors of base class. * Initialize necessary member variables */ CStringCH( ); CStringCH( const CString& stringSrc ); CStringCH( TCHAR ch, int nRepeat = 1 ); CStringCH( LPCTSTR lpch, int nLength ); CStringCH( const unsigned char* psz ); CStringCH( LPCWSTR lpsz ); CStringCH( LPCSTR lpsz ); /* Overwritten destrutor of base class. Delete member variables*/ ~CStringCH(); /* * Fillup the string using space character * @param short nRepeat: Number of charaters to be filled in */ voidFill(short nRepeat); /* * Fillup the string using space character * @param int nRepeat: Number of charaters to be filled in */ voidFill(int nRepeat); /* * Fillup the string using space character * @param long nRepeat: Number of charaters to be filled in */ voidFill(long nRepeat); /* * Fillup the string using a certain character * @param short nRepeat: Number of charaters to be filled in * @param WCHAR wcCharacter: The charater to be filled in */ voidFill(short nRepeat, WCHAR wcCharacter); /* * Fillup the string using a certain character * @param int nRepeat: Number of charaters to be filled in * @param WCHAR wcCharacter: The charater to be filled in */ voidFill(int nRepeat, WCHAR wcCharacter); /* * Fillup the string using a certain character * @param long nRepeat: Number of charaters to be filled in * @param WCHAR wcCharacter: The charater to be filled in */ voidFill(long nRepeat, WCHAR wcCharacter); /* * Do a 'split' on the current string using the delimiter passed in as * parameter. The 'Split Results' are copied to CStringArray. If split * failed because string is empty or delimiter is not in string, * CStringArray will contain one item (empty string or the hole * (unsplitted) string. Mulitple call is harmless though split will * just be redone each time. * @param The Delimiter */ voidSplit(const CString& Delimiter); /* * Return number of 'CString's in CStringArray build by 'Split()'. */ intGetSplitCount(); /* * Return CStringArray representing the result of 'Split()'. */ CStringArray*GetSplitResults(); /* * Return CMapStringToString representing the result of 'Split()'. */ CMapStringToString*GetSplitResultsMap(); private: /* * Create necessary pointer variables on heap */ voidInitialize(); /* * CStringArray representing the result of 'Split()'. */ CStringArray*m_pSplitResults; /* * CMapStringToString representing the result of 'Split()'. */ CMapStringToString*m_pSplitResultsMap; /* * Fillup the string using a certain character * @param long nRepeat: Number of charaters to be filled in * @param WCHAR wcCharacter: The charater to be filled in */ voidFillInt(long nRepeat, WCHAR wcCharacter); }; #endif // !defined(STRINGCH_H__E5DB739E_213D_468D_9371_BD8E4FC99309__INCLUDED_)

Seitenanfang


Code:

/* * StringCH.cpp : Implementation-File to CStringCH class * Hints: * Also add StringCH.h to your project file and * '..\Libs' as additional include folder */ #include <StringCH.h> #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CStringCH::CStringCH( ) :CString() { Initialize(); } CStringCH::CStringCH( const CString& stringSrc ) :CString( stringSrc ) { Initialize(); } CStringCH::CStringCH( TCHAR ch, int nRepeat ) :CString( ch, nRepeat ) { Initialize(); } CStringCH::CStringCH( LPCTSTR lpch, int nLength ) :CString( lpch, nLength ) { Initialize(); } CStringCH::CStringCH( const unsigned char* psz ) :CString( psz ) { Initialize(); } CStringCH::CStringCH( LPCWSTR lpsz ) :CString( lpsz ) { Initialize(); } CStringCH::CStringCH( LPCSTR lpsz ) :CString( lpsz ) { Initialize(); } CStringCH::~CStringCH() { // Free memory if (m_pSplitResults) { if (m_pSplitResults->GetSize()) m_pSplitResults->RemoveAll(); delete m_pSplitResults; } m_pSplitResults = NULL; if (m_pSplitResultsMap) { if (m_pSplitResultsMap->GetCount()) m_pSplitResultsMap->RemoveAll(); delete m_pSplitResultsMap; } m_pSplitResultsMap = NULL; } void CStringCH::Initialize() { // Create necessary pointers to member variables m_pSplitResults = new CStringArray; m_pSplitResultsMap = new CMapStringToString; } void CStringCH::Fill(int nRepeat) { WCHAR wcCharacter = ' '; FillInt((long)nRepeat, wcCharacter); } void CStringCH::Fill(short nRepeat) { WCHAR wcCharacter = ' '; FillInt((long)nRepeat, wcCharacter); } void CStringCH::Fill(long nRepeat) { WCHAR wcCharacter = ' '; FillInt(nRepeat, wcCharacter); } void CStringCH::Fill(int nRepeat, WCHAR wcCharacter) { FillInt((long)nRepeat, wcCharacter); } void CStringCH::Fill(short nRepeat, WCHAR wcCharacter) { FillInt((long)nRepeat, wcCharacter); } void CStringCH::Fill(long nRepeat, WCHAR wcCharacter) { FillInt(nRepeat, wcCharacter); } void CStringCH::FillInt(long nRepeat, WCHAR wcCharacter) { for (long i = GetLength(); i < nRepeat; i++)*this += _T(wcCharacter); } void CStringCH::Split(const CString& Delimiter) { int iPos = 0; int newPos = -1; int sizeS2 = Delimiter.GetLength(); int isize = this->GetLength(); CArray positions; // Reset m_pSplitResults->RemoveAll(); m_pSplitResultsMap->RemoveAll(); newPos = this->Find (Delimiter, 0); // If the String could not be splitted because the delimiter is not // present, we just copy the hole string to the (1st pos. of the ) // String Array. This is done in 'GetSplitResults()' here we just set // 'm_nSplitCount' to 1. if( newPos < 0 ) return; int numFound = 0; while( newPos > iPos ) { numFound++; positions.Add(newPos); iPos = newPos; newPos = this->Find (Delimiter, iPos + sizeS2 + 1); } for( int i=0; i <= positions.GetSize(); i++ ) { CString s; if( i == 0 ) s = this->Mid( i, positions[i] ); else { int offset = positions[i-1] + sizeS2; if( offset < isize ) { if( i == positions.GetSize() ) s = this->Mid(offset); else if( i > 0 ) s = this->Mid( positions[i-1] + sizeS2, positions[i] - positions[i-1] - sizeS2 ); } } if( s.GetLength() > 0 ) m_pSplitResults->Add(s); } } intCStringCH::GetSplitCount() { // Split count is always at least 1. Even is 'Split()' would fail, split // result is a CStringArray containing one (even) empty item. return m_pSplitResults->GetSize() == 0 ? 1 : m_pSplitResults->GetSize(); } CStringArray* CStringCH::GetSplitResults() { // If String could not be splitted because delimiter is not present or // 'SplitString()' wasn't yet called, we just copy the hole string to // the (1st pos.) of the String Array. if (m_pSplitResults->GetSize() == 0) m_pSplitResults->Add(this->GetBuffer(this->GetLength())); return m_pSplitResults; } CMapStringToString* CStringCH::GetSplitResultsMap() { // If String could not be splitted because delimiter is not present or // 'SplitString()' wasn't yet called, we just copy the hole string to // the (1st pos.) of the String Array. if (m_pSplitResults->GetSize() == 0) m_pSplitResults->Add(this->GetBuffer(this->GetLength())); // If map yet wasn't build (after last call to 'SplitString()') // build it now if (m_pSplitResultsMap->GetCount() == 0) { CString sTemp; for (int i = 0; i < m_pSplitResults->GetSize(); ++i) { sTemp = m_pSplitResults->GetAt(i); m_pSplitResultsMap->SetAt(sTemp, sTemp); } } return m_pSplitResultsMap; }

Seitenanfang


Wider dem Blindflug!

Wenn Sie wissen wollen, was so alles auf Ihrem PC passiert!

Lesen Sie dies ...


Einfach - Schnell - Günstig!

Tischrechner als Software.

Jetzt herunterladen und kostenlos testen!

Mehr lesen ...


Effektiver Arbeiten!

Tastenkombinationen können PC-Arbeit erheblich beschleunigen.

Mehr lesen ...


Kleine Helfer für Sie:

Eine Reihe von kostenlosen Online-Berechnungen zur Erleichterung der täglichen Arbeit.

Ausprobieren ...


Wissenswertes!

Sicherheit im PC-Bereich

Es existiert eine kostenlose, einfache und äußert effektive Methode, fast alle Viren, Trojaner, Würmer ...

Mehr lesen ...