Board index » cppbuilder » macro: __LINE__ - do a "sourceline" exists ?
danz
![]() CBuilder Developer |
danz
![]() CBuilder Developer |
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 ? |
Hans Galema
![]() CBuilder Developer |
2005-04-05 05:30:09 PM
Re:macro: __LINE__ - do a "sourceline" exists ?
danz wrote:
QuoteIs there any macro for getting the actual sourceline content ? { 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. |
Hans Galema
![]() CBuilder Developer |
2005-04-05 05:42:37 PM
Re:macro: __LINE__ - do a "sourceline" exists ?
danz wrote:
QuoteIs there any macro for getting the actual sourceline content ? void __fastcall TForm1::Button2Click(TObject *Sender) { ShowMessage ( __FUNC__ ); } Hans. {smallsort} |
danz
![]() CBuilder Developer |
2005-04-05 07:19:31 PM
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: QuoteTry this: |
danz
![]() CBuilder Developer |
2005-04-05 07:20:05 PM
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 Quotedanz wrote: |
Hans Galema
![]() CBuilder Developer |
2005-04-05 08:16:04 PM
Re:macro: __LINE__ - do a "sourceline" exists ?
danz wrote:
Quotewell this wont really work since it should be at compiletime - and Quotethe other thing will force you to deliver the sourcecode with the text ? May be you have to explain first why you need this. Hans. |
danz
![]() CBuilder Developer |
2005-04-07 03:19:54 PM
Re:macro: __LINE__ - do a "sourceline" exists ?
"Hans Galema">
QuoteWhy are you insisting on macro-wise ? What would that bring ? QuoteWhat would your customer do with the sourcelines in a logfile then? i have to supply the complete sourceline. QuoteWhy don't you just write a string to the logfile with an adequate You can compare it with __FUNC__ & __LINE__ - you could easily write this in a text yourself too. |
Dennis Jones
![]() CBuilder Developer |
2005-04-09 02:24:22 AM
Re:macro: __LINE__ - do a "sourceline" exists ?
"danz" < XXXX@XXXXX.COM >wrote in message
Quotehi, 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 |