Creating an XML document ?

V

viveklinux

Hi ,

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.

So , something like

#define INSERTTAB (x) /* not sure what will come here */

INSERTTAB (depth)

thanks in advance,
vivekian
 
I

Ian Collins

Hi ,

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.
Why a macro, what's wrong with a function?
 
L

Logan Shaw

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Why not just use a library that already handles creating an XML
rather than re-inventing the wheel? XML is complicated enough that
it's unlikely you're producing valid XML anyway.

For what it's worth, if you really have to do it yourself manually
for some reason, the simplest way is to create a tree as an intermediate
form. Then walk the tree and spit out open tags when you first visit
a node and close tags when you leave the node. (That ignores the
"<foo/>"-style tags where in effect the open and close tags are
combined, but that's a simple extension.)

- Logan
 
B

Ben C

Hi ,

writing a small application in C which has to create an XML document by
reading a binary format file. Trying to get the indentation right by
using a depth global variable which will insert appropriate number of
tab spaces depending up on the depth.

Is there any way that this can be done using a macro.

So , something like

#define INSERTTAB (x) /* not sure what will come here */

INSERTTAB (depth)

The only reason I can think of for a macro (rather than a function) is
because you want a macro that expands to a string literal:

printf(INSERTTAB(n)"%s\n", tag);

which needs to expand to:

printf("\t\t\t\t""%s\n", "hello");

I'm fairly sure there's no way to do this-- even if the C preprocessor
had more functionality, n isn't known until runtime, so it's got to be
runtime code that "prints" the tabs one way or another.

You could try:

printf("%s%s\n", make_tabs(n), tag);

where make_tabs returns a string of tabs. But then you have to worry
about allocating strings. There are various options, but none of them
are very nice if the indent level gets high.

So a function to actually put the tabs in is your best bet I would say:

void insert_tabs(FILE *fp, unsigned n)
{
...
}

insert_tabs(stdout, 4);
printf("%s\n", tag);

The other option you've got is perhaps generate the xml without any
indentation at all, and then pipe it through one of the many xml
indentation programs that exist-- the xslt program to do this is
practically a one-liner.
 
M

Michael Wojcik

The only reason I can think of for a macro (rather than a function) is
because you want a macro that expands to a string literal:

printf(INSERTTAB(n)"%s\n", tag);

which needs to expand to:

printf("\t\t\t\t""%s\n", "hello");

I'm fairly sure there's no way to do this-- even if the C preprocessor
had more functionality, n isn't known until runtime, so it's got to be
runtime code that "prints" the tabs one way or another.

You're insufficiently perverse.

-----
#include <stdio.h>

#define INSERTTAB(n) (8-(n))+"\t\t\t\t\t\t\t\t"

int main(void)
{
int i;
for (i=0; i<=8; i++)
puts(INSERTTAB(i) "*");

return 0;
}
-----

Obviously this is fragile, limited, and generally awful, but it does
work for the specific case you cited.
You could try:

printf("%s%s\n", make_tabs(n), tag);

where make_tabs returns a string of tabs. But then you have to worry
about allocating strings. There are various options, but none of them
are very nice if the indent level gets high.

Here I'd almost be ready to recommend a macro that indexed into a
long constant string of tabs. No need to allocate anything, if you
can set a maximum on your indentation level. You could also limit
the indentation level in the macro, though that requires evaluating
the argument more than once except for some restricted cases (eg
where you can use binary-and or a similar operation to truncate it).

--
Michael Wojcik (e-mail address removed)

The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
 
B

Ben C

You're insufficiently perverse.

-----
#include <stdio.h>

#define INSERTTAB(n) (8-(n))+"\t\t\t\t\t\t\t\t"

int main(void)
{
int i;
for (i=0; i<=8; i++)
puts(INSERTTAB(i) "*");

return 0;
}

Most ingenious!
Here I'd almost be ready to recommend a macro that indexed into a
long constant string of tabs. No need to allocate anything, if you
can set a maximum on your indentation level.

I did think of that one, and lumped it with "solutions that aren't very
nice if the indent level gets high".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top