Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 3 files changed
  • 0 comments
  • 1 contributor
Jun 28, 2013
cabrero erl_interface: add vprintf style functions
As stated at: http://c-faq.com/varargs/handoff.html it is not possible to write
a function which takes a variable number of arguments and passes them to some other function (which takes a variable number of arguments). You should provide a version of that other function which accepts a va_list pointer.

So this provides:
  * ei_x_format => ei_x_vformat
  * ei_x_format_wo_ver => ei_x_vformat_wo_ver
and refactors function implementation in order to avoid code duplication.
c557be1
6  lib/erl_interface/doc/src/ei.xml
@@ -695,11 +695,13 @@ ei_x_encode_empty_list(&x);
695 695
     </func>
696 696
     <func>
697 697
       <name><ret>int</ret><nametext>ei_x_format(ei_x_buff* x, const char* fmt, ...)</nametext></name>
698  
-      <name><ret>int</ret><nametext>ei_x_format_wo_ver(ei_x_buff* x, const char *fmt, ... )</nametext></name>
  698
+      <name><ret>int</ret><nametext>ei_x_format_wo_ver(ei_x_buff* x, const char *fmt, ...)</nametext></name>
  699
+      <name><ret>int</ret><nametext>ei_x_vformat(ei_x_buff* x, const char* fmt, va_list ap)</nametext></name>
  700
+      <name><ret>int</ret><nametext>ei_x_vformat_wo_ver(ei_x_buff* x, const char *fmt, va_list ap)</nametext></name>
699 701
       <fsummary>Format a term from a format string and parameters.</fsummary>
700 702
       <desc>
701 703
         <p>Format a term, given as a string, to a buffer. This
702  
-          functions works like a sprintf for erlang terms. The
  704
+          functions works like a sprintf and svprintf for erlang terms. The
703 705
           <c><![CDATA[fmt]]></c> contains a format string, with arguments like
704 706
           <c><![CDATA[~d]]></c>, to insert terms from variables. The following
705 707
           formats are supported (with the C types given):</p>
4  lib/erl_interface/include/ei.h
@@ -525,11 +525,13 @@ int ei_print_term(FILE *fp, const char* buf, int* index);
525 525
 int ei_s_print_term(char** s, const char* buf, int* index);
526 526
 
527 527
 /*
528  
- * format to build binary format terms a bit like printf
  528
+ * format to build binary format terms a bit like printf & vprintf
529 529
  */
530 530
 
531 531
 int ei_x_format(ei_x_buff* x, const char* fmt, ...);
532 532
 int ei_x_format_wo_ver(ei_x_buff* x, const char *fmt, ...);
  533
+int ei_x_vformat(ei_x_buff* x, const char* fmt, va_list args);
  534
+int ei_x_vformat_wo_ver(ei_x_buff* x, const char *fmt, va_list args);
533 535
 
534 536
 int ei_x_new(ei_x_buff* x);
535 537
 int ei_x_new_with_version(ei_x_buff* x);
43  lib/erl_interface/src/misc/ei_format.c
@@ -442,26 +442,39 @@ static int read_args(const char* fmt, va_list ap, union arg **argp)
442 442
     return 0;
443 443
 }
444 444
        
445  
-int ei_x_format(ei_x_buff* x, const char* fmt, ... )
  445
+int ei_x_vformat_wo_ver(ei_x_buff* x, const char* fmt, va_list ap)
446 446
 {
447  
-    va_list ap;
  447
+    int res;
448 448
     union arg* args;
449 449
     union arg* saved_args; 
  450
+
  451
+    res = read_args(fmt,ap,&args);
  452
+    saved_args = args;
  453
+    if (res < 0) return -1;
  454
+
  455
+    res = eiformat(&fmt, &args, x);
  456
+    ei_free(saved_args);
  457
+
  458
+    return res;
  459
+}
  460
+
  461
+int ei_x_vformat(ei_x_buff* x, const char* fmt, va_list ap)
  462
+{
450 463
     int res;
451 464
 
452 465
     res = ei_x_encode_version(x);
453 466
     if (res < 0) return res;
  467
+    return ei_x_vformat_wo_ver(x,fmt,ap);
  468
+}
  469
+
  470
+int ei_x_format(ei_x_buff* x, const char* fmt, ... )
  471
+{
  472
+    va_list ap;
  473
+    int res;
454 474
 
455 475
     va_start(ap, fmt);
456  
-    res = read_args(fmt,ap,&args);
457  
-    saved_args = args;
  476
+    res = ei_x_vformat(x,fmt,ap);
458 477
     va_end(ap);
459  
-    if (res < 0) {
460  
-	return -1;
461  
-    }
462  
-
463  
-    res = eiformat(&fmt, &args, x);
464  
-    ei_free(saved_args);
465 478
 
466 479
     return res;
467 480
 }
@@ -469,19 +482,11 @@ int ei_x_format(ei_x_buff* x, const char* fmt, ... )
469 482
 int ei_x_format_wo_ver(ei_x_buff* x, const char* fmt, ... )
470 483
 {
471 484
     va_list ap;
472  
-    union arg* args; 
473  
-    union arg* saved_args; 
474 485
     int res;
475 486
 
476 487
     va_start(ap, fmt);
477  
-    res = read_args(fmt,ap,&args);
478  
-    saved_args = args;
  488
+    res = ei_x_vformat_wo_ver(x,fmt,ap);
479 489
     va_end(ap);
480  
-    if (res < 0) {
481  
-	return -1;
482  
-    }
483  
-    res = eiformat(&fmt, &args, x);
484  
-    ei_free(saved_args);
485 490
 
486 491
     return res;
487 492
 }

No commit comments for this range

Something went wrong with that request. Please try again.