Weird problem with std::string

From: Chris Mantoulidis (cmad_x_at_yahoo.com)
Date: 02/27/04


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;

}



Relevant Pages

  • Re: Is Java Applet Best?
    ... * @param pName ... DirFilter(String[] pAvoidDirs) ... * Accept only junk files to be deleted. ... * exec command to invoke NT command processor customised for my ...
    (comp.lang.java.programmer)
  • Re: Copying Directories
    ... Here is a program for getting rid of junk files. ... * @param pName ... DirFilter(String[] pAvoidDirs) ... * exec command to invoke NT command processor customised for my ...
    (comp.lang.java.programmer)
  • Re: Graph traversieren wie ein Petri-Netz
    ... @param strValue zu prüfender String ... public static final boolean empty{ ... @param strBuffValue zu prüfender StringBuffer ...
    (de.comp.lang.java)
  • Re: Hashtable question
    ... * @param modelName is a filename less the .pim extension where the ... public void load() throws IOException { ... public void add(String keyString, Item item) ... * @param keyString the string to lookup by. ...
    (comp.lang.java.programmer)
  • Re: which JAR to use ?
    ... scan for a string that leads into the piece you want with indexOf. ... public static String getWantedCurrencies throws IOException ... * @param result Results to save, ... public static String get(String websiteURL, String relativeURL, ...
    (comp.lang.java.programmer)