Tips to improve my program
Posted: Wed Nov 24, 2010 3:02 pm
Hello,
I am new to Lisp and wrote a little program that:
(1) Load a file with sports. (One sport per line.)
(2) Load a Java file as template.
(3) Clean the single sport word.
(4) Replace a placeholder (*template-holder*) in the template file with the sport word.
(5) Store the template in a new file with the sport as file name.
I'm still in the learning phase and would be happy if there are some comments on how I can make the code more simple and better. I know that this code is not really good.
I am new to Lisp and wrote a little program that:
(1) Load a file with sports. (One sport per line.)
(2) Load a Java file as template.
(3) Clean the single sport word.
(4) Replace a placeholder (*template-holder*) in the template file with the sport word.
(5) Store the template in a new file with the sport as file name.
Code: Select all
(defparameter *template* "Action-Template.java")
(defparameter *sports* "sports.csv")
(defparameter *action-folder* "actions/")
(defparameter *action-class* ".java")
(defparameter *template-holder* "#NAME#")
(defun load-file-ms (file)
"Loads the single lines of the template file into a list."
(with-open-file (stream file :direction :input)
(loop for line = (read-line stream nil nil)
and line-count from 0
while line
collect line into lines
finally (return (values lines line-count)))))
(defun load-file-os (file)
"Returns the whole content of the file in one string."
(with-open-file (stream file)
(let* ((len (file-length stream))
(data (make-string len)))
(values data (read-sequence data stream)))))
(defun save-file(file content)
"Save the content in the given file."
(with-open-file (stream file :direction :output)
(print (output content) stream)))
(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part
is replaced with replacement. !! For long strings, this version
is notptimized!!
This code is from: http://cl-cookbook.sourceforge.net/strings.html"
(with-output-to-string (out)
(loop with part-length = (length part)
for old-pos = 0 then (+ pos part-length)
for pos = (search part string
:start2 old-pos
:test test)
do (write-string string out
:start old-pos
:end (or pos (length string)))
when pos do (write-string replacement out)
while pos)))
(defun clean(string)
"Replaces bad characters in the given string."
(setq string (replace-all string " " "_"))
(dolist (toreplace '("?" "/" "(" ")" "," "."))
(setq string (replace-all string toreplace "")))
(dolist (toreplace '("-" "__"))
(setq string (replace-all string toreplace "_")))
string)
(defun create-classes()
"Replaces a template string in the template file with another
string and stores the result in a file that has the name of
the replacement string."
(let ((sports (load-file-ms *sports*))
(jclass (load-file-os *template*)))
(dolist (item sports) (create-sport jclass (clean item)))))
(defun create-sport (template item)
"Save the template in a file and replaces the occurence of the
*template-holder* by the item."
(save-file (concatenate 'string *action-folder* item *action-class*) (replace-all template *template-holder* item)))