With unified diff.

Liam

On Thu, May 28, 2009 at 10:51 PM, Liam Healy <l...@healy.washington.dc.us> 
wrote:
> Never mind my previous patch, this one is better.
>
> Previously, in order to read double floats, I reset
> *read-default-float-format*, but that makes global changes, sometimes
> undesirable.  So instead, in this version, if the format is specified
> as "D", it will create a number that Lisp will read as double float.
> Otherwise the :format argument should be NIL or a C format
> string ("D" is unused in C).
>
> So for example
> (constant (+mksa-plancks-constant-h+ GSL_CONST_MKSA_PLANCKS_CONSTANT_H)
>          :format "D")
> becomes
> (cl:defconstant +mksa-plancks-constant-h+ 6.626069d-34)
>
> If the :format argument is not given or is nil, the expansion of constant
> should be identical to the original.
>
> Liam
>
diff -rN -u old-cffi/grovel/grovel.lisp new-cffi/grovel/grovel.lisp
--- old-cffi/grovel/grovel.lisp	2009-06-03 22:14:43.000000000 -0400
+++ new-cffi/grovel/grovel.lisp	2009-06-03 22:14:43.000000000 -0400
@@ -169,6 +169,14 @@
 #define SIGNEDP(x) (((x)-1)<0)
 #define SIGNED_(x) (SIGNEDP(x)?\"\":\"un\")
 #define SIGNED64P(x) ( x <= 0x7FFFFFFFFFFFFFFFLL )
+#define FPRINTF_WITH_A_D(stream,d) \\
+{ \\
+  char buf[32], *p; \\
+  sprintf(buf, \"%e\", d); \\
+  for( p = buf; p; p++ ) { \\
+    if( *p == 'e' ) { *p = 'd'; break; }} \\
+  fprintf(stream,\"%s\", buf); \\
+}
 
 void type_name(FILE *output, int signed_p, int size);
 
@@ -450,7 +458,7 @@
 
 ;;; Syntax differs from anything else in CFFI.  Fix?
 (define-grovel-syntax constant ((lisp-name &rest c-names)
-                                &key documentation optional)
+                                &key documentation optional format)
   (when (keywordp lisp-name)
     (setf lisp-name (format-symbol "~A" lisp-name)))
   (c-section-header out "constant" lisp-name)
@@ -460,10 +468,15 @@
     (c-format out "(cl:defconstant ")
     (c-print-symbol out lisp-name t)
     (c-format out " ")
-    (format out "~&  if(SIGNED64P(~A))~%" c-name)
-    (format out "    fprintf(output, \"%lli\", (int64_t) ~A);" c-name)
-    (format out "~&  else~%")
-    (format out "    fprintf(output, \"%llu\", (uint64_t) ~A);" c-name)
+    (if format
+        (if (string= format "D")        ; double float format
+            (format out "    FPRINTF_WITH_A_D(output, ~A);" c-name)
+            (format out "    fprintf(output, \"%~a\", ~A);" format c-name))
+        (progn
+          (format out "~&  if(SIGNED64P(~A))~%" c-name)
+          (format out "    fprintf(output, \"%lli\", (int64_t) ~A);" c-name)
+          (format out "~&  else~%")
+          (format out "    fprintf(output, \"%llu\", (uint64_t) ~A);" c-name)))
     (when documentation
       (c-format out " ~S" documentation))
     (c-format out ")~%")

_______________________________________________
cffi-devel mailing list
cffi-devel@common-lisp.net
http://common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel

Reply via email to