Weird problem with std::string
From: Chris Mantoulidis (cmad_x_at_yahoo.com)
Date: 02/27/04
- Next message: Rob Williscroft: "Re: Problem with convoluted function pointer"
- Previous message: John Harrison: "Re: Problem with convoluted function pointer"
- Next in thread: Heinz Ozwirk: "Re: Weird problem with std::string"
- Reply: Heinz Ozwirk: "Re: Weird problem with std::string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 27 Feb 2004 08:20:25 -0800
PROBLEM: I'm having some weird problems with string::find() (in
ParamGenerate()), but since I'm not sure if that is the source of all
bad output in my program, I'm posting least code that's compilable.
OTHER INFO: Just so you know what the program does (and so you can
give it some input to test it), at this stage I want it to break the
input into some parts. Here is the input style:
<middle param> <middle param> ...... <middle param> (<final param>)
There can be infinite <middle param>s but ONLY ONE (and optional)
<final param>. Each middle param is seperated from the others with a
space and cannot contain a column (':'). The final param is just one
and starts with a ':' and can contain any kind of character.
- MORE INFO
- here's a sample command (the column is included in the beginning)
- because a column is the start of an IRC command
- :cmad!cmad_x@host PRIVMSG #cmad :this is a test message
-
- I would like this text broken into those parts
- params[0] = "cmad!cmad_x@host"
- params[1] = "PRIVMSG"
- params[2] = "#cmad"
- params[3] = ":this is a test message"
-
- but this is not the output I get
(BTW: If I try:
params.resize(ParamCount(s));
in ParamGenerate() I get a SegFault, so I set the vector size to 1024)
Here's the program code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string arg;
vector<string> params(1024);
/*
COMMAND IS LIKE
:user@host <CMD> <PARAMS>
and params are like
<PARAMS> = <MIDDLE PARAMS> <MIDDLE PARAMS> ... <MIDDLE PARAMS> <FINAL
PARAM>
middle params are params that contain no spaces or columns ':'
... TO BE CONTINUED ...
*/
string RemoveLeadingColumn(const string &s) {
string s2 = s;
if (s2[0] == ':') s2.erase(0,1);
return s2;
}
int ParamCount(const string &s) {
int count=1;
string::size_type i;
for (i=0; i < s.length(); i++) {
if (s[i] == ' ' /*&& !found_column*/ )
count++;
else if (s[i] == ':' /*&& !found_column*/ ) {
return count;
}
}
return count;
}
void ParamGenerate(const string &s) {
cout << "initializing vars...\n";
string::size_type first_column = s.find(':');
if (first_column == string::npos) first_column=s.length();
string::size_type white_space=0;
string::size_type last=0;
int curr_param=0;
cout << "starting search...\n";
while ((white_space = s.find(' ', white_space+1)) != string::npos &&
white_space <= first_column) {
cout << "found space...\n";
params[curr_param] = s.substr(last,white_space);
cout << "substring is \"" << s.substr(last,white_space) << "\"\n";
last=white_space+1;
curr_param++;
}
params[curr_param] = s.substr(first_column,s.length());
}
int main() {
cout << "gimme a cmd:\n";
getline(cin,arg,'\n');
cout << "you gave me: " << arg << "\n";
cout << "without leading ':' it is: " << RemoveLeadingColumn(arg) <<
"\n";
if (IsAddr(RemoveLeadingColumn(arg)))
cout << "nickname: " << NickFromAddr(RemoveLeadingColumn(arg)) <<
"\n";
cout << "ParamCount: " << ParamCount(RemoveLeadingColumn(arg)) <<
"\n";
ParamGenerate(RemoveLeadingColumn(arg));
cout << "Params generated\n";
int what;
do {
cout << "tell me a param num: ";
cin >> what;
if (what <= ParamCount(RemoveLeadingColumn(arg)) && what >= 1)
cout << "param[" << what << "] = " << params[what-1] << "\n";
else
cout << "wrong index\n";
} while (what != 0);
return 0;
}
- Next message: Rob Williscroft: "Re: Problem with convoluted function pointer"
- Previous message: John Harrison: "Re: Problem with convoluted function pointer"
- Next in thread: Heinz Ozwirk: "Re: Weird problem with std::string"
- Reply: Heinz Ozwirk: "Re: Weird problem with std::string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|