School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 309,254 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,513 people online right now. Registration is fast and FREE... Join Now!




PrintfExt Idea

 

PrintfExt Idea, An extended printf idea!

Hyper

27 Jan, 2009 - 01:59 PM
Post #1

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 99 times
Dream Kudos: 425
My Contributions
Hello, I've pretty much spent a day or so on this.
It'd be very useful if it were complete, and I lack the knowledge to complete it (or at least the will power)...

So here's what (very little) I have so far, basically, because people on this forum like to ask "How do I clear this single line?" or "How do I add color?" or "How do I move text to a specific spot?" alot, I've decided to write this:

printfExt
#include <stdarg.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

void printfExt(char *Text, ...) {

va_list vl;
va_start(vl, Text);

while (Text != NULL) {
for (int x = 0; x < strlen(Text); x++) {
if (Text[x] == '%') {
switch (Text[x + 1]) {
case 'i': printf("TOKEN \"i\" detected\n"); break;
case 'c': printf("TOKEN \"c\" detected\n"); break;
case 's': printf("TOKEN \"s\" detected\n"); break;
}
}
}

// printf("Argument size: %i\n", strlen(Text));
Text = va_arg(vl, char *);
}

va_end(vl);
}


int main() {

printfExt("%c%i%s", NULL);

cin.get();
return 0;
}


The basic idea was to have it function identical to printf with all the normal tokens (%i, %f, %d, %c, %s, etc...) and just simply add (hence EXTended) two or three tokens, namely:
- "%m" - takes a COORD type to move it to X, Y coordinates
- "%z" - takes a COORD type to hold background/foreground colors (enumeration would make it VERY simple)

A basic idea was this (as an example):
You want to print out (very colorfully) "Happy Birthday!" for a friend, and you want each and every other letter colored differently, now of course you could go something like this...
CODE
printf("H");
/* change color */
printf("a");
/* change color */
printf("p");
/* change color*/
... etc


But that's a hassle obviously. You could take the route of using a modified cout, so you could do something like this:
CODE
cout << "H" << GREEN << "a" << RED << "p" << GREY << "p" << PURPLE << "p" << YELLOW << "y";
etc...


But what if you wanted to do more than that? What if you wanted to print certain data to the screen, on different parts at the same time on a single call and color them differently? Of course, I doubt you'd ever want to do all it at once, but very very seldom, you might need/want to. Options are always a great thing to have in life. Especially when it comes to programming, but back to the point!

An example of what I mean would be something like, a call like this:
CODE
printfExt("%m%zScore: %i%m%zLives: %i%m%zAmmunition: %i", SCORE_COORD, SCORE_COLOR, Score, LIVES_COORD, LIVES_COLOR, Lives, AMMO_COORD, AMMO_COLOR, Ammo);


I apologize for the poor names, and formatting, I'm sure there's an alternative method that could be achieved, such as having the "%m" token accept two numbers instead of one (like the convetional printf, eg: printf("Value: %i", Value);), sort of like: printfExt("%mHello World!", 15, 15); would print "Hello World" on the X, Y coordinates of 15, 15. The same could go for background/foreground color. Although if the user omits one, it could somehow detect it and compensate for it.

Summarization:
- I spent very little time on this
- I spent a great deal thinking about it
- I'm hoping somebody else will complete it (and take full credit)
- I'm hoping everybody will benefit from it, and use it

If this ever does become a usable function, there'll be no more "SetColor" "gotoxy" etc, functions.
It'll become a readable, standardized (hopefully) formatted function. Although, there's nothing wrong with explicity using SetColor or gotoxy or any other semi-standard calls, it's just when you change the colors alot it can get a little cumbersome to keep track of.

Thanks (if anybody finishes to it or helps) in advance, I'll continue to think of ideas of how to solve it.
Welcome to anybody who might find this useful. smile.gif

EDIT: Fixed grammar, and spelling errors.
NOTE: This is a 2nd post, the first was in "C and C++," figured this fit the bill in both places.

User is offlineProfile CardPM
+Quote Post


Jayman

RE: PrintfExt Idea

27 Jan, 2009 - 02:41 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 8,591



Thanked: 238 times
Dream Kudos: 500
Expert In: Everything

My Contributions
I've deleted your other topic. It only needs to be posted once.
User is offlineProfile CardPM
+Quote Post

Hyper

RE: PrintfExt Idea

5 Mar, 2009 - 09:49 PM
Post #3

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 99 times
Dream Kudos: 425
My Contributions
x2 months later, and a massively side-tracked project:

printfExt
void printfExt(const char *Text, ...) {

va_list argptr;
va_start(argptr, Text);

while (*Text != '\0') {

/* # is a token */
if (*Text == '#') {
*Text++;

if (*Text == 'c') {
int color = va_arg(argptr, int);
SetColor(color);
}

if (*Text == 'p') {
COORD MoveCursor = va_arg(argptr, COORD);
PlaceCursor(MoveCursor.X, MoveCursor.Y);
}
} else { printf("%c", *Text); }

*Text++;
}

va_end(argptr);
}


Now I can improve what I write. smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/26/09 09:54AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month