Exemplo n.º 1
0
void cheat_manager::load_cheats(const char *filename)
{
	std::string searchstr(machine().options().cheat_path());
	std::string curpath;
	for (path_iterator path(searchstr); path.next(curpath); )
	{
		searchstr.append(";").append(curpath).append(PATH_SEPARATOR).append("cheat");
	}
	emu_file cheatfile(std::move(searchstr), OPEN_FLAG_READ);
	try
	{
		// loop over all instrances of the files found in our search paths
		for (osd_file::error filerr = cheatfile.open(filename, ".xml"); filerr == osd_file::error::NONE; filerr = cheatfile.open_next())
		{
			osd_printf_verbose("Loading cheats file from %s\n", cheatfile.fullpath());

			// read the XML file into internal data structures
			xml_parse_options options = { nullptr };
			xml_parse_error error;
			options.error = &error;
			std::unique_ptr<xml_data_node, void (*)(xml_data_node *)> rootnode(xml_data_node::file_read(cheatfile, &options), [] (xml_data_node *node) { node->file_free(); });

			// if unable to parse the file, just bail
			if (rootnode == nullptr)
				throw emu_fatalerror("%s.xml(%d): error parsing XML (%s)\n", filename, error.error_line, error.error_message);

			// find the layout node
			xml_data_node *mamecheatnode = rootnode->get_child("mamecheat");
			if (mamecheatnode == nullptr)
				throw emu_fatalerror("%s.xml: missing mamecheatnode node", filename);

			// validate the config data version
			int version = mamecheatnode->get_attribute_int("version", 0);
			if (version != CHEAT_VERSION)
				throw emu_fatalerror("%s.xml(%d): Invalid cheat XML file: unsupported version", filename, mamecheatnode->line);

			// parse all the elements
			for (xml_data_node const *cheatnode = mamecheatnode->get_child("cheat"); cheatnode != nullptr; cheatnode = cheatnode->get_next_sibling("cheat"))
			{
				// load this entry
				auto curcheat = std::make_unique<cheat_entry>(*this, m_symtable, filename, *cheatnode);

				// make sure we're not a duplicate
				if (REMOVE_DUPLICATE_CHEATS && curcheat->is_duplicate())
				{
					osd_printf_verbose("Ignoring duplicate cheat '%s' from file %s\n", curcheat->description(), cheatfile.fullpath());
				}
				else // add to the end of the list
					m_cheatlist.push_back(std::move(curcheat));
			}
		}
	}

	// handle errors cleanly
	catch (emu_fatalerror &err)
	{
		osd_printf_error("%s\n", err.string());
		m_cheatlist.clear();
	}
}
Exemplo n.º 2
0
int main()
{
   char * s = "ababbbcabaabbbabababbbababbba";
   char * p = "ababbba";

   printf("rsrch %s\n", rsearchstr(strlen(s), s, strlen(p), p));
   printf("rsrch %s\n", rsearchstr(strlen(s)-1, s, strlen(p), p));

   const uint8_t* pos1 = searchstr(strlen(s), s, strlen(p), p);
   printf("srch %s\n", pos1);
   printf("srch %s\n", searchstr(strlen(pos1+1), pos1+1, strlen(p), p));

   char data[10000];
   memset(data, 'a', sizeof(data));
   for (int i = 0; i < 10000; i += 10) {
      data[i] = 'b';
   }
   data[10000-10] = 'a';
   data[10000-11] = 'b';
   p = "aaaaaaaaaa";
   printf("srch %s\n", searchstr(sizeof(data), data, strlen(p), p));

   return 0;
}
Exemplo n.º 3
0
int main(void)
{
    struct node *head=NULL;                 /* The head of linked list*/
    char        inputBuff[MAXLINE];         /* Input buffer  to hold the command entered */
    char        inputValue[MAXLINE];        /* Store input buffer for history record*/
    char        *subinputValue;             /* Store string value along to r command*/
    char        *args[MAXLINE/2+1];         /* Command line arguments */
    char        *history_args[MAXLINE/2+1]; /* Command line arguments */
    int         bkgnd;                      /* Equals 1 if a command is followed by '&', else 0 */
    int         size=0;                     /* The length of history*/
    int         index=1;                    /* The current index of command*/
    int         capacity=8;                 /* the initial capacity of history is 8 */



    head=(struct node *) malloc(sizeof(struct node));  /*create a fake head, avoid null point error*/

    while (1){                                          /* Program terminates normally inside setup */

	bkgnd = 0;

	printf("CSE2431Sh$");                               /* Shell prompt */
        fflush(0);

    setup(inputBuff, args, &bkgnd,inputValue);          /* Get next command */
    
    if(inputBuff[0]=='\0'){                             /*if input is null, break */
        continue; 
    }

    if(!strcmp(args[0],"r")&&args[1]==NULL){            /*if r without arguments, show error message*/
        printf("r with no history number\n");
        continue;
    }

                                                        
    if(!strcmp(args[0],"r")){                           /* Used to check the input value followed by command 'r' is num or string */

        if((args[1][0]>='A')) { 
            int         tempi=0;                        /* Start index */
            struct node *temp=NULL;                     /* Point to the command followed by 'r' */

            while(inputValue[tempi]=='r'||inputValue[tempi]==' '){
                tempi++;
            }

            subinputValue = &inputValue[tempi];           /*copy input value followed by 'r' to variable subinputValue*/

            temp = searchstr(subinputValue,&head);        /* call searchstr() to find target command*/
            
            if(temp==NULL){
                printf("can not find that command in history\n");
                continue;
            }
            else{
                strcpy(inputValue,temp->argsBuff);
            }
        }else if(searchindex(atoi(args[1]),&head,inputValue)==0){
            continue;
        }
        
    }
    /*if command is 'sethistory'*/
    if(!strcmp(args[0],"sethistory")){ 

        if(args[1]==NULL||*args[1]=='0'){
            printf("sethistory: must provide exactly one size\n");
        }else{
            capacity=atoi(args[1]);
        }
        continue;
    }

    /* The command read through command line or execute through command 'rr' or 'r' will store in history*/
  
    if((!strcmp(args[0],"r"))         
        ||(!strcmp(args[0],"rr")&&searchindex(index-1,&head,inputValue)==1)
            ||(strcmp(args[0],"r")&&strcmp(args[0],"rr"))){
                add(index,inputValue,head,&size,capacity);
                index++;
    }
     
    /** In the child processs, first check the command, if it is 'history','h','rr','r','sethistory',then enter specific routine,
    execute specific operations.otherwise, call execvp()
    ***/

     pid_t child;
     child = fork();    /*fork another process*/
     
     if(child<0) {    /*if fork failed, exit the process*/
        fprintf(stderr,"Fork Failed");
        exit(-1);
     } else if(child==0) {  
 
        if(!strcmp(args[0],"history")||!strcmp(args[0],"h")) {       /*if command is 'history' or 'h', call showhistory()*/
            showhistory(head->next);
            exit(-1);

        } else if((!strcmp(args[0],"r")||!strcmp(args[0],"rr"))&&!recently(inputValue,history_args)) { /*if command is 'r' or 'rr'*/
            if(!strcmp(history_args[0],"history")||!strcmp(history_args[0],"h")){
                showhistory(head->next);
                exit(-1);

            }else if(execvp(history_args[0],history_args)){
                printf("execvp: No such file or directory \n");
                exit(-1);
            }

        } else if(strcmp(args[0],"sethistory")&&execvp(args[0],args)){   /*execute command line in child process*/
            printf("execvp: No such file or directory \n");
            exit(-1);
        } 

    } else {
        int status=0;
        if(bkgnd==0) {
            waitpid(child,&status,0);  /* parent will wait for the child process to complete*/
        }
        if(status==1){
            printf("error!");
        }
    }
    }
}
Exemplo n.º 4
0
S4 getvarind_comp (U1 *str)
{
    S4 i, ind = NOTDEF;
    U1 ok = FALSE;
    U1 varname[MAXLINELEN + 1];

    if (str[0] != '_')
    {
        if ((searchstr (str, AT_SB, 0, strlen (str) - 1, TRUE) == -1) || strlen (str) == 1)
        {
            /* function name not already tacked on = do it! */

            #if DEBUG
                printf ("getvarind: @ tacked on function name!\n");
            #endif

            strcpy (varname, str);
            strcat (varname, AT_SB);
            strcat (varname, function[function_ind].name);
        }
        else
        {
            strcpy (varname, str);
        }
    }
    else
    {
        /* global variable starting with "_": exp: _foobar */
        strcpy (varname, str);
    }

    #if DEBUG
        printf ("getvarind_comp: '%s'\n", varname);
    #endif

    if (varname[0] == PRIVATE_VAR_SB)
    {
        /* private varlist */

        i = 0;

        #if DEBUG
            printf ("PRIVATE VAR\n");
        #endif

        while (! ok)
        {
            if (strcmp (pvarlist_obj[i].name, varname) == 0)
            {
                ind = i, ok = TRUE;
            }
            if (i < pvarlist_state.maxvarlist)
            {
                i++;
            }
            else
            {
                ok = TRUE;
            }
        }
    }
    else
    {
        /* normal varlist */

        i = VARLIST_START;

        while (! ok)
        {
            if (strcmp (varlist[i].name, varname) == 0)
            {
                ind = i, ok = TRUE;
            }
            if (i < varlist_state.maxvarlist)
            {
                i++;
            }
            else
            {
                ok = TRUE;
            }
        }
    }

    #if DEBUG
        printf ("getvarind_comp: ind: %li\n", ind);
    #endif

    return (ind);
}
Exemplo n.º 5
0
U1 getvarstr_comp (U1 *argstr, S2 pos_start)
{
    S2 pos, pos_end, endvn, dimstart, dimend;
    S4 varind, dims;
    U1 pstr[MAXLINELEN + 1], comm[2], dimensstr[MAXLINELEN + 1], private_var = NORMAL_VAR;
    U1 ok = FALSE;

    U1 varname[MAXLINELEN + 1];
    U1 buf[MAXLINELEN + 1];

    comm[0] = AROPEN_SB;
    comm[1] = BINUL;

     /* bugfix */
    pos_start = 0;
    pos_end = strlen (argstr) - 1;

    endvn = searchstr (argstr, comm, pos_start, pos_end, TRUE);
    if (endvn != -1)
    {
        if (argstr[0] != '_')
        {
            partstr (argstr, varname, pos_start, endvn - 1);
            strcat (varname, AT_SB);
            strcat (varname, function[function_ind].name);
            partstr (argstr, buf, endvn, pos_end);
            strcat (varname, buf);
        }
        else
        {
            partstr (argstr, varname, pos_start, pos_end);
        }
    }
    else
    {
        if (argstr[0] != '_')
        {
            strcpy (varname, argstr);
            strcat (varname, AT_SB);
            strcat (varname, function[function_ind].name);
        }
        else
        {
            /* global variable starting with "_": exp: _foobar */
            strcpy (varname, argstr);
        }
    }

    t_var.dims = NODIMS;
    pos_end = strlen (varname) - 1;

    if (varname[0] == PRIVATE_VAR_SB)
    {
        /* use pvarlist for dimens */

        private_var = PRIVATE_VAR;

        #if DEBUG
            printf ("getvarstr: PRIVATE_VAR SET\n");
        #endif
    }

    partstr (varname, pstr, pos_start, pos_end);

    #if DEBUG
        printf ("getvarstr: argstr: '%s'\n", varname);
    #endif

    /* bugfix */
    pos_start = 0;
    pos_end = strlen (pstr) - 1;

    endvn = searchstr (pstr, comm, pos_start, pos_end, TRUE);
    if (endvn != -1)
    {
        /* area variable, exp. a[5][x] */

        dimstart = endvn;
        endvn--;
        dims = NODIMS;

        while (! ok)
        {
            comm[0] = ARCLOSE_SB;
            comm[1] = BINUL;

            dimend = searchstr (pstr, comm, dimstart + 1, pos_end, TRUE);
            if (dimend != -1)
            {
                if (dims < MAXDIMENS - 1)
                {
                    dims++;
                }
                else
                {
                    printerr (OVERFLOW_IND, plist_ind, ST_PRE, t_var.varname);
                    return (FALSE);
                }

                /*  getstr (U1 *str, U1 *retstr, S2 maxlen, S2 start, S2 end) */

                if (private_var == NORMAL_VAR)
                {
                    getstr (pstr, dimensstr, MAXLINELEN, dimstart + 1, dimend - 1);
                    if (checkdigit (dimensstr) == TRUE)
                    {
                        if (! make_val (INT_VAR, varlist, NORMAL_VAR))
                        {
                            printerr (MEMORY, plist_ind, ST_PRE, t_var.varname);
                            return (FALSE);
                        }
                        t_var.dimens[dims] = t_var.varlist_ind;
                    }
                    else
                    {
                        /* variable, */
                        /* check if already defined */

                        varind = getvarind_comp (dimensstr);
                        if (varind != -1)
                        {
                            if (varlist[varind].type != STRING_VAR && varlist[varind].type != BYTE_VAR)
                            {
                                t_var.dimens[dims] = varind;
                            }
                            else
                            {
                                /* error: its a string or byte var */

                                printerr (WRONG_VARTYPE, plist_ind, ST_PRE, dimensstr);
                                return (FALSE);
                            }
                        }
                        else
                        {
                            printerr (NOTDEF_VAR, plist_ind, ST_PRE, dimensstr);
                            return (FALSE);
                        }
                    }
                }
                else
                {
                    /* private variable */

                    #if DEBUG
                        printf ("getvarstr: PRIVATE VAR!");
                    #endif

                    getstr (pstr, dimensstr, MAXLINELEN, dimstart + 1, dimend - 1);
                    if (checkdigit (dimensstr) == TRUE)
                    {
                        if (! make_val (INT_VAR, pvarlist_obj, PRIVATE_VAR))
                        {
                            printerr (MEMORY, plist_ind, ST_PRE, t_var.varname);
                            return (FALSE);
                        }
                        t_var.dimens[dims] = t_var.varlist_ind;

                        #if DEBUG
                            printf ("getvarstr: PRIVATE VAR: %li", t_var.varlist_ind);
                        #endif
                    }
                    else
                    {
                        /* variable, */
                        /* check if already defined */

                        varind = getvarind_comp (dimensstr);
                        if (varind != -1)
                        {
                            if (pvarlist_obj[varind].type != STRING_VAR && pvarlist_obj[varind].type != BYTE_VAR)
                            {
                                t_var.dimens[dims] = varind;
                            }
                            else
                            {
                                /* error: its a string or byte var */

                                printerr (WRONG_VARTYPE, plist_ind, ST_PRE, dimensstr);
                                return (FALSE);
                            }
                        }
                        else
                        {
                            printerr (NOTDEF_VAR, plist_ind, ST_PRE, dimensstr);
                            return (FALSE);
                        }
                    }
                }
            }
            else
            {
                /* no ] */

                printerr (BRNOT_OK, plist_ind, ST_PRE, "");
                return (FALSE);
            }

            comm[0] = AROPEN_SB;
            comm[1] = BINUL;

            if (dimend < pos_end)
            {
                pos = searchstr (pstr, comm, dimend + 1, pos_end, TRUE);
                if (pos != -1)
                {
                    dimstart = pos;
                }
                else
                {
                    ok = TRUE;
                }
            }
            else
            {
                ok = TRUE;
            }
        }
        getstr (pstr, t_var.varname, MAXVARNAME, pos_start, endvn);
        t_var.dims = dims;

        #if DEBUG
            printf ("getvarstr: dims = %li\n", dims);
        #endif
    }
    else
    {
        getstr (pstr, t_var.varname, MAXVARNAME, pos_start, pos_end);
    }
    return (TRUE);
}
Exemplo n.º 6
0
int hamming_search_test(const char* filename)
{
	long result = errors_counter();

	typedef containers::ternary_tree<std::string, const char*> Tst;
	Tst names;
	names["ABCD"] = "ABCD";
	names["Abcd"] = "Abcd";
	names["ABcd"] = "ABcd";
	names["aBCd"] = "aBCd";
	names["abc"] = "abc";
	names["abcde"] = "abcde";
	names["bcd"] = "bcd";
	names["abCD"] = "abCD";
	names["abCd"] = "abCd";
	names["AbcD"] = "AbcD";
	names["ABcD"] = "ABcD";
	names["aBCD"] = "aBCD";
	names["abCDE"] = "abCDE";
	names["abCDd"] = "abCDd";
	names["abCcd"] = "abCcd";
	names["bcdc"] = "bcdc";
	names["aab"] = "aab";

	// hamming_search(2): ABcd, AbcD, Abcd, aBCd, abCD, abCd, abc, abcde
	// levenshtein_search(2): hamming + abCDd, abCcd bcd, bcdc

	Tst::search_results_list matches = names.create_search_results();
	typedef Tst::search_results_list::iterator ResultIter;

	//names.levenshtein_search_count = 0;
	names.levenshtein_search("abcd", std::back_inserter(matches), 2);
	//std::cout << "l-count: " << names.levenshtein_search_count << "\n";
	//names.levenshtein_search_count = 0;
	BOOST_CHECK(matches.size() == 12);
	if (matches.size() != 12)
	{
		// compare with DDJ
		{
			Tptr root = 0;
			for (Tst::iterator it = names.begin(); it != names.end(); ++it) {
				insertstr = (char*)*it;
				root = insert2(root, insertstr);
			}
			nearsearch(root, "abcd", 2);
			std::cout << "DDJ nearsearch abcd:\n";
			for (int i = 0; i != srchtop; i++)
				std::cout << srcharr[i] << "\n";
			std::cout << "\n";
			cleanup2(root);
		}
		int i = 0;
		for (ResultIter rit = matches.begin(); rit != matches.end(); ++rit, ++i) {
			std::cout << **rit << " = " << (*rit).key();
			std::cout /*<< " = " << matches[i].key()*/ << "\n";
		}
	}

	typedef std::vector<std::string> Dictionary;
	extern size_t fill_dictionary(const char*, Dictionary&, size_t, size_t = 0);
	Dictionary dictionary;
	size_t longest_in_file = fill_dictionary(filename, dictionary, 300);
	std::random_shuffle(dictionary.begin(), dictionary.end());

	names.clear();

	Dictionary wildnames;
	// Add names with mini-variations
	long zeroes = 0;
	Dictionary::iterator dit;
	for (dit = dictionary.begin(); dit != dictionary.end(); ++dit) {
		const std::string& name(*dit);
		std::string namecopy(*dit);
		names[namecopy] = name.c_str();
		std::string searchstr(name);
		for (int i = 0; i < 5; ++i) {
			int where = rand() % name.size();
			// make string with ? at place of changed char
			if (searchstr[where] == '?')
				continue;
			searchstr[where] = '?';
			wildnames.push_back(searchstr);
			char c = (char)rand();
			if (!c) zeroes++;
			namecopy[where] = c;
			names[namecopy] = name.c_str();
		}
	}

	for(dit = wildnames.begin(); dit != wildnames.end(); ++dit) {
		const std::string& name(*dit);
		for (int diff = 1; diff != 3; ++diff) {
			Tst::search_results_list matchresults = names.create_search_results();
			names.hamming_search(name, std::back_inserter(matchresults), diff);
			//if (matchresults.size() == 0)
			//	std::cout << "couldn't find " << name << '\n';
			//BOOST_CHECK(found == matchresults.size());
			for (size_t i = 0; i != matchresults.size(); ++i)
				BOOST_CHECK(check_hamming_match(matchresults[i].key(), name, diff));
		}
	}

	return errors_counter() - result;
}