Esempio n. 1
0
File: ltpl.c Progetto: adderly/cmoon
int ltpl_config(const struct dirent *ent)
{
    if (reg_search(".*.hdf", ent->d_name))
        return 1;
    else
        return 0;
}
Esempio n. 2
0
NEOERR* session_init(CGI *cgi, HASH *dbh, session_t **ses)
{
    session_t *lses;
    char tok[_POSIX_PATH_MAX];
    
    *ses = NULL;

    lses = calloc(1, sizeof(session_t));
    if (!lses) return nerr_raise(NERR_NOMEM, "calloc memory for session_t failure");
    
    lses->reqtype = CGI_REQ_HTML;
    hdf_get_copy(cgi->hdf, PRE_COOKIE".uname", &lses->uname, NULL);

    char *uri = hdf_get_value(cgi->hdf, PRE_REQ_URI_RW, NULL);
    if (!uri) {
        uri = "terminal";
        lses->reqtype = CGI_REQ_TERMINAL;
    }
    /* TODO uniq req uri */
    //uri = mmisc_str_uniq(uri, '/');
    mmisc_str_repchr(uri, '/', '_');
    uri = mmisc_str_strip(uri, '_');
    if (!strncmp(uri, "json_", 5)) {
        uri = uri+5;
        lses->reqtype = CGI_REQ_AJAX;
    }
    switch (CGI_REQ_METHOD(cgi)) {
        case CGI_REQ_POST:
            snprintf(tok, sizeof(tok), "%s_data_mod", uri);
            break;
        case CGI_REQ_PUT:
            snprintf(tok, sizeof(tok), "%s_data_add", uri);
            break;
        case CGI_REQ_DEL:
            snprintf(tok, sizeof(tok), "%s_data_del", uri);
            break;
        default:
        case CGI_REQ_GET:
            snprintf(tok, sizeof(tok), "%s_data_get", uri);
            break;
    }
    lses->dataer = strdup(tok);
    lses->render = strdup(uri);
    
    /* process cache */
    HDF *node = hdf_get_obj(g_cfg, PRE_CFG_FILECACHE".0");
    while (node != NULL) {
        if (reg_search(hdf_get_value(node, "uri", "NULL"), uri)) {
            lses->tm_cache_browser = hdf_get_int_value(node, "tm_cache", 0);
            break;
        }
        node = hdf_obj_next(node);
    }

    *ses = lses;
    
    return STATUS_OK;
}
 // Cut from 156ms to 80ms, by using reference.
 // If check node->children[i] is valid, can reduce
 // a recursion level. It can cut from 80ms to 76ms.
 // But in this way, it will be more clear.
 bool reg_search(string& word, int idx, DictNode* node) {
     // recursive ending
     if (node == NULL) return false;
     if (idx >= word.size()) {
         return node->isWord;
     }
     
     if (word[idx] == '.') {
         // all match '.', search next level
         for (int i = 0; i < 26; i++) {
             if (reg_search(word, idx + 1, node->children[i]))
                     return true;
         }
         // no children match the left sub string
         return false;
     }
     // try to match `word[idx] - 'a'`.
     return reg_search(word, idx + 1, node->children[word[idx] - 'a']);
 }
 bool reg_search(string& word, int idx, DictNode* node) {
     // all is match, the last thing is whether current node
     // is a word or not
     if (idx >= word.size()) return node->isWord;
     
     if (word[idx] == '.') {
         // all children match it, find it in next level
         for(auto it = node->children.begin(); it != node->children.end(); it++) {
             if (reg_search(word, idx+1, it->second))
                 return true;
         }
         return false;
     }
     
     // no children match it
     if (node->children.find(word[idx]) == node->children.end())
         return false;
     // match, find in next level
     return reg_search(word, idx+1, node->children[word[idx]]);
 }
Esempio n. 5
0
// reg_ensure - insert value into a register; loading it if needed
reg* reg_ensure(VALUE value){
	reg *res;
	
	res=reg_search(value);
	if(res==NULL)
		res=reg_allocate(value);

	#ifdef VERBOSE
		printf(" [reg_ensure]\t \'%s\' ~~~> \'%s\'\n", value, res->name);
	#endif

	return res;
}
Esempio n. 6
0
File: ltypes.c Progetto: bigml/mgate
NEOERR* session_init(CGI *cgi, HASH *dbh, session_t **ses)
{
    session_t *lses;
    HDF *node, *onode;
    char tok[LEN_HDF_KEY], *s;
    NEOERR *err;

    /*
     * follow cgi_parse(), to process _type_object
     */
    s = hdf_get_value(cgi->hdf, PRE_QUERY"._type_object", NULL);
    if (s) {
        ULIST *list;
        string_array_split(&list, s, ",", 50);
        ITERATE_MLIST(list) {
            snprintf(tok, sizeof(tok), "%s.%s",
                     PRE_QUERY, neos_strip((char*)list->items[t_rsv_i]));
            onode = hdf_get_obj(cgi->hdf, tok);
            if (onode) {
                err = mjson_string_to_hdf(onode, NULL, MJSON_EXPORT_NONE);
                TRACE_NOK(err);
            }
        }
        uListDestroy(&list, ULIST_FREE);
    }

    *ses = NULL;

    lses = calloc(1, sizeof(session_t));
    if (!lses) return nerr_raise(NERR_NOMEM, "calloc memory for session_t failure");

    /*
     * mname
     */
    HDF_FETCH_STR(cgi->hdf, PRE_COOKIE".mname", s);
    if (!s) HDF_FETCH_STR(cgi->hdf, PRE_COOKIE".username", s);
    if (s) lses->mname = strdup(s);

    /*
     * province
     */
    HDF_FETCH_STR(cgi->hdf, PRE_COOKIE".province", s);
    hdf_init(&lses->province);
    if (s) {
        neos_unescape((UINT8*)s, strlen(s), '%');
        hdf_set_value(lses->province, NULL, s);
        mjson_export_to_hdf(lses->province, NULL, MJSON_EXPORT_NONE, false);
    }

    /*
     * city
     */
    HDF_FETCH_STR(cgi->hdf, PRE_COOKIE".city", s);
    hdf_init(&lses->city);
    if (s) {
        neos_unescape((UINT8*)s, strlen(s), '%');
        hdf_set_value(lses->city, NULL, s);
        mjson_export_to_hdf(lses->city, NULL, MJSON_EXPORT_NONE, false);
    }

    /*
     * browser
     */
    HDF_FETCH_STR(cgi->hdf, PRE_HTTP".UserAgent", s);
    if (s) {
        mstr_repchr(s, ' ', '\0');
        for (int i = 0; i < m_browsers_size; i++) {
            if (!strncasecmp(s, m_browsers[i], strlen(m_browsers[i]))) {
                lses->browser = i;
                break;
            }
        }
        s = strchr(s, '/');
        if (s) lses->bversion = strtof(s+1, NULL);
    }

    /*
     * reqtype
     */
    lses->reqtype = CGI_REQ_HTML;
    char *uri = hdf_get_value(cgi->hdf, PRE_REQ_URI_RW, NULL);
    if (!uri) {
        uri = "terminal";
        lses->reqtype = CGI_REQ_TERMINAL;
    }
    mstr_repchr(uri, '/', '_');
    uri = mstr_strip(uri, '_');
    if (!strncmp(uri, "json_", 5)) {
        uri = uri+5;
        lses->reqtype = CGI_REQ_AJAX;
    } else if (!strncmp(uri, "image_", 6)) {
        uri = uri+6;
        lses->reqtype = CGI_REQ_IMAGE;
    }

    /*
     * dataer, render
     */
    switch (http_req_method(cgi)) {
        case CGI_REQ_POST:
            snprintf(tok, sizeof(tok), "%s_data_mod", uri);
            break;
        case CGI_REQ_PUT:
            snprintf(tok, sizeof(tok), "%s_data_add", uri);
            break;
        case CGI_REQ_DEL:
            snprintf(tok, sizeof(tok), "%s_data_del", uri);
            break;
        default:
        case CGI_REQ_GET:
            snprintf(tok, sizeof(tok), "%s_data_get", uri);
            break;
    }
    lses->dataer = strdup(tok);
    lses->render = strdup(uri);

    /*
     * tm_cache_browser
     */
    node = hdf_get_obj(g_cfg, PRE_CFG_FILECACHE".0");
    while (node != NULL) {
        if (reg_search(hdf_get_value(node, "uri", "NULL"), uri)) {
            lses->tm_cache_browser = hdf_get_int_value(node, "tm_cache", 0);
            break;
        }
        node = hdf_obj_next(node);
    }

    /*
     * DONE
     */
    *ses = lses;

    return STATUS_OK;
}
Esempio n. 7
0
/* ------------------------------------------------------------------------------------------ */
void checkInstruction(ins *p){
	reg *rz=NULL;
	reg *rx=NULL;
	reg *ry=NULL;
	reg *rt=NULL;	// temporary register (may or not be used)
	ins *ip=NULL;	// auxiliary instruction pointer (for other registers)
	ins *ti=NULL;	// temporary instruction pointer (to be used with the temporary register)
	ins *mi=NULL;	// main instruction pointer (used with the final instruction)


	/*
	 *  >> input: rz = rx <op> ry
	 *
	 *
	 *	rx = ARP + lookup(rx->value)
	 *    	rx = * rx
	 *    	ry = ARP + lookup(ry->value)
	 *    	ry = * ry
	 *    	rt = rx <op> ry
	 *    	rz = ARP + lookup(rz->value)
	 *	*rz = rt
	 */


	if(p == NULL)
		return;

	#ifdef VERBOSE
		printf("\n");
		table_print(registers);
		printf("[checkInstruction] ");
		printInstruction(p);
	#endif



// :: -------------------------------- ::   THE ALGORITHM   ::

// 1st step:  ensure that 'rx' and 'ry' have register
// --

	// checking 'rx'
	if((p->src1[0] != '\0') && !isNumeric(p->src1)){
		rx=reg_search(p->src1);
		if(rx==NULL){
	
			// allocates register
			rx=reg_ensure(p->src1);
			if(isVar(p->src1)){
				
				// loading the local variable from memory
				load(p->src1);
				
				ip = createInstruction(idx++);
				copy(ip->dst, rx->name);
				ip->arp=true;
				ip->offset=lookup(p->src1);
				append(ip);

				ip = createInstruction(idx++);
				copy(ip->dst, rx->name);
				ip->ops1='*';
				copy(ip->src1, rx->name);
				append(ip);
			}
			if(rx!=NULL){
				// set properties for register
				rx->dist=distance(p, rx->value);
				rx->dirty=false;
			}
		}
	} else rx=NULL;

	// checking 'ry'
	if((p->src2[0] != '\0') && !isNumeric(p->src2)){
		ry=reg_search(p->src2);
		if(ry==NULL){

			// allocates register
			ry=reg_ensure(p->src2);
			if(isVar(p->src2)){
				
				// loading the local variable 'ry' from memory
				load(p->src2);
				
				// loading the local variable 'ry' from memory
				ip = createInstruction(idx++);
				copy(ip->dst, ry->name);
				ip->arp=true;
				ip->offset=lookup(p->src2);
				append(ip);
	
				ip = createInstruction(idx++);
				copy(ip->dst, ry->name);
				ip->ops1='*';
				copy(ip->src1, ry->name);
				append(ip);
			}
			if(ry!=NULL){
				// set properties for register
				ry->dist=distance(p, ry->value);
				ry->dirty=true;
			}
		}
	} else ry=NULL;

// 2nd step: allocate the 'rt' temporary register; creates the 'ti' temporary instruction
// --

	ti = createInstruction(idx++);

	// get 'rx'
	if(isNumeric(p->src1))
		copy(ti->src1, p->src1);  // found a constant
	else if(rx!=NULL)
		copy(ti->src1, rx->name); // got the 'rx'

	// get the operator
	ti->ops2=p->ops2;

	// get 'ry'
	if(isNumeric(p->src2))
		copy(ti->src2, p->src2);  // found a constant
	else if(ry!=NULL)
		copy(ti->src2, ry->name); // got the 'ry'

	if((p->dst[0] != '\0') && !isNumeric(p->dst)){

		// allocate the 'rt' register ("r0" by default)
		rt=reg_search("store");
//		rt=reg_get();
		if(rt!=NULL)
			rt->dirty=false;
	} else rt=NULL; // this could lead to an error
	if(rt!=NULL)
		copy(ti->dst, rt->name);

	append(ti);

// 3rd step: frees if possible frees 'rx' and 'ry'
// --

	// free 'rx'
	if((rx!=NULL) && (rx->dist==MAXDIST || rx->dist==-2))
		reg_free(rx);
	// free 'ry'
	if((ry!=NULL) && (ry->dist==MAXDIST || ry->dist==-2))
		reg_free(ry);

// 4th step: allocate the 'rz' register and create the main instruction 'mi'
// --

	mi = createInstruction(idx++);

	// allocate the 'rz' register
	if((p->dst[0] != '\0') && !isNumeric(p->dst)){

		// store
		store(p->dst);
		rz=reg_search(p->dst);
		if(rz==NULL){
			
			// allocates register
			rz=reg_ensure(p->dst);

			if(isVar(p->dst)){
				// loads the local variable for store operation
				ip = createInstruction(idx++);
				copy(ip->dst, rz->name);
				ip->arp=true;
				ip->offset=lookup(p->dst);
				append(ip);
			}
			if(rz!=NULL){
				// set properties for register
				rz->dist=distance(p, rz->value);
				rz->dirty=false;
			}
		}
	} else rz=NULL; // this would be an error
	if(rz!=NULL)
		copy(mi->dst, rz->name);
	if(rt!=NULL)
		copy(mi->src1, rt->name);
	if(isVar(p->dst))
		mi->opd='*';
	append(mi);


// 5th step: frees 'rt'; if possible frees 'rz'
// --

	#ifdef VERBOSE
		if(rt!=NULL) printf(" [rt] store: %s :: (%s)\n", rt->name, rt->value);
		else printf(" [rt] is null\n");
		if(rz!=NULL) printf(" [rz] store: %s :: (%s)\n", rz->name, rz->value);
		else printf(" [rz] is null\n");
	#endif
	// free 'rt'
	if(rt!=NULL) reg_free(rt);
	// free 'rz'
	if((rz!=NULL) && (rz->dist==MAXDIST || rz->dist<0))
		reg_free(rz);

// 6th step: set the dirty property for the registers
// --
	// check 'rx'
	if(rx!=NULL)
		rx->dirty=true;
	// check 'ry'
	if(ry!=NULL)
		ry->dirty=true;
	// check 'rt'
	if(rt!=NULL)
		rt->dirty=true;
	// check 'rz'
	if(rz!=NULL)
		rz->dirty=false;

// nota: um registo e' dirty apenas quando o seu conteudo e' manipulado na memoria !!!!
//      (confirmar e corrigir se necessario o 6o passo)
//      mudar os valores de dirty para oposto:  'false' <-> 'true'

// :: -------------------------------- ::   THE END   ::

	#ifdef VERBOSE
		table_print(registers);
		printf("\n");
	#endif
	return;
}
 // Returns if the word is in the data structure. A word could
 // contain the dot character '.' to represent any one letter.
 bool search(string word) {
     return reg_search(word, 0, trie);
 }
 // Returns if the word is in the data structure. A word could
 // contain the dot character '.' to represent any one letter.
 bool search(string word) {
     if (word.empty()) return false;
     return reg_search(word, 0, root);
 }