CoCreate User Forum  

Go Back   CoCreate User Forum > Support > Customization
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
  #1  
Old 05-04-2004, 12:25 AM
Harry Harry is offline
Registered User
 
Join Date: Jan 2004
Posts: 85
Question read file and place line in property list

Hello,

I want to make a LISP function to read a line of a file and place it in a property list.

Code:
(defun read-param-file (&key paramfile)
	;function: read param file
	;date    : 03-05-2004
	;----------------------------------------------------------------------
	;input : :paramfile [string] , name parameter file

	(progn
		;local vars
		(let (a_line a_list)

			;read file and fill list variable
			(with-open-file (input-stream paramfile :direction :input)
				(loop
					(setf a_line (read-line input-stream nil 'eof))
					(when (eq a_line 'eof) (loop-finish))
					(push a_line a_list)
				);loop
			)
		);let
	);progn

);defun
the paramfile looks like this:

Code:
:objectfile "Z:/users/harry/lisp/utils/models/assy1.pkg" :vx 0 :vy 0 :vz 0 :ax 0 :ay 0 :az 0
:objectfile "Z:/users/harry/lisp/utils/models/assy2.pkg" :vx 100 :vy 200 :vz 50 :ax 0 :ay 0 :az 0

I have to convert a_line into a property list. Has anyone done this before?
Reply With Quote
  #2  
Old 05-04-2004, 03:27 AM
dorothea dorothea is offline
Registered User
 
Join Date: Nov 2002
Location: Nufringen near Stuttgart
Posts: 157
Hello Harry,

I haven't done this before. But here are some commands you could use.

* (sd-string-split a-line " ") splits up your string into a list of single strings. Every single string can be handled by itself.
* (read-from-string a-string) converts the data from string to keyword or number or whatever

Perhaps this helps you to come a step further.

Dorothea
Reply With Quote
  #3  
Old 05-04-2004, 10:49 AM
Andy Poulsen Andy Poulsen is offline
Administrator
 
Join Date: Apr 2003
Location: Fort Collins, Colorado
Posts: 273
Hi Harry,

You can make life much easier if you are able to change the parameter file to have parentheses around each record i.e.

Code:
(:objectfile "Z:/users/harry/lisp/utils/models/assy1.pkg" :vx 0 :vy 0 :vz 0 :ax 0 :ay 0 :az 0)
(:objectfile "Z:/users/harry/lisp/utils/models/assy2.pkg" :vx 100 :vy 200 :vz 50 :ax 0 :ay 0 :az 0)
The advantage of doing it this way is that the lisp reader will read the entire object for you, including the keywords. For example, the following function will read in your file and build a list of records:

Code:
(in-package :test)
(use-package :oli)

(defun read-param-file (fname)
  (let ((paramlist) ; this will store the records
	(data) ; temp location for each record
	)
    (with-open-file (f fname :direction :input)
      (loop while (setf data (read f nil)) do
	(setq paramlist (append
			 paramlist
			 (list data)
			 ) ;append
	      ) ; setq
	) ; loop
      ) ; with-open-file

    ;; now display the list we've collected for verification
    (display paramlist)   
    paramlist   ; returns list to calling function
    )
  )
(Note that the use of "append" in this case leaves the order of the records the same as they are listed in the file. Using "push" will reverse the record order -- just making sure you get what you want!)

After calling the above function, you can get access to the individual data records in the list using any of the list operators such as nth, first, dolist, etc. For example:

Code:
(defun show-filenames (fname)
  (let ((paramlist))
    (setq paramlist (read-param-file fname))
    (dolist (data paramlist) ;iterates over paramlist
      (display (format nil "Filename: ~A"
		       (getf data :objectfile)))
      )
    )
  )
this function will print out

Filename: Z:/users/harry/lisp/utils/models/assy1.pkg
Filename: Z:/users/harry/lisp/utils/models/assy2.pkg

from the data above (when given the right filename, of course).

Notice that this keeps your property list intact for each record -- all of the reference keywords are used just as you entered them in the data file, and you don't have to do any fancy manipulation to get them organized.

If this isn't clear, please let me know. I hope there is enough detail above to help you get what you need.

If you are not able to put the parens in your parameter file, Dorthea's suggestions are a good workaround, though it's much nicer to let the lisp reader do the work for you!

Good luck!

andy
__________________
Andy Poulsen
AI MAXTools: Dream. Design. Done. It's that easy!
Add-ins bringing new functionality and speed to Creo Elements/Direct and CoCreate products. Now available for v17-v20+!
See them in action at www.ai-maxtools.com and then try them for yourself -- FREE!

Last edited by Andy Poulsen; 05-04-2004 at 10:54 AM.
Reply With Quote
  #4  
Old 05-06-2004, 10:47 PM
Harry Harry is offline
Registered User
 
Join Date: Jan 2004
Posts: 85
Thanks Dorothea and Andy for your help.

I could solve the problem.

Best regards Harry
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 09:08 PM.



Hosted by SureServer    Forums   Modeling FAQ   Macro Site   Vendor/Contractors   Software Resellers   CoCreate   Gallery   Home   Board Members   Regional User Groups  By-Laws  

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
You Rated this Thread: