reverse format string function

Hi,

I would like to take a user input string into c, perform a conversion on it and pass it to the format parameter of the printf function. Is there a function that makes this conversion?

So if the user inputs “%s\n\r” I would like to pass this to the printf as if it where typed in the code.

printf("%s\n\r", string);

same as

formatIn = (user input);

FormatConvert(format, formatIn);

printf(format, string);

TIA

Augie

Augie Henriques <augiehenriques@hotmail.com> wrote:

Hi,

I would like to take a user input string into c, perform a conversion
on it and pass it to the format parameter of the printf function.
Is there a function that makes this conversion?

So if the user inputs “%s\n\r” I would like to pass this to the
printf as if it where typed in the code.

This seems ok – why do you need to do a conversion on it?

Try this little sample program:


#include <stdio.h>
void main()
{
char ibuf[80];
char *b;

b = gets(ibuf);
printf(ibuf, b );
printf("\n");
}

If I compile & run it:

Script started on Mon Aug 12 15:16:24 2002

make form

cc form.c -o form

form

%s
%s

form

%x
8047cd0

form

%d
134511824

Script done on Mon Aug 12 15:16:45 2002

Or, maybe I’m missing something you’re trying to do…

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

David Gibbs <dagibbs@qnx.com> wrote:

Augie Henriques <> augiehenriques@hotmail.com> > wrote:
Hi,

I would like to take a user input string into c, perform a conversion
on it and pass it to the format parameter of the printf function.
Is there a function that makes this conversion?

So if the user inputs “%s\n\r” I would like to pass this to the
printf as if it where typed in the code.

This seems ok – why do you need to do a conversion on it?

“\n” isn’t the same as ‘\’ + ‘n’ :slight_smile:
That’s about the only thing I can think of – Augie will need to do
backslash escape and conversion… Otherwise, “%c\n”, will literally
print the backslash and the n.

Apart from that, like Dave sez, it’s pretty straightforward. I was
waiting for “the catch”… :slight_smile:

Cheers,
-RK

Try this little sample program:



#include <stdio.h
void main()
{
char ibuf[80];
char *b;

b = gets(ibuf);
printf(ibuf, b );
printf("\n");
}

If I compile & run it:

Script started on Mon Aug 12 15:16:24 2002

make form

cc form.c -o form

form

%s
%s

form

%x
8047cd0

form

%d
134511824

Script done on Mon Aug 12 15:16:45 2002

Or, maybe I’m missing something you’re trying to do…

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.


Robert Krten, PARSE Software Devices +1 613 599 8316.
Realtime Systems Architecture, Books, Video-based and Instructor-led
Training and Consulting at www.parse.com.
Email my initials at parse dot com.

Robert Krten <nospam83@parse.com> wrote:

David Gibbs <> dagibbs@qnx.com> > wrote:
Augie Henriques <> augiehenriques@hotmail.com> > wrote:
Hi,

I would like to take a user input string into c, perform a conversion
on it and pass it to the format parameter of the printf function.
Is there a function that makes this conversion?

So if the user inputs “%s\n\r” I would like to pass this to the
printf as if it where typed in the code.

This seems ok – why do you need to do a conversion on it?

“\n” isn’t the same as ‘\’ + ‘n’ > :slight_smile:
That’s about the only thing I can think of – Augie will need to do
backslash escape and conversion… Otherwise, “%c\n”, will literally
print the backslash and the n.

Right, the \n conversions are done by the C compiler, not by printf().

I don’t know of any function that does those sorts of conversions
automatically. I took a quick look at the source to the echo
utility (which basically does that processing), and it just has
a loop that walks through the string, with a switch/case triggered
by a \ and cases for each of the special cases.

So, I would guess that the library doesn’t have one.

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

“David Gibbs” <dagibbs@qnx.com> wrote in message
news:aj93kd$a1p$1@nntp.qnx.com

Robert Krten <> nospam83@parse.com> > wrote:
David Gibbs <> dagibbs@qnx.com> > wrote:
Augie Henriques <> augiehenriques@hotmail.com> > wrote:
Hi,

I would like to take a user input string into c, perform a conversion
on it and pass it to the format parameter of the printf function.
Is there a function that makes this conversion?

So if the user inputs “%s\n\r” I would like to pass this to the
printf as if it where typed in the code.

This seems ok – why do you need to do a conversion on it?

“\n” isn’t the same as ‘\’ + ‘n’ > :slight_smile:
That’s about the only thing I can think of – Augie will need to do
backslash escape and conversion… Otherwise, “%c\n”, will literally
print the backslash and the n.

Right, the \n conversions are done by the C compiler, not by printf().

I don’t know of any function that does those sorts of conversions
automatically. I took a quick look at the source to the echo
utility (which basically does that processing), and it just has
a loop that walks through the string, with a switch/case triggered
by a \ and cases for each of the special cases.

So, I would guess that the library doesn’t have one.

I have a reverse escape sequence conversion function. But what if the user
puts in octal or hex codes in the escape sequence. Does your function
handle this? Can I have the source code?

TIA

Augie

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Augie Henriques <augiehenriques@hotmail.com> wrote:


I have a reverse escape sequence conversion function. But what if the user
puts in octal or hex codes in the escape sequence. Does your function
handle this? Can I have the source code?

It handles octal, but not hex.

For octal, it basically has a case for \ followed by ‘0’.
Then it does a little loop for up to 3 octal digits, and
sums the three digits, then does a putchar() on that
sum – in your case, it you would put that literal number
in the out string.

Something like:


case ‘0’:
p++;
sum = 0;
int count=0;
while( count < 3 && ‘0’<=*p && *p<=‘7’ ){
sum *=8;
sum += (*p-‘0’);
count++;
p++;
}
*outp = sum;

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.