Board index » cppbuilder » macro: __LINE__ - do a "sourceline" exists ?

macro: __LINE__ - do a "sourceline" exists ?


2005-04-05 04:38:37 PM
cppbuilder83
hi,
Is there any macro for getting the actual sourceline content ?
My dream would be this kinda macro:
SOURCELINE (__LINE__ - 4)
Which would get the contents of the line - positioned -4 lines from where im
standing, so the result for example looked like this:
"if (OkState() == true)"
I want to save this into a logfile - but is it possible at all ?
 
 

Re:macro: __LINE__ - do a "sourceline" exists ?

danz wrote:
Quote
Is there any macro for getting the actual sourceline content ?

My dream would be this kinda macro:

SOURCELINE (__LINE__ - 4)

Which would get the contents of the line - positioned -4 lines from where im
standing, so the result for example looked like this:

"if (OkState() == true)"

I want to save this into a logfile - but is it possible at all ?
Try this:
AnsiString SOURCELINE ( int linenr )
{
TStringList *StringList = new TStringList;
StringList->LoadFromFile ( __FILE__ );
AnsiString Line = StringList->Strings[linenr - 1];
delete StringList;
return Line;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage ( SOURCELINE ( __LINE__ - 2 ) );
}
Hans.
 

Re:macro: __LINE__ - do a "sourceline" exists ?

danz wrote:
Quote
Is there any macro for getting the actual sourceline content ?
Not that I know.
Some inspiration:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowMessage ( __FUNC__ );
}
Hans.
 

{smallsort}

Re:macro: __LINE__ - do a "sourceline" exists ?

well this wont really work since it should be at compiletime - and
maco-wise, the other thing will force you to deliver the sourcecode with the
.exe - and if changes are made in that file at any time, it wont work.
Hanz:
Quote
Try this:

AnsiString SOURCELINE ( int linenr )
{
TStringList *StringList = new TStringList;

StringList->LoadFromFile ( __FILE__ );

AnsiString Line = StringList->Strings[linenr - 1];

delete StringList;

return Line;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage ( SOURCELINE ( __LINE__ - 2 ) );
}

Hans.
 

Re:macro: __LINE__ - do a "sourceline" exists ?

yep, know the __FUNC__ - but it still only writes the functioname, i need
the specific contents of the sourceline.
"Hans Galema" < XXXX@XXXXX.COM >skrev i en meddelelse
Quote
danz wrote:

>Is there any macro for getting the actual sourceline content ?

Not that I know.

Some inspiration:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowMessage ( __FUNC__ );
}

Hans.
 

Re:macro: __LINE__ - do a "sourceline" exists ?

danz wrote:
Quote
well this wont really work since it should be at compiletime - and
maco-wise,
Why are you insisting on macro-wise ? What would that bring ?
Quote
the other thing will force you to deliver the sourcecode with the
.exe - and if changes are made in that file at any time, it wont work.
What would your customer do with the sourcelines in a logfile then?
Why don't you just write a string to the logfile with an adequate
text ?
May be you have to explain first why you need this.
Hans.
 

Re:macro: __LINE__ - do a "sourceline" exists ?

"Hans Galema">
Quote
Why are you insisting on macro-wise ? What would that bring ?
Well it would give me the textline of a sourceline.....just like __FUNC__
gives me the functionname and so on.
Quote
What would your customer do with the sourcelines in a logfile then?
Im not loggin the complete source :) - Im controlling what lines a want to
show in the logfile if i had a macro, i wouldnt really be able to do that if
i have to supply the complete sourceline.
Quote
Why don't you just write a string to the logfile with an adequate
text ?
Ofcourse this is a way to do it - a macro would have been nicer though -
since i dont have to change things two places, if any changes are present.
You can compare it with __FUNC__ & __LINE__ - you could easily write this in
a text yourself too.
 

Re:macro: __LINE__ - do a "sourceline" exists ?

"danz" < XXXX@XXXXX.COM >wrote in message
Quote
hi,

Is there any macro for getting the actual sourceline content ?

My dream would be this kinda macro:

SOURCELINE (__LINE__ - 4)

Which would get the contents of the line - positioned -4 lines from where
im
standing, so the result for example looked like this:

"if (OkState() == true)"

I want to save this into a logfile - but is it possible at all ?
Yes! It is definitely possible, but you will have to do all the work
yourself...the compiler has no built-in macros that will do it for you.
The keys to solving this problem are
1) Include the text of the source file within the source file itself (so you
don't have to distribute it).
2) Provide a function to read the text at runtime.
Here is a little trick I learned quite a few years ago that is not
intuitively obvious, but does exactly what is necessary to accomplish #1
above. Let's say for example that you had some code like this:
char *filetext[] =
{
"line 1",
"line 2"
}
Obviously, you could read the text, "line 1", by simply accessing
filetext[0], right? So, if you could get the text of the source file into
an array, this problem wouldn't be difficult at all, would it? Ideally, we
would like to do something like this:
char filetext[] =
{
#include __file__
}
But, unfortunately, this will not work, for at least two reasons: First,
the compiler won't let you open a file while it is being compiled. Second,
the file is not in a format the char[] array understands anyway. So, what
you have to do convert the file into a format that can be understood by the
compiler when it is included directly into the source file in this manner.
So, with a specialized tool (which I am posting for you in the attachments
group), we would first convert the file into something the compiler will
accept and understand, and then we can include it in our source file:
At the command line:
bin2cdata SourceFile.cpp>SourceFile.data
Then, in your source file:
// load the contents of the file, "SourceFile.cpp" into an array
char sourcefiledata[] =
{
#include "SourceFile.data"
};
The text of this source file is now accessible from within the source file
itself, through the "sourcefiledata" array. However, since this is nothing
more than an array of characters, it will be necessary to convert it into a
format from which you have access to each individual line. A TStringList is
ideal for this:
std::auto_ptr<TStringList>pSourceStrings( new TStringList );
pSourceStrings->Text = sourcefiledata;
Now every line of your source file is accessible through the
pSourceStrings->Strings[] property, without actually distributing the source
file itself!
#define SOURCELINE( line ) pSourceStrings->Strings[line]
Note that the result is an AnsiString; you will have to apply the c_str()
method if you want a C string instead. (This solution can never result in a
string literal, but you did not identify that as a requirement.)
There are three drawbacks to this solution:
1) Your object file now contains the text of the source (although it is
encoded in binary form, if someone were looking for it, it wouldn't be easy
to find, but it is there nonetheless)
2) Your EXE will be somewhat larger now
3) You must run the source file through the command-line tool every time you
make a change to the source file before you compile it, otherwise the
contents of the data file will be out of sync with the source file (which
may not be important until you actually release)
Whether or not you are willing to live with these drawbacks is a decision
you will have to make, but this solution does what you want.
I have posted my bin2cdata tool to the attachments group. Good luck.
- Dennis