printf... how to do it?

Programming, for all ages and all languages.
jzgriffin
Member
Member
Posts: 190
Joined: Tue Sep 26, 2006 1:40 pm
Libera.chat IRC: Nokurn
Location: Ontario, CA, USA
Contact:

Post by jzgriffin »

Even more unwieldy than the GPL? Wow. Didn't know that was possible. ;-)

In short, there seem to be two good public domain printfs out there, just use those. Or, better yet, read them and figure out how they work, then do your own. :-D
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Jeremiah Griffin wrote:There doesn't seem to be a copyright anywhere, so I'm assuming it's public domain.
That's a very bad assumption to make, and IMHO.. and illegal one.. ;)

http://en.wikipedia.org/wiki/Berne_Conv ... stic_Works
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Brynet-Inc wrote:
Jeremiah Griffin wrote:There doesn't seem to be a copyright anywhere, so I'm assuming it's public domain.
That's a very bad assumption to make, and IMHO.. and illegal one.. ;)
My point exactly. A common misbelief (especially among hobbyist coders) is that if a license doesn't forbid something, it's implicitly allowed. The contrary is the case in most countries. The original Linux license said you are allowed to redistribute the kernel as long as you provide source and don't ask a commercial fee for it. It doesn't say zilch about modifying, using parts of it or anything. Linus might agree to you using early code snippets under GPL terms, but legally speaking that would still require written consent of all authors involved...

Ah, copyright law. Good thing I haven't had breakfast yet.
Every good solution is obvious once you've found it.
eddyb

Post by eddyb »

for the begin:

Code: Select all

void printf (char format[], ... )
{
  va_list arguments;                     // A place to store the list of arguments
  int i, dz_fl=0;
  char ch;
  unsigned char uctmp;
  signed char sctmp;
  unsigned short ustmp;
  signed short sstmp;
  unsigned int uitmp;
  signed int sitmp;
  va_start(arguments, format); 
  for(i=0;i<strlen(format);i++)
  {
	  ch = format[i];
	  if(dz_fl)
	  {
		  switch(ch)
		  {
		  case '%':
			  putch('%');
			  break;
		  case 'c':
			  putch(va_arg(arguments, char));
			  break;
		  case 's':
			  puts(va_arg(arguments, char));
			  break;
		  default:
			  break;

		  }
	  }
	  else
	  {
		  switch(ch)
		  {
		  case '%':
			  dz_fl=1;
			  break;
		  default:
			  if(ch != '%')
				  putch(ch);
			  break;
		  }
	  }
  }
          
  va_end(arguments);
}
But i have problems: strings(%s) don't work, and it prints the % symbol(normally, it mustn't) chars(%c) work
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Deal with it yourself. We are not here to debug your code. I am sick of your mindless spamposts, when it is obvious you have put NO effort in yourself.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Uh-huh...
  • None of your temporary variables is actually used.
  • dz_fl - which I take is the flag for "now I am within a format specifier" - is never reset to 0.
  • For strings, you pull a char from the stack (should be char *).
All in all, this looks like a classic: You jumped at what looked the easiest part of the implementation, before you had a clear design in mind. While this might result in some success immediately, it also results in much redesign later on.

Note that printf() is effectively a call to fprintf() with stdout as target stream. fprintf() in turn is simply a wrapper for vfprintf()... and you need the same format-parsing logic for the sprintf() family, too.

And perhaps you want to keep in mind that "%#+6.3d" is a valid format specifier, and that for implementing that you have to go far beyond a simple switch...
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Lets not forget the classic "%*" which will **** up the logic in an ill-designed system.
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Post by quok »

In my system, I just ported over Solar's excellent printf from PDCLib. As he says, it doesn't do floats yet, but that's not a big deal to me.

I also ported some functions from OpenBSD's libc.

Once long ago I did use Chris Giese's printf() implementation. That one is safe to use from a licensing standpoint; he explicity denies copyright to the code and places it in the public domain. Okay, well, some countries don't have the concept of public domain, but that code should still be safe to use. Just don't strip the header information from those files. :) Beware there are limitations to this implementation as well (it also doesn't do floats, and a lot of other things that PDCLib does do).

So, IMO, when it comes to printf() and friends, you have three choices.
1) Solar's PDCLib
2) Chris Geise's printf() implementation (http://my.execpc.com/~geezer/osd/libc/printf.c)
3) Rip it from OpenBSD. Their code is ISC and BSD licensed, so it's safe to use even for those of you that are creating closed-source projects.
eddyb

Post by eddyb »

now works well :D . I'm gonna implement other format types 8)
Post Reply