home *** CD-ROM | disk | FTP | other *** search
- /***
- * examplep.prg
- *
- * Sample Clipper User Defined Functions
- */
-
-
- /***
- * AMPM( <cTime> ) --> cTime
- *
- * Convert a time string to 12-hour format
- */
-
- FUNCTION AMPM( cTime )
-
- IF VAL(cTime) = 0
- cTime := "12" + SUBSTR(cTime, 3) + " am"
-
- ELSEIF VAL(cTime) < 12
- cTime += " am"
-
- ELSEIF VAL(cTime) = 12
- cTime += " pm"
-
- ELSE
- cTime := STR(VAL(cTime) - 12, 2) + SUBSTR(cTime, 3) + " pm"
-
- ENDIF
-
- RETURN cTime
-
-
- /***
- * StrZero( <nNumber>, <nLength>, <nDecimals> ) --> cNumber
- *
- * Convert a numeric to a string padded with leading zeros
- */
-
- FUNCTION StrZero( n, nLength, nDecimals )
-
- LOCAL cNumber
-
- IF PCOUNT() = 3
- cNumber = STR(n, nLength, nDecimals)
- ELSEIF PCOUNT() = 2
- cNumber = STR(n, nLength)
- ELSE
- cNumber = STR(n)
- ENDIF
-
- IF "-" $ cNumber
- // Negative number, move the minus sign in front of zeros
- RETURN "-" + REPLICATE("0", LEN(cNumber) - LEN(LTRIM(cNumber))) +;
- SUBSTR(cNumber, AT("-", cNumber) + 1)
- ENDIF
-
- // Positive number
- RETURN REPLICATE("0", LEN(cNumber) - LEN(LTRIM(cNumber))) + LTRIM(cNumber)
-
-
- /***
- * DAYS( <nSeconds> )
- *
- * Return integer number of days from numeric seconds
- *
- * Note: The remainder under 24 hours is returned by the TSTRING() function.
- */
-
- FUNCTION DAYS(cl_secs)
- RETURN INT( cl_secs / 86400 )
-
-
- /***
- * DBF()
- *
- * Return the alias of the currently selected database.
- *
- * Note: Supposed to return the name of the currently selected database file.
- */
-
- FUNCTION DBF
- RETURN ALIAS()
-
-
- /***
- * ELAPTIME( <nStartTime>, <nEndTime> )
- *
- * Return a time string showing the difference between start and end times
- *
- * Note: If start time is greater than end time, this algorithm assumes
- * that the day changed at midnight. Only for timings under 24 hours.
- * 86400 is the number of seconds in 24 hours.
- */
-
- FUNCTION ELAPTIME(cl_start, cl_end)
-
- RETURN TSTRING( IF( cl_end < cl_start, 86400, 0) + ;
- SECS(cl_end) - SECS(cl_start) )
-
-
- /***
- * FKLABEL( <nFunKey> )
- *
- * Return the name of the <expN>th programmable function key
- */
-
- FUNCTION FKLABEL(cl_1)
-
- RETURN IF( cl_1 <= 40 .AND. cl_1 > 0, "F"+LTRIM(STR(cl_1)), "")
-
-
- /***
- * FKMAX()
- * Return the maximum number of programmable function keys on the computer
- *
- * Note: specific to IBM PC/XT/AT and clones.
- */
-
- FUNCTION FKMAX()
- RETURN (40) // IBM specific
-
-
- /***
- * LENNUM( <nNum> )
- * Return the string length of <nNum>
- */
-
- FUNCTION LENNUM(cl_number)
- RETURN LEN(LTRIM(STR(cl_number)))
-
-
- /***
- * MOD( <expN1>, <expN2> )
- *
- * Return remainder of <expN1> divided by <expN2>
- *
- * Note: Difference between the dBASE modulus function and the Clipper
- * modulus operator is indicated by an arrow <-->:
- *
- * Clipper operator: dBASE function:
- * ----------------- -----------------
- * 3 % 3 ::= 0.00 MOD( 3, 3) ::= 0
- * 3 % 2 ::= 1.00 MOD( 3, 2) ::= 1
- * 3 % 1 ::= 0.00 MOD( 3, 1) ::= 0
- * 3 % 0 ::= 0.00 <--> MOD( 3, 0) ::= 3
- * 3 % -1 ::= 0.00 MOD( 3,-1) ::= 0
- * 3 % -2 ::= 1.00 <--> MOD( 3,-2) ::= -1
- * 3 % -3 ::= 0.00 MOD( 3,-3) ::= 0
- *
- * -3 % 3 ::= 0.00 MOD(-3, 3) ::= 0
- * -3 % 2 ::= -1.00 <--> MOD(-3, 2) ::= 1
- * -3 % 1 ::= 0.00 MOD(-3, 1) ::= 0
- * -3 % 0 ::= 0.00 <--> MOD(-3, 0) ::= -3
- * -3 % -1 ::= 0.00 MOD(-3,-1) ::= 0
- * -3 % -2 ::= -1.00 MOD(-3,-2) ::= -1
- * -3 % -3 ::= 0.00 MOD(-3,-3) ::= 0
- *
- * 3 % 3 ::= 0.00 MOD( 3, 3) ::= 0
- * 2 % 3 ::= 2.00 MOD( 2, 3) ::= 2
- * 1 % 3 ::= 1.00 MOD( 1, 3) ::= 1
- * 0 % 3 ::= 0.00 MOD( 0, 3) ::= 0
- * -1 % 3 ::= -1.00 <--> MOD(-1, 3) ::= 2
- * -2 % 3 ::= -2.00 <--> MOD(-2, 3) ::= 1
- * -3 % 3 ::= 0.00 MOD(-3, 3) ::= 0
- *
- * 3 % -3 ::= 0.00 MOD( 3,-3) ::= 0
- * 2 % -3 ::= 2.00 <--> MOD( 2,-3) ::= -1
- * 1 % -3 ::= 1.00 <--> MOD( 1,-3) ::= -2
- * 0 % -3 ::= 0.00 MOD( 0,-3) ::= 0
- * -1 % -3 ::= -1.00 MOD(-1,-3) ::= -1
- * -2 % -3 ::= -2.00 MOD(-2,-3) ::= -2
- * -3 % -3 ::= 0.00 MOD(-3,-3) ::= 0
- */
-
- FUNCTION MOD(cl_num, cl_base)
- LOCAL cl_result
-
- cl_result = cl_num % cl_base
-
- RETURN IF( cl_base = 0, ;
- cl_num,;
- IF(cl_result * cl_base < 0, cl_result + cl_base, cl_result) )
-
-
- /***
- * READKEY()
- *
- * Return a number representing the key pressed to exit from full-screen mode
- *
- * Note: Differences between dBASE's READKEY() and Clipper's LASTKEY():
- *
- * Exit Key: dBASE: Clipper:
- * --------- ------ --------
- * Backspace 0 no exit
- * ^D, ^L 1 no exit
- * Lt arrow 2 no exit
- * Rt arrow 3 no exit
- * Up arrow 4 no exit
- * Dn arrow 5 no exit
- * PgUp 6 18
- * PgDn 7 3
- * Esc, ^Q 12 27 (Esc only)
- * ^End, ^W 14 23 (^W only)
- * type past end 15 ascii of last char typed
- * Enter 15 13
- * ^Home 33 no exit
- * ^PgUp 34 no exit
- * ^PgDn 35 no exit
- * F1 36 no exit
- *
- * dBASE III adds 256 to the exit code if the user changed anything.
- * Clipper uses its UPDATED() function to determine if anything changed.
- */
-
- FUNCTION READKEY()
-
- DO CASE
- CASE LASTKEY() = 18 // PgUp
- RETURN 6 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() = 3 // PgDn
- RETURN 7 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() = 27 // Esc
- RETURN 12 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() = 23 // ^W
- RETURN 14 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() = 13 // Enter
- RETURN 15 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() = 31 // ^PgUp
- RETURN 34 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() = 30 // ^PgDn
- RETURN 35 + IF(UPDATED(), 256, 0)
- CASE LASTKEY() >= 32 // type past end
- RETURN 15 + IF(UPDATED(), 256, 0)
- OTHERWISE
- RETURN 0
- ENDCASE
-
- RETURN (0) // never gets here
-
-
- /***
- * SECS( <time string> )
- *
- * Return numeric seconds as a quantity of the time string
- *
- * Note: Seconds in time period
- * ------- -----------
- * 60 1 minute
- * 3600 1 hour
- * 86400 1 day
- */
-
- FUNCTION SECS(cl_time)
-
- RETURN VAL( cl_time ) * 3600 +;
- VAL(SUBSTR(cl_time,4)) * 60 +;
- VAL(SUBSTR(cl_time,7))
-
-
- /***
- * TSTRING( <seconds> )
- *
- * Return a 24-hour time string from numeric seconds
- *
- * Note: Time quantities over 24 hours are returned by the DAYS() function.
- */
-
- FUNCTION TSTRING(cl_secs)
-
- RETURN STRZERO( INT(MOD(cl_secs/3600, 24)), 2, 0 ) +':'+;
- STRZERO( INT(MOD(cl_secs/ 60, 60)), 2, 0 ) +':'+;
- STRZERO( INT(MOD(cl_secs , 60)), 2, 0 )
-