home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / dbase / tn9010.arj / SETFAR.TXT < prev    next >
Text File  |  1991-03-05  |  14KB  |  339 lines

  1. This article is reprinted from the October 1990 edition of
  2. TechNotes/dBASE IV.  Due to the limitations of this media, certain
  3. graphic elements such as screen shots, illustrations and some tables
  4. have been omitted.  Where possible, reference to such items has been
  5. deleted.  As a result, continuity may be compromised.  
  6.  
  7. TechNotes is a monthly publication from the Ashton-Tate Software
  8. Support Center.  For subscription information, call 800-545-9364.
  9.  
  10. Taking SET()
  11. One Step Further
  12. Adam Menkes
  13.  
  14. As extensive as the dBASE IV SET() function has become with the
  15. release of version 1.1, it is still limited in the types of results
  16. that can be returned.  You know that SET("Color") will return either
  17. ON or OFF, but what if you want specific information such as
  18. SET("Color of Normal") without getting the error message Invalid SET
  19. Expression?  
  20.  
  21. Moving Beyond SET() with SET1()
  22.  
  23. The user-defined function SET1() supplements the SET() function and
  24. has the ability to return values for attributes that have more than
  25. one word (such as PRINTER 1 FONT 1).  This function could be used not
  26. only to return the value of the SET() expression, but also to reset
  27. all attributes to the default settings in the Config.db file (see
  28. Procedure SetDefa).  Since there are a few SET commands that can have
  29. more than one attribute such as BELL (ON/OFF - Tone, Duration), CLOCK
  30. (ON/OFF - Row, Col), CURRENCY (Justification LEFT/RIGHT, Symbol), and
  31. DELIMITERS (ON/OFF - Symbol), you may want to alter the Config.db so
  32. that the ON/OFF commands come after the other ones, or alter the
  33. SetDbf.dbf to suit your needs to return a value for Set1() for these
  34. particular commands.  For example, you may want to change CURRENCY to
  35. CURRENCYJ and CURRENCYS in SetDbf so that Set1("CurrencyJ") will
  36. return either LEFT or RIGHT and Set1("CurrencyS") will return the
  37. currency sign ("$", "¢", "£", "¥", or some other symbol).  No changes
  38. need be made to the Config.db file for the procedure SetDefa to work
  39. for both attributes of the same SET command.  For instance,
  40.  
  41. .? Set1("Currency")
  42. $
  43. .? Set1("Printer 1 Font 1")
  44. {ESC}(8U, {ESC}(#@ NAME "ROMAN-8 SYMBOL SET"
  45.  
  46. SET1() Uses SetDBF.dbf
  47.  
  48. Although this function (and associated procedure) can return valid
  49. expressions, the returned values are read from the Config.db initial
  50. settings unless specific changes are made to the database SetDBF.dbf. 
  51. Therefore, if your Config.db file contains: 
  52.  
  53. COLOR OF NORMAL = W+/B 
  54.  
  55. and later you set color to BG/N, issuing the command
  56.  
  57. ? SET1("Color of Normal") 
  58.  
  59. will still return W+/B.. 
  60.  
  61. At right is a sample of a typical Config.db file which shows how the
  62. data is retrieved and stored in a database (SetDbf). The data is
  63. APPENDed FROM Config.db TYPE SDF, then the "=" is stripped off, the
  64. blank lines (records) are removed, as well as the comment lines
  65. (records). The database is then indexed so a SEEK may be performed.
  66.  
  67. Remember that for this to work the Config.db file must have the
  68. settings in distinct columns in order to be appended properly.  If you
  69. use Dbsetup, this is done automatically and no modifications need to
  70. be made.  
  71.  
  72. The procedure SetDefa resets all your SET selections to the defaults
  73. in your Config.db file. In order to ensure that certain commands can
  74. be reset, select the SET option in the Dbsetup program, even if you
  75. are going to leave it as the default (as dBASE IV does not write the
  76. defaults to the Config.db file).  For example, the F2 function key
  77. (default is "ASSIST") does not appear in your Config.db, yet when you
  78. display your SET options, it appears.  If the F2 key were selected in
  79. Dbsetup and saved even if the key remains "ASSIST", it will be saved
  80. to your Config.db and any changes to this key during program execution
  81. will be reset when this procedure is executed.  However, keys that
  82. were not included in the Config.db that are changed within dBASE IV
  83. will not be reset unless you change your Config.db file to show the
  84. key equal to nothing (example: Ctrl-F5 = ) or modify your SetDbf.dbf
  85. database to include the keys you want reset to the defaults or blanks.
  86.  
  87. A  procedure called Nada is referenced in the other procedures.  It is
  88. not listed here since it is only one command line in length.  The
  89. command RETURN is the only command it contains.   Why is this
  90. procedure even necessary? As the name suggests, it does nothing, or
  91. does it?  Actually, this procedure is essential to ignore errors and
  92. continue your program, as the command "ON ERROR"  by itself will call
  93. up the dBASE error message and wait until you CANCEL, IGNORE, or
  94. SUSPEND before continuing.  Whereas the command "ON ERROR DO
  95. Something" will  bypass the dBASE error checking and accept the
  96. user-defined error message which, in this case, is nothing, and allow
  97. the program to continue without interruption. Alternatively, your
  98. command statement could be "ON ERROR DummyVar = 0" or any other data
  99. stored to a dummy memory variable. 
  100.  
  101. FUNCTION Set1()
  102. FUNCTION Set1
  103.                 PARAMETER mSet
  104.  
  105.                 IF .NOT. FILE("SetDbf.DBF") .OR. .NOT. FILE("SetDbf.MDX")
  106.                 DO SetDbf
  107.                 ELSE
  108.                 DO SelArea1 WITH "SetDbf"                               && *&*
  109.                    SET ORDER TO dCommand
  110.                 ENDIF
  111.  
  112.                 SEEK UPPER(mSet)
  113.                 IF FOUND()
  114.                    mSet1 = LTRIM(Attribute)
  115.                 ELSE
  116.                 mSet1 = "None"
  117.                 ENDIF
  118. RETURN mSet1
  119.  
  120. Listing:  SetDBF.prg
  121. PROCEDURE SetDbf
  122.                 mSafety = SET("Safety")
  123.                 DO Efile WITH "Temp"                            && *&*
  124.                 DO SelArea1 WITH "Catalog.CAT"          && *&*
  125.                 * Any Database may be used to create an empty STRUCTURE
  126. EXTENDED file.
  127.                 COPY STRUCTURE EXTENDED TO Temp.DBF
  128.  
  129.                 SELECT SELECT()         && Select next highest available work
  130. area.
  131.                 USE Temp
  132.                 ZAP             && Leaves an empty STRUCTURE EXTENDED
  133. database.
  134.  
  135.                 APPE BLANK
  136.                 REPLACE Field_Name WITH "dCommand", Field_Len WITH 20,;
  137.                                   Field_Type WITH "C"
  138.                 APPE BLANK
  139.                 REPLACE Field_Name WITH "Attribute", Field_Len WITH 50,;
  140.                                   Field_Type WITH "C"
  141.                 *       Adds 2 character fields to the database (dCommand and
  142. Attribute).
  143.  
  144.                 DO Efile WITH "Setdbf"                          && *&*
  145.                 DO SelArea1 WITH "Temp"                         && *&*
  146.                 USE
  147.                 SELECT SELECT()         && Select next highest available work
  148. area.
  149.                 CREATE SetDbf FROM Temp
  150.                 * CREATE a database from the STRUCTURE EXTENDED database.
  151.                 *       This section gets the DOS environmental variable
  152. 'DPATH' which
  153.                 *       you SET to the location of the Config.DB file prior to
  154. entering
  155.                 *       dBase. In your Autoexec.BAT (or any batch file to
  156. enter dBase),
  157.                 *       put in SET DPATH = <drive>:<path>.
  158.                 *               Example: SET DPATH = C:\DATA\DBASE\
  159.  
  160.                 mPath = GETENV("dPath")
  161.                 IF RIGHT(mPath, 1) <> "\" .AND. LEN(TRIM(mPath)) <> 0
  162.                         mPath = mPath + "\"
  163.                 ENDIF
  164.                 * Checks to see if DPATH was SET in DOS and if it has an
  165. ending "\".
  166.  
  167.                 APPEND FROM &mPath.Config.DB TYPE SDF
  168.  
  169.                 * Note that the first period terminates the macro.
  170.                 * In this example, '&mpath.Config.DB' is
  171. 'C:\DATA\DBASE\Config.DB'.
  172.                 * If you prefer not using DOS variables, simply hard-code in
  173. the
  174.                 * full path to your Config.DB in the above command line
  175. instead
  176.                 * of using the macro substitution, and remove all the code
  177. between
  178.  
  179.                 SCAN
  180.                         IF LEN(TRIM(dCommand)) = 0 .OR. SUBSTR(dCommand, 1, 1)
  181. = "*"
  182.                                 DELETE  && DELETE all non-command records.
  183.                         ENDIF
  184.                 ENDSCAN
  185.  
  186.                 REPLACE ALL Attribute WITH UPPER(SUBSTR(Attribute, AT("=", ;
  187.                         Attribute) + 2, LEN(Attribute)))
  188.                 * Remove the '=' from the ATTRIBUTE field.
  189.  
  190.                 REPLACE ALL dCommand WITH UPPER(dCommand)
  191.                 * Convert all commands to uppercase.
  192.  
  193.                 PACK                    && Get rid of DELETEd records.
  194.                 INDEX ON dCommand TAG dCommand
  195.                 SET SAFETY &mSafety     && Restore SAFETY settings.
  196. RETURN
  197.  
  198. Listing: SetDefa.prg
  199. PROCEDURE SetDefa
  200.                 PRIVATE mdCommand, mAttribute
  201.                 ON ERROR DO Nada
  202.                 *       This ON ERROR routine is necessary because errors will
  203.                 *       be encountered in the SCAN / ENDSCAN loop as some SET
  204.                 *       commands SET something, others SET a command TO
  205. something.
  206.  
  207.                 IF .NOT. FILE("SetDbf.DBF") .OR. .NOT. FILE("SetDbf.MDX")
  208.                    DO SetDbf
  209.                 ELSE
  210.                         DO SelArea1 WITH "SetDbf"                       && *&*
  211.                         SET ORDER TO dCommand
  212.                 ENDIF
  213.  
  214.                 SCAN
  215.                         mdCommand = dCommand
  216.                         mAttribute = Attribute
  217.  
  218.                         SET &mdCommand TO &mAttribute
  219.                         SET &mdCommand &mAttribute
  220.                         SET FUNCTION &mdCommand TO &mAttribute
  221.                         *       Without keeping a database of acceptable SET,
  222. SET ... TO,
  223.                         *       and SET FUNCTION ... TO commands, this will
  224. try the same 
  225.                         *       command all three ways and, because of the ON
  226. ERROR, ignore 
  227.                         *       the ones that are unacceptable.
  228.  
  229.                 ENDSCAN
  230.                 ON ERROR                                                &&
  231. Reset to default
  232. RETURN
  233.  
  234. MacroMan! Procedures and Functions  (see July 1990 edition of
  235. TechNotes/dBASE IV)
  236.  
  237. PROCEDURE Efile
  238.                 *─ Erases a specified file, even if the file is currently in
  239. USE.
  240.                 *─ SYNTAX : DO Procedure WITH <expC>
  241.                 *─ Example: DO Efile WITH "BadFile.TXT"
  242.                 PARAMETER vDbf                                  && File to be
  243. erased.
  244.                 vDbf=FullDbf(vDbf)              && ** Checks for .DBF
  245. extension.
  246.                 IF FILE(vDbf)
  247.                         IF Inuse(vDbf)<>0                       && ** If
  248. database is SELECTed
  249.                                 MSelDbf=Inuse(vDbf)             && ** it
  250. returns the Work Area
  251.                                 SELECT(MSelDbf) && and SELECTs that Work Area
  252.                                 USE                                     
  253. && and closes it so that it can
  254.                         ENDIF                                           
  255. && be erased.
  256.                         ERASE(vDbf)                                     
  257.                 ENDIF
  258. RETURN
  259.  
  260. FUNCTION InUse
  261.                 PARAMETER vDbf
  262.                 SET EXACT ON
  263.                 *─ SET EXACT OFF would cause potential problems if you were 
  264.                 *   checking for a database such as "Client" without
  265. specifying the 
  266.                 *   extension if a file called Client1, Myclient, or similar
  267. existed 
  268.                 *   in the same directory. 
  269.  
  270.                 mAlias=0
  271.                 vDbf=FullDbf(vDbf)                      && See next UDF below
  272.                 mCount=1
  273.                 DO WHILE mCount<=10             && This loop checks all 10
  274. Areas.
  275.                         SELECT(mCount)
  276.                         IF (vDbf) $ UPPER(DBF())
  277.                                 *─ The '$' is used instead of '=' so the PATH
  278. does not have
  279.                                 *    to be specified in the variable.
  280.  
  281.                                 mAlias=mCount   && Sets the Work Area where it
  282. is being USEd.
  283.                                 RETURN mAlias
  284.                                 *─ Exit the loop once the Area is found 
  285.                         ENDIF
  286.                         mCount=mCount+1                         && Increments
  287. to check next Area.
  288.                 ENDDO
  289.                 SET EXACT OFF
  290. RETURN mAlias 
  291.            
  292.  
  293. FUNCTION FullDbf
  294.                 PARAMETER vDbf
  295.  
  296.                 vDbf=UPPER(vDbf)                                && Convert to
  297. Uppercase.
  298.                 IF LEN(TRIM(vDbf))<>0                   && Checks for null.
  299.                         IF AT(".",vDbf)=0            && Checks for an
  300. extension.
  301.                                 vDbf=TRIM(vDbf)+".DBF"  && Adds '.DBF' to the
  302. parameter.
  303.                         ENDIF
  304.                 ENDIF
  305. RETURN vDbf
  306.  
  307.  
  308. PROCEDURE SelArea1
  309.                 *─ Selects Area where DATABASE is in
  310.                 *   use or opens it in an unused area.
  311.                 *─ Must be used in conjunction with SelArea2
  312.  
  313.                 PARAMETER mParam1
  314.                 PUBLIC mWasOpen,mMaindbf
  315.  
  316.                 IF Inuse(mParam1) <>
  317. 0                                               
  318.                         mMainDbf=Inuse(mParam1)         && See Inuse(),
  319. previous page           
  320.                         SELECT(mMainDbf)
  321.                 
  322. mWasOpen=.T.                                                     
  323.                         *─ mWasOpen is TRUE when the database is in USE prior
  324. to
  325.                         *    being opened by a procedure or function.
  326.                 ELSE
  327.                         mMainDbf=SELECT()
  328.                         SELECT(mMainDbf)
  329.                         USE(mParam1)
  330.                         mWasOpen=.F.
  331.                         *─ mWasOpen is FALSE if the database was not in USE,
  332. whether
  333.                         *    or not it gets USEd during a procedure or
  334. function.
  335.                 ENDIF
  336. RETURN
  337.  
  338.  
  339.