Edit
Attach
Printable
topic end
<!-- * Set TOPICTITLE = <nop>CoCreate Modeling code examples: A platform-independent ls -l replacement --> <style type="text/css"> pre { background-color:#FFEECC; } </style> ---++ <nop>CoCreate Modeling: A platform-independent ls -l replacement Oftentimes, users overlook the wealth of file handling functionality which is built-in into Common Lisp and also into <nop>CoCreate Modeling's dialect of the language. Way too often, it is assumed that any kind of OS-level work should best be done by calling external components, for example by running DOS or UNIX utilities via a command shell. If you need access to external utilities, =oli:sd-sys-exec= is your friend, but as a rule of thumb, if you're using =oli:sd-sys-exec=, chances are that there are much better and less platform-dependent ways to achieve the same. As an example, here is some code which does the rough equivalent of <pre> ls -l /some/directory/*somepattern* >somelogfile </pre> Sure enough, the command-line above looks simple and takes only a few seconds to type, but it simply doesn't port to non-UNIX systems (except if you install a UNIX-like shell environment on them). Here's a platform-independent approximation: <pre> ;; -*-Lisp-*- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Description: Platform-independent replacement for ls -l ;; Author: Claus Brod ;; Language: Lisp ;; ;; (C) Copyright 2005 Claus Brod, all rights reserved ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; (in-package :clausbrod.de) (export 'ls-l) (defun file-mod-time(f) (multiple-value-bind (sec minutes hour date month year) (decode-universal-time (file-write-date f)) (format nil "~D/~2,'0D/~2,'0D ~2,'0D:~2,'0D:~2,'0D" year month date hour minutes sec))) (defun print-file-info(s f) (format s "~A ~A ~A~%" (file-namestring f) (file-mod-time f) (oli:sd-inq-file-size (file-namestring f)))) (defun ls-l(dir pattern logfile) (oli:sd-with-current-working-directory dir (with-open-file (s logfile :direction :output :if-exists :supersede) (dolist (f (directory pattern)) (print-file-info s f))))) </pre> Usage example: <pre> (clausbrod.de:ls-l "c:/temp" "*.pkg" "pkg.log") </pre> It's too bad that Common Lisp doesn't have a direct equivalent of [[http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html][strftime]] - this would have saved me from <tt>format</tt>'s funky parameter syntax. But still, now we've got code which... * ... is platform-independent, * ... does not need to fork costly external processes to do its job, * ... is adaptable to particular formatting requirements, unlike external utilities such as =dir= or =ls=, where you have to live with the way they format their output and somehow cope with it. -- Main.ClausBrod - 11 Oct 2005 %COMMENT{type="below" nonotify="on"}%
to top
End of topic
Skip to action links
|
Back to top
Edit
|
Attach image or document
|
Printable version
|
Raw text
|
Refresh
|
More topic actions
Revisions: | r1.10 |
>
|
r1.9
|
>
|
r1.8
|
Total page history
|
Backlinks
You are here:
CoCreateModeling
>
MacroListDirectory
r1.10 - 13 Dec 2017 - 12:29 -
DerWolfgang
to top
CoCreateModeling
CoCreate Modeling
FAQ
Introduction
Hardware
Operating system
Memory management
File handling
Installation
Licensing
Graphics
App. knowhow
Lisp
Learning
Programming
Debugging
DDE
Compiler
Customization
Troubleshooting
Links
Code examples
Viewbench
News
Changes
Index
Search
Impressum
Home
Webs
Atari
Blog
Claus
CoCreateModeling
Klassentreffen
Main
Sandbox
Sommelier
TWiki
Xplm
My links
edit
TWiki
Welcome
TWiki Web TWiki Web Home Changes Topics Index Search
TWiki Webs Atari Blog Main
OneSpaceModeling
?
Sandbox TWiki TWiki Webs Atari Blog Main
OneSpaceModeling
?
Sandbox TWiki
Jump:
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback