[Redbutton-devel] SF.net SVN: redbutton: [352] redbutton-author/trunk/ccc.y
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-08-26 11:09:42
|
Revision: 352
http://redbutton.svn.sourceforge.net/redbutton/?rev=352&view=rev
Author: skilvington
Date: 2007-08-26 03:10:07 -0700 (Sun, 26 Aug 2007)
Log Message:
-----------
allow format strings in buf_append
Modified Paths:
--------------
redbutton-author/trunk/ccc.y
Modified: redbutton-author/trunk/ccc.y
===================================================================
--- redbutton-author/trunk/ccc.y 2007-08-24 16:07:40 UTC (rev 351)
+++ redbutton-author/trunk/ccc.y 2007-08-26 10:10:07 UTC (rev 352)
@@ -1,9 +1,11 @@
%{
+#define _GNU_SOURCE /* for vasprintf */
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <ctype.h>
#define YYSTYPE char *
@@ -70,7 +72,7 @@
void add_oneormore(struct str_list **, char *);
void buf_init(struct buf *);
-void buf_append(struct buf *, char *);
+void buf_append(struct buf *, char *, ...);
/* input line we are currently parsing */
int yylineno = 1;
@@ -270,8 +272,7 @@
struct item *item;
struct item *next;
- buf_append(&state.grammar, name);
- buf_append(&state.grammar, ":\n\t");
+ buf_append(&state.grammar, "%s:\n\t", name);
/* output each item that makes up this identifier */
for(item=state.items; item; item=item->next)
@@ -342,8 +343,7 @@
case IT_ONEORMORE:
/* add "OneOrMoreIdentifier" to the grammar */
- buf_append(&state.grammar, "OneOrMore");
- buf_append(&state.grammar, item->name);
+ buf_append(&state.grammar, "OneOrMore%s", item->name);
/* add the OneOrMoreIdentifier to our list */
add_oneormore(&state.oneormores, item->name);
break;
@@ -413,10 +413,7 @@
}
/* add it to the lex output file */
- buf_append(&state.lexer, quoted);
- buf_append(&state.lexer, "\treturn ");
- buf_append(&state.lexer, t->name);
- buf_append(&state.lexer, ";\n");
+ buf_append(&state.lexer, "%s\treturn %s;\n", quoted, t->name);
return t->name;
}
@@ -531,10 +528,17 @@
}
void
-buf_append(struct buf *b, char *app_str)
+buf_append(struct buf *b, char *fmt, ...)
{
- size_t app_len = strlen(app_str);
+ va_list ap;
+ char *app_str;
+ size_t app_len;
+ va_start(ap, fmt);
+ if((app_len = vasprintf(&app_str, fmt, ap)) < 0)
+ fatal("Out of memory or illegal format string");
+ va_end(ap);
+
/* +1 for the \0 terminator */
while(b->nalloced < b->len + app_len + 1)
{
@@ -547,6 +551,8 @@
b->len += app_len;
b->str[b->len] = '\0';
+ free(app_str);
+
return;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|