 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Olives Guest
|
Posted: Mon Apr 09, 2007 5:25 am Post subject: Extracting Text isn't working right :( |
|
|
I'm having some trouble getting the information from a text file I'm using
as a config file.
The config file is loaded into a TStringList
--- config.cfg (text file ---
#ProgramInfo is used to determine what program and version this
configuration is for.
<ProgramInfo>
ProgramName=Image Creator
MajorVersion=1
MinorVersion=0
ReleaseVersion=0
BuildVersion=10
</ProgramInfo>
#ProgramFiles contains all the files in the Install folder.
#%InstallDir% is a variable to the location of where the program was
installed.
<ProgramFiles>
%InstallDir%\Image Creator.exe
</ProgramFiles>
#SystemFiles contains special files that need to be stored in the windows
system folder.
#%System32% is a variable to the location of the Windows System folder.
<SystemFiles>
%System32%\somefile.dll
</SystemFiles>
#Registry contains all the Registry entries for the program;
<Registry>
HKEY_LOCAL_MACHINE\Software\MicroMind\Image Creator\Version
HKEY_LOCAL_MACHINE\Software\MicroMind\Image Creator\Install Path
HKEY_LOCAL_MACHINE\Software\MicroMind\Image Creator\Install Date
</Registry>
--- end ---
I removed the comment and blank lines using this function
TStringList *TMain::StripComment(TStringList *List, AnsiString ComChar)
{
// Remove Lines starting with the ComChar.
for(int i=0; i<List->Count; i++) {
AnsiString Line = List->Strings[i];
AnsiString CurChar = Line.SubString(0,1);
if(CurChar == ComChar) List->Delete(i);
}
return List;
}
// Loaded File
File->LoadFromFile(ConfigFile);
if(FileExists(ConfigFile)) {
// Remove Empty or comment lines.
for(int i=0; i<File->Count; i++) {
AnsiString Line = File->Strings[i];
AnsiString Char = File->Strings[i].SubString(0,1);
// Blank Lines
StripComment(File, "");
// Comment Lines
StripComment(File, "#");
AnsiString openTag = "<SystemFiles>";
AnsiString closeTag = "</SystemFiles>";
int Start = 0;
int End = 0;
Start = File->Text.AnsiPos(openTag) + openTag.Length();
End = File->Text.AnsiPos(closeTag) - closeTag.Length();
// Get Search Data
File->Text = File->Text.SubString(Start, End);
ShowMessage(File->Text);
}
The probelm i'm having is that i'm getting this:
%System32%\somefile.dll
</SystemFiles>
<Registry>
HKEY_LOCAL_MACHINE\Software\MicroMind\Image Creator\Version
HKEY_LOCAL_MACHINE\Software\MicroMind\Image Creator\Install Path
HKEY_LOCAL_MACHINE\Software\MicroMind\Image Creator\Install Date
</Registry>
When I want this:
%System32%\somefile.dll
I've also tried search for the text my self using a for loop but I get the
same results.
I must be doing something wrong but I just can't seem to find the answer.
Thanks |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Apr 09, 2007 8:10 am Post subject: Re: Extracting Text isn't working right :( |
|
|
"Olives" <junkmail78 (AT) charter (DOT) net> wrote in message
news:H_fSh.39$Xn5.33 (AT) newsfe03 (DOT) lga...
| Quote: | --- config.cfg (text file ---
|
That looks like a pseudo-XML file. Why not use real XML? Or at least
use a real INI file instead. Either way, BCB has classes/components
available for working with INI and XML files.
| Quote: | I removed the comment and blank lines using this function
|
AnsiString is 1-indexed, not 0-indexed.
Also, because you are looping front to back, each time you remove an
item, your loop will skip an item. You need to loop from back to
front instead to prevent that.
| Quote: | // Loaded File
File->LoadFromFile(ConfigFile);
if(FileExists(ConfigFile)) {
|
That is redundant. If the file did not exist, LoadFromFile() would
fail and an exception would be thrown.
| Quote: | // Remove Empty or comment lines.
|
You are calling StripComment() on every iteration of a loop. That is
a lot of unnecessary overhead. You should be calling it outside of
the loop, since you only need to remove the comments/blanks once. The
loop then processes whatever is left.
| Quote: | Start = File->Text.AnsiPos(openTag) + openTag.Length();
End = File->Text.AnsiPos(closeTag) - closeTag.Length();
// Get Search Data
File->Text = File->Text.SubString(Start, End);
|
That is very inefficient. Also, your End index is wrong. You should
not be subtracting the closeTag's length like that. Also, you are
passing an index to the second parameter of SubString() when it is
expecting a length instead.
| Quote: | The probelm i'm having is that i'm getting this:
|
As you should be, because all of your parsing is completely wrong in
general.
Try this instead:
// you did not say which version of BCB you are using,
// so I am giving you code that works in all versions...
void TMain::StripCommentsAndBlanks(TStrings *List)
{
for(int i = List->Count-1; i >= 0; --i)
{
AnsiString s = List->Strings[i];
if( (s.Length() > 0) && (s[1] != '#') )
continue;
List->Delete(i);
}
}
int FindLine(TStrings *List, const AnsiString &Str)
{
for(int i = 0; i < List->Count; ++i)
{
if( AnsiSameText(List->Strings[i], Str) )
return i;
}
return -1;
}
{
// Loaded File
if( FileExists(ConfigFile) )
{
File->LoadFromFile(ConfigFile);
StripCommentsAndBlanks(File);
int Index = FindLine(File, "<SystemFiles>");
if( Index != -1 )
{
for(int i = Index; i >= 0; --i)
File->Delete(i);
}
Index = FindLine(File, "</SystemFiles>");
if( Index != -1 )
{
for(int i = File->Count-1; i >= Index; --i)
File->Delete(i);
}
ShowMessage(File->Text);
}
}
Now, with that said, because of the way your file is laid out, I would
suggest an alternative implementation that is more generic, as I am
assuming that you will eventually want to extract values from all of
the available file sections, ie:
void GetSectionValues(TStrings *List, const AnsiString &Section,
TStrings *Values)
{
AnsiString OpenTag = "<" + Section + ">";
AnsiString CloseTag = "</" + Section + ">";
Values->Clear();
for(int i = 0; i < List->Count; ++i)
{
if( AnsiSameText(List->Strings[i], OpenTag) )
{
for(int j = i+1; j < List->Count; ++j)
{
AnsiString s = List->Strings[j];
if( (s.Length() > 0) && (s[1] != '#') )
{
if( AnsiSameText(s, CloseTag) )
break;
Values->Add(s);
}
}
return;
}
}
}
{
// Loaded File
if( FileExists(ConfigFile) )
{
File->LoadFromFile(ConfigFile);
TStringList *Values = new TStringList;
GetSectionValues(Files, "ProgramInfo", Values);
ShowMessage(Values->Text);
GetSectionValues(Files, "ProgramFiles", Values);
ShowMessage(Values->Text);
GetSectionValues(Files, "SystemFiles", Values);
ShowMessage(Values->Text);
GetSectionValues(Files, "Registry", Values);
ShowMessage(Values->Text);
delete Values;
}
}
Gambit |
|
| Back to top |
|
 |
Olives Guest
|
Posted: Tue Apr 10, 2007 8:11 am Post subject: Re: Extracting Text isn't working right :( |
|
|
I don't really know much about xml so I guess I need to do some research and
learn it.
I found the TXMLDocument component so I guess i'll try using that from now
on.
Your code and comments helpped me a lot and I did get it to work fine now
Thanks for all your help.
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4619e5c2$1 (AT) newsgroups (DOT) borland.com...
| Quote: | That looks like a pseudo-XML file. Why not use real XML? Or at least
use a real INI file instead. Either way, BCB has classes/components
available for working with INI and XML files. |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|