map functions help
extern "C" int WINAPI GetKey(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
string channel = data;
iT = KeyStorer.find(channel);
string output = (*iT).second.c_str( );
_fstrcpy(data, output.c_str());
return 3;
}
whats wrong with that function? im trying to return the data value which is stored in the channel map name
the main declarations at the top are
typedef map<string, string> MapFunction;
MapFunction KeyStorer;
MapFunction::iterator iT;
and the way i add info to the map something like this which i have in a seperate function
KeyStorer.insert(MapFunction::value_type("channel", "key1"));
but my problem is with the getkey function which doesnt work properly
[784 byte] By [
pouncer] at [2007-11-11 7:49:52]

# 1 Re: map functions help
just about everything's wrong...
what are the types of iT and KeyStorer?
Assuming iterator to string, and string, you need to check the return value of find. What if it doesn't find the sought after value?
Secondly, this expression:
string output = (*iT).second.c_str( );
You can write it in a much simpler form:
string output = iT->second;
Finally, why do you need fstrcpy when you're using string objects?
data = output;
is so much cleaner
Danny at 2007-11-11 21:01:55 >

# 2 Re: map functions help
its in a dll, with functions, which i call from an external app mIRC, here are the functions
//bits at the top
typedef map<string, string> MapFunction;
MapFunction KeyStorer;
MapFunction::iterator iT;
extern "C" int WINAPI StoreKey(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
string channel = GetTok(data, 1, 32);
string key = GetTok(data, 2, 32);
KeyStorer.insert(MapFunction::value_type(channel, key));
return 1;
}
extern "C" int WINAPI GetKey(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
string channel = data;
string qkey;
MapFunction::iterator it = KeyStorer.find( channel );
if ( it != KeyStorer.end( ) )
lstrcpy( data, (*it).second.c_str( ) );
else
lstrcpy( data, "Key Not Found" );
return 3;
}
all the declarations are at the top. the store key definately works, from mirc i do this
//dll Test.dll StoreKey #channel key
and then
this is the bit which should echo 'key' but it doesnt work, just keeps echoing 'no key found'
//echo -a $dll(Test.dll, GetKey, #channel)
# 3 Re: map functions help
Are you certain that the KeyStore map contains the sought after value? For debugging pruposes, insert the first pair inside the GetKey function just before calling find. If you get the desired output, then bingo!
Try also to echo the values of data before and after calling lstrcpy.
Danny at 2007-11-11 21:03:54 >

# 4 Re: map functions help
extern "C" int WINAPI StoreKey(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
string channel = GetTok(data, 1, 32);
string key = GetTok(data, 2, 32);
KeyStorer.insert(MapFunction::value_type(channel, key));
MapFunction::iterator it = KeyStorer.find( channel );
if ( it != KeyStorer.end( ) )
lstrcpy( data, (*it).second.c_str( ) );
else
lstrcpy( data, "Key Not Found" );
return 3;
}
//echo -a $dll(Test.dll, StoreKey, #channel key) echos key
so it definately stores it right, but it just doesnt seem to work when i try and get the key value from a different function
it seems as though the .find bit will only work in the same function as the .insert bit :S
# 5 Re: map functions help
It took me a while to find the error: you're not passing the correct string to find! you need to pass the key, not the associated value (channel).
Danny at 2007-11-11 21:05:59 >
