Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a free implementation of printf for .net?

The problems:

  • I can't use string.Format, I have C style format strings;
  • I can't call the native printf (no P/Invoke);
  • I can't use http://www.codeproject.com/KB/printing/PrintfImplementationinCS.aspx because of the license, I need something GPL-compatible.

Is there a free implementation of printf/sprintf for the .net framework? Other than the above link, I couldn't find anything.

Thanks!

Update:

Thanks for the help, even though you couldn't find anything. That means I'll just have to do it myself (I was trying to avoid it, but oh well...)
I cooked up a sprintf function that supports basic format strings, you can find it here: https://sourceforge.net/projects/printfnet/. I'll try to make it a complete implementation if I can.

like image 769
Hali Avatar asked Mar 01 '10 21:03

Hali


2 Answers

Why don't you find a GPL-compatible implementation of printf written in C and port it to .NET?

like image 78
Adam Maras Avatar answered Oct 13 '22 20:10

Adam Maras


I think you want this: http://www.codeproject.com/KB/printing/PrintfImplementationinCS.aspx

It's a free implementation of a port of the C printf function to C#. You should be aware the author points out that not all features of printf are currently supported - but this may be a good starting point.

EDIT: I see that the license for that version isn't compatible with what you need - in that case, I definitely recommend looking at calling the unmanaged version directly as the following blog article discusses. It's probably the most compatible and safest thing to do.

If that doesn't cut it, here's a blog article about actually calling the unmanaged printf function:

http://community.bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx

It looks like this is all you need to call the unmanaged printf from C#:

[DllImport("msvcrt40.dll")]
public static extern int printf(string format, __arglist);

static void Main(string[] args)
{
   printf("Hello %s!\n", __arglist("Bart"));
}
like image 25
LBushkin Avatar answered Oct 13 '22 21:10

LBushkin