home *** CD-ROM | disk | FTP | other *** search
- *****************************************************************
- * UDF1.PRG *
- * Written by Greg Martin *
- * These functions may be freely used, but may not be published. *
- *****************************************************************
-
-
-
- *********************
- * Opening Databases *
- *********************
-
- FUNCTION G_Use
- PARAMETERS File, Indices, Alias, Try_Forever, Excl_Use
- * First, select an empty work area
- IF .not. Select_0()
- RETURN(.f.)
- ENDIF
- * Make sure the database exists
- IF .not. FILE(File + ".DBF")
- DECLARE LINE[4]
- IF Excl_Use
- Line[1] = "ERROR IN OPENING DATABASE FOR EXCLUSIVE USE !"
- ELSE
- Line[1] = "ERROR IN OPENING DATABASE FOR SHARED USE !"
- ENDIF
- Line[2] = "ERROR: DATABASE DOES NOT EXIST!"
- Line[3] = "DATABASE: " + File
- Line[4] = "PRESS ANY KEY TO ABORT OPERATION"
- ErrorBox(Line)
- RETURN(.f.)
- ENDIF
- * Attempt to open database until successful or aborted
- DO WHILE .t.
- IF Excl_Use
- USE &File ALIAS &Alias EXCLUSIVE
- ELSE
- USE &File ALIAS &Alias
- ENDIF
- IF .not. NETERR()
- IF EMPTY(Indices)
- RETURN(.t.)
- ELSE
- IF G_Index(Indices)
- RETURN(.t.)
- ELSE
- USE
- RETURN(.f.)
- ENDIF
- ENDIF
- ENDIF
- DECLARE LINE[4]
- IF Excl_Use
- Line[1] = "ERROR IN OPENING DATABASE FOR EXCLUSIVE USE !"
- Line[2] = "ERROR: EXCLUSIVE USE OF DBF DENIED."
- ELSE
- Line[1] = "ERROR IN OPENING DATABASE FOR SHARED USE !"
- Line[2] = "ERROR: SHARED USE OF DBF DENIED."
- ENDIF
- Line[3] = "DATABASE: " + File
- IF .not. Try_Forever
- Line[4] = "PRESS ANY KEY TO RETRY OR Esc TO ABORT OPERATION"
- ELSE
- Line[4] = "PRESS ANY KEY TO CONTINUE TRYING"
- ENDIF
- Key = ErrorBox(Line)
- IF Key = Esc .and. .not. Try_Forever
- RETURN(.f.)
- ENDIF
- ENDDO
-
-
- FUNCTION Select_0
- * Select an empty work area.
- SELECT 0
- * Make sure an empty work area was found.
- IF .not. EMPTY(ALIAS())
- DECLARE Line[2]
- Line[1] = "ERROR: NO EMPTY WORK AREAS FOUND!"
- Line[2] = "PRESS ANY KEY TO ABORT OPERATION."
- ErrorBox(Line)
- RETURN(.f.)
- ELSE
- RETURN(.t.)
- ENDIF
-
-
- FUNCTION G_Index
- PARAMETER Indices
- PRIVATE RecNo, Num, Temp, Token
- PRIVATE Index_1, Index_2, Index_3, Index_4, Index_5, Index_6, Index_7, Index_8
- PRIVATE Index_9, Index_10, Index_11, Index_12, Index_13, Index_14, Index_15
- IF EMPTY(Indices)
- * If no index specified then close any open indexes.
- RecNo = RECNO()
- SET INDEX TO
- GO RecNo
- RETURN(.t.)
- ELSE
- * Open the specified indexes.
- Indices = UPPER(Indices)
- * Open up to 15 indexes by breaking down the listed indexes.
- FOR Temp = 1 TO 15
- Token = ""
- FirstToken(@Indices, @Token)
- Num = LTRIM(STR(Temp, 2, 0))
- IF .not. EMPTY(Token)
- Index_&Num = Token + ".NTX"
- * Make sure index file exists.
- IF .not. FILE(Index_&Num)
- DECLARE LINE[4]
- Line[1] = "ERROR IN OPENING INDEX FILE !"
- Line[2] = "ERROR: INDEX FILE DOES NOT EXIST."
- Line[3] = "INDEX: " + Index_&Num
- Line[4] = "PRESS ANY KEY TO ABORT OPERATION"
- ErrorBox(Line)
- RETURN(.f.)
- ENDIF
- ELSE
- Index_&Num = ""
- ENDIF
- NEXT Temp
- * Save the record number.
- RecNo = RECNO()
- * Open all indexes.
- * In this one case Clipper ignores empty string macros.
- SET INDEX TO &Index_1, &Index_2, &Index_3, &Index_4, &Index_5, &Index_6, &Index_7, &Index_8, &Index_9, &Index_10, &Index_11, &Index_12, &Index_13, &Index_14, &Index_15
- * Reset the record number.
- GO RecNo
- RETURN(.t.)
- ENDIF
- RETURN(.f.)
-
-
-
- ***************************
- * Record and File Locking *
- ***************************
-
- FUNCTION G_RLock
- PARAMETERS P1, P2
- PRIVATE Append, Try_Forever, PCount, Temp, No, Sec, PCount, Key
- Append = .f.
- Try_Forever = .f.
- PCount = PCOUNT()
- * Process any passed parameters
- FOR Temp = 1 TO PCount
- No = STR(Temp, 1)
- IF TYPE("m->P&No") == "L"
- Append = P&No
- ELSE
- Try_Forever = (UPPER(P&No) == "FOREVER")
- ENDIF
- NEXT Temp
- IF Append
- * Append blank if specified.
- RETURN(APPENDBLANK(Try_Forever))
- ENDIF
- DO WHILE .t.
- Sec = SECONDS() + 5
- * Try for 5 seconds.
- DO WHILE SECONDS() < Sec
- * Lock current record.
- IF RLOCK()
- RETURN(.t.)
- ENDIF
- ENDDO
- * 5 second time limit reached.
- DECLARE Line[4]
- Line[1] = "ERROR WHILE ATTEMPTING TO LOCK A RECORD !"
- Line[2] = "ERROR: UNABLE TO LOCK RECORD."
- Line[3] = "DATABASE: " + ALIAS()
- IF .not. Try_Forever
- Line[4] = "PRESS ANY KEY TO RETRY OR Esc TO ABORT OPERATION"
- ELSE
- Line[4] = "PRESS ANY KEY TO CONTINUE TRYING"
- ENDIF
- Key = ErrorBox(Line)
- IF Key = Esc .and. .not. Try_Forever
- RETURN(.f.)
- ENDIF
- ENDDO
-
-
- FUNCTION AppendBlank
- PARAMETER Try_Forever
- PRIVATE Sec, Key
- DO WHILE .t.
- Sec = SECONDS() + 5
- * Try for 5 seconds.
- DO WHILE SECONDS() < Sec
- APPEND BLANK
- IF .not. NETERR()
- RETURN(.t.)
- ENDIF
- ENDDO
- * 5 second time limit reached.
- DECLARE Line[4]
- Line[1] = "ERROR WHILE ATTEMPTING TO APPEND A BLANK RECORD !"
- Line[2] = "ERROR: UNABLE TO APPEND RECORD."
- Line[3] = "DATABASE: " + ALIAS()
- IF .not. Try_Forever
- Line[4] = "PRESS ANY KEY TO RETRY OR Esc TO ABORT OPERATION"
- ELSE
- Line[4] = "PRESS ANY KEY TO CONTINUE TRYING"
- ENDIF
- Key = ErrorBox(Line)
- IF Key = Esc .and. .not. Try_Forever
- RETURN(.f.)
- ENDIF
- ENDDO
- RETURN(.t.)
-
-
- FUNCTION G_FLock
- PARAMETERS P1
- PRIVATE Try_Forever, Sec, Key
- Try_Forever = .f.
- IF PCOUNT() > 0
- Try_Forever = (UPPER(P1) = "FOREVER")
- ENDIF
- DO WHILE .t.
- Sec = SECONDS() + 5
- * Try for 5 seconds.
- DO WHILE SECONDS() < Sec
- IF FLOCK()
- RETURN(.t.)
- ENDIF
- ENDDO
- * 5 second time limit reached.
- DECLARE LINE[4]
- Line[1] = "ERROR WHILE ATTEMPTING TO LOCK A FILE !"
- Line[2] = "ERROR: UNABLE TO LOCK FILE."
- Line[3] = "DATABASE: " + ALIAS()
- IF .not. Try_Forever
- Line[4] = "PRESS ANY KEY TO RETRY OR Esc TO ABORT OPERATION"
- ELSE
- Line[4] = "PRESS ANY KEY TO CONTINUE TRYING"
- ENDIF
- Key = ErrorBox(Line)
- IF Key = Esc .and. .not. Try_Forever
- RETURN(.f.)
- ENDIF
- ENDDO
-