Board index » cppbuilder » TStringList of WideString

TStringList of WideString


2005-07-29 04:23:30 AM
cppbuilder71
Hi guys,
is there anybody who already implement a StringList of WideStrings? I would
need for my application. If not, I will develop myself, but I do not know
where and how to start.
I only need the Add and Clear function and the Text property. That would be
all.
How would you start building such a class?
Greetings
Jorg
 
 

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote in message
Quote
is there anybody who already implement a StringList of WideStrings?
Which version of BCB are you actually using? BCB6 has a TWideStrings class
available in the WSDLIntf unit.
Gambit
 

Re:TStringList of WideString

Quote
Which version of BCB are you actually using? BCB6 has a TWideStrings
class
available in the WSDLIntf unit.
Hi gambit,
i only have bcb5 :(
so i need to write my own class. could you explain me how and where to
start?
greetingsd
jorg
 

{smallsort}

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote in message
Quote
so i need to write my own class. could you explain me how and where to
start?
Use a std::vector of WideStrings. Do you need to read / write the Text
property or just read ?
Jonathan
 

Re:TStringList of WideString

Quote
Use a std::vector of WideStrings. Do you need to read / write the Text
property or just read ?
Hi Jonathan,
at the moment i also need to read the text property, but to write would be
also very interested.
Jorg
 

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote:
Quote

[...] so i need to write my own class.
Just dynamically allocate the WideString(s) and add them to a
TList instead of a TStringList.
~ JD
 

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote in message
Quote
at the moment i also need to read the text property, but to write would
be
also very interested.
If you only need reading the Text property, then it makes a class like this
fairly easy:
// Untested.
class TWideStringList
{
private:
std::vector<WideString>Strings;
WideString __fastcall GetText()
{
WideString Temp;
for( unsigned int i = 0; i < Strings.size(); ++i )
Temp += Strings[i] + "\r\n";
return Temp;
};
public:
TWideStringList() { };
void __fastcall Add( WideString Str ) { Strings.push_back( Str ); };
void __fastcall Clear() { Strings.clear(); };
__property WideString Text = { read = GetText };
};
Jonathan
 

Re:TStringList of WideString

Hi Jonathan,
this looks very good. three more chanages and the class would be perfekt for
me.
1. a write possibility for the text property
2. Count the number of lines
2. a functionality to get each line like
WideString test;
for (int i = 0; i < widestringlist->Count; i++)
test = widestringlist->Strings[i];
i have no experience with std::vector. i hope you can help me there.
Jorg
"Jonathan Benedicto" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote
"Jorg" < XXXX@XXXXX.COM >wrote in message
news:42e95899$ XXXX@XXXXX.COM ...
>at the moment i also need to read the text property, but to write would
>be
>also very interested.

If you only need reading the Text property, then it makes a class like
this
fairly easy:

// Untested.

class TWideStringList
{
private:
std::vector<WideString>Strings;

WideString __fastcall GetText()
{
WideString Temp;
for( unsigned int i = 0; i < Strings.size(); ++i )
Temp += Strings[i] + "\r\n";
return Temp;
};
public:
TWideStringList() { };

void __fastcall Add( WideString Str ) { Strings.push_back( Str ); };
void __fastcall Clear() { Strings.clear(); };

__property WideString Text = { read = GetText };
};

Jonathan


 

Re:TStringList of WideString

On Thu, 28 Jul 2005 22:23:30 +0200, "Jorg" < XXXX@XXXXX.COM >wrote:
Quote
Hi guys,

is there anybody who already implement a StringList of WideStrings? I would
Hi,
have a look at "TntWare Delphi Unicode Controls":
www.tntware.com/delphicontrols/unicode/
They have a TTntWideStrings class.
 

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote in message
Quote
1. a write possibility for the text property

2. Count the number of lines

3. a functionality to get each line like
class TWideStringList
{
private:
std::vector<WideString>FStrings;
unsigned int __fastcall GetCount()
{
return FStrings.size();
}
WideString __fastcall GetString(unsigned int Index)
{
return FStrings[Index];
}
void __fastcall SetString(unsigned int Index, const WideString
&Value)
{
FStrings[Index] = Value;
}
WideString __fastcall GetText()
{
WideString Temp;
int Len = 0;
for(unsigned int i = 0; i < FStrings.size(); ++i)
Len += (FStrings[i].Length() + 2);
Temp.SetLength(Len);
wchar_t *ptr = Temp.c_bstr();
for(unsigned int i = 0; i < FStrings.size(); ++i)
{
WideString &w = FStrings[i];
memcpy(ptr, w.c_bstr(), w.Length() * 2);
ptr += w.Length();
memcpy(ptr, L"\r\n", 4);
ptr += 2;
}
return Temp;
}
void __fastcall SetText(const WideString &Value)
{
Clear();
wchar_t *ptr = Value.c_bstr();
if( ptr != NULL )
{
while( *ptr != 0 )
{
wchar_t *start = ptr;
while( (*ptr != 0) && (*ptr != 10) && (*ptr != 13) )
++ptr;
Add(WideString(start, (ptr - start) / 2));
if( *ptr == 13 ) ++ptr;
if( *ptr == 10 ) ++ptr;
}
}
}
public:
TWideStringList() { }
void __fastcall Add(const WideString &Str)
{
FStrings.push_back(Str);
}
void __fastcall Clear()
{
FStrings.clear();
}
__property unsigned int Count = { read = GetCount };
__property WideString Strings[unsigned int Index] = { read =
GetString, write = SetString };
__property WideString Text = { read = GetText, write = SetText };
};
Gambit
 

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote in message
Quote
Hi Jonathan,

this looks very good. three more chanages and the class would be perfekt
for
me.

1. a write possibility for the text property

2. Count the number of lines

2. a functionality to get each line like

WideString test;
for (int i = 0; i < widestringlist->Count; i++)
test = widestringlist->Strings[i];

i have no experience with std::vector. i hope you can help me there.
Sure. Here's v2. Again untested.
class TWideStringList
{
private:
std::vector<WideString>FStrings;
WideString __fastcall GetText()
{
WideString Temp;
for( unsigned int i = 0; i < FStrings.size(); ++i )
Temp += FStrings[i] + "\r\n";
return Temp;
};
void __fastcall SetText( WideString value )
{
wchar_t *valuePtr = value.c_bstr();
wchar_t *tempPtr = NULL;
WideString Newline = "\r\n";
while( valuePtr )
{
tempPtr = wcsstr( valuePtr, Newline.c_bstr() );
if( tempPtr == NULL )
break;
Add( WideString( valuePtr, tempPtr - valuePtr ) );
valuePtr = tempPtr + 1;
};
if( valuePtr )
Add( WideString( valuePtr ) );
};
int __fastcall GetCount() { return FStrings.size(); };
public:
TWideStringList() { };
void __fastcall Add( WideString Str ) { FStrings.push_back( Str ); };
void __fastcall Clear() { FStrings.clear(); };
__property WideString Text = { read = GetText, write = SetText };
__property int Count = { read = GetCount };
__property std::vector<WideString>Strings = { read = FStrings };
};
Jonathan
 

Re:TStringList of WideString

"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
Quote
>1. a write possibility for the text property
>i have no experience with std::vector. i hope you can help me there.

Sure. Here's v2. Again untested.
I didn't mention that although v2 has a __property std::vector<WideString>,
this is so you can do:
WideStringList->Strings[8] = "Test";
Jonathan
 

Re:TStringList of WideString

Hi gambit,
before i start i already installed the tntware unicode compontents:
www.tntware.com/delphicontrols/unicode/
I load a XML file into the TntRichEdit. From there I load the text into the
text property of the TWideStringList class.
widestringlist->Text = tntrichedit->Text;
When I traverse the TWideStringList now
for (int i = 0; i < widestringlist->Count; i++)
ShowMessage(widestringlist->Strings[i]);
i do not always get the full line. Sometimes there are some letters or words
missing. In this example there was only an easy ansi text. could this be a
problem with the TntRichEdit->LoadFromFile method.
perhabs it is besser to implement a LoadFromFile and also SaveToFile method
to the TWideStringList
Jorg
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote

"Jorg" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>1. a write possibility for the text property
>
>2. Count the number of lines
>
>3. a functionality to get each line like

class TWideStringList
{
private:
std::vector<WideString>FStrings;

unsigned int __fastcall GetCount()
{
return FStrings.size();
}

WideString __fastcall GetString(unsigned int Index)
{
return FStrings[Index];
}

void __fastcall SetString(unsigned int Index, const WideString
&Value)
{
FStrings[Index] = Value;
}

WideString __fastcall GetText()
{
WideString Temp;
int Len = 0;

for(unsigned int i = 0; i < FStrings.size(); ++i)
Len += (FStrings[i].Length() + 2);

Temp.SetLength(Len);
wchar_t *ptr = Temp.c_bstr();

for(unsigned int i = 0; i < FStrings.size(); ++i)
{
WideString &w = FStrings[i];
memcpy(ptr, w.c_bstr(), w.Length() * 2);
ptr += w.Length();
memcpy(ptr, L"\r\n", 4);
ptr += 2;
}

return Temp;
}

void __fastcall SetText(const WideString &Value)
{
Clear();
wchar_t *ptr = Value.c_bstr();
if( ptr != NULL )
{
while( *ptr != 0 )
{
wchar_t *start = ptr;
while( (*ptr != 0) && (*ptr != 10) && (*ptr != 13) )
++ptr;
Add(WideString(start, (ptr - start) / 2));
if( *ptr == 13 ) ++ptr;
if( *ptr == 10 ) ++ptr;
}
}
}

public:
TWideStringList() { }

void __fastcall Add(const WideString &Str)
{
FStrings.push_back(Str);
}

void __fastcall Clear()
{
FStrings.clear();
}

__property unsigned int Count = { read = GetCount };
__property WideString Strings[unsigned int Index] = { read =
GetString, write = SetString };
__property WideString Text = { read = GetText, write = SetText };
};


Gambit


 

Re:TStringList of WideString

"Jorg" < XXXX@XXXXX.COM >wrote in message
Quote
I load a XML file into the TntRichEdit. From there I load the text into
the
text property of the TWideStringList class.
If all you are using the TntRichEdit for is to load the TWideStringList, and
not for any visual purposes as well, then that is an abuse of a visual
control to perform a non-visual operation. It would be a lot easier to just
add a LoadFromFile() method to the TWideStringList class instead.
Also, I should mention that XML is typically not encoded in Unicode to begin
with. The default encoding is UTF-8 instead, which is compatible with
ASCII. You can use a normal TStringList to load a UTF-8 encoded XML file.
Gambit
 

Re:TStringList of WideString

"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
Quote
WideString __fastcall GetText()
{
WideString Temp;
for( unsigned int i = 0; i < FStrings.size(); ++i )
Temp += FStrings[i] + "\r\n";
return Temp;
};
That is very inefficient. The more strings there are, the more memory
allocations are performed. See my other reply, where I changed the code to
calculate the needed size so that a single memory allocation is performed.
Quote
void __fastcall SetText( WideString value )
Also, it is very inefficient to pass WideString by value in a function
parameter. WideString is not reference-counted like AnsiString is. Every
time you pass WideString around, a copy is made. By passing the value by
const reference instead (also shown in my previous reply), the original
WideString value can be used directly without any copy made.
Quote
void __fastcall Add( WideString Str ) { FStrings.push_back( Str ); };
Again, a copy a made.
Gambit