Ejemplo n.º 1
0
void test_match_str(){
    {
        const char* a = "abc";
        char* b = (char*)"abcdefg";
        char* r = match_str(a, b, b+strlen(b));
        if (r != b + strlen(a)) printf("error on test case 1\n");
    }

    {
        const char* a = "abc";
        char* b = (char*)"a";
        char* r = match_str(a, b, b+strlen(b));
        if (r != NULL) printf("error on test case 2\n");
    }

    {
        const char* a = "abc";
        char* b = (char*)"abc";
        char* r = match_str(a, b, b+strlen(b));
        if (r != b + strlen(a)) printf("error on test case 3\n");
    }

    {
        const char* a = "abc";
        char* b = (char*)"accd";
        char* r = match_str(a, b, b+strlen(b));
        if (r != NULL) printf("error on test case 4\n");
    }
    
    printf("all done\n");
}
Ejemplo n.º 2
0
/**
 * tries to determine the NUMA Node, a cpu belongs to
 */
int get_numa_node(int cpu)
{
    int node = -1, ndir, m;
    struct dirent **namelist;
    struct stat statbuf;

    if (cpu == -1) { cpu = get_cpu(); }
    if (cpu != -1)
    {
        strcpy(path, "/sys/devices/system/node/");
        ndir = scandir(path, &namelist, 0, 0);
        if (ndir >= 0)
        {
            while(ndir--)
            {
                if(match_str(namelist[ndir]->d_name, "node", &m))
                {
                    sprintf(path, "/sys/devices/system/node/node%i/cpu%i", m, cpu);
                    if(!stat(path, &statbuf)) {
                        node = m;
                    }
                }
                free(namelist[ndir]);
            }
            free(namelist);
        }
    }

    return node;
}
Ejemplo n.º 3
0
int generic_num_cores_per_package()
{
    struct dirent **namelist;
    int ndir, m, n, pkg_id_tocount = -1;
    char tmppath[_HW_DETECT_MAX_OUTPUT];
    char buf[20];
    id_le *core_id_list = NULL;

    if (num_cores_per_package_sav != 0) return num_cores_per_package_sav;
    num_cores_per_package_sav=-1;

    strcpy(path, "/sys/devices/system/cpu/");
    ndir = scandir(path, &namelist, 0, 0);
    if(ndir >= 0)
    {
        while(ndir--) {
            if(match_str(namelist[ndir]->d_name, "cpu", &m)) {
                strncpy(tmppath, path,sizeof(tmppath));
                snprintf(buf, sizeof(buf), "cpu%i", m);
                strcat(tmppath, buf);
                strcat(tmppath, "/online");
                read_file(tmppath, output, sizeof(output));
                if (strncmp(output, "0", 1)!=0) {
                    strcpy(tmppath, path);
                    sprintf(buf, "cpu%i", m);
                    strcat(tmppath, buf);
                    strcat(tmppath, "/topology/physical_package_id");
                    read_file(tmppath, output, _HW_DETECT_MAX_OUTPUT);
                    m = atoi(output);
                    if(pkg_id_tocount == -1) pkg_id_tocount = m;

                    strcpy(tmppath, path);
                    strcat(tmppath, buf);
                    strcat(tmppath, "/topology/core_id");
                    read_file(tmppath, output, _HW_DETECT_MAX_OUTPUT);
                    n = atoi(output);

                    if(m == pkg_id_tocount) /*FIXME: only counts cores from first package_id that is found, assumes that every package has the same amount of cores*/
                    {
                        //if (num<n+1) num=n+1; //doesn't work if there is a gap in between the ids
                        inc_id_count(n, &core_id_list);
                    }
                }
            }
            free(namelist[ndir]);
        }
        free(namelist);
        num_cores_per_package_sav = id_total_count(core_id_list);
        free_id_list(&core_id_list);
    }
    else num_cores_per_package_sav = -1;

    if (num_cores_per_package_sav == 0) num_cores_per_package_sav = -1;

    return num_cores_per_package_sav;
}
Ejemplo n.º 4
0
void json_value() {
	if(look_ahead == '{')
		json_object();
	else if(look_ahead == '[')
		json_array();
	else if(look_ahead == '\"'){
		std::string d = match_str();
		std::cout << "Get Value: " << d <<std::endl;
	}else{
		// number,true, false, null
		if(look_ahead == '\"')
			match_str();
		else if(look_ahead == 't' || look_ahead == 'f'){
			match_bool(look_ahead);
		}else if(look_ahead<='9' && look_ahead >='0'){
			match_digit();
		}else
		;
	}
}
Ejemplo n.º 5
0
int generic_num_threads_per_core()
{
    struct dirent **namelist;
    int ndir, m, n, pkg_id_tocount = -1, core_id_tocount = -1;
    char tmppath[_HW_DETECT_MAX_OUTPUT];
    char buf[20];

    if (num_threads_per_core_sav != 0) return num_threads_per_core_sav;

    strcpy(path, "/sys/devices/system/cpu/");
    ndir = scandir(path, &namelist, 0, 0);
    if(ndir >= 0)
    {
        while(ndir--) {
            if(match_str(namelist[ndir]->d_name, "cpu", &m)) {
                strncpy(tmppath, path, sizeof(tmppath));
                snprintf(buf, sizeof(buf), "cpu%i", m);
                strcat(tmppath, buf);
                strcat(tmppath, "/online");
                read_file(tmppath, output, sizeof(output));
                if (strncmp(output,"0",1) != 0) {
                    strcpy(tmppath, path);
                    sprintf(buf, "cpu%i", m);
                    strcat(tmppath, buf);
                    strcat(tmppath, "/topology/core_id");
                    read_file(tmppath, output, _HW_DETECT_MAX_OUTPUT);
                    m = atoi(output);
                    if(core_id_tocount == -1) core_id_tocount = m;

                    strcpy(tmppath, path);
                    strcat(tmppath, buf);
                    strcat(tmppath, "/topology/physical_package_id");
                    read_file(tmppath, output, _HW_DETECT_MAX_OUTPUT);
                    n = atoi(output);
                    if(pkg_id_tocount == -1) pkg_id_tocount = n;

                    if(m == core_id_tocount && n == pkg_id_tocount) /*FIXME: only counts threads from the first core_id and package_id that are found, assumes that every core has the same amount of threads*/
                    {
                        num_threads_per_core_sav++;
                    }
                }
            }
            free(namelist[ndir]);
        }
        free(namelist);
    }
    else num_threads_per_core_sav = -1;

    if (num_threads_per_core_sav == 0) num_threads_per_core_sav = generic_num_threads_per_package() / generic_num_cores_per_package();
    if (num_threads_per_core_sav != generic_num_threads_per_package() / generic_num_cores_per_package()) num_threads_per_core_sav = -1;

    return num_threads_per_core_sav;
}
Ejemplo n.º 6
0
/**
 * Determine number of CPUs in System
 */
int num_cpus()
{
    struct dirent **namelist;
    int ndir, c, num=0, m;
    char tmppath[_HW_DETECT_MAX_OUTPUT];
    char buf[20];

    /* try sysconf online cpus */
    num = sysconf(_SC_NPROCESSORS_ONLN);

    /* extract information from sysfs */
    if (num <= 0) {
        strcpy(path, "/sys/devices/system/cpu/");
        ndir = scandir(path, &namelist, 0, 0);
        if(ndir >= 0)
        {
            c = 0;
            while(ndir--) {
                if(match_str(namelist[ndir]->d_name, "cpu", &m)) {
                    strncpy(tmppath, path, sizeof(tmppath));
                    snprintf(buf, sizeof(buf), "cpu%i", m);
                    strcat(tmppath, buf);
                    strcat(tmppath, "/online");
                    read_file(tmppath, output, sizeof(output));
                    if (strncmp(output,"0",1) != 0) {
                        c++;
                    }
                }
                free(namelist[ndir]);
            }
            free(namelist);
            if(c > 0) num = c;
            else num = -1;
        }
    }

    /*TODO proc/cpuinfo*/

    /* try sysconf configured cpus */
    if (num <= 0) { num = sysconf(_SC_NPROCESSORS_CONF); }

    /*assume 1 if all detection methods fail*/
    if (num < 1) { num = 1; }

    return num;
}
Ejemplo n.º 7
0
/**
 * determines how many NUMA Nodes are in the system
 */
int num_numa_nodes()
{
    struct dirent **namelist;
    int ndir, c, m;

    strcpy(path, "/sys/devices/system/node/");

    ndir = scandir(path, &namelist, 0, 0);
    if(ndir >= 0)
    {
        c = 0;
        while(ndir--) {
            if(match_str(namelist[ndir]->d_name, "node", &m)) { c++; }
            free(namelist[ndir]);
        }
        free(namelist);
        if(c > 0) { return c; }
    }
    return -1;
}
Ejemplo n.º 8
0
/**
 * number of caches (of one cpu)
 */
int generic_num_caches(int cpu)
{
    struct dirent **namelist;
    int ndir, c, m;

    if (cpu==-1) { cpu = get_cpu(); }
    if (cpu==-1) { return -1; }

    sprintf(path, "/sys/devices/system/cpu/cpu%i/cache/", cpu);
    ndir = scandir(path, &namelist, 0, 0);
    if(ndir >= 0)
    {
        c = 0;
        while(ndir--) {
            if(match_str(namelist[ndir]->d_name, "index", &m)) c++;
            free(namelist[ndir]);
        }
        free(namelist);
        if(c > 0) return c;
    }
    return -1;
}
Ejemplo n.º 9
0
/**
* the following four functions describe how the CPUs are distributed among packages
* num_cpus() = num_packages() * num_threads_per_package()
* num_threads_per_package() = num_cores_per_package() * num_threads_per_core()
*/
int generic_num_packages()
{
    struct dirent **namelist;
    int ndir, m;
    char tmppath[_HW_DETECT_MAX_OUTPUT];
    char buf[20];
    id_le * pkg_id_list = NULL;

    if (num_packages_sav != 0) return num_packages_sav;
    num_packages_sav = -1;

    strcpy(path, "/sys/devices/system/cpu/");
    ndir = scandir(path, &namelist, 0, 0);
    if(ndir >= 0)
    {
        while(ndir--) {
            if(match_str(namelist[ndir]->d_name, "cpu", &m)) {
                strncpy(tmppath, path, sizeof(tmppath));
                snprintf(buf, sizeof(buf), "cpu%i", m);
                strcat(tmppath, buf);
                strcat(tmppath, "/online");
                read_file(tmppath, output, sizeof(output));
                if (strncmp(output,"0",1) != 0) {
                    strncpy(tmppath, path, sizeof(tmppath));
                    snprintf(buf, sizeof(buf), "cpu%i", m);
                    strcat(tmppath, buf);
                    strcat(tmppath, "/topology/physical_package_id");
                    if(read_file(tmppath, output, sizeof(output)))
                        inc_id_count(atoi(output), &pkg_id_list);
                }
            }
            free(namelist[ndir]);
        }
        free(namelist);
        num_packages_sav = id_total_count(pkg_id_list);
        free_id_list(&pkg_id_list);
    }
    return num_packages_sav;
}
Ejemplo n.º 10
0
Archivo: re.c Proyecto: tokuhirom/Pone
static pone_val* match_str(pone_world* world, pone_val* self, int n, int indent) {
    pone_val* orig = pone_obj_get_ivar(world, self, "$!orig");
    pone_int_t from = pone_intify(world, pone_obj_get_ivar(world, self, "$!from"));
    pone_int_t to = pone_intify(world, pone_obj_get_ivar(world, self, "$!to"));
    pone_val* list = pone_obj_get_ivar(world, self, "@!list");

    pone_val* buf = pone_str_new_strdup(world, "", 0);
    for (int i = 0; i < indent; ++i) {
        pone_str_append_c(world, buf, " ", strlen(" "));
    }
    if (n >= 0) {
        pone_str_appendf(world, buf, "%d ", n);
    }
    pone_str_append_c(world, buf, "「", strlen("「"));
    pone_str_append_c(world, buf, pone_str_ptr(orig) + from, to - from);
    pone_str_append_c(world, buf, "」\n", strlen("」\n"));
    for (pone_int_t i = 0; i < pone_ary_size(list); ++i) {
        pone_val* match = pone_ary_at_pos(list, i);
        pone_str_append(world, buf, match_str(world, match, i, indent + 1));
    }
    return buf;
}
Ejemplo n.º 11
0
/**
 * execute example_cmd.
 * @ param unit chip unit
 * @ a argument list 
 * @return stat.
 */
cmd_result_t cmd_example_exec(int unit, args_t *a) {
  ApplRefFunction_t * pexpls;
  char *subcmd;
  int rc = 0;

  printk("Invoking example cmd\n");

  if ( (subcmd = ARG_GET(a)) == NULL )
    return CMD_USAGE;

  if ( exampleCmdGetCmdList == NULL || 
       (pexpls = (*exampleCmdGetCmdList)()) == NULL ||
       pexpls->pf == NULL ) 
    {
      printk("No Examples Available\n");
      return CMD_OK;
    }
  
  if ( sal_strcasecmp(subcmd, "list") == 0 ) {
    ApplRefFunction_t *pe;
    char buf[128];
    buf[0] = 0;

    for( pe = pexpls; pe->pf != NULL; pe++ ) {

      if ( match_str( pe->pname, buf, sizeof(buf)-1 ) )
	printk("===================================================================================\n");

      printk("  %-25s : %s\n", pe->pname, pe->pbriefdescr);
    }
    return CMD_OK;
  }


  if ( sal_strcasecmp(subcmd, "alllist") == 0 ) {
    ApplRefFunction_t *pe;
    char buf[128];
    buf[0] = 0;

    for( pe = pexpls; pe->pf != NULL; pe++ ) {

      if ( match_str( pe->pname, buf, sizeof(buf)-1 ) )
	printk("===================================================================================\n");

      printk("  %-25s : %s\n", pe->pname, pe->pbriefdescr);

      printk("\n**********************************************\n");
      printk("%s\n", pe->pdescr);
      printk("\n**********************************************\n");      

    }
    return CMD_OK;
  }


  if ( sal_strcasecmp(subcmd, "info") == 0 ) {
    ApplRefFunction_t *pe;
    
    if ( (subcmd = ARG_GET(a)) == NULL )
      return CMD_USAGE;
    
    pe = findFunc( pexpls, subcmd );
    if ( pe == NULL ) {
      printk("Example %s is not found\n", subcmd);
      return CMD_FAIL;
    }

    printk("\n**********************************************\n");
    printk("%s\n", pe->pdescr);
    printk("\n**********************************************\n");
    
    return CMD_OK;
  }

  if ( sal_strcasecmp(subcmd, "exec") == 0 ) {
    ApplRefFunction_t *pe;

    if ( (subcmd = ARG_GET(a)) == NULL )
      return CMD_USAGE;
    
    pe = findFunc( pexpls, subcmd );
    if ( pe == NULL ) {
      printk("Example %s is not found\n", subcmd);
      return CMD_FAIL;
    }

    /** invoke command */
    {
      char *args[16]; /** limit to 16 args */
      int n = 0;
      
      while ( (n < 16) && ( (args[n] = ARG_GET(a)) != NULL ) )
	n++;

      if ( n == 16 ) {
	printk("Excessive amount of arguments\n");
	return CMD_FAIL;
      }

      if ( BCM_FAILURE(rc = (*pe->dispatch)(pe->pf, n, args)) ) {
	printk( "Error : fail to execute %d %s\n", rc, bcm_errmsg(rc) );
	return rc;
      }
      	
    }
    
    return CMD_OK;
  }

  return CMD_USAGE;

}
Ejemplo n.º 12
0
int http_parse(h_parser_ctx * ctx)
{
    h_head_info * info;
    lx_buffer *  orig_buff;
    char * end, *buff,temp_buff;
    int parser_index,maxlen,tint;
    lx_kvlist_node * headers,*header ; 

 loop:
    orig_buff = &ctx->orig_buff;

    buff = orig_buff->base;
    maxlen = orig_buff->len;
    parser_index = orig_buff->offset;
    info = (h_head_info *)&ctx->info;

    if(orig_buff->offset >=  orig_buff->len )
        goto need_more;

    switch(ctx->cur_stat)
    {
    case S_NONE:

        if(ctx->type == T_REQ){
            ctx->cur_stat = S_REQ_METHOD;
        }else{
            ctx->cur_stat = S_RESP_PROTOCAL;
        }

        info->type = ctx->type;
        info->base = orig_buff->base;
        goto loop;

    case S_REQ_METHOD:
        
        //if(info->mtod_str.offset == 0)
          //  info->mtod_str.offset = parser_index;
        
        if((end = read2space(buff +parser_index , maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        if( (tint=match_str(buff+ parser_index, h_mtod_str,sizeof(h_mtod_str)/sizeof(h_mtod_str[0]) -1 )  ) == -1)
            return HEC_INVALID_METHOD;
        info->mtod = tint;    
        orig_buff->offset = end - buff;
        ctx->cur_stat = S_ITEM_SPACE;
        ctx->next_stat = S_REQ_URI;
        goto loop;
        
    case S_REQ_URI:
         
        if(info->uri.offset == 0)
            info->uri.offset =  parser_index;
        
        if((end = read2space(buff + parser_index, maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        orig_buff->offset = end - buff;
        ctx->last_stat = S_REQ_URI;
        ctx->cur_stat = S_ITEM_SPACE;
        ctx->next_stat = S_REQ_PROTOCAL;
        goto loop;
    
    case S_REQ_PROTOCAL:
        if(ctx->type ==T_REQ && info->prot_str.offset == 0)
            info->prot_str.offset = parser_index;
        
        if((end = read2nl(buff + parser_index , maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        if( tint = get_http_prot(buff + parser_index,end - buff - parser_index) == -1)
            return HEC_INVALID_PROT;
        info->prot = tint;

        orig_buff->offset = end - buff;
                
        ctx->cur_stat = S_NEW_LINE;
        ctx->next_stat = S_HEADER_KEY;
        
        goto loop;

    case S_RESP_PROTOCAL: 
        //if(info->prot_str.offset == 0)
          //  info->prot_str.offset = parser_index;
        
        if((end = read2space(buff + parser_index , maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        if( tint = get_http_prot(buff + parser_index,end - buff - parser_index) == -1)
            return HEC_INVALID_PROT;
        info->prot = tint;

        orig_buff->offset = end - buff;
        
        ctx->cur_stat = S_ITEM_SPACE;
        ctx->next_stat = S_RESP_CODE;
        
        goto loop;

    case S_RESP_CODE:
         if(ctx->temp_buff.offset == 0)
            ctx->temp_buff.offset = parser_index;
        
        if((end = read2space(buff +parser_index , maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        if( ((tint = atoi(buff+ parser_index)) < 0))
            return HEC_INVALID_CODE;
        info->resp_code = tint;   

        orig_buff->offset = end - buff;
        ctx->cur_stat = S_ITEM_SPACE;
        ctx->next_stat = S_RESP_STR;
        goto loop;
        
    case S_RESP_STR:
         if(info->code_str.offset == 0)
            info->code_str.offset = parser_index;
        
        if((end = read2nl(buff + parser_index , maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        orig_buff->offset = end - buff;
        
        ctx->cur_stat = S_NEW_LINE;
        ctx->next_stat = S_HEADER_KEY;
        goto loop;
 
    case S_HEADER_KEY:
        if(info->header_key.offset == 0)
            info->header_key.offset = parser_index;
        
        if( (end = read2str(buff+parser_index, maxlen - parser_index,HTTP_HEADER_SEP_STR)) == NULL)
            goto need_more;
        
        orig_buff->offset = end - buff;
        ctx->cur_stat = S_HEADER_SEP;
        goto loop;

    case S_HEADER_VALUE:
        if(info->header_value.offset == 0 )
            info->header_value.offset = parser_index;

        if((end = read2nl(buff + parser_index , maxlen - parser_index ))
               == NULL )
            goto need_more;
        
        header =(lx_kvlist_node *)list_append((lx_list_node **)&info->headers,sizeof(lx_kvlist_node),ctx->hmalloc); 
        if(header == NULL)
            return HEC_APPNODE_ERR;
       
        header->key =  info->header_key;   
        header->value = info->header_value;
        
        info->header_key.offset = 0;
        info->header_value.offset = 0;

        orig_buff->offset = end - buff;
        ctx->cur_stat = S_NEW_LINE;
        ctx->next_stat = S_HEADER_KEY; 
        
        goto loop;

    case S_HEADER_SEP:
        orig_buff->base[ orig_buff->offset] = 0;
        orig_buff->offset += strlen(HTTP_HEADER_SEP_STR);
        ctx->cur_stat = S_HEADER_VALUE;
        goto loop;

    case S_BODY:
        break;
    case S_ITEM_SPACE:
        orig_buff->base[ orig_buff->offset] = 0;
        orig_buff->offset++;

        if(ctx->last_stat == S_REQ_URI)
            if(http_parse_uri(ctx))
                return HEC_PARSE_URI_ERR;
        ctx ->last_stat = S_NONE;

        ctx->cur_stat = ctx->next_stat;
        goto loop;

    case S_NEW_LINE:
        if(maxlen - orig_buff->offset <(int)(2 * strlen(HTTP_NEW_LINE_STR)) )
            goto need_more;
        
        orig_buff->base[ orig_buff->offset] = 0;
        orig_buff->offset +=2;
        
        if( strncmp(orig_buff->base + orig_buff->offset
            , HTTP_NEW_LINE_STR,strlen(HTTP_NEW_LINE_STR ) ) == 0 )
        {
            orig_buff->offset += 2; 
            return HEC_OK;
        }

        ctx->cur_stat = ctx->next_stat;
        goto loop;
    default: 
        ;
    }

    return 0;    

need_more:
    if(orig_buff->len == orig_buff->maxlen)
    {
        if( ctx->hextend(orig_buff) )
            return HEC_EXTEND_ERR;
        info->base = orig_buff->base;    
    }
    return HEC_NEED_MORE;
};
Ejemplo n.º 13
0
int nm2type(int nnm, t_nm2type nm2t[], t_symtab *tab, t_atoms *atoms,
            gpp_atomtype_t atype, int *nbonds, t_params *bonds)
{
    int      cur = 0;
#define prev (1-cur)
    int      i, j, k, m, n, nresolved, nb, maxbond, ai, aj, best, im, nqual[2][ematchNR];
    int     *bbb, *n_mask, *m_mask, **match, **quality;
    char    *aname_i, *aname_m, *aname_n, *type;
    double   qq, mm;
    t_param *param;

    snew(param, 1);
    maxbond = 0;
    for (i = 0; (i < atoms->nr); i++)
    {
        maxbond = max(maxbond, nbonds[i]);
    }
    if (debug)
    {
        fprintf(debug, "Max number of bonds per atom is %d\n", maxbond);
    }
    snew(bbb, maxbond);
    snew(n_mask, maxbond);
    snew(m_mask, maxbond);
    snew(match, maxbond);
    for (i = 0; (i < maxbond); i++)
    {
        snew(match[i], maxbond);
    }

    nresolved = 0;
    for (i = 0; (i < atoms->nr); i++)
    {
        aname_i = *atoms->atomname[i];
        nb      = 0;
        for (j = 0; (j < bonds->nr); j++)
        {
            ai = bonds->param[j].AI;
            aj = bonds->param[j].AJ;
            if (ai == i)
            {
                bbb[nb++] = aj;
            }
            else if (aj == i)
            {
                bbb[nb++] = ai;
            }
        }
        if (nb != nbonds[i])
        {
            gmx_fatal(FARGS, "Counting number of bonds nb = %d, nbonds[%d] = %d",
                      nb, i, nbonds[i]);
        }
        if (debug)
        {
            fprintf(debug, "%4s has bonds to", aname_i);
            for (j = 0; (j < nb); j++)
            {
                fprintf(debug, " %4s", *atoms->atomname[bbb[j]]);
            }
            fprintf(debug, "\n");
        }
        best = -1;
        for (k = 0; (k < ematchNR); k++)
        {
            nqual[prev][k] = 0;
        }

        /* First check for names */
        for (k = 0; (k < nnm); k++)
        {
            if (nm2t[k].nbonds == nb)
            {
                im = match_str(*atoms->atomname[i], nm2t[k].elem);
                if (im > ematchWild)
                {
                    for (j = 0; (j < ematchNR); j++)
                    {
                        nqual[cur][j] = 0;
                    }

                    /* Fill a matrix with matching quality */
                    for (m = 0; (m < nb); m++)
                    {
                        aname_m = *atoms->atomname[bbb[m]];
                        for (n = 0; (n < nb); n++)
                        {
                            aname_n     = nm2t[k].bond[n];
                            match[m][n] = match_str(aname_m, aname_n);
                        }
                    }
                    /* Now pick the best matches */
                    for (m = 0; (m < nb); m++)
                    {
                        n_mask[m] = 0;
                        m_mask[m] = 0;
                    }
                    for (j = ematchNR-1; (j > 0); j--)
                    {
                        for (m = 0; (m < nb); m++)
                        {
                            for (n = 0; (n < nb); n++)
                            {
                                if ((n_mask[n] == 0) &&
                                    (m_mask[m] == 0) &&
                                    (match[m][n] == j))
                                {
                                    n_mask[n] = 1;
                                    m_mask[m] = 1;
                                    nqual[cur][j]++;
                                }
                            }
                        }
                    }
                    if ((nqual[cur][ematchExact]+
                         nqual[cur][ematchElem]+
                         nqual[cur][ematchWild]) == nb)
                    {
                        if ((nqual[cur][ematchExact] > nqual[prev][ematchExact]) ||

                            ((nqual[cur][ematchExact] == nqual[prev][ematchExact]) &&
                             (nqual[cur][ematchElem] > nqual[prev][ematchElem])) ||

                            ((nqual[cur][ematchExact] == nqual[prev][ematchExact]) &&
                             (nqual[cur][ematchElem] == nqual[prev][ematchElem]) &&
                             (nqual[cur][ematchWild] > nqual[prev][ematchWild])))
                        {
                            best = k;
                            cur  = prev;
                        }
                    }
                }
            }
        }
        if (best != -1)
        {
            int  atomnr = 0;
            real alpha  = 0;

            qq   = nm2t[best].q;
            mm   = nm2t[best].m;
            type = nm2t[best].type;

            if ((k = get_atomtype_type(type, atype)) == NOTSET)
            {
                atoms->atom[i].qB = alpha;
                atoms->atom[i].m  = atoms->atom[i].mB = mm;
                k                 = add_atomtype(atype, tab, &(atoms->atom[i]), type, param,
                                                 atoms->atom[i].type, 0, 0, 0, atomnr, 0, 0);
            }
            atoms->atom[i].type  = k;
            atoms->atom[i].typeB = k;
            atoms->atom[i].q     = qq;
            atoms->atom[i].qB    = qq;
            atoms->atom[i].m     = mm;
            atoms->atom[i].mB    = mm;
            nresolved++;
        }
        else
        {
            fprintf(stderr, "Can not find forcefield for atom %s-%d with %d bonds\n",
                    *atoms->atomname[i], i+1, nb);
        }
    }
    sfree(bbb);
    sfree(n_mask);
    sfree(m_mask);
    sfree(param);

    return nresolved;
}
Ejemplo n.º 14
0
void name_value() {
	std::string name;
	name = match_str();match(':');json_value();
	std::cout << "Get Name: " << name << std::endl;
}
Ejemplo n.º 15
0
Archivo: main.cpp Proyecto: weizn11/C
int create_client(GtkWidget *widget,gpointer Parameter)
{
	ClientConfigStruct *CCS=(ClientConfigStruct *)Parameter;
	FILE *file=NULL;
	unsigned long FileSize=0;
	char *FileBuffer=NULL,*strbuffer=NULL,*pStart=NULL,tmp=85;
	int i;

	if(release_client()!=0)
	{
		MessageBox(NULL,"Create client failed!","ERROR",MB_OK);
		return -1;
	}

	file=fopen("Client.exe","rb+");
	if(file==NULL)
	{
		MessageBox(NULL,"Open file failed!","ERROR",MB_OK);
	}
	fseek(file,0,SEEK_END);
	FileSize=ftell(file);
	fseek(file,0,SEEK_SET);
	FileBuffer=(char *)malloc(sizeof(char)*FileSize);
	if(FileBuffer==NULL)
		exit(-1);
	memset(FileBuffer,NULL,sizeof(char)*FileSize);
	fread(FileBuffer,sizeof(char),FileSize,file);

	/*-------------------修改客户端文件内容---------------------*/
	//修改服务端IP地址
	pStart=match_str(FileBuffer,"SERVER_IP:",FileSize,strlen("SERVER_IP:"));
	if(pStart==NULL)
	{
		MessageBox(NULL,"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	pStart++;
	strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Server_IP_entry));
	memcpy(pStart,strbuffer,strlen(strbuffer));
	for(pStart-=strlen("SERVER_IP:"),i=0;i<50;pStart++,i++)
        *pStart^=tmp;

	//修改服务端监听端口
	pStart=match_str(FileBuffer,"SERVER_PORT:",FileSize,strlen("SERVER_PORT:"));
	if(pStart==NULL)
	{
		MessageBox(NULL,"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	pStart++;
	strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Server_Port_entry));
	memcpy(pStart,strbuffer,strlen(strbuffer));
	for(pStart-=strlen("SERVER_PORT:"),i=0;i<50;pStart++,i++)
        *pStart^=tmp;

	//修改HTTP代理服务器IP
	pStart=match_str(FileBuffer,"PROXY_IP:",FileSize,strlen("PROXY_IP:"));
	if(pStart==NULL)
	{
		MessageBox(NULL,"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	pStart++;
	strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_IP_entry));
	memcpy(pStart,strbuffer,strlen(strbuffer));
	for(pStart-=strlen("PROXY_IP:"),i=0;i<50;pStart++,i++)
        *pStart^=tmp;

	//修改HTTP代理服务器连接端口
	pStart=match_str(FileBuffer,"PROXY_PORT:",FileSize,strlen("PROXY_PORT:"));
	if(pStart==NULL)
	{
		MessageBox(NULL,"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	pStart++;
	strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_Port_entry));
	memcpy(pStart,strbuffer,strlen(strbuffer));
	for(pStart-=strlen("PROXY_PORT:"),i=0;i<50;pStart++,i++)
        *pStart^=tmp;

	//修改代理服务器登陆账号
	pStart=match_str(FileBuffer,"PROXY_USERNAME:"******"PROXY_USERNAME:"******"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	pStart++;
	strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_Username_entry));
	memcpy(pStart,strbuffer,strlen(strbuffer));
	for(pStart-=15,i=0;i<50;pStart++,i++)
        *pStart^=tmp;

	//修改代理服务器登陆密码
	pStart=match_str(FileBuffer,"PROXY_PASSWORD:"******"PROXY_PASSWORD:"******"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	pStart++;
	strbuffer=(char *)gtk_entry_get_text(GTK_ENTRY(CCS->Proxy_Password_entry));
	memcpy(pStart,strbuffer,strlen(strbuffer));
	for(pStart-=strlen("PROXY_PASSWORD:"******"RC4_KEY:",FileSize,strlen("RC4_KEY:"));
	for(pStart-=strlen("RC4_KEY:"),i=0;i<256;pStart++,i++)
        *pStart^=tmp;

	/*-------------------保存修改的内容---------------------*/
	fclose(file);
	if((file=fopen("Client.exe","wb"))==NULL)
	{
		MessageBox(NULL,"Create client failed!","ERROR",MB_OK);
		fclose(file);
		free(FileBuffer);
		return -1;
	}
	fwrite(FileBuffer,sizeof(char),FileSize,file);

	fclose(file);
	free(FileBuffer);
	MessageBox(NULL,"Create client successfully!","Success",MB_OK);

	return 0;
}
Ejemplo n.º 16
0
void l_scan (SOURCE_FILE * sf)
{
	int r = 1,in_sub = FALSE;
	int block_stack[STACK_SIZE],block_size = 0;

	list_init(&list_sub);
	list_init(&list_var);
	list_init(&list_array);

	while(r)
	{
		r = l_get_line(sf);
		pline = line;
		l_get_token ();
		// skip empty line
		if (token_type==TT_LINE_END) continue;
		// check command
		if (IS_KEYWORD(token_type))
		{
			if (token_type==KEY_SUB)
			{
				USER_SUB * sub;
				
				if(in_sub)
				{
					merror_msg("sub procedure can not be nested");
				}
				in_sub = TRUE;

				match_type(TT_ID);
				sub = (USER_SUB*)calloc(sizeof(USER_SUB),1);
				sub->name	= s_strdup(token);
				sub->pos	= sf->pos;
				list_push(&list_sub,sub);
				match_type(TT_LINE_END);
				push_block(B_SUB);
				continue;
			}
			else if (token_type==KEY_END)
			{
				if (block_size<=0 || block_top==B_REPEAT)
					merror_illegal_token();
				match_type(TT_LINE_END);
				if(pop_block==B_SUB)
				{
					in_sub = FALSE;
				}
				continue;
			}
			else if(token_type==KEY_VAR)
			{
				while(1)
				{
					match_type(TT_ID);
					l_get_token();
					if (token_type==TT_END)			break;
					else if (token_type==TT_COM)	continue;
					else 							merror_illegal_token();
				}
			}
			if (!in_sub) merror_illegal_token();
			if(token_type==KEY_IF)
			{
				match_exp(pline);
				match_type(TT_LINE_END);
				push_block(B_IF);
			}
			else if (token_type==KEY_ELSEIF)
			{
				if (block_size<=0 || block_top!=B_IF)
					merror_illegal_token();
				match_exp(pline);
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_ELSE)
			{
				if (block_size<=0 || !(block_top==B_IF || block_top==B_CASE))
					merror_illegal_token();
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_WHILE)
			{
				match_exp(pline);
				match_type(TT_LINE_END);
				push_block(B_WHILE);
			}
			else if (token_type==KEY_FOR)
			{
				// 'for' ID '=' EXP 'to' EXP
				match_type(TT_ID);
				match_type(OPR_EQ);
				match_exp(pline);
				match_str("to");
				match_exp(pline);
				l_get_token();
				if (token_type!=TT_LINE_END)
				{
					if (!str_eq(token,"step"))
						merror_expect("step");
					match_exp(pline);
					match_type(TT_LINE_END);
				}
				push_block(B_FOR);
			}
			else if (token_type==KEY_CASE)
			{
				match_type(TT_ID);
				match_type(TT_LINE_END);
				push_block(B_CASE);
			}
			else if (token_type==KEY_REPEAT)
			{
				match_type(TT_LINE_END);
				push_block(B_REPEAT);
			}
			else if (token_type==KEY_UNTIL)
			{
				if (block_size<=0 || block_top!=B_REPEAT)
					merror_illegal_token();
				match_exp(pline);
				match_type(TT_LINE_END);
				pop_block;
			}
			else if (token_type==KEY_WHEN)
			{
				if (block_size<=0 || block_top!=B_CASE)
					merror_illegal_token();
				match_exp(pline);
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_GOSUB)
			{
				match_type(TT_ID);
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_EXIT)
			{
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_BREAK)
			{
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_RETURN)
			{
				match_type(TT_LINE_END);
			}
			else if (token_type==KEY_DIM)
			{
				while(1)
				{
					match_type	(TT_ID);
					match_type	(TT_LBK);
					match_exp	(pline);
					match_type	(TT_RBK);
					l_get_token();
					if		(token_type==TT_COM)		continue;
					else if	(token_type==TT_LINE_END)	break;
					else								merror_illegal_token();
				}
			}
		}
		else if (token_type==TT_ID)
		{
			if (!in_sub) merror_illegal_token();
			if (str_eq(token,"print"))
			{
				while(1)
				{
					l_get_token();
					if(token_type!=TT_STRING)
					{
						l_put_back();
						match_exp(pline);
					}
					l_get_token();
					if(token_type==TT_LINE_END) break;
					else if (token_type==TT_COM) continue;
					else merror_expect(",");
				}
			}
			else if (str_eq(token,"input"))
			{
				while(1)
				{
					match_type(TT_ID);
					l_get_token();
					if(token_type==TT_LINE_END) break;
					else if (token_type==TT_COM) continue;
					else merror_expect(",");
				}
			}
			else
			{	// match: [var][=][exp]
				l_get_token();
				if (token_type==OPR_EQ)
				{
					match_exp(pline);
					match_type(TT_LINE_END);
				}
				else
				{
					if (token_type!=TT_LBK)merror_expect("(");
					match_exp(pline);
					match_type(TT_RBK);
					match_type(OPR_EQ);
					match_exp(pline);
					match_type(TT_LINE_END);
				}
			}
		}
		else 
			merror_illegal_token ();
	}
	if (block_size>0)
		merror_msg("incompleted '%s' block!",BLOCK_NAME[block_top]);
}
Ejemplo n.º 17
0
// match optional expression
void match_exp (const char * exp)
{
	int state = 1;

	pline = exp;

	while(1)
	{
		//printf("state=%d\t",state);getch();
		switch(state)
		{
			//-----------------------------------------------------------------------
			// <CONSTANT>			=> 2
			// <ID>					=> 2
			// <UNARY OPR>			=> 1
			// (					=> 3
			// <FUNC>				=> 4
			case 1:
			{
				l_get_token();//printf("[%s]\n",token);
				if (token_type==OPR_SUB)
				{
					token_type = OPR_NEG;
					move(1);
				}
				else if (IS_OPR(token_type) && IS_UNARY_OPR(token_type))
				{
					move(1);
				}
				else if (IS_CONSTANT(token_type)) 
				{
					move(2);
				}
				else if (token_type==TT_ID)
				{
					move(5);
				}
				else if (token_type==TT_LBK)
				{
					move(3);
				}
				else if (IS_RESWORD(token_type))
				{
					move(4);
				}
				else
					merror_illegal_token();
			}break;
			//-----------------------------------------------------------------------
			// <OPR>				=> 1
			// <OTHER THINGS!>		=> END!
			case 2:
			{
				l_get_token();//printf("[%s]\n",token);
				if (IS_OPR(token_type))
				{
					if(IS_BINARY_OPR(token_type))
					{
						move(1);
					}
					else
					{
						merror_illegal_token();
					}
				}
				// END!
				l_put_back();
				return;
			}break;
			//-----------------------------------------------------------------------
			// 	<EXP> then )		=> 2
			case 3:
			{
				//puts("<match inner expr>");
				match_exp(pline);
				match_str(")");
				move(2);
			}break;
			//-----------------------------------------------------------------------
			// ( then <EXP>,<EXP>,<EXP>... then )	=> 2
			case 4:
			{
				int argc = token_ext,i;
				match_str("(");
				for(i=0;i<argc;++i)
				{
					match_exp(pline);
					if (i != argc-1)	match_str(",");
					else				match_str(")");
				}
				move(2);
			}break;
			//-----------------------------------------------------------------------
			// 
			case 5:
			{
				l_get_token();
				if (token_type==TT_LBK)	
				{
					move(3);
				}
				else
				{
					l_put_back();
					move(2);
				}
			}break;
		}
	}
}