Author | Message | Time |
---|---|---|
Mephisto | Sorry if my terminology is wrong here...but I want to create a function which formats a string based on its contained arguments. For instance, I have it logically stored in my head that p = ping, u = username, etc. In the string they are represented as %p and %u...in other words %argument. This is primarily for greets, but could be used for other things (this is for a bot). An example greet message is "Hello, and welcome to the channel. Your username is %u and your ping at logon was %p." Now, this is my logical thinking in how to format this string. I have a function called FormatString. It takes the string you want formatted as its argument and formats it, and due to passing by reference the string itself is formatted after the function is called. The function creates a few variables. Here is the list: * int integer_values = 0; // this variable stores values for integer arguments, such as %p * char string_values[512] = {0}; // this variables stores values for string arguments, such as %u * char *temp_string = \0; // this variable is for temporarily holding parts of the string as it loops through * char *string = \0; // the argument passed to the function is copied into this string * char argument = \0; // the argument, example: p, u, etc. First, the function calls strchr(string, '%') and if it returns NULL the function returns as no formatting is required. If it returns true then formatting is required and it proceeds. If formatting is required then the function assigns the return value of strchr(string, '%') to temp_string. It then proceeds into a while loop with the condition of while(temp_string) stating that while there are still arguments in the string continue (when there are no more arguments temp_string will become NULL by the strchr function return value). In the loop it assigns the variable "argument" with temp_string[1] which will be the character following the % as a result to being assigned the return value from strchr. So now argument has the argument letter. It then does a switch/case expression and evaluates the value of that argument (bot specific implementation, no need to describe here really) and assigns the argument value to either string_argument or integer_argument. After getting the first argument value it reassigns temp_string with strchr(&temp_string[1], '%') to find the next argument (the next %) and repeats. Question: With the argument value, how do I place it into the string? I'm not sure once I obtain the argument value how I replace the %arg with the value in the string...Help? :) | June 13, 2005, 6:13 AM |
OnlyMeat | You could do something like this:- [code] #include <iostream> // console io #include <string> const int FORMAT_SPEC_TOKEN_LENGTH = 2; using namespace std; // argv[1] == "Format - %x %y %z %t" int _tmain(int argc, _TCHAR* argv[]) { string strFormat = argv[1]; string::size_type idx = 0; // Replace format specifications while ( (idx = strFormat.find('%')) != string::npos ) { switch ( strFormat.at(idx+1) ) { case 'x': strFormat.replace(idx,FORMAT_SPEC_TOKEN_LENGTH,"Argx"); cout << "Replaced %x specifier" << endl; break; case 'y': strFormat.replace(idx,FORMAT_SPEC_TOKEN_LENGTH,"Argy"); cout << "Replaced %y specifier" << endl; break; case 'z': strFormat.replace(idx,FORMAT_SPEC_TOKEN_LENGTH,"Argz"); cout << "Replaced %z specifier" << endl; break; case 't': strFormat.replace(idx,FORMAT_SPEC_TOKEN_LENGTH,"Argt"); cout << "Replaced %t specifier" << endl; break; default: strFormat.replace(idx,FORMAT_SPEC_TOKEN_LENGTH,""); cout << "Unknown format specifier removed" << endl; break; } } cout << "Resulting string: " << strFormat << endl; char c; cin >> c; return 0; } [/code] argv[1] is the format string passed in through the command line. Assuming you don't mind using the stl that is. If you want to do this using a raw char[] array then you will have to implement your own find/replace/array resize code. | June 13, 2005, 11:47 AM |
Mephisto | Thanks, I modified it quite a bit to adjust to my bot and not use command-line arguments, but the general usage of std::string is the same. I just had to do a quick conversion back to normal character arrays with .c_str() when I was done. :) | June 13, 2005, 5:52 PM |