std::string KeyValueMap::getString(const std::string& key, const std::string& defaultValue) const { for(unsigned int ik=0; ik<items.size(); ik++) if(!items[ik].first.empty() && comparestr(items[ik].first, key)) return items[ik].second; return defaultValue; }
int main(void) { char a[50]; char b[50]; int len1,len2; scanf("%s%s",a,b); len1=strlen(a); len2=strlen(b); if(len1==len2) { int count = comparestr(a,b,len1); if(count==1) { printf("Strings are equal"); } else { printf("Strings are not equal"); } } else { printf("Strings are not equal"); } return 0; }
bool KeyValueMap::contains(const std::string& key) const { for(unsigned int ik=0; ik<items.size(); ik++) if(!items[ik].first.empty() && comparestr(items[ik].first, key)) return true; return false; }
KeyValueMap& ConfigFile::findSection(const std::string& sec) { for(unsigned int is=0; is<sections.size(); is++) if(comparestr(sections[is].first, sec)) return sections[is].second; // not found -- add new section sections.push_back(std::pair<std::string, KeyValueMap>(sec, KeyValueMap())); return sections.back().second; }
bool KeyValueMap::unset(const std::string& key) { for(unsigned int ik=0; ik<items.size(); ik++) if(!items[ik].first.empty() && comparestr(items[ik].first, key)) { items.erase(items.begin()+ik); return true; } return false; }
void KeyValueMap::set(const std::string& key, const std::string& value) { modified = true; // don't check if the new value is different from the old one for(unsigned int ik=0; ik<items.size(); ik++) if(!items[ik].first.empty() && comparestr(items[ik].first, key)) { items[ik].second = value; return; } items.push_back(std::pair<std::string, std::string>(key, value)); // key not found -- add new }
ConfigFile::ConfigFile(const std::string& _fileName) : fileName(_fileName) { std::ifstream strm(fileName.c_str()); if(!strm) throw std::runtime_error("File does not exist: "+_fileName); std::string buffer; int secIndex = -1; while(std::getline(strm, buffer)) { // remove all comment lines if(buffer.size()>0 && (buffer[0] == '#' || buffer[0] == ';')) continue; std::string::size_type indx = buffer.find('['); std::string::size_type indx1= buffer.find(']'); if(indx!=std::string::npos && indx1!=std::string::npos && indx1>indx) { // section start - parse section name buffer = buffer.substr(indx+1, indx1-indx-1); indx = buffer.find_first_not_of(" \t\n\r"); if(indx!=std::string::npos && indx>0) buffer.erase(0, indx); indx = buffer.find_last_not_of(" \t\n\r"); if(indx!=std::string::npos && indx+1<buffer.size()) buffer.erase(indx+1); if(buffer.empty()) continue; // find if this section already existed in the list secIndex = -1; for(size_t i=0; i<sections.size(); i++) if(comparestr(sections[i].first, buffer)) secIndex = i; if(secIndex<0) { // section not found before, add it sections.push_back(std::pair<std::string, KeyValueMap>(buffer, KeyValueMap())); secIndex = sections.size()-1; } } else { // not a section if(sections.empty()) { // no sections has been created yet sections.push_back(std::pair<std::string, KeyValueMap>("", KeyValueMap())); // add an empty section secIndex = 0; } sections[secIndex].second.add(buffer.c_str()); } } strm.close(); }
void run(){ char domainname[80]; char op[20],mailid[80],temp[80]; char* add_email="add_email"; struct sigaction usr_action; sigset_t block_mask; printf("Enter op: "); scanf("%s",op); if(comparestr(op,"add_email")){ printf("Entered addition zone. Enter mailid\n"); scanf("%s",mailid); strcpy(temp,mailid); getdomain(temp,domainname); int searchVal=searchDomain(domainname); if(searchVal == -1){ //create new domain printf("\n------------------------------------\nAdding a new process\n"); //fork a new process here pid_t childpid; /* variable to store the child's pid */ int retval; /* child process: user-provided return code */ int status; /* parent process: child's exit status */ /* only 1 int variable is needed because each process would have its own instance of the variable here, 2 int variables are used for clarity */ /* now create new process */ childpid = fork(); if (childpid >= 0) /* fork succeeded */ { if (childpid == 0){ /* fork() returns 0 to the child process */ noEmails=0; sigfillset (&block_mask); usr_action.sa_handler = child_handler; usr_action.sa_mask = block_mask; usr_action.sa_flags = 0; sigaction (SIGUSR1, &usr_action, NULL); struct sigaction act2; memset (&act2, '\0', sizeof(act2)); /* Use the sa_sigaction field because the handles has two additional parameters */ act2.sa_sigaction = &child_handler_kill; /* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */ act2.sa_flags = SA_SIGINFO; if (sigaction(SIGTERM, &act2, NULL) < 0) { printf ("sigaction"); } while(1){;} } // connect to (and possibly create) the segment: // if(childpid > 0){ sigfillset (&block_mask); usr_action.sa_handler = parent_handler; usr_action.sa_mask = block_mask; usr_action.sa_flags = 0; sigaction (SIGUSR2, &usr_action, NULL); strcpy(p2cshared->email, mailid); strcpy(p2cshared->op, op); addemail4parent(mailid,childpid); } } else /* fork returns -1 on failure */ { perror("fork"); /* display error message */ exit(0); } } else{ strcpy(p2cshared->email, mailid); strcpy(p2cshared->op, op); sleep(2); kill(data[searchVal].pid,SIGUSR1); sleep(2); } } else if(comparestr(op,"delete_email")){ printf("Entered deleting zone. Enter mailid\n"); scanf("%s",mailid); operationp(mailid,op); } else if(comparestr(op,"search_email")){ printf("Entered searching zone. Enter mailid\n"); scanf("%s",mailid); operationp(mailid,op); } else if(comparestr(op,"delete_domain")){ printf("Entered deleting zone. Enter mailid\n"); scanf("%s",mailid); int id=searchDomain(mailid); if(id==-1){ printf("Parent: Domain does not exist\n"); } else{ kill(data[id].pid,SIGTERM); waitpid(data[id].pid, 0, 0); //important for wait so that child doesnt become zombie //A child that terminates, but has not been waited for becomes a "zombie". printf("Parent process - Domain %s with PID - %d deleted\n", mailid, data[id].pid); data[id].pid = -1; data[id].domain[0]='\0'; } } else if(comparestr(op,"Quit")){ printf("Quit\n"); int i=0; for(i=0;i<noDomains;i++){ if(data[i].pid != 0){ kill(data[i].pid, SIGTERM); waitpid(data[i].pid, 0, 0); //important for wait so that child doesnt become zombie } } printf("Parent : Killed all children, now suicide awaits\n"); //raise(SIGKILL); } else{ printf("Incorrect input: try again"); } }
int string::compare(unsigned pos1,unsigned n1,const char* s,unsigned n2) const{ return comparestr(s,pos1,pos1+n1-1,0,n2-1);}
int string::compare(unsigned pos1,unsigned n1,const string& str,unsigned pos2,unsigned n2) const{ return comparestr(str.data,pos1,pos1+n1-1,pos2,pos2+n2-1);}
int string::compare(unsigned pos1,unsigned n1,const char* s) const{ return comparestr(s,pos1,pos1+n1-1,0,strlen(s)-1);}
int string::compare(unsigned pos1,unsigned n1,const string& str) const{ return comparestr(str.data,pos1,pos1+n1-1,0,str.datalen-1);}
int string::compare(const string& str) const{ return comparestr(str.data,0,datalen-1,0,str.datalen-1);}
//compare int string::compare(const char* str) const{ return comparestr(str,0,datalen-1,0,strlen(str)-1);}