;; expand-num.kl 1.0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; converts nums into strings with format options

;;AUTOLOAD: expand-num
;;AUTODOC: expand-num "converts a number into a string in a fixed place"
;;(expand-num number size &key :filler :where :overflow)
;;
;;Returns a string which is a printed representation of number expansed to fill
;;a string of size chars
;;optional arguments (specified by keywords)
;;    :filler    the char to be used to fill the space. defaults to #\0
;;    :where     where to add the space to fill: 'left (default) or 'right
;;    :overflow  what to do if it cannot fit: evaluates the expression and
;;               returns it. Default is 
;;               '(String (make-list size :initial-element #\?))
;;               which just fills the field with size "?" chars
;;               The global variables 'number, 's and 'size are set to the
;;               current number, current string representing the number, and
;;               current size respectively

(defun expand-num (number size &key
    (:filler #\0)
    (:where :left)
    (:overflow '(String (make-list size :initial-element #\?)))
    s
    fill-string
  )
  (if (or (not (typep number Int))
      (< number 0)
    )
    (error "expand-num: argument %0 must be a positive integer" number)
  )
  (setq s (String number))
  (if (< (length s) size)
    (progn
      (setq fill-string (String (make-list (- size (length s))
	    :initial-element filler
      )))
      (if (/= :left where)
	(+ s fill-string)
	(+ fill-string s)
    ))
    
    (> (length s) size)
    (eval overflow)
    
    s    
))
