Calling a var args function from another var args function

First of all, I have no access to CRT functions.

What I want is to have a nice little printf and sprintf implementation.

I managed to do the printf with a simple parser (it knows %s, %s, %x, \n and \\ in case I want to escape something).
This was a bit... not so nicely done because I had the video memory address hardcoded as global variable and from the parser I was calling a function that simply knew how to write an attribute and a character to that hardocded address.
1
2
3
4
5
INT32
printfParser(
	PCHAR	format,
	...
	);


The function that prints to the video memory is nothing special:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void
PutChar(
	char	printThis
	)
{
	WORD	color			= MakeColor(COLOR_WHITE, COLOR_BLACK);
	DWORD	writeTo			= terminalRow * VGA_WIDTH + terminalColumn;

	terminal[writeTo]		= (printThis | color << 8);

	ModifyPosition(
		terminalRow, 
		terminalColumn + 1);
}

ModifyPosition is self-explanatory.

Then I realized that sprintf is quite a useful little guy, so I proceeded to write it. The prototype is the usual:
1
2
3
4
5
6
INT32 
sprintf ( 
	PCHAR	str, 
	PCHAR	format, 
	... 
	);



So I told myself it's time to make the parser friendly to others so I made it to take a pointer to a buffer
1
2
3
4
5
6
INT32
printfParser(
        PCHAR   target,
	PCHAR	format,
	...
	);


The main idea is that now printfParser will simply write a formatted string to a char buffer (so it's more like sprintf). printf's job will be to take that string and write it to the terminal (adding attributes and all those fun stuff like color text).
Now, sprintf works simply by doing just this:
1
2
3
4
return printfParser(
    str,
    format
    );


But now there are a few things I don't like about printf:
1. it must use a local buffer first and I have no way of knowing how big that buffer must be so I go by 80 * 25 which is the size of the video memory.
2. it does not work (slightly more important than the first).
So, from printf I call sprintf:
[code]
[/co
INT32
printf_(
PCHAR format,
...
)
{
char buffer[80 * 25];
INT32 returnValue = 0;
returnValue = sprintf_(
buffer,
format
);

OutputString(buffer);
return returnValue;
[code]

The first string (in other words format) is successfully sent to the parser, but the rest is lost). This seems a bit obvious as there is no way for printf to know how many arguments to pass further so sprintf and so on. But if this is the case, why is sprintf working? It is also receiving a variable nr of arguments and it sends all of them to the parser.
You want to write vsprintf(): http://www.cplusplus.com/reference/cstdio/vsprintf/
Once you have this the other functions become calls to it.
> First of all, I have no access to CRT functions.
> What I want is to have a nice little printf and sprintf implementation.

Rip them off from here (non-viral license):
https://code.google.com/p/freebsd-head/source/browse/lib/libc/stdio/?r=d2f27255efc5e2d16c5cd4293bb31fbb204071fa
@dhayden: I think that will simplify things and also move all the important code in one place so it will be easier to debug. It should have been the obvious choice.

@JLBorges: that's slightly more complex than I'd like (all I need is a quick way of logging something to the screen and building formatted strings), but it could serve as a source of inspiration for what I want to do.

Thank you both :)

Topic archived. No new replies allowed.