I read an old thread about how to make temporary filenames (unique filenames is what the topic was about) and I have written a simple unit that sort of does this without needing a second to elapse (or millisecond for that matter) to pass in order to get the next unique name. I have heard of a possible unit or routine that MAY be available to us FPC coders that does some sort of GUID algorithm… this would interest me.
My algorithm is platform independent but I know my technique may be frowned upon by many but it works.
I create a file called something intuitive: counterlockfile.lock
Each call to my unit TRIES to open the file for REWRITE – if it can’t (because another process has it – then it retries a few times with a sleep/yield call and both the repeat attempts and sleep duration are configurable.
When the file CAN be opened, this means ownership of a second file called something intuitive: mycounter.txt
This file is simply one line of text that is a zero padded string e.g.: 00000001
I’m assuming readers understand there is logic in the code to create the files if they don’t already exist and there is a way for all programs wishing to use this little system to know how to locate the same directory that was set up for this purpose.
Each time a program calls the function that reads the counter, it works like an autonumber in a database application, returning the current value and then adding 1 to it and saving the new value out there for the next time.
The cleanup is merely closing the file that is/was named counterlockfile.lock that has been opened for rewrite during the counter read, add one, save, close process.
A bit nasty… but it works – and it works EVERY TIME.
So this message is to one: maybe help a GO32 user who needs unique filenames, and anyone else on (I would assume) any platform.
Again – if there is anyone who knows of a more elegant way to do this reliably – in an environment that may have a ton of different EXE’s all trying to do the same thing at the same time – without choking – please let me know!
Jason P Sage |
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal