Page 1 of 2
[IDE Tool] Add date to source code!
Posted: Sun Feb 27, 2011 9:31 am
by Nituvious
I wrote a neat little tool after being inspired srod's asm tool.
To configure, compile program with whatever name you wish then click Tools > Configure Tools > Click New
Now edit to something similar:
Commandline: C:\Tools\MyToolsName.exe
Arguments: %FILE
Name: Whatever you want
Event to trigger the tool:
Sourcecode saved
To prevent an annoying console from popping up every save, I have Run Hidden checked
Code modifications have been created in below posts. Check them out if you use this tool!
Code: Select all
; ---------------------------------------------------------------------------------------------
; author: nituvious
; notes:
; This program will try to modify your source code by adding the date
; upon saving the file. If it finds a date recorded already, it will attempt
; to overwrite it with the current date and time.
; Otherwise it will automatically add the date at the beginning of the file.
; This program includes fully changable date/time functions as well.
;
; Please let me know of any bugs!
;
; ---------------------------------------------------------------------------------------------
; last modified: February 27th, 2011 @ 3:24:05 AM
; ---------------------------------------------------------------------------------------------
#C12HOUR = 12 ; silly constants, trying to pretend to be a clock!
#C24HOUR = 24
Procedure.s GetDate()
gMonth.b = Month(Date()) : gDay.b = Day(Date()) : gYear.i = Year(Date())
Dim monthName.s(12) ; set each month by it's english name into an array
; I am not aware of any native PB function that formats the date with a string
monthName.s(0) = "none" ; <-- the array should only have 11 elements, but for sake of simplicity(my favorite thing in the world) it will have 12 elements instead
monthName.s(1) = "January"
monthName.s(2) = "February"
monthName.s(3) = "March"
monthName.s(4) = "April"
monthName.s(5) = "May"
monthName.s(6) = "June"
monthName.s(7) = "July"
monthName.s(8) = "August"
monthName.s(9) = "September"
monthName.s(10) = "October"
monthName.s(11) = "November"
monthName.s(12) = "December" ; <-- my favorite month
; give each day ending with 1, 2, 3, 4... 23,24 etc their proper ordinal suffix
If (Val(Mid(Str(gDay.b),Len(Str(gDay.b)))) = 1) : ordSuffix.s = "st"
ElseIf (Val(Mid(Str(gDay.b),Len(Str(gDay.b)))) = 2) : ordSuffix.s = "nd"
ElseIf (Val(Mid(Str(gDay.b),Len(Str(gDay.b)))) = 3) : ordSuffix.s = "rd"
Else : ordSuffix.s = "th" : EndIf
gDate.s = monthName.s(gMonth.b) + " " + Str(gDay.b)+ ordSuffix.s + ", " + Str(gYear.i)
ProcedureReturn gDate.s
EndProcedure
Procedure.S GetTime(const)
; depending on user settings, should this date be displayed as a 24 hour clock, or 12 hour?
; format 12 hour clock...
gHour.i = Hour(Date())
If const = #C12HOUR ; this is my crappy time conversion function which will add meridiem if the 12 hour clock is used
If gHour.i >= 13
gHour_n.i = gHour.i - 12
gMeridiem.s = "PM"
Else
If gHour.i = 0
gHour_n = 12
EndIf
gHour_n.i = gHour.i
gMeridiem.s = "AM"
EndIf
gTime.s = Str(gHour_n.i) + ":" + FormatDate("%ii:%ss",Date()) + " " + gMeridiem.s
ProcedureReturn gTime.s
ElseIf const = #C24HOUR
gTime.s = FormatDate("%hh:%ii:%ss",Date())
ProcedureReturn gTime.s
EndIf
EndProcedure
Procedure AddDateToSource(modString.s)
source.s = ProgramParameter(0)
file = OpenFile(#PB_Any,source.s)
If file <> 0
While Eof(file) = 0
string$ = ReadString(file)
bufferSize = Lof(file) - Loc(file)
If FindString(string$, "; last modified: ",1) ; all i am doing here is looking for the "last modified" string, if it's found then I add date in it's location
*bufferFile = AllocateMemory(bufferSize)
location = Loc(file) - Len(string$) - 2 ; -- Why does it require len - 2?? beginning/ending of line maybe?
ReadData(file, *bufferFile,bufferSize)
FileSeek(file, location)
TruncateFile(file)
WriteStringN(file, modString.s)
WriteData(file, *bufferFile, bufferSize)
CloseFile(file)
ProcedureReturn 1 ; done, exit loop and return
EndIf
Wend
; I can't seem to think of any other way to reset location to the beginning of the file after reaching the EoF
; no entry found, so add one at the beginning of the file
bufferSize = Lof(file)
FileSeek(file,0) ; <-- go back to beginning of the file
*bufferFile = AllocateMemory(bufferSize)
ReadData(file, *bufferFile, bufferSize)
FileSeek(file,0) ; <-- go back again after copying everything
TruncateFile(file)
WriteStringN(file,modString.s)
WriteData(file,*bufferFile,bufferSize)
CloseFile(file)
; all done :-(
EndIf
EndProcedure
AddDateToSource("; last modified: " + GetDate() + " @ " + GetTime(#C12HOUR))
Would love to get some tips on how I could improve the code!
Re: [IDE Tool] Add date to source code!
Posted: Mon Feb 28, 2011 12:28 am
by WilliamL
[deleted]
Re: [IDE Tool] Add date to source code!
Posted: Mon Feb 28, 2011 10:53 am
by Nituvious
WilliamL wrote:...on a Mac
I get an 'Invalid memory access.' in line 47 (ProcedureReturn gDate.s). If I remove the Dim monthName.s(12) from the Procedure and make it a Global Dim monthName.s(12) it runs without error. I can't imagine why I get this error! (we'll have to ask Fred

)
Now to see if it is modifying my code...
Well that's weird! I originally used Static Dim monthName.s(12), maybe that is the reason it's throwing memory errors out?
I've only tested my code on Windows 7 x86 with PB 4.51. I've used it for several days on single file and a multi-file project with no screw ups. I'm happy to know it works(kinda) on Mac though! I wonder how it fairs against the likes of Linux?
Re: [IDE Tool] Add date to source code!
Posted: Mon Feb 28, 2011 11:31 am
by HeX0R
I would do this GetDate()-Procedure more like this:
Code: Select all
Procedure.s GetDate()
Protected Result.s, gMonth, gDay.s, gYear.s, monthNames.s
gMonth = Month(Date())
gDay = Str(Day(Date()))
gYear = Str(Year(Date()))
monthNames = "January|February|March|April|May|June|July|August|September|October|November|December"
; give each day ending with 1, 2, 3, 4... 23,24 etc their proper ordinal suffix
If gDay = "1"
gDay + "st"
ElseIf gDay = "2"
gDay + "nd"
ElseIf gDay = "3"
gDay + "rd"
Else
gDay + "th"
EndIf
Result = StringField(monthNames, gMonth, "|") + " " + gDay + ", " + gYear
ProcedureReturn Result
EndProcedure
And bytes or words only make sense in structures and/or huge arrays/lists, where you want to keep the amount of memory low.
For anything else just use .i (or nothing).
Re: [IDE Tool] Add date to source code!
Posted: Mon Feb 28, 2011 11:16 pm
by Nituvious
HeX0R wrote:I would do this GetDate()-Procedure more like this:
Code: Select all
Procedure.s GetDate()
Protected Result.s, gMonth, gDay.s, gYear.s, monthNames.s
gMonth = Month(Date())
gDay = Str(Day(Date()))
gYear = Str(Year(Date()))
monthNames = "January|February|March|April|May|June|July|August|September|October|November|December"
; give each day ending with 1, 2, 3, 4... 23,24 etc their proper ordinal suffix
If gDay = "1"
gDay + "st"
ElseIf gDay = "2"
gDay + "nd"
ElseIf gDay = "3"
gDay + "rd"
Else
gDay + "th"
EndIf
Result = StringField(monthNames, gMonth, "|") + " " + gDay + ", " + gYear
ProcedureReturn Result
EndProcedure
And bytes or words only make sense in structures and/or huge arrays/lists, where you want to keep the amount of memory low.
For anything else just use .i (or nothing).
Thanks for the tip! And the GetDate() function, it looks really good! I never even thought about StringGadget(). The only problem is that it breaks the ordinal suffix at 21th, 22th, 23th, etc. After inspecting my original code I realized that my original getdate function is also somewhat broken at 11st, 12nd, 13rd.
Man that is actually an excellent use of StringGadget()! I can't get over how efficient that is! Thanks again!
Re: [IDE Tool] Add date to source code!
Posted: Mon Feb 28, 2011 11:50 pm
by LuCiFeR[SD]
which is easy enough to fix eh?
Code: Select all
Procedure.s GetDate()
Protected Result.s, gMonth, gDay.s, gYear.s, monthNames.s
gMonth = Month(Date())
gDay = Str(Day(Date()))
gYear = Str(Year(Date()))
monthNames = "January|February|March|April|May|June|July|August|September|October|November|December"
; give each day ending with 1, 2, 3, 4... 23,24 etc their proper ordinal suffix
Select gDay
Case "1", "21","31"
gDay + "st"
Case "2","22"
gDay + "nd"
Case "3","23"
gDay + "rd"
Default
gDay + "th"
EndSelect
Result = StringField(monthNames, gMonth, "|") + " " + gDay + ", " + gYear
ProcedureReturn Result
EndProcedure
Debug GetDate()
[Edit] Missed a little bit of code out. Now corrected.
Re: [IDE Tool] Add date to source code!
Posted: Tue Mar 01, 2011 7:49 pm
by WilliamL
Ok, it works on the Mac.
If the "; last modified: " string is there in the beginning of the program, with the date, every thing is fine. If the string is not there then it finds it anyway in the line 'If FindString(string$, "; last modified: ",1) ' and that screws everything up.
So it appears to only work if the date is already there.
If I take out the 'While Eof(file)' loop, so that I just get a date at the first line, I get the date at the last line deleting the 'AddDateToSource(' line.
Re: [IDE Tool] Add date to source code!
Posted: Thu Mar 03, 2011 11:17 pm
by michel51
WilliamL wrote:Ok, it works on the Mac.
That' right, it works fine
If the "; last modified: " string is there in the beginning of the program, with the date, every thing is fine. If the string is not there then it finds it anyway in the line 'If FindString(string$, "; last modified: ",1) ' and that screws everything up.
So it appears to only work if the date is already there.
That's right too, but no problem (for me

)
I use this line within the source header and the date + time is set correctly.
Code: Select all
; ----------------------------------------------------
; last modified: March 3rd, 2011 @ 22:40:35
; ----------------------------------------------------
but the tool trigger seems to be buggy !
The tool works, if I use the menu entry (tools / toolname). The new Date/time is set and the source in IDE is changed.
But then the source is marked as "not saved".
If I saving the code source with <cmd><S>, the date/time is written into the source file.
This I can see if the code is opened with a texteditor.
But the source in IDE is NOT changed.
The tool settings are: Sourcecode saved, wait until tool quits (checked), reloadf source after tool has quit (checked) - into current source.
This is the only problem I have.
Hope anyone can solve the problem.
Re: [IDE Tool] Add date to source code!
Posted: Thu Mar 03, 2011 11:30 pm
by WilliamL
@michel51,
That sounds like something I said in this bug report
http://www.purebasic.fr/english/viewtop ... 23#p347946 . Maybe you have found a work around.
Re: [IDE Tool] Add date to source code!
Posted: Fri Mar 04, 2011 12:38 am
by michel51
@williamL,
you're right, is the same problem.
I tried a lot of different settings, always the date/time is written into the file, but the source in IDE is not changed.
Some time ago i've built a tool to format the code in IDE.
There I used the following code line
Code: Select all
RunProgram("open", Chr(34) + source + ".pb" + Chr(34), "", #PB_Program_Wait)
That worked as expected.
The tool is needed no longer because the function is implemented now.
But in this PB version the code is not running. Don't know why.
Re: [IDE Tool] Add date to source code!
Posted: Fri Mar 04, 2011 9:34 am
by Nituvious
Hi WilliamL!
You say that the program doesn't work if the code modified comment is not already found in the source code? That's really strange! The code seems to work perfectly on Windows. Every now and then there is a jitter if the code is apart of a project and two opened sources have been edited before saving. This doesn't cause any harm, though.
Does the program(on Mac) not copy the code from the top of the file, add the new line and then write the copied lines back to the source file? Has it ever deleted your entire source code? I've never had any of my source deleted and I constantly make changes while smashing F5.
If you are testing the tool with the tool source code while it does not have a date, the tool will modify the procedure as it finds the comment it is looking for there. The tool will also remove anything found inside of the commented line. So if the line is:
Code: Select all
myVariable.i = 1 ; last modified: March 4th, 2011 @ 3:36:25 AM
myVariable.i = 1 will be deleted from the line.
The tool does not reload the source code so that date changes can be seen. However, simply closing the IDE and then reloading at a different time will display the date changes. I'm not aware of any possible ways to have a tool reload the source code.

Re: [IDE Tool] Add date to source code!
Posted: Fri Mar 04, 2011 6:13 pm
by WilliamL
Nituvious,
You bring up a good point about using the 'Add date' source code as a test. Maybe another source that couldn't contain the last modified string would give a better understanding of what is happening.
I have to admit, trying to create a tool and modify it then run it becomes confusing and prone to false results. I will look at the code again with a different source.
[later]
Using a different source, one not containing the last modified string, the 'Add date' tool worked correctly and added the last modified string as the first line of code.
Re: [IDE Tool] Add date to source code!
Posted: Sat Mar 05, 2011 10:51 am
by michel51
WilliamL wrote:Nituvious,
You bring up a good point about using the 'Add date' source code as a test. Maybe another source that couldn't contain the last modified string would give a better understanding of what is happening.
I have to admit, trying to create a tool and modify it then run it becomes confusing and prone to false results. I will look at the code again with a different source.
[later]
Using a different source, one not containing the last modified string, the 'Add date' tool worked correctly and added the last modified string as the first line of code.
I agree ! The tool works as expected.
@Nituvious
Thanks for sharing.
Re: [IDE Tool] Add date to source code!
Posted: Sun Mar 06, 2011 7:23 pm
by michel51
Hello Nituvious, hello WilliamL
after testing a long time I found another way to change Date and Time like Nituvious idea.
Here the procedure. Replace with the corresponding in Nituvious code.
Code: Select all
Procedure AddDateToSource(modstring.s) ; New routine, FileName "AddDateToSource", Toolname: "AddDateToSource"
Protected done
done = 1
source.s = ProgramParameter(0)
tempsource.s = ProgramParameter(1)
If ReadFile(1, source) ; open file for reading
If CreateFile(2, source + ".pb") ; create 2. file for writing the changed file
Repeat
string.s = ReadString(1) ; read string from source
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
EndIf
EndIf
CloseFile(2)
CloseFile(1)
DeleteFile(source) ; delete old file
RenameFile(source +".pb", source) ; rename new File (nr. 2) to old file
CopyFile(source, tempsource)
EndProcedure
Importent difference: You have to use 2 arguments: %FILE and %TEMPFILE
All other arguments are the same.
Date and Time will be changed and the will be displayed in IDE.
It works for me. Hope the same for you.
Re: [IDE Tool] Add date to source code!
Posted: Sun Mar 06, 2011 10:15 pm
by WilliamL
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)
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? It is very confusing. How do you run the Tool to see if it is working.. before you make it into a Tool? Although it doesn't seem to change the code to run it.
Here is my compilation of the various changes into a whole program: (could have used either AddDateToSource procedure

)
Code: Select all
; To configure, compile program With whatever name you wish then click Tools > Configure Tools > Click New
; Now edit To something similar:
; New routine, FileName "AddDateToSource", Toolname: "AddDateToSource"
; Commandline: 'Browse' to "AddDateToSource"
; Arguments: %FILE %TEMPFILE
; Name: "AddDateToSource"
; Event To trigger the tool: Sourcecode saved
; To prevent an annoying console from popping up every save, I have Run Hidden checked
; ---------------------------------------------------------------------------------------------
; author: nituvious (modifed by mitchel51)
; notes:
; This program will try to modify your source code by adding the date
; upon saving the file. If it finds a date recorded already, it will attempt
; to overwrite it with the current date and time.
; Otherwise it will automatically add the date at the beginning of the file.
; This program includes fully changable date/time functions as well.
; ---------------------------------------------------------------------------------------------
EnableExplicit
#C12HOUR = 12 ; silly constants, trying to pretend to be a clock!
#C24HOUR = 24
Procedure.s GetDate() ; modified by LuCiFeR[SD]
Protected Result.s, gMonth, gDay.s, gYear.s, monthNames.s
gMonth = Month(Date())
gDay = Str(Day(Date()))
gYear = Str(Year(Date()))
monthNames = "January|February|March|April|May|June|July|August|September|October|November|December"
; give each day ending with 1, 2, 3, 4... 23,24 etc their proper ordinal suffix
Select gDay
Case "1", "21","31"
gDay + "st"
Case "2","22"
gDay + "nd"
Case "3","23"
gDay + "rd"
Default
gDay + "th"
EndSelect
Result = StringField(monthNames, gMonth, "|") + " " + gDay + ", " + gYear
ProcedureReturn Result
EndProcedure
Procedure.S GetTime(const)
; depending on user settings, should this date be displayed as a 24 hour clock, or 12 hour?
; format 12 hour clock...
Protected gHour,gHour_n,gMeridiem.s,gTime.s
gHour.i = Hour(Date())
If const = #C12HOUR ; this is my crappy time conversion function which will add meridiem if the 12 hour clock is used
If gHour>= 13
gHour_n.i = gHour.i - 12
gMeridiem.s = "PM"
Else
If gHour.i = 0
gHour_n = 12
EndIf
gHour_n.i = gHour.i
gMeridiem.s = "AM"
EndIf
gTime.s = Str(gHour_n.i) + ":" + FormatDate("%ii:%ss",Date()) + " " + gMeridiem.s
ProcedureReturn gTime.s
ElseIf const = #C24HOUR
gTime.s = FormatDate("%hh:%ii:%ss",Date())
ProcedureReturn gTime.s
EndIf
EndProcedure
Procedure AddDateToSource(modstring.s) ; by mitchel51
Protected done,source.s,tempsource.s,string.s,pos1
done = 1
source.s = ProgramParameter(0)
tempsource.s = ProgramParameter(1)
If ReadFile(1, source) ; open file for reading
If CreateFile(2, source + ".pb") ; create 2. file for writing the changed file
Repeat
string.s = ReadString(1) ; read string from source
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
EndIf
EndIf
CloseFile(2)
CloseFile(1)
DeleteFile(source) ; delete old file
RenameFile(source +".pb", source) ; rename new File (nr. 2) to old file
CopyFile(source, tempsource)
EndProcedure
AddDateToSource("; last modified: " + GetDate() + " @ " + GetTime(#C12HOUR))