Lock et Unlock Reccord (In Random Acces File)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Lock et Unlock Reccord (In Random Acces File)

Post by GeBonet »

Translate from french with Google...
-------------------------------------------
Hello,

I come to rest an issue that bothered me since I started with PB (2.5 years ago).
It is the lack of direct access file! Style PowerBasic, QB, or other QBX Pascal, etc. ...

As responses have often been like why bother with it when you can read an entire file (even large) all at once. And that once read is so much faster in memory! Huh? And it's true, except that!

Well, now I know why it bothered me (hey, yes I am getting old :( and it took me time!).
Well, it was not only to recover the old codes, but also (and that's what we are talking mostly):

How to lock a record in a file when you're working on it when it is networked ...

Must necessarily leave the file available to all that is to say, on disk, plus it does not block the file, but only recording on which we work and what if a dozen people or are more likely to work on the same file and want to accidentally access THIS Reccord there at the same time?

Before, I had two orders (instructions), Lock and Unlock ... Which enabled me to lock the record and banned all access until the first who called him was using and was released at the time he had finished and thus became available for the following.
This is the only guarantee for the work of one does not overwrite that of another. A change or development is taken into account etc..

In short: wink:, Until now I have not seen or heard of the possibility of gender (and also never got too worried), but I just started a project that requires this kind of approach and I am always thinking back on this issue.

Precision, I do not want to use the systems management of database SQL, MySQL or ODBC. This for simple reasons are that I prefer to remain master of my file management system that I can rewrite the limit! I have a personal ISAM, which needs to be adapted but still the question of foreclosure ... ?

I have an idea how to do it, but before I commit, maybe there is something that I missed and that the issue is already solved.

OR BETTER, that the concept of direct access file (Random Access) with LOCK and UNLOCK be implanted in PureBasic ....

Here, here ... I do not know if this has already been discussed, resolved or what you think?
Gerhard (GeBonet)
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Lock et Unlock Reccord (In Random Acces File)

Post by GeBonet »

I do not know what you mean ? :shock:
I'm so stupid? :oops:
I'm too old to be respected? :twisted:
Haaa! I am, I can not speak English? Even if I had read about ... ! :twisted:
Or maybe you've missed?
Or the translator is bad at this point ... !

I would post anything from you, you do not worth the effort! Sorry


Image
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Lock et Unlock Reccord (In Random Acces File)

Post by Rings »

@GeBonet:

i removed these nonsense....
SPAMINATOR NR.1
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Lock et Unlock Reccord (In Random Acces File)

Post by GeBonet »

@Ring
Thank you very much,I would not consider this episode ...
At the next
Gerhard
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Lock et Unlock Reccord (In Random Acces File)

Post by GeBonet »

Hi,
Here is what i want ? From PowerBasic ! This open the door of the MultiTasking...
LOCK statement
Purpose : Lock part or all of an open file, to prevent other processes from accessing it.
Syntax : LOCK [#] filenum& [, {record&& | start&& TO end&&}]

Remarks
LOCK prevents another process from accessing a record, range of records, byte, or range of bytes in a file opened as file number filenum&.
If the file was opened in random-access mode, record&&, start&&, and end&& specify record numbers. When used with binary mode files, record&&, start&&, and end&& specify byte positions, starting from either zero or one (the default).
If a record is specified, only that record (or byte) is locked. Otherwise, a range of records (or bytes) is locked, from start&& to end&&.
If no records are specified, or if the file was opened in sequential mode, the entire file is locked.
All records (or bytes) to be locked must be subsequently unlocked using the UNLOCK statement. Multiple locks may be placed on a file, and locks may be unlocked in any order. However, the parameters used for each UNLOCK statement must exactly match those used for the previous corresponding LOCK statement.
All locked records (or bytes) must be unlocked using the UNLOCK statement before the file can be closed.
If a lock attempt fails, PowerBASIC sets the ERR system variable to reflect a run-time Error 70 ("Permission denied"), or Error 75 ("Path/file access error").

See also : OPEN, UNLOCK
Example
OPEN "PATIENTS.DAT" FOR RANDOM AS #1 LEN = 1024
' determine the record number to retrieve
LOCK #1, recnum
GET #1, recnum
' process the record here
PUT #1, recnum
UNLOCK #1, recnum
CLOSE #1
This what i mind ! :lol:
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Lock et Unlock Reccord (In Random Acces File)

Post by Rings »

Windows has a api for that, see here:
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
SPAMINATOR NR.1
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Lock et Unlock Reccord (In Random Acces File)

Post by GeBonet »

Tanks for the answers, but "LockFile Function in API" lock the "File"... I can do that with PB and API !
What i need and exist in other langage, is Lock the "Record", or from the position x TO Positiotion Y in the file and the rest of the file remains free...
if I can not make myself clear there will see what I mean :oops:
http://www.powerbasic.com/support/help/ ... pdates.htm
I also mentioned it on the french forum and it reaches agreement on the basis LOCK and UNLOCK

Thank
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Re: Lock et Unlock Reccord (In Random Acces File)

Post by Rings »

you are wrong, the api locks a block inside the file, not the file complete:

Code: Select all

Global hFile
Procedure OpenFilehandle(Filename.s)
  dwDesiredAccess=#GENERIC_READ | #GENERIC_WRITE
  dwShareMode=#FILE_SHARE_READ + #FILE_SHARE_WRITE
  lpSecurityAttributes=0;.SECURITY_ATTRIBUTES
  dwCreationDisposition=#OPEN_EXISTING
  dwFlagsAndAttributes=0
  hTemplateFile=0
  hFile=CreateFile_(@FileName.s,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDisposition,dwFlagsAndAttributes,hTemplateFile)
  ProcedureReturn hFile  
EndProcedure  

Procedure CloseFilehandle(hFile)
   CloseHandle_(hFile)
EndProcedure  

Procedure _LockFile(hFile,Offset,Length)
  If hFile
   dwFileOffsetLow=Offset
   dwFileOffsetHigh=0
   nNumberOfBytesToLockLow=Length
   nNumberOfBytesToLockHigh=0
   result=LockFile_(hFile,dwFileOffsetLow,dwFileOffsetHigh,nNumberOfBytesToLockLow,nNumberOfBytesToLockHigh)
   ProcedureReturn result
  EndIf
EndProcedure

Procedure _UnLockFile(hFile,Offset,Length)
  If hFile
   dwFileOffsetLow=Offset
   dwFileOffsetHigh=0
   nNumberOfBytesToLockLow=Length
   nNumberOfBytesToLockHigh=0
   result=UnlockFile_(hFile,dwFileOffsetLow,dwFileOffsetHigh,nNumberOfBytesToLockLow,nNumberOfBytesToLockHigh)
   ProcedureReturn result
  EndIf
EndProcedure



Filename.s="PureBasicTestFile.bin"
MessageRequester("PureBasic", "Welcome !"+Chr(13)+"PureBasic will write a file named: "+Filename, 0)

If FileSize(Filename)<1 
 If CreateFile(0, Filename)
  mem=AllocateMemory(1024*10)
  WriteData(0,mem,MemorySize(mem) )
  CloseFile(0)
 Else
  MessageRequester("PureBasic", "Error: can't write the file, existing ?", 0)
  ;End
 EndIf
EndIf

hFile=OpenFilehandle(Filename)
If hfile
  
  MessageRequester("PureBasic", "Length="+Str(FileSize(Filename)), 0)
  
  ;Now lock the second block
  Result=_LockFile(hfile,1024*2,1024)
  MessageRequester("PureBasic", "Stop - Locked  (2. Block) .." + Str(Result), 0)
  
  Result= _LockFile(hFile,1024*2,1024)
  MessageRequester("PureBasic", "Stop - next Locking (2. Block) will fail (is allready locked) .."+ Str(Result), 0)
 
  
   Result= _LockFile(hFile,1024*3,1024)
  MessageRequester("PureBasic", "Stop - next Locking (3. Block) Works .."+ Str(Result), 0)
 
  Result=_UnLockFile(hFile,1024*2,1024) ;Now unlock 
  MessageRequester("PureBasic", "unLocking (2.Block) ="+ Str(Result), 0)
  Result=_UnLockFile(hFile,1024*3,1024) ;Now unlock 
  
  CloseFilehandle(hFile)
  
Else
  MessageRequester("PureBasic", "Error: Can't open file", 0)
EndIf
SPAMINATOR NR.1
User avatar
GeBonet
Enthusiast
Enthusiast
Posts: 135
Joined: Fri Apr 04, 2008 6:20 pm
Location: Belgium

Re: Lock et Unlock Reccord (In Random Acces File)

Post by GeBonet »

Thank you,
You are right, I have not watched enough API ... And it's perfect.
But precisely what would be easier to have two functions and corresponding PB would do that!
Like a lot of PB function or instruction that is based purely on APIs ... :wink:
No ? :lol:
Image
Sorry for my english :wink: ! (Windows Xp, Vista and Windows 7, Windows 10)
Post Reply