aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2011-05-19 09:49:20 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2011-05-19 09:49:20 +0300
commit304935a296997c9781688f08693ef70180dd24e4 (patch)
tree7b7801f3a97a780af1719826b2b9ef471151a352 /src
parent6ef143bd53c35635b6d7edc6410d45fa58f54da2 (diff)
downloadgrecs-304935a296997c9781688f08693ef70180dd24e4.tar.gz
grecs-304935a296997c9781688f08693ef70180dd24e4.tar.bz2
Reimplement grecs_format_ familiy as output-independent functions.
* src/format.c (grecs_format_docstring): Rename to grecs_print_docstring. (grecs_format_simple_statement): Rename to grecs_print_simple_statement. (grecs_format_block_statement): Rename to grecs_print_block_statement. (grecs_format_statement_array): Rename to grecs_print_statement_array. (grecs_format_locus): Rename to grecs_print_locus. (grecs_format_node_path): Rename to grecs_print_node_path. (grecs_format_value): Rename to grecs_print_value. (grecs_format_node): Rename to grecs_print_node. (grecs_format_locus,grecs_format_node_path) (grecs_format_value,grecs_format_node): Reimplement as output-independent functions. All uses changed. * src/grecs.h: Update prototypes. * doc/grecs_format_locus.3: Update. * doc/grecs_format_node.3: Update. * doc/grecs_format_node_path.3: Update. * doc/grecs_format_value.3: Update.
Diffstat (limited to 'src')
-rw-r--r--src/format.c203
-rw-r--r--src/grecs.h49
2 files changed, 140 insertions, 112 deletions
diff --git a/src/format.c b/src/format.c
index a6b3bd9..9e88412 100644
--- a/src/format.c
+++ b/src/format.c
@@ -76,7 +76,7 @@ format_level(unsigned level, FILE *stream)
}
void
-grecs_format_docstring(const char *docstring, unsigned level, FILE *stream)
+grecs_print_docstring(const char *docstring, unsigned level, FILE *stream)
{
size_t len = strlen(docstring);
int width = 78 - level * 2;
@@ -120,13 +120,13 @@ grecs_format_docstring(const char *docstring, unsigned level, FILE *stream)
}
void
-grecs_format_simple_statement(struct grecs_keyword *kwp, unsigned level,
+grecs_print_simple_statement(struct grecs_keyword *kwp, unsigned level,
FILE *stream)
{
const char *argstr;
if (kwp->docstring)
- grecs_format_docstring(kwp->docstring, level, stream);
+ grecs_print_docstring(kwp->docstring, level, stream);
format_level(level, stream);
if (kwp->argname)
@@ -153,65 +153,75 @@ grecs_format_simple_statement(struct grecs_keyword *kwp, unsigned level,
}
void
-grecs_format_block_statement(struct grecs_keyword *kwp, unsigned level,
+grecs_print_block_statement(struct grecs_keyword *kwp, unsigned level,
FILE *stream)
{
if (kwp->docstring)
- grecs_format_docstring(kwp->docstring, level, stream);
+ grecs_print_docstring(kwp->docstring, level, stream);
format_level(level, stream);
fprintf(stream, "%s", kwp->ident);
if (kwp->argname)
fprintf(stream, " <%s>", gettext(kwp->argname));
fprintf(stream, " {\n");
- grecs_format_statement_array(kwp->kwd, 0, level + 1, stream);
+ grecs_print_statement_array(kwp->kwd, 0, level + 1, stream);
format_level(level, stream);
fprintf(stream, "}\n");
}
void
-grecs_format_statement_array(struct grecs_keyword *kwp,
- unsigned n,
- unsigned level,
- FILE *stream)
+grecs_print_statement_array(struct grecs_keyword *kwp,
+ unsigned n,
+ unsigned level,
+ FILE *stream)
{
for (; kwp->ident; kwp++, n++) {
if (n)
fputc('\n', stream);
if (kwp->type == grecs_type_section)
- grecs_format_block_statement(kwp, level, stream);
+ grecs_print_block_statement(kwp, level, stream);
else
- grecs_format_simple_statement(kwp, level, stream);
+ grecs_print_simple_statement(kwp, level, stream);
}
}
void
-grecs_format_locus(grecs_locus_t *locus, FILE *fp)
+grecs_format_locus(grecs_locus_t *locus, struct grecs_format_closure *clos)
{
- if (locus)
- fprintf(fp, "%s:%d", locus->file, locus->line);
+ if (locus) {
+ char *str = NULL;
+ size_t size = 0;
+ grecs_asprintf(&str, &size, "%s:%d", locus->file, locus->line);
+ clos->fmtfun(str, clos->data);
+ free(str);
+ }
}
void
-grecs_format_node_path(struct grecs_node *node, int flags, FILE *fp)
+grecs_format_node_path(struct grecs_node *node, int flags,
+ struct grecs_format_closure *clos)
{
- int delim = flags & 0xff;
+ char delim[2] = ".";
+
if (node->up)
- grecs_format_node_path(node->up, flags, fp);
+ grecs_format_node_path(node->up, flags, clos);
if (node->type == grecs_node_root)
return;
- fputc(delim ? delim : '.', fp);
- fprintf(fp, "%s", node->ident);
+ if (flags & 0xff)
+ delim[0] = flags & 0xff;
+ clos->fmtfun(delim, clos->data);
+ clos->fmtfun(node->ident, clos->data);
if (node->type == grecs_node_block &&
!GRECS_VALUE_EMPTY_P(node->v.value)) {
- fputc('=', fp);
+ clos->fmtfun("=", clos->data);
grecs_format_value(node->v.value, flags|GRECS_NODE_FLAG_QUOTE,
- fp);
+ clos);
}
}
void
-grecs_format_value(struct grecs_value *val, int flags, FILE *fp)
+grecs_format_value(struct grecs_value *val, int flags,
+ struct grecs_format_closure *clos)
{
int i;
struct grecs_list_entry *ep;
@@ -234,89 +244,36 @@ grecs_format_value(struct grecs_value *val, int flags, FILE *fp)
wordsplit_c_quote_copy(cbuf, val->v.string,
flags & GRECS_NODE_FLAG_QUOTE_HEX);
cbuf[clen] = 0;
- fprintf(fp, "\"%s\"", cbuf);
- grecs_free(cbuf);
- } else
- fprintf(fp, "%s", val->v.string);
- break;
-
- case GRECS_TYPE_LIST:
- fputc('(', fp);
- for (ep = val->v.list->head; ep; ep = ep->next) {
- grecs_format_value(ep->data, flags, fp);
- if (ep->next) {
- fputc(',', fp);
- fputc(' ', fp);
- }
- }
- fputc(')', fp);
- break;
-
- case GRECS_TYPE_ARRAY:
- for (i = 0; i < val->v.arg.c; i++) {
- if (i)
- fputc(' ', fp);
- grecs_format_value(val->v.arg.v[i], flags, fp);
- }
- }
-}
-
-void
-grecs_txtacc_format_value(struct grecs_value *val, int flags,
- struct grecs_txtacc *acc)
-{
- int i;
- struct grecs_list_entry *ep;
- size_t clen;
- int need_quote;
-
- if (!val)
- return;
- switch (val->type) {
- case GRECS_TYPE_STRING:
- clen = wordsplit_c_quoted_length(val->v.string,
- flags & GRECS_NODE_FLAG_QUOTE_HEX,
- &need_quote);
- if (flags & GRECS_NODE_FLAG_QUOTE)
- need_quote = 1;
- else if (flags & GRECS_NODE_FLAG_NOQUOTE)
- need_quote = 0;
- if (need_quote) {
- char *cbuf = grecs_malloc(clen + 1);
- wordsplit_c_quote_copy(cbuf, val->v.string,
- flags & GRECS_NODE_FLAG_QUOTE_HEX);
- grecs_txtacc_grow_char(acc, '"');
- grecs_txtacc_grow(acc, cbuf, clen);
- grecs_txtacc_grow_char(acc, '"');
+ clos->fmtfun("\"", clos->data);
+ clos->fmtfun(cbuf, clos->data);
+ clos->fmtfun("\"", clos->data);
grecs_free(cbuf);
} else
- grecs_txtacc_grow(acc, val->v.string,
- strlen(val->v.string));
+ clos->fmtfun(val->v.string, clos->data);
break;
case GRECS_TYPE_LIST:
- grecs_txtacc_grow_char(acc, '(');
+ clos->fmtfun("(", clos->data);
for (ep = val->v.list->head; ep; ep = ep->next) {
- grecs_txtacc_format_value(ep->data, flags, acc);
- if (ep->next) {
- grecs_txtacc_grow_char(acc, ',');
- grecs_txtacc_grow_char(acc, ' ');
- }
+ grecs_format_value(ep->data, flags, clos);
+ if (ep->next)
+ clos->fmtfun(", ", clos->data);
}
- grecs_txtacc_grow_char(acc, ')');
+ clos->fmtfun(")", clos->data);
break;
case GRECS_TYPE_ARRAY:
for (i = 0; i < val->v.arg.c; i++) {
if (i)
- grecs_txtacc_grow_char(acc, ' ');
- grecs_txtacc_format_value(val->v.arg.v[i], flags, acc);
+ clos->fmtfun(" ", clos->data);
+ grecs_format_value(val->v.arg.v[i], flags, clos);
}
}
}
void
-grecs_format_node(struct grecs_node *node, int flags, FILE *fp)
+grecs_format_node(struct grecs_node *node, int flags,
+ struct grecs_format_closure *clos)
{
const char *delim_str = NULL;
@@ -327,31 +284,85 @@ grecs_format_node(struct grecs_node *node, int flags, FILE *fp)
case grecs_node_block:
if (!(flags & GRECS_NODE_FLAG_NODESCEND)) {
for (node = node->down; node; node = node->next) {
- grecs_format_node(node, flags, fp);
+ grecs_format_node(node, flags, clos);
if (node->next)
- fputc('\n', fp);
+ clos->fmtfun("\n", clos->data);
}
break;
}
case grecs_node_stmt:
if (flags & GRECS_NODE_FLAG_LOCUS) {
- grecs_format_locus(&node->locus, fp);
+ grecs_format_locus(&node->locus, clos);
delim_str = ": ";
}
if (flags & GRECS_NODE_FLAG_PATH) {
if (delim_str)
- fprintf(fp, "%s", delim_str);
- grecs_format_node_path(node, flags, fp);
+ clos->fmtfun(delim_str, clos->data);
+ grecs_format_node_path(node, flags, clos);
delim_str = ": ";
}
if (flags & GRECS_NODE_FLAG_VALUE) {
if (delim_str)
- fprintf(fp, "%s", delim_str);
- grecs_format_value(node->v.value, flags, fp);
+ clos->fmtfun(delim_str, clos->data);
+ grecs_format_value(node->v.value, flags, clos);
}
}
}
+
+
+static int
+txtacc_fmt(const char *str, void *data)
+{
+ struct grecs_txtacc *acc = data;
+ grecs_txtacc_grow(acc, str, strlen(str));
+ return 0;
+}
+
+void
+grecs_txtacc_format_value(struct grecs_value *val, int flags,
+ struct grecs_txtacc *acc)
+{
+ struct grecs_format_closure clos = { txtacc_fmt, acc };
+ grecs_format_value(val, flags, &clos);
+}
+
+
+static int
+file_fmt(const char *str, void *data)
+{
+ fputs(str, (FILE*)data);
+ return 0;
+}
+
+void
+grecs_print_locus(grecs_locus_t *locus, FILE *fp)
+{
+ struct grecs_format_closure clos = { file_fmt, fp };
+ grecs_format_locus(locus, &clos);
+}
+
+void
+grecs_print_node_path(struct grecs_node *node, int flag, FILE *fp)
+{
+ struct grecs_format_closure clos = { file_fmt, fp };
+ grecs_format_node_path(node, flag, &clos);
+}
+
+void
+grecs_print_value(struct grecs_value *val, int flags, FILE *fp)
+{
+ struct grecs_format_closure clos = { file_fmt, fp };
+ grecs_format_value(val, flags, &clos);
+}
+
+void
+grecs_print_node(struct grecs_node *node, int flags, FILE *fp)
+{
+ struct grecs_format_closure clos = { file_fmt, fp };
+ grecs_format_node(node, flags, &clos);
+}
+
diff --git a/src/grecs.h b/src/grecs.h
index 3662204..fd44e40 100644
--- a/src/grecs.h
+++ b/src/grecs.h
@@ -279,23 +279,27 @@ void grecs_include_path_setup_v(char **dirs);
ssize_t grecs_getline(char **pbuf, size_t *psize, FILE *fp);
const char *grecs_data_type_string(enum grecs_data_type type);
-void grecs_format_docstring(const char *docstring, unsigned level,
- FILE *stream);
-void grecs_format_simple_statement(struct grecs_keyword *kwp,
- unsigned level, FILE *stream);
-void grecs_format_block_statement(struct grecs_keyword *kwp,
+void grecs_print_docstring(const char *docstring, unsigned level,
+ FILE *stream);
+void grecs_print_simple_statement(struct grecs_keyword *kwp,
unsigned level, FILE *stream);
-void grecs_format_statement_array(struct grecs_keyword *kwp,
- unsigned n,
- unsigned level, FILE *stream);
-
-void grecs_format_locus(grecs_locus_t *locus, FILE *fp);
-void grecs_format_node_path(struct grecs_node *node, int flag, FILE *fp);
-void grecs_format_value(struct grecs_value *val, int flags, FILE *fp);
+void grecs_print_block_statement(struct grecs_keyword *kwp,
+ unsigned level, FILE *stream);
+void grecs_print_statement_array(struct grecs_keyword *kwp,
+ unsigned n,
+ unsigned level, FILE *stream);
+
+struct grecs_format_closure
+{
+ int (*fmtfun)(const char *, void *);
+ void *data;
+};
-struct grecs_txtacc;
-void grecs_txtacc_format_value(struct grecs_value *val, int flags,
- struct grecs_txtacc *acc);
+void grecs_format_locus(grecs_locus_t *locus, struct grecs_format_closure *fp);
+void grecs_format_node_path(struct grecs_node *node, int flag,
+ struct grecs_format_closure *fp);
+void grecs_format_value(struct grecs_value *val, int flags,
+ struct grecs_format_closure *fp);
#define GRECS_NODE_FLAG_LOCUS 0x0100
#define GRECS_NODE_FLAG_PATH 0x0200
@@ -306,7 +310,20 @@ void grecs_txtacc_format_value(struct grecs_value *val, int flags,
#define GRECS_NODE_FLAG_NODESCEND 0x4000
#define GRECS_NODE_FLAG_DEFAULT \
(GRECS_NODE_FLAG_PATH|GRECS_NODE_FLAG_VALUE|GRECS_NODE_FLAG_QUOTE)
-void grecs_format_node(struct grecs_node *node, int flags, FILE *fp);
+void grecs_format_node(struct grecs_node *node, int flags,
+ struct grecs_format_closure *fp);
+
+void grecs_print_locus(grecs_locus_t *locus, FILE *fp);
+void grecs_print_node_path(struct grecs_node *node, int flag, FILE *fp);
+void grecs_print_value(struct grecs_value *val, int flags, FILE *fp);
+
+void grecs_print_node(struct grecs_node *node, int flags, FILE *fp);
+
+struct grecs_txtacc;
+void grecs_txtacc_format_value(struct grecs_value *val, int flags,
+ struct grecs_txtacc *acc);
+
+
struct grecs_list *grecs_list_create(void);
size_t grecs_list_size(struct grecs_list *lp);

Return to:

Send suggestions and report system problems to the System administrator.