hello all, im trying to make my progam smaller by making some function . one of them needs to have 2 map and a char * token , (for searching ) as arguments!
sth like this Search(map1<>, map2<>,char *)
.well i tried , but i couldnt make it my self. dear laiserlight gave me this "
Code:
Code:
template<typename K, typename V>
class MatchSecond
{
public:
MatchSecond(const V& value) : value(value) {}
bool operator()(const std::pair<K,V>& element) const
{
return element.second == value;
}
private:
V value;
};
i made this a header file (searcher.h) included this to my project , then i used them in such a way to search in maps!
(LABEL and declaration , are located in "CLA.h"(private session!) .
Code:
Code:
///###################################### Search in LABEL and DECLARATIONS ################################
int decimal_retrieved_number = 0;
int converted_org = 0;
int converted_octal_number = 0;
char * octalstring;
char * ptr;
map<int,string>::iterator iter = find_if(LABEL.begin(), LABEL.end(), MatchSecond<int,string>(token));
//till you havent found the token in label, go on .
if (iter != LABEL.end())
{ //if you found it retrieve the number.
///Attention: converted_org may be "0", because of local scope limitation. if there is an error , remmember to manually convert org {maybe form orgsafe)to decimal and then add it to retrieved number.
decimal_retrieved_number = iter->first;
converted_org = Oct2Dec(org); //convert org from octal to decimal
decimal_retrieved_number += converted_org; //adds the org number to the retrieved line number , to get the memory location.(after this step, the result must be converted to octal.and then be saved to memory location.)
octalstring = Dec2Oct(decimal_retrieved_number);
converted_octal_number = atoi(octalstring);
offsetptr = octalstring; //save the octal string , to offsetptr (this is the value that our generic label represented).
} ///end of LABEL searching.
//else if you couldn't find it in label, search the token in declaration until you find it
///if there is a problem here , uncomment the line below , and change the iter to iter2.
map<int,string>::iterator iter2 = find_if(declaration.begin(), declaration.end(), MatchSecond<int,string>(token));
if (iter2 != declaration.end()) ///remember if you uncomment the above line , make this "iter2."
{
int temp;
char * converted;
string message;
temp = iter2->first; ///check to see if it is octal. if it is not we convert it to octal and generate a warning.
message = Oct2Bin(temp);//lets see if this returned value is octal or not
if (message == "no")
{
converted = Dec2Oct(temp);
cout<<"conversion occurred!"<<" line "<<currentline;
// we can use this statement >>( offsetptr = itoa(temp,ptr,8); ) instead of Dec2oct(temp);
offsetptr = converted; //copy the octal string in offsetptr
}
else if( message != "no" )
{
//if it was octal . well do the routine .copy it to offset,convert it to binary
char * oo;
char * o;
o = itoa(temp,oo,8); //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
cout<<o;
offsetptr = o;
}
}//end of else if (ite!=dclaration.end())
///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End of Search in LABEL and DECLARATIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
its doing its job pretty great . tanx a billion to laiserlight , but as you see, there are more than 20 cases in which i have to search in maps, so this made my program really big in a manner of lines !
so i managed to make the above codes into a the "below codes,(and i would add "search.h " to let it work.
Code:
///###################################### Search in LABEL and DECLARATIONS ################################
char * search(map1<>,map2<>,char * token)
{
int decimal_retrieved_number = 0;
int converted_org = 0;
int converted_octal_number = 0;
char * octalstring;
char * ptr;
char * offsetptr;
map<int,string>::iterator iter = find_if(K.begin(), K.end(), MatchSecond<int,string>(token));
//till you havent found the token in label, go on .
if (iter != K.end())
{ //if you found it retrieve the number.
///Attention: converted_org may be "0", because of local scope limitation. if there is an error , remmember to manually convert org {maybe form orgsafe)to decimal and then add it to retrieved number.
decimal_retrieved_number = iter->first;
converted_org = Oct2Dec(org); //convert org from octal to decimal
decimal_retrieved_number += converted_org; //adds the org number to the retrieved line number , to get the memory location.(after this step, the result must be converted to octal.and then be saved to memory location.)
octalstring = Dec2Oct(decimal_retrieved_number);
converted_octal_number = atoi(octalstring);
offsetptr = octalstring; //save the octal string , to offsetptr (this is the value that our generic label represented).
} ///end of LABEL searching.
//else if you couldnt find it in label, search the token in declaration untill you find it
///if there is a problem here , uncomment the line below , and change the iter to iter2.
map<int,string>::iterator iter2 = find_if(V.begin(), V.end(), MatchSecond<int,string>(token));
if (iter2 != V.end()) ///remmember if you uncomment the above line , make this "iter2."
{
int temp;
char * converted;
string message;
temp = iter2->first; ///check to see if it is octal. if it is not we convert it to octal and generate a warning.
message = Oct2Bin(temp);//lets see if this returned value is octal or not
if (message == "no")
{
converted = Dec2Oct(temp);
cout<<"conversion occured!"<<" line "<<currentline;
// we can use this statement >>( offsetptr = itoa(temp,ptr,8); ) instead of Dec2oct(temp);
offsetptr = converted; //copy the octal string in offsetptr
}
else if( message != "no" )
{
//if it was octal . well do the routin .copy it to offset,convert it to binary
char * oo;
char * o;
o = itoa(temp,oo,8); //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
offsetptr = o;//itoa(temp,ptr,8); //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
}
}//end of declaraion if
return offsetptr;
}//end of function
///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End of Search in LABEL and DECLARATIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
but , there were error, first i need to find a way to send the maps as an argument , to be able to get the offset. and another is whenever i include other function headers( such as "Oct2Dec.h" it says , redeclaration and stuff! so i managed to include these functions to the search function too , so that i m not to include their respective headerfiles.
anyway , can you please help me on this ? !