home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / defext.prg < prev    next >
Text File  |  1993-10-14  |  2KB  |  76 lines

  1. /*
  2.  * File......: DEFEXT.PRG
  3.  * Author....: Dave Pearson
  4.  * BBS.......: The Dark Knight Returns
  5.  * Net/Node..: 050/069
  6.  * User Name.: Dave Pearson
  7.  * Date......: $Date$
  8.  * Revision..: $Revision$
  9.  * Log file..: $Logfile$
  10.  *
  11.  * This is an original work by Dave Pearson and is placed in the public
  12.  * domain.
  13.  *
  14.  * Modification history:
  15.  * ---------------------
  16.  *
  17.  * $Log$
  18.  *
  19.  */
  20.  
  21. #include "gt_lib.ch"
  22.  
  23. /*  $DOC$
  24.  *  $FUNCNAME$
  25.  *      GT_DEFEXT()
  26.  *  $CATEGORY$
  27.  *      General
  28.  *  $ONELINER$
  29.  *      Give a file name a default extension.
  30.  *  $SYNTAX$
  31.  *      GT_DefExt(<cFileName>,[<cDefExt>]) --> cNewFileName
  32.  *  $ARGUMENTS$
  33.  *      <cFileName> is the file name to be checked.
  34.  *
  35.  *      <cDefExt> is an optional character paramater which is the
  36.  *      extension to add to the file. This can be in the format of
  37.  *      `.xxx' or just `xxx'. If not supplied the default is "".
  38.  *  $RETURNS$
  39.  *      The file name as it was if it had an extension, or the file name
  40.  *      with the default extension if it didn't have one.
  41.  *  $DESCRIPTION$
  42.  *      GT_DefExt() can be used to supply a default extension to a file.
  43.  *      This can be of use in file i/o functions or for tidying up user
  44.  *      input.
  45.  *  $EXAMPLES$
  46.  *      // Get a file name form the user and default the extension.
  47.  *
  48.  *      cFileName := space(12)
  49.  *      @ 10,10 say "File To Open:" get cFileName picture "!!!!!!!!!!!!"
  50.  *      read
  51.  *      cFileName := GT_DefExt(cFileName,"DBF")
  52.  *  $END$
  53.  */
  54.  
  55. function GT_DefExt(cFileName,cExt)
  56. local nLoopCount := 0,;
  57.       lExtFound  := FALSE
  58. if valtype(cFileName) == TYPE_CHAR
  59.    default cExt to NULL
  60.    cExt      := alltrim(cExt)
  61.    cFileName := alltrim(cFileName)
  62.    for nLoopCount := len(cFileName) to 1 step -1
  63.       if substr(cFileName,nLoopCount,1) == "\"
  64.          exit
  65.       endif
  66.       if substr(cFileName,nLoopCount,1) == "."
  67.          lExtFound := TRUE
  68.          exit
  69.       endif
  70.    next
  71.    if !lExtFound
  72.       cFileName += if("." $ cExt,cExt,if(!empty(cExt),".",NULL)+cExt)
  73.    endif
  74. endif
  75. return(cFileName)
  76.