Function: org-babel-C-val-to-C-type

org-babel-C-val-to-C-type is a byte-compiled function defined in ob-C.el.gz.

Signature

(org-babel-C-val-to-C-type VAL)

Documentation

Determine the type of VAL.

Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type. FORMAT can be either a format string or a function which is called with VAL.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-C.el.gz
(defun org-babel-C-val-to-C-type (val)
  "Determine the type of VAL.
Return a list (TYPE-NAME FORMAT).  TYPE-NAME should be the name of the type.
FORMAT can be either a format string or a function which is called with VAL."
  (let* ((basetype (org-babel-C-val-to-base-type val))
	 (type
	  (pcase basetype
	    (`integerp '("int" "%d"))
	    (`floatp '("double" "%s")) ;; %f rounds, use %s to print the float literally
	    (`stringp
	     (list
	      (if (eq org-babel-c-variant 'd) "string" "const char*")
	      "\"%s\""))
            (_ (error "Unknown type %S" basetype)))))
    (cond
     ((integerp val) type) ;; an integer declared in the #+begin_src line
     ((floatp val) type) ;; a numeric declared in the #+begin_src line
     ((and (listp val) (listp (car val))) ;; a table
      `(,(car type)
	(lambda (val)
	  (cons
           (pcase org-babel-c-variant
             ((or `c `cpp) (format "[%d][%d]" (length val) (length (car val))))
             (`d           (format "[%d][%d]" (length (car val)) (length val))))
	   (concat
	    (if (eq org-babel-c-variant 'd) "[\n" "{\n")
	    (mapconcat
	     (lambda (v)
	       (concat
		(if (eq org-babel-c-variant 'd) " [" " {")
		(mapconcat (lambda (w) (format ,(cadr type) w)) v ",")
		(if (eq org-babel-c-variant 'd) "]" "}")))
	     val
	     ",\n")
	    (if (eq org-babel-c-variant 'd) "\n]" "\n}"))))))
     ((or (listp val) (vectorp val)) ;; a list declared in the #+begin_src line
      `(,(car type)
	(lambda (val)
	  (cons
	   (format "[%d]" (length val))
	   (concat
	    (if (eq org-babel-c-variant 'd) "[" "{")
	    (mapconcat (lambda (v) (format ,(cadr type) v)) val ",")
	    (if (eq org-babel-c-variant 'd) "]" "}"))))))
     (t ;; treat unknown types as string
      type))))