[IDE Tool] Add date to source code!

Share your advanced PureBasic knowledge/code with the community.
User avatar
michel51
Enthusiast
Enthusiast
Posts: 290
Joined: Mon Nov 21, 2005 10:21 pm
Location: Germany

Re: [IDE Tool] Add date to source code!

Post by michel51 »

WilliamL wrote:mitchel51,

Your code works! It is a different approach and one that occurred to me also.

Oh, I almost missed that the Arguments line has to be changed to %File %TEMPFILE (I hope that is the right form-no ,;: etc)
Thanks and yes, the arguments are right. You have to write >>"%FILE" "%TEMPFILE" <<. All in UPPERCASE and with quotes (is this correctly ??)
I had added 'EnableExplicit' in my version and had to add several defines and then wanted to run the code to be sure it compiled but wouldn't that change the code?
This will make the code saver and I will do the same :-)
How do you run the Tool to see if it is working.. before you make it into a Tool?

I don't know a way to test a tool as to test it as a tool (crazy sentence :-( )
I run PB parallel to an editor like >TextWrangler<, both started with the code. The tool was compiled an activated, as you have described in sourceheader.
Then I test the tool with saving the code. The Date/Time changes have to be seen in both applications, editor and PB-IDE.
Testing with a source without the sequence "; last modified" will then show the new lines at the beginning of the source.

Sometimes ist is possible to test such a tool with real filenames, like "source.s = filename.pb" and with the whole path and of course with the other needed vars.
That is NOT VIA TOOLSETTINGS.
So you can test and if it is running fine, switch to tool and set the needed arguments.

Additional to the settings I have checked in toolsettings the following:

Wait Until Tool Quits - checked
Reload Source after tool has quit - checked
option - into current source

hope it will be a little bit clearer :-)
michel51

Mac OS X Snow Leopard (10.6.8 ) Intel
PureBasic V 5.21(x64), V 5.22beta
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: [IDE Tool] Add date to source code!

Post by yrreti »

I appreciate you sharing this code.
Looking at your code was interesting to me as I was looking to be able to do an interesting variation of that.
But your code has a big problem in that it adds line feeds each time for each line. The problem is caused by adding
that #CR$ at the end of each WriteStringN , which is not needed because the N of WriteStringN already adds the CR.
Partial code shown. If you comment out the #CR$ at the end, it works fine.

Code: Select all

        pos1 = FindString(string.s, "; last modified:", 1)
         If pos1 = 1    ; yes, this is the line ! And it begins at position 1 !!
           WriteStringN(2, modString.s); + #CR$)    ; write the new values into 2. file
           done = 0
         Else
           WriteStringN(2, string.s); + #CR$)
         EndIf
         
       Until Eof(1)
       
       If done = 1
         FileSeek(1, 0)
         If CreateFile(2, source + ".pb")
           WriteStringN(2, "; ----------------------------------------------------"); + #CR$)
           WriteStringN(2, modstring.s) ;+ #CR$)
           WriteStringN(2, "; ----------------------------------------------------"); + #CR$)
           Repeat
             WriteStringN(2, ReadString(1)); + #CR$)
           Until Eof(1)
         EndIf
         
       EndIf
Now the interesting variation of this idea. I liked the idea of adding the last modified Date and Time to the
beginning part of the code when saved. It's a good future reference when looking at past code.
But I also wanted a way to add it to the finished executable program, so I could call it using a hot key for reference,
if some one using it in the field has a program question. Yes you could put version info in under compiler options, but
a lot of people don't really know how to use explorer to find it. Thus the hot key approach, and it works great!

I used your existing code except for the extra #CR$
But I added the following code after it in which I create a cdt.pb file only containing the string
cdt$="last modified: Date at Time"

The AddDateToSource.exe executable is created in my D:\PureBasic4.5\ directory, and uses the
same tool settings as above.

Code: Select all

dat$="last modified: " + GetDate() + " at " + GetTime(#C12HOUR)
CreateFile(1,"cdt.pb")
WriteString(1,"cdt$="+Chr(34)+dat$+Chr(34))
CloseFile(1)
Then you need to add the following to the programs code that you would like to add this to.

Code: Select all

;Located at first part of your programs code.
ver$="2.0"; add your version info here. 

;include this file which has that one string  cdt$ , with the Date and Time in it.
IncludeFile "D:\PureBasic4.5\cdt.pb" 
;Debug cdt$
;combine all info into a string called  vcdt$
vcdt$="Version: "+ver$+Chr(10)+Chr(10)+"Creation Date & Time: "+Chr(10)+cdt$

;add #ctrlv in your Enumeration list   #ctrlv is Ctrl v
;make sure you use the right window #   mine in this case was 0
;add AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V, #ctrlV) ;vcdt$ view version date time
;to you main window hot keys list. 

;in your main loop add:
    Case #ctrlv
      ;Note ver$ (version) needs to be changed at first line of this code when you need to change versions.
      MessageRequester("Program Name  Version: Date: & Time:", vcdt$ , $C0)
The neat thing about this is that when ever you run the program from the IDE and press Ctrl v , it will display
that info, and it will change each time. But when you compile to an exe for release, its Date and Time is fixed
in the exe, and can be called any time using the hot key. You don't need the cdt.pb file any longer because it's
info has been compiled into your executable when it was compiled to an exe.
It works really great, and is a good add on to your code.
Thanks for sharing your code, and hope this added part is also of use to others on this forum.
Post Reply