basic string formating

Aug 2, 2010 at 1:23am
hi, im working on a simple script language, and so far i got it working great (with help from Athar, thanx again buddy). now one thing im having a little trouble with is taking text from the file, such as a line to print and formatting it to display the variables in the script. the way i was doing it was using the replace function from the string class (using the example for it on this site as a starting point) but it wasnt able to get it to work. i want to be able to format it in a similar way to the C/C++ printf function. thnks in advance.
Aug 2, 2010 at 1:26am
So, what exactly is the problem? What isn't working?
Aug 2, 2010 at 1:33am
it wasnt replacing the variables properly. i was getting it to search for a specific string like %d, %s and replacing it with the variables provided from the script. heres and example of a line from the script

printf;string to print. ran program %d time(s);int;ran

the int before the variable ran tells the script parser that the first variable to place in the string is an integer. now the problem isnt if theres one item to format, its when you have more than one.

until i have that little problem fixed i just have a function to print the variable by itself. but i would like to make it easier by having it all in one line of code in the script.
Aug 2, 2010 at 2:45am
I am still not very clear on what you need, but here is some code showing how I might approach writing a string format parser. Perhaps you will find some of it useful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <sstream>
#include <vector>

union var_t
{
	int d;
	float f;
	const char* s;

	var_t(int d): d(d){}
	var_t(float f): f(f){}
	var_t(const char* s): s(s){}
};

void printit(const std::string& format
	, const std::vector<var_t>& value)
{
	std::ostringstream oss; // output accumulator
	std::istringstream iss(format); // convert format to stream

	// iterate through parameter values
	std::vector<var_t>::const_iterator v = value.begin();

	std::string part;
	std::getline(iss, part, '%');

	// part = any text before the format spec (if any)
	oss << part;

	while(std::getline(iss, part, '%') && v != value.end())
	{
		// part = format spec + rest of text
		if(part.length())
		{
			switch(part[0])
			{
				case 'd': oss << (*v++).d;
					break;
				case 'f': oss << (*v++).f;
					break;
				case 's': oss << (*v++).s;
					break;
			}
			oss << part.substr(1);
		}
	}
	std::cout << oss.str();
}

int main ()
{
	std::vector<var_t> value;

	// add different type variables to the argument values
	value.push_back(var_t("John Doe"));
	value.push_back(var_t(37));
	value.push_back(var_t(5.97f));

	printit("Mr %s was %d years old and %f feet tall.", value);

  return 0;
}
Aug 2, 2010 at 4:16am
that works wonders. i was able to intergrate that into my code, with no changes to the function code. just had to change how it added info to the value vector slightly, as im using string class variables for my parameters. but all in all, it works great. thanks for the help.
Topic archived. No new replies allowed.