Author Topic: DOS_OPENP in Lisp  (Read 1126 times)

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
DOS_OPENP in Lisp
« on: October 27, 2023, 10:38:38 AM »
Is this a good alternative to DOS_OPENP?

Code - Auto/Visual Lisp: [Select]
  1. (defun ALE_OpenP (FilNam)
  2.  (not (vl-file-rename FilNam FilNam))
  3. )



EDIT (John): added code tags.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: DOS_OPENP in Lisp
« Reply #1 on: October 27, 2023, 11:52:01 AM »
Thought: if you just OPEN the file readonly to check if it can be opened should suffice (right?). NOTE: issuing a CLOSE on a NIL will toss an error so our return would be either T or an errror.
Code - Auto/Visual Lisp: [Select]
  1. (defun openp (path)
  2.   (not
  3.     (close (open path "r")))
  4.   )
  5.  
  6. (openp "nosuch.file")
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: DOS_OPENP in Lisp
« Reply #2 on: October 27, 2023, 01:28:43 PM »
Sorry, I was distracted for a bit...

Of course you could also be more specific in your returns (-i.e. not just toss an error).
Code - Auto/Visual Lisp: [Select]
  1. (defun openp (path / fd)
  2.   (setq fd (open path "r"))
  3.   (if fd (close fd))
  4.   fd
  5.   )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

danAllen

  • Newt
  • Posts: 133
Re: DOS_OPENP in Lisp
« Reply #3 on: October 27, 2023, 03:18:53 PM »
Here is my version

For posterity, original post (I think)
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/is-document-open/m-p/809172/highlight/true#M34830

Code - Auto/Visual Lisp: [Select]
  1. ;;;=========================================================================
  2. ;;; From: Tony Tanzillo (tony.tanzillo_at_caddzone_dot_com)
  3. ;;; Subject: Re: Is Document Open
  4. ;;; Newsgroups: autodesk.autocad.customization
  5. ;;; Date: 2002-06-01 01:38:08 PST
  6. ;;;
  7. ;;; Modified by Dan to return nil if file does not exist
  8. ;;; Avoids creating blank file when none existed
  9. ;;;=========================================================================
  10. (defun SAA_IsFileWriteEnabled (file / fh)
  11.   (if (findfile file)
  12.     (if (setq fh (open file "a"))
  13.         (not (close fh))
  14.     )
  15.   )
  16. )
  17.  
  18.  
« Last Edit: October 27, 2023, 03:23:57 PM by danAllen »

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: DOS_OPENP in Lisp
« Reply #4 on: October 27, 2023, 03:37:06 PM »
danAllen, I like the routine except for the findfile part. -i.e. you should know where the file is located before checking if the file can be opened so that part I think can be removed. ...And I just noticed that the procedure uses the "APPEND" not "READ" to check if the file can be opened; I would use "READ" (per the docs).

EDIT: I just noticed your function name "is file write enabled" so my last statement does not really apply to yours I guess, but it should apply to the OP's request.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: DOS_OPENP in Lisp
« Reply #5 on: October 28, 2023, 05:11:25 AM »

I changed my function
Code: [Select]
(defun ALE_OpenP (FilNam)
  (and
    (findfile FilNam)
    (not (vl-file-rename FilNam FilNam))
  )
)
it must be considered that SAA_IsFileWriteEnabled is the inverse function
Code: [Select]
"Z:\\Temp\\foo.txt"
"SAA_IsFileWriteEnabled => " T
"DOS_OPENP              => " nil
"ALE_OpenP              => " nil
"openp                  => " #<FILE internal> <<<


"Z:\\Temp\\inexistent.txt"
"SAA_IsFileWriteEnabled => " nil  <<<
"DOS_OPENP              => " nil
"ALE_OpenP              => " nil
"openp                  => " nil


"Z:\\Temp\\opened.txt"
"SAA_IsFileWriteEnabled => " nil
"DOS_OPENP              => " T
"ALE_OpenP              => " T
"openp                  => " #<FILE internal>

Code: [Select]
(defun c:test ( / #Myfile)
  (print (setq #Myfile "Z:\\Temp\\foo.txt"))
  (print "SAA_IsFileWriteEnabled => ") (princ (SAA_IsFileWriteEnabled #Myfile))
  (print "DOS_OPENP              => ") (princ (DOS_OPENP #Myfile))
  (print "ALE_OpenP              => ") (princ (ALE_OpenP #Myfile))
  (print "openp                  => ") (princ (openp #Myfile))
  (print (setq #Myfile "Z:\\Temp\\inexistent.txt"))
  (print "SAA_IsFileWriteEnabled => ") (princ (SAA_IsFileWriteEnabled #Myfile))
  (print "DOS_OPENP              => ") (princ (DOS_OPENP #Myfile))
  (print "ALE_OpenP              => ") (princ (ALE_OpenP #Myfile))
  (print "openp                  => ") (princ (openp #Myfile))
  (print (setq #Myfile "Z:\\Temp\\opened.txt"))
  (print "SAA_IsFileWriteEnabled => ") (princ (SAA_IsFileWriteEnabled #Myfile))
  (print "DOS_OPENP              => ") (princ (DOS_OPENP #Myfile))
  (print "ALE_OpenP              => ") (princ (ALE_OpenP #Myfile))
  (print "openp                  => ") (princ (openp #Myfile))
  (princ)
)

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: DOS_OPENP in Lisp
« Reply #6 on: October 28, 2023, 06:57:20 AM »
(vl-file-systime fn)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: DOS_OPENP in Lisp
« Reply #7 on: October 28, 2023, 07:58:40 AM »
(vl-file-systime fn)

Code: [Select]

"Z:\\Temp\\foo.txt"
"DOS_OPENP              => " nil
"ALE_OpenP              => " nil
"vl-file-systime        => " nil
"Z:\\Temp\\inexistent.txt"
"DOS_OPENP              => " nil
"ALE_OpenP              => " nil
"vl-file-systime        => " T     <<<<<<<
"Z:\\Temp\\opened.txt"
"DOS_OPENP              => " T
"ALE_OpenP              => " T
"vl-file-systime        => " T
Code: [Select]
(defun c:test ( / #Myfile)
   (print (setq #Myfile "Z:\\Temp\\foo.txt"))
   (print "DOS_OPENP              => ") (princ (DOS_OPENP #Myfile))
   (print "ALE_OpenP              => ") (princ (ALE_OpenP #Myfile))
   (print "vl-file-systime        => ") (princ (not (vl-file-systime #Myfile)))
   (print (setq #Myfile "Z:\\Temp\\inexistent.txt"))
   (print "DOS_OPENP              => ") (princ (DOS_OPENP #Myfile))
   (print "ALE_OpenP              => ") (princ (ALE_OpenP #Myfile))
   (print "vl-file-systime        => ") (princ (not (vl-file-systime #Myfile)))
   (print (setq #Myfile "Z:\\Temp\\opened.txt"))
   (print "DOS_OPENP              => ") (princ (DOS_OPENP #Myfile))
   (print "ALE_OpenP              => ") (princ (ALE_OpenP #Myfile))
   (print "vl-file-systime        => ") (princ (not (vl-file-systime #Myfile)))
   (princ)
)