LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2008, 04:09 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
c++ - How to handle user specified string formatting?


I want a user to be able to give me a string:

foo.%04d.bar

such that when event '100' comes around, that string turns into:

foo.0100.bar

... and when event 597 comes around, we get

foo.0597.bar

... and so on

Is there a neat way of doing this without parsing the string char by char to find a '%', a 'd' and then figuring what's in between them? I seem to remember something like this existing, but I can't seem to remember how its done.

Thanks
 
Old 02-26-2008, 04:21 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
argh. Found the answer - vprintf

Code:
#include <stdio.h>
#include <stdarg.h>

using namespace std;
void WriteFormatted (char * format, ...)
{
  va_list args;
  va_start (args, format);
  vprintf (format, args);
  va_end (args);
}

int main ()
{
  char* foo="Hello %s, %03d.\n";
  WriteFormatted ("Call with %d variable argument.\n",1);
  WriteFormatted ("Call with %d variable %s.\n",2,"arguments");
  WriteFormatted (foo,"there",7);

   return 0;
}
outputs:
Code:
Call with 1 variable argument.
Call with 2 variable arguments.
Hello there, 007.

If there's a better way, I'll take it. Until then, this works.

(example is from cplusplus.com)

Last edited by BrianK; 02-26-2008 at 04:25 PM.
 
Old 02-26-2008, 04:46 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Code:
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
struct User_string
{
	User_string(std::string const& prefix, std::string const& postfix,const& int p):
		m_prefix(prefix),m_postfix(postfix),m_precision(p){}

	std::string create(int const& i)
	{
		std::stringstream s;
		s <<m_prefix <<std::setfill('0')<<std::setw(m_precision)<<i <<m_postfix;
		return s.str();
	}
private:
	std::string m_prefix;
	std::string m_postfix;
	int m_precision;
};

int main(int argc, char *argv[]) 
{
 	User_string s("foo.",".bar",4);
	for(int i = 0; i< 1000; ++i)
		std::cout <<s.create(i) <<std::endl;
	return 0;
}
 
Old 02-26-2008, 05:05 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by BrianK View Post
argh. Found the answer - vprintf
You should still check to make sure that you have enough arguments to match the number of %s so you don't overrun the stack. Also, you need to parse out %n because those can be used to hack your program, and maybe %m since it won't take an argument.
http://en.wikipedia.org/wiki/Format_...ulnerabilities

ta0kira
 
Old 02-26-2008, 05:55 PM   #5
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by dmail View Post
some code
no offense, but where does the user give me formatting instructions and how does this differ from printf? For the record, I get a single string from the user & am trying to avoid parsing that string by hand. Maybe I'm just missing something in your example.
Quote:
Originally Posted by ta0kira View Post
You should still check to make sure that you have enough arguments to match the number of %s so you don't overrun the stack. Also, you need to parse out %n because those can be used to hack your program, and maybe %m since it won't take an argument.
http://en.wikipedia.org/wiki/Format_...ulnerabilities
Thanks for the tip.
 
Old 02-27-2008, 08:14 AM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by BrianK View Post
no offense, but where does the user give me formatting instructions
They pass you a User_string or whatever else you want to call it.
Quote:
and how does this differ from printf?
It is type safe and does not wander into the non type safe world of the dreaded va_args.
Quote:
For the record, I get a single string from the user & am trying to avoid parsing that string by hand.
This is not what you originally wrote, it sounded like you were at the design stage.
Quote:
I want a user to be able to give me a string:
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
Handle escape characters in a string Helene Programming 7 05-01-2004 11:43 PM
parsing a user input string daphne19 Programming 1 04-22-2004 07:40 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration