;;;; -*-Mode:LISP; Package:LISP; Base:10; Syntax:ISLISP -*-
;;;; Title:	testext.lsp
;;;; Author:	C. Jullien
;;;; License:   New BSD license
;;;; CVS:	"$Id: testext.lsp,v 1.9 2013/06/15 06:24:33 jullien Exp $"

;;; Test external definition

(require "extern")

(defconstant size_t '(<integer>  . "size_t"))
(defconstant file_t '(<external> . "FILE *"))

(defun test-external ()
   (external-module external external ("<stdio.h>")
      (constant    <integer>     |OLMAXIOBUFLEN|)
      (constant    <external>    (stdout *stdout*))
      (constant    <external>    (stdin  *stdin*))
      (constant    <external>    (stderr *stderr*))
      (<external>  |fopen|
                   (<string> <string>))
      (<integer>   |fread|
                   (<rstring> #.size_t #.size_t #.file_t))
      (<string>    |fgets|
                   (<string> <integer> #.file_t))
      (<character> |fgetc|
                   (#.file_t))
      (<character> |fputc|
                   (<character> #.file_t))
      (<integer>   |fwrite|
                   (<string> #.size_t #.size_t #.file_t))
      (<integer>   |fflush|
                   (#.file_t))
      (<integer>   |ftell|
                   (#.file_t))
      (<null>      |fclose|
                   (#.file_t))
   )
)

(test-external)

#|
;; usage sample:

(defun file-content (file)
   (let ((fd   (fopen file "rb"))
         (buf  (create-string |OLMAXIOBUFLEN|))
         (res  "")
         (sum  0)
         (len  0))
        (unwind-protect
                (while (> (setf len (fread buf 1 (length buf) fd)) 0)
                       (setf sum (+ sum len))
                       (when (< len (length buf))
                             (setf buf (subseq buf 0 len)))
                       (setf res (string-append res buf)))
                (fclose fd))
        res))
|#