1 (edited by JohnWasilewski 2012-05-24 21:07:37)

Topic: Option to recognise a date string in source code , and auto-update it

Jeff,

My projects generally
(i) show the program version numbers in the code and
(ii) contain code to display program version no. and version date in the  program output.
To achieve this, I have to enter the date I wish the program to display, as a text string, in the source code.

EXAMPLE

Code fragment
      PROGRAM INSTRUCT
C    ---------------------
C    Last amended 27.04.12
      :
      USE DISLIN
      :
      :
      PARAMETER (PROG8  = 'INSTRUCT')
      PARAMETER (VERS4  = '1.20')
      PARAMETER (PDAT8  = '27.04.12')
      PARAMETER (DESC24 = 'Plane frame analysis')
      PARAMETER (AUTH16 = 'John Wasilewski')
      PARAMETER (CPRT16 = '(c) JW 2004-2012')
      :
      :
      CALL SWGTXT(idSCR,'vers. '  //VERS4//' :   '//PDAT8)
      CALL SWGTXT(idSCR,'vers. '  //VERS4//' :   '//PDAT8)

C    (note: SWGTXT is a DISLIN routine to display a text string)

This produces program output thus:

Program INSTRUCT
vers. 1.20 : 


Can you find a way of having the editor generate and the current date WITHIN THE source code?
It would need to display the date as a text string within the source code line, exactly as if typed in, but with the date string actually being a date-text field, not a simple text string.

Important: the date field would need to auto-update only in certain precise circumstances, namely,
(1) whenever any source file in the project is amended and saved.
(2) whenever the project structure is re-saved.
(3) whenever the project is re-built.

BUT not merely
(4) because another day has passed and the date has changed
(5) when printing.

What I was thinking of would cause the above example to look exactly the same as above, but the two locations in the code where the date "27.04.12" appears, would actually have been entered in the source code as date-string field, not typed in, and would automatically update according to the above rules.  They would of course need to be visually identifiable to someone reading the code, as fields not source code text.  This could be done by either a text colour or, perhaps, a background colour.  Another idea would be by having the text appear in italics. 

Another idea would be just to provide a Simply Fortran option that recognises a date string in a certain format as an updating field.  I like this idea the most because the source code really would then just be a text file, making it portable to other compilers.
---
John

Re: Option to recognise a date string in source code , and auto-update it

Jeff, you don't seem ever to have commented on this request.
I can see that it would be a bit unusual. 
Is it not something you think worth exploring?
---
John

Re: Option to recognise a date string in source code , and auto-update it

John,

I started looking into this a bit, but I think there is a simpler situation.  One thing you can do is add the "-cpp" flag to the Fortran compiler flags, which will cause the Fortran source code to be passed through the C preprocessor.  The C preprocessor has a predefined macro __DATE__.  This macro will be converted to the date at compilation time in the form "Feb 15 2013" when encountered.  A sample bit of code appears below:

program helloworld
implicit none

    Print *, __DATE__
    
end program helloworld

The program will simply output the date as long as "-cpp" is added to the Fortran compiler flags.

Alternatively, if the file has a uppercase extension, like .F or .F03, the compiler flag isn't necessary; these files will always be passed through the C preprocessor.

Some other useful macros can be found at:

http://gcc.gnu.org/onlinedocs/cpp/Prede … ned-Macros

Jeff Armstrong
Approximatrix, LLC

Re: Option to recognise a date string in source code , and auto-update it

Sounds a really neat trick Jeff but I don't know enough about C to make it work.

eg I put the -cpp flag in the compilet options and then tried this:



      CHARACTER DateToday*9, DateCompiled*11
      EXTERNAL DisplayBanner, Read_Eng

      DateCompiled = __DATE__  ! Invokes a macro in the C language
                                             ! as long as compiler flag -cpp is set.
                                             ! Date will be in the form, "Feb 15 2013".
      PROG8  = 'INSTRUCT'
      VERS4   = '1.20'
C     PDAT8  = '21.02.13'
      PDAT8   = DateCompiled(5:6)//' '//
     +              DateCompiled(1:2)//' '//
     +              DateCompiled(10:11)

      DESC24 = 'Plane frame analysis'
      AUTH16 = 'John Wasilewski'
      CPRT17  = '(c) 2004-2013 Jmw'





It fails to compile because Fortran doesn't recognise
The compilation error is this:

"C:\Program Files (x86)\FORTRAN\SF\mingw-w64\bin\gfortran.exe" -c -o "build\INSTRUCT.o" -g -IG:/InStruct120/../MODULES -IG:/InStruct120/../INCLUDE -fdollar-ok -Jmodules ".\Source\INSTRUCT.for"
.\Source\INSTRUCT.for:229.22:

      DateCompiled = __DATE__  ! Invokes a macro in the C language     
                      1
Error: Invalid character in name at (1)
Error(E42): Last command making (build\I.NSTRUCT.o) returned a bad status
Error(E02): Make execution terminated

Further guidance would be a great help.

---
John

Re: Option to recognise a date string in source code , and auto-update it

John,

Just glancing at your compiler output, I can see that the -cpp option isn't in your compiler line.  Which options did you add it to?  I added it to "Compiler Flags" under "Fortran" in the Compiler Options window.  Alternatively, it should work if you add it to the Fortran Compiler flags in Project Options.  Also make sure your Makefile is set to be regenerated.

Jeff Armstrong
Approximatrix, LLC

Re: Option to recognise a date string in source code , and auto-update it

I've just tried again to compile.
I list at the end the options I had selected, which were already in the project file as saved.

Here, first, is the compilation output:

==============================================================================
Open Watcom Make Version 1.9 (Built on Feb  4 2013)
Portions Copyright (c) 1988-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
    "C:\Program Files (x86)\FORTRAN\SF\mingw-w64\bin\gfortran.exe" -c -o "build\INSTRUCT.o" -g -IG:/InStruct120/../MODULES -IG:/InStruct120/../INCLUDE -fdollar-ok -Jmodules ".\Source\INSTRUCT.for"
.\Source\INSTRUCT.for:230.22:

      DateCompiled = __DATE__  ! Invokes a macro in the C language     
                      1
Error: Invalid character in name at (1)
Error(E42): Last command making (build\INSTRUCT.o) returned a bad status
Error(E02): Make execution terminated

* Complete *


Relevant options set are (I am copying and pasting here:

COMPILER
Fortran compiler flags: -cpp
C compiler flags: -cpp

PROJECT
Project flags
Fortran compiler: -fdollar-ok -cpp
C compiler: -cpp
Linker: -luser32 -lgdi32 -lopengl32

I have tried many combinations of where to put  '-cpp', as a compiler flag, a project flag, in Fortran, then C, then Fortran and C. 

I can see that '-cpp' isn't in the gfortran compiler line output report, but it IS in the options I selected.
I can't offer any explanation for this.
----
John

Re: Option to recognise a date string in source code , and auto-update it

To all:

Jeff has point out to me,

It appears that "Always Regenerate Makefile" is still disabled in your
project.  It should work if you re-enable it from the Project Options
dialog.  Alternatively, you can select "Generate Makefile" from the
Build menu directly.

Thanks Jeff.  I really must check everything more carefully!
---
John

Re: Option to recognise a date string in source code , and auto-update it

This works perfectly.  It is a great way to generate a date string which can be used to display in program output, next to teh program version number, the build date.  many thanks, Jeff.
---
John