Exemplo n.º 1
0
zcThreadSeq*   
zc_threadseq_new (int th)
//zc_threadseq_new (int maxth, int minth, int idle)
{
    zcThreadSeq    *q; 

    q = (zcThreadSeq*)zc_malloc(sizeof(zcThreadSeq));
    memset(q, 0, sizeof(zcThreadSeq));

    q->max_thread  = th;
    q->min_thread  = th;
    q->idle_thread = 0;
    q->status      = ZC_THREADSEQ_RUN;

    q->share = (zcThreadInfo*)zc_malloc(sizeof(zcThreadInfo) * q->max_thread);
    if (NULL == q->share) {
        zc_free(q);
        return NULL;
    }   

    int ret = pthread_mutex_init(&q->lock, NULL);
    if (ret != 0) {
        ZCERROR("pthread_mutex_init error");
        zc_free(q->share);
        zc_free(q);
        return NULL;
    }

    return q;
}
Exemplo n.º 2
0
zcSocket*
zc_socket_new (int family, int type, int proto, int timeout)
{
    zcSocket *s;
    s = (zcSocket *) zc_malloc (sizeof(zcSocket));
    if (s != NULL) {
        memset(s, 0, sizeof(zcSocket));
        s->fd = socket(family, type, proto);
        //ZCINFO("fd:%d", s->fd);
        if (s->fd == INVALID_SOCKET) {
            zc_free(s);
            //WSACleanup();
            ZCINFO("socket create error! %s\n", strerror(errno));
            return  NULL;
        }
        
        s->family = family;
        s->type = type;
        s->proto = proto;
        s->timeout = timeout; 

        /*if (timeout >= 0) {
            ZCINFO("socket noblock!\n");
            zc_socket_setblock(s, 0);
        }*/

    }
    return s;
}
Exemplo n.º 3
0
zcHashSetNode*
zc_hashset_node_new(char *key, int keylen)
{
    zcHashSetNode *node = (zcHashSetNode*)zc_malloc(sizeof(zcHashSetNode));
    memset(node, 0, sizeof(zcHashSetNode));
    node->key = zc_strdup(key, 0);
    return node;
}
Exemplo n.º 4
0
void* my_producer(void *x)
{
    char *a = zc_malloc(128);
    snprintf(a, 128, "producer %d", i);
    ZCINFO("%s", a);
    sleep(1); 
    i++;
    return a;
}
Exemplo n.º 5
0
int
zc_hashset_new2(zcHashSet **hash, int size)
{
    zcHashSet   *h;

    h = (zcHashSet*)zc_malloc(sizeof(zcHashSet));
    memset(h, 0, sizeof(zcHashSet));
    h->size  = size;
    h->cmp   = zc_cmp_str;
    h->hash  = zc_hash_bkdr;
    h->keydel  = zc_free_func;
    h->nodedel = zc_hashset_node_delete;

    h->bunks = (void**)zc_malloc(sizeof(void*) * size);
    memset(h->bunks, 0, sizeof(zcHashSetNode*) * size);

    *hash = h;
    return ZC_OK;
}
Exemplo n.º 6
0
zcSocket*
zc_socket_create ()
{
    zcSocket   *s;
    s = (zcSocket *) zc_malloc (sizeof(zcSocket));
    if (NULL == s) 
        return NULL;
    memset(s, 0,  sizeof(zcSocket));
    return s;
}
Exemplo n.º 7
0
zcSockAddr*
zc_sockaddr_new()
{
    zcSockAddr  *sa;
    sa = (zcSockAddr*)zc_malloc(sizeof(zcSockAddr));
    if (NULL == sa) {
        return NULL;
    }
    memset(sa, 0, sizeof(zcSockAddr));
    return sa;
}
Exemplo n.º 8
0
zcDict*
zc_dict_new_full(int minsize, char reduce, zcFuncDel keydel, zcFuncDel valdel)
{
    if (minsize <= 0)
        minsize = 8;

    zcDict *ht = (zcDict*)zc_malloc(sizeof(zcDict));
    memset(ht, 0, sizeof(zcDict));

    int newsize;
    for(newsize=8; newsize<= minsize && newsize>0; newsize<<=1);

    ht->__type = ZC_DICT;
    ht->size   = newsize;
    ht->reduce = reduce;
    ht->table  = (zcDictNode*)zc_malloc(sizeof(zcDictNode)*newsize);//new zcDictNode<T>[size];
    memset(ht->table, 0, sizeof(zcDictNode)*newsize);
    ht->filled = ht->len = 0;
    ht->hash   = zc_hash_bkdr;
    ht->keydel = zc_free_func;
    ht->valdel = valdel;

    return ht;
}
Exemplo n.º 9
0
zcDBRec*
zc_mysqlrec_new(void *stmt)
{
    zcMySQLRec *mrec = (zcMySQLRec*)zc_malloc(sizeof(zcMySQLRec));
    memset(mrec, 0, sizeof(zcMySQLRec));

    mrec->res = stmt;

    mrec->_fields = 0;
    mrec->_rows = 0;
    mrec->_pos  = 0;
    mrec->row   = NULL;
    mrec->_get_row_next = 0;
    mrec->fieldmap = zc_dict_new(8);

    if (stmt) {
        mrec->_fields = mysql_num_fields(mrec->res);
        mrec->_rows   = mysql_num_rows(mrec->res);
        mysql_data_seek(mrec->res, mrec->_pos);

        MYSQL_FIELD *fields;
        fields = mysql_fetch_fields(mrec->res);
        long i;
        for (i = 0; i < mrec->_fields; i++) {
            //mrec->fieldmap[fields[i].name] = i;
            zc_dict_add(mrec->fieldmap, fields[i].name, 0, (void*)i);
        }
    }

    mrec->del             = zc_mysqlrec_delete;
    mrec->reset           = zc_mysqlrec_reset;
    mrec->row_next        = zc_mysqlrec_row_next;
    mrec->field_int_pos   = zc_mysqlrec_field_int_pos;
    mrec->field_int       = zc_mysqlrec_field_int;
    mrec->field_int64_pos = zc_mysqlrec_field_int64_pos;
    mrec->field_int64     = zc_mysqlrec_field_int64;
    mrec->field_float_pos = zc_mysqlrec_field_float_pos;
    mrec->field_float     = zc_mysqlrec_field_float;
    mrec->field_str_pos   = zc_mysqlrec_field_str_pos;
    mrec->field_str       = zc_mysqlrec_field_str;
    mrec->field_blob_pos  = zc_mysqlrec_field_blob_pos;
    mrec->field_blob      = zc_mysqlrec_field_blob;
    mrec->fetchone        = zc_mysqlrec_fetchone;

    return (zcDBRec*)mrec;
}
Exemplo n.º 10
0
long ffparse(char *string)
{
    //return int(atof(string)*10000);
    
    //this function below isn't working too well yet
    //clean_numeric_string(string);
    double negcheck = atof(string);
    
    //if no decimal point, ascii to int conversion
    char *ptr=strchr(string, '.');
    
    if(!ptr)
    {
        return atoi(string)*10000;
    }
    
    long ret=0;
    char *tempstring1;
    tempstring1=(char *)zc_malloc(strlen(string)+5);
    sprintf(tempstring1, string);
    
    for(int i=0; i<4; ++i)
    {
        tempstring1[strlen(string)+i]='0';
    }
    
    ptr=strchr(tempstring1, '.');
    *ptr=0;
    ret=atoi(tempstring1)*10000;
    
    ++ptr;
    char *ptr2=ptr;
    ptr2+=4;
    *ptr2=0;
    
    if(negcheck<0)
        ret-=atoi(ptr);
    else ret+=atoi(ptr);
    
    zc_free(tempstring1);
    return ret;
}
Exemplo n.º 11
0
int
zc_dict_resize(zcDict *ht, uint32_t minsize)
{
    // minsize default = 0
    uint32_t newsize;
    if (minsize <= 0) {
        //minsize = (len > 50000 ? 2 : 4)*len;
        minsize = (ht->len > 50000 ? 2 : 4)*ht->size;
    }
    for(newsize=8; newsize<= minsize && newsize>0; newsize<<=1);

    //ZCINFO("newsize:%d\n", newsize);
    if (newsize == ht->size)
        return ZC_OK;
    if (!ht->reduce && newsize <= ht->size)
        return ZC_OK;

    //ZCINFO("newsize:%d", newsize);
    zcDictNode *newtable = (zcDictNode*)zc_malloc(sizeof(zcDictNode)*newsize);
    memset(newtable, 0, sizeof(zcDictNode)*newsize);

    zcDictNode *node;
    int n = ht->len;
    uint32_t newmask = newsize-1;
    uint32_t p;
    uint32_t newlen = 0;
    for (node=ht->table; n>0; node++) {
        if (node->key != NULL && node->key != ZC_KEY_DUMMY) {
            uint32_t i = node->hash & newmask;
            zcDictNode *n2 = &newtable[i];
            for (p = node->hash; n2->key != NULL; p >>= 5) {
                i = (i << 2) + i + p + 1;
                n2 = &newtable[i & newmask];
            }
            newlen++;
            memcpy(n2, node, sizeof(zcDictNode));
            n--;
            node->key = NULL;
        }
    }
Exemplo n.º 12
0
zcDB*
zc_mysqldb_new(zcMySQLConf *cf)
{
    zcMySQLDB *mdb = (zcMySQLDB*)zc_malloc(sizeof(zcMySQLDB));
    memset(mdb, 0, sizeof(zcMySQLDB));

    memcpy(&mdb->conf, cf, sizeof(zcMySQLConf));
    mdb->_mysql = mysql_init(NULL);
    if (NULL == mdb->_mysql) {
        ZCERROR("mysql init error!\n");
        zc_free(mdb);
        return NULL;
    }

    if (cf->conn_timeout > 0) {
        mysql_options(mdb->_mysql, MYSQL_OPT_CONNECT_TIMEOUT, (const void*)&cf->conn_timeout);
    }
    if (cf->read_timeout > 0) {
        mysql_options(mdb->_mysql, MYSQL_OPT_READ_TIMEOUT, (const void*)&cf->read_timeout);
    }
    if (cf->write_timeout > 0) {
        mysql_options(mdb->_mysql, MYSQL_OPT_WRITE_TIMEOUT, (const void*)&cf->write_timeout);
    }

    mdb->_isopen = 0;

    mdb->del   = zc_mysqldb_delete;
    mdb->exec  = zc_mysqldb_exec;
    mdb->execf = zc_mysqldb_execf;
    mdb->query = zc_mysqldb_query;
    mdb->start = zc_mysqldb_start;
    mdb->commit   = zc_mysqldb_commit;
    mdb->rollback = zc_mysqldb_rollback;
    mdb->last_insert_id = zc_mysqldb_last_insert_id;

    zc_mysqldb_open((zcDB*)mdb);

    return (zcDB*)mdb;
}
Exemplo n.º 13
0
int
zc_hashset_resize(zcHashSet *h, int newsize)
{
    void **newbunks = (void**)zc_malloc(sizeof(void*) * newsize);
    zcHashSetNode  *node, *nextnode;
    int i;
    for (i = 0; i < h->size; i++) {
        node = (zcHashSetNode*)h->bunks[i];
        while (node) {
            nextnode = (zcHashSetNode*)node->next;
            uint32_t hv = h->hash(node->key, strlen(node->key)) % newsize;
            zcHashSetNode  *root = (zcHashSetNode*)newbunks[hv];
            node->next = root;
            newbunks[hv] = node;
            node = nextnode;
        }
    }
    zc_free(h->bunks);
    h->bunks = newbunks;
    h->size  = newsize;

    return ZC_OK;
}
Exemplo n.º 14
0
/* fs_flist_proc:
  *  Dialog procedure for the file selector list.
  */
static int fs_flist_proc(int msg, DIALOG *d, int c)
{
    static int recurse_flag = 0;
    char *s = (char *) file_selector[FS_EDIT].dp;
    char tmp[32];
    /* of s (in bytes) */
    int size = (file_selector[FS_EDIT].d1 + 1) * uwidth_max(U_CURRENT);
    int sel = d->d1;
    int i, ret;
    int ch, count;
    
    if(msg == MSG_START)
    {
        if(!flist)
        {
            flist = (FLIST *) zc_malloc(sizeof(FLIST));
            
            if(!flist)
            {
                *allegro_errno = ENOMEM;
                return D_CLOSE;
            }
        }
        else
        {
            for(i=0; i<flist->size; i++)
                if(flist->name[i])
                    zc_free(flist->name[i]);
        }
        
        flist->size = 0;
        
        replace_filename(flist->dir, s, uconvert_ascii("*.*", tmp), sizeof(flist->dir));
        
        /* The semantics of the attributes passed to file_select_ex() is
          * different from that of for_each_file_ex() in one case: when
          * the 'd' attribute is not mentioned in the set of characters,
          * the other attributes are not taken into account for directories,
          * i.e the directories are all included. So we can't filter with
          * for_each_file_ex() in that case.
          */
        if(attrb_state[ATTRB_DIREC] == ATTRB_ABSENT)
            /* accept all dirs */
            for_each_file_ex(flist->dir, 0 , FA_LABEL, fs_flist_putter, (void *)1UL /* check */);
        else
            /* don't check */
            for_each_file_ex(flist->dir, build_attrb_flag(ATTRB_SET), build_attrb_flag(ATTRB_UNSET) | FA_LABEL, fs_flist_putter, (void *)0UL);
            
        usetc(get_filename(flist->dir), 0);
        d->d1 = d->d2 = 0;
        sel = 0;
    }
    
    if(msg == MSG_END)
    {
        if(flist)
        {
            for(i=0; i<flist->size; i++)
                if(flist->name[i])
                    zc_free(flist->name[i]);
                    
            zc_free(flist);
            flist = NULL;
        }
    }
    
    recurse_flag++;
    ret = jwin_abclist_proc(msg,d,c);                         /* call the parent procedure */
    
    recurse_flag--;
    
    if(((sel != d->d1) || (ret == D_CLOSE)) && (recurse_flag == 0))
    {
        replace_filename(s, flist->dir, flist->name[d->d1], size);
        
        /* check if we want to `cd ..' */
        if((!ustrncmp(flist->name[d->d1], uconvert_ascii("..", tmp), 2)) && (ret == D_CLOSE))
        {
            /* let's remember the previous directory */
            usetc(updir, 0);
            i = ustrlen(flist->dir);
            count = 0;
            
            while(i>0)
            {
                ch = ugetat(flist->dir, i);
                
                if((ch == '/') || (ch == OTHER_PATH_SEPARATOR))
                {
                    if(++count == 2)
                        break;
                }
                
                uinsert(updir, 0, ch);
                i--;
            }
            
            /* ok, we have the dirname in updir */
        }
        else
        {
            usetc(updir, 0);
        }
        
        object_message(file_selector+FS_EDIT, MSG_START, 0);
        object_message(file_selector+FS_EDIT, MSG_DRAW, 0);
        
        if(ret == D_CLOSE)
            return object_message(file_selector+FS_EDIT, MSG_KEY, 0);
    }
    
    return ret;
}
Exemplo n.º 15
0
/* fs_flist_putter:
  *  Callback routine for for_each_file() to fill the file selector listbox.
  */
static int fs_flist_putter(AL_CONST char *str, int attrib, void *check_attrib)
{
    char *s, *ext, *name;
    int c, c2;
    
    s = get_filename(str);
    fix_filename_case(s);
    
    if(!(attrib & FA_DIREC))
    {
        /* Check if file extension matches. */
        if(fext_p)
        {
            ext = get_extension(s);
            
            for(c=0; c<fext_size; c++)
            {
                if(ustricmp(ext, fext_p[c]) == 0)
                    goto Next;
            }
            
            return 0;
        }
        
Next:

        /* Check if file attributes match. */
        if(check_attrib)
        {
            for(c=0; c<ATTRB_MAX; c++)
            {
                if((attrb_state[c] == ATTRB_SET) && (!(attrib & attrb_flag[c])))
                    return 0;
                    
                if((attrb_state[c] == ATTRB_UNSET) && (attrib & attrb_flag[c]))
                    return 0;
            }
        }
    }
    
    if((flist->size < FLIST_SIZE) && ((ugetc(s) != '.') || (ugetat(s, 1))))
    {
        int size = ustrsizez(s) + ((attrib & FA_DIREC) ? ucwidth(OTHER_PATH_SEPARATOR) : 0);
        name = (char *) zc_malloc(size);
        
        if(!name)
            return -1;
            
        ustrzcpy(name, size, s);
        
        if(attrib & FA_DIREC)
            put_backslash(name);
            
        /* Sort alphabetically with directories first. */
        for(c=0; c<flist->size; c++)
        {
            if(ugetat(flist->name[c], -1) == OTHER_PATH_SEPARATOR)
            {
                if(attrib & FA_DIREC)
                    if(ustrfilecmp(name, flist->name[c]) < 0)
                        break;
            }
            else
            {
                if(attrib & FA_DIREC)
                    break;
                    
                if(ustrfilecmp(name, flist->name[c]) < 0)
                    break;
            }
        }
        
        /* Shift in preparation for inserting the new entry. */
        for(c2=flist->size; c2>c; c2--)
            flist->name[c2] = flist->name[c2-1];
            
        /* Insert the new entry. */
        flist->name[c] = name;
        flist->size++;
    }
    
    return 0;
}
Exemplo n.º 16
0
int ListQTs(bool edit)
{
    qtlist_dlg[0].dp2=lfont;
    int index=0;
    quest_template *BackupQTs = (quest_template*)zc_malloc(sizeof(quest_template)*MAXQTS);
    
    memcpy(BackupQTs,QuestTemplates,sizeof(quest_template)*qt_count);
    
    int backup_qt_count=qt_count;
    
    while(index>-1)
    {
        bool hasroom=false;
        
        if(qt_count<MAXQTS)
        {
            hasroom=true;
            
            if(edit)
            {
                strcpy(QuestTemplates[qt_count++].name,"<new template>");
            }
        }
        
        if(is_large)
            large_dialog(qtlist_dlg);
            
        qtlist_dlg[2].x=int(qtlist_dlg[0].x+(edit?5:15)*(is_large?1.5:1));
        qtlist_dlg[3].proc=edit?jwin_button_proc:d_dummy_proc;
        qtlist_dlg[4].proc=edit?jwin_button_proc:d_dummy_proc;
        qtlist_dlg[5].proc=edit?jwin_button_proc:d_dummy_proc;
        qtlist_dlg[6].x=int(qtlist_dlg[0].x+(edit?110:80)*(is_large?1.5:1));
        qtlist_dlg[7].x=int(qtlist_dlg[0].x+(edit?190:160)*(is_large?1.5:1));
        qtlist_dlg[8].proc=edit?d_keyboard_proc:d_dummy_proc;
        
        int ret=zc_popup_dialog(qtlist_dlg,2);
        
        index=qtlist_dlg[2].d1;
        
        int doedit=false;
        
        switch(ret)
        {
        case 2:
            if(edit)
            {
                doedit=true;
            }
            else
            {
                if(index>0&&!valid_zqt(QuestTemplates[index].path))
                {
                    jwin_alert("ZQuest","Invalid Quest Template",NULL,NULL,"O&K",NULL,'k',0,lfont);
                }
                else
                {
                    strcpy(header.templatepath, QuestTemplates[index].path);
                    index=-1;
                }
            }
            
            break;
            
        case 5:
            doedit=true;
            break;
            
        case 3:
            if(index>1&&index<qt_count-1)
            {
                zc_swap(QuestTemplates[index],QuestTemplates[index-1]);
                --qtlist_dlg[2].d1;
                index=qtlist_dlg[2].d1;
            }
            
            break;
            
        case 4:
            if(index>0&&index<qt_count-2)
            {
                zc_swap(QuestTemplates[index],QuestTemplates[index+1]);
                ++qtlist_dlg[2].d1;
                index=qtlist_dlg[2].d1;
            }
            
            break;
            
        case 6:
            if(index>0&&!valid_zqt(QuestTemplates[index].path))
            {
                jwin_alert("ZQuest","Invalid Quest Template",NULL,NULL,"O&K",NULL,'k',0,lfont);
            }
            else
            {
                if(!edit)
                {
                    strcpy(header.templatepath, QuestTemplates[index].path);
                }
                
                index=-1;
            }
            
            break;
            
        case 0:
        case 7:
            if(edit)
            {
                qt_count=backup_qt_count+1;
                memcpy(QuestTemplates,BackupQTs,sizeof(quest_template)*qt_count);
            }
            
            index=-2;
            break;
            
        case 8:
            char buf[30];
            strncpy(buf,QuestTemplates[index].name,30);
            
            if(jwin_alert("Confirm Deletion", "Delete this quest template?",buf,"(The file will still exist.)","Yes","No",'y',27,lfont)==1)
            {
                for(int i=index; i<MAXQTS-1; i++)
                    QuestTemplates[i]=QuestTemplates[i+1];
                    
                reset_qt(MAXQTS-1);
                --qt_count;
            }
            
            break;
        }
        
        if(edit&&hasroom)
        {
            //      strcpy(QuestTemplates[--qt_count].name,"              ");
            reset_qt(--qt_count);
            sprintf(QuestTemplates[qt_count].name,"Untitled");
        }
        
        if(index>0 && doedit)
        {
            edit_qt(index);
        }
    }
    
    zc_free(BackupQTs);
    return index;
}