Пример #1
0
cell * create_cell1(MN * mn, cell * ci, char bdiff, char dflag) {
	cell * c = new_cell();
	accept_cell(c, (c_comp *) comp_to_kit(mn));
	c->basflg = c->bas1 = c->bas2 = c->bas3 = c->bas4 = 0;
	c->difflg = dflag;
	c->bdiff = bdiff;
	c->reasno = 0;
	c->cg_flag = 0;
	c->dens = 255;
	if (if_dust(c) & 0x0c)
		c->flg = c_f_dust;
	else
		c->flg = c_f_bad;
	set_bad_cell(c);
	c->pos_inc = erect_no;
	c->accent_leader = 0;
	c->bottom_accent = 0;
	c->n_baton = NO_BATONS;
	c->flg_new = 0;
	c->save_stick_inc = c->stick_inc = NO_INCLINE;
	c->cg_flag_fine = 0;
	c->dupstart = 0;
	c->dupend = 0;
	c->dlang_dup = 0; // Nick 17.02.2001 - was error!
	insert_cell1(c, ci);
	return c;
}
Пример #2
0
world new_world(long seed) {
	world w;
	
	w = malloc(sizeof(worldStruct));

	if( w != NULL ) {
		int i;
		float x, y, s;

        srand(seed);
	
		w->cells = NULL;
		w->cells_counter = 10;
	
		w->cells = malloc(sizeof(cell) * w->cells_counter);
	
		for(i = 0 ; i < w->cells_counter ; i++) {
			x = ((rand() % 1000)+1) / 5.0;
			y = ((rand() % 1000)+1) / 5.0;
			s = ((rand() % 500)+100) / 500.0;
			w->cells[i] = new_cell(s, x, y, rand()%256, rand()%256, rand()%256, (rand()%156)+100);
		}
		//w->cells[0] = new_cell(10, 0, 0, 10, 200, 10, 255);
		//w->cells[1] = new_cell(10, 10, 0, 200, 10, 10, 255);
	}
		
	return w;
}
Пример #3
0
Cell*
cell_sub(Cell *c, int start, int end) {
    Cell *d;
    int i, l;
    const char *p;

    d = new_cell("");
    if (NULL == d) return d;

    l = c->length;
    p = c->data;

    if (end < 0) return d;
    if (start < 0) start = 0;
    if (end == 0) end = l;
    if (end <= start) return d;
    if (start >= l) return d;

    if (end >= l) {
	return cell_add_str(d, &p[start]);
    }

    for (i=start; i<end; i++) {
	cell_add_char(d, p[i]);
    }

    return d;
}
Пример #4
0
cell * rest_cell(void *k, cell *ci)
{
 cell * c = new_cell();
 rest_kit((puchar) c, k);
 insert_cell(c,ci);
 return c;
}
Пример #5
0
cell * create_cell_work(MN * mn, cell * ci, char bdiff, char dflag)
{
 cell * c = new_cell();
 accept_cell (c,(c_comp *)comp_to_kit(mn));
 c->basflg=c->bas1=c->bas2=c->bas3=c->bas4=0;
 c->difflg=dflag;
 c->bdiff=bdiff; c->reasno=0; c->cg_flag=0; c->dens=255;
 if (if_dust(c) & 0x0c)   c->flg=c_f_dust;
 else			c->flg=c_f_bad;
 set_bad_cell(c);
 c->col        += ci->col;
 c->r_col      += ci->r_col;
 c->row        += ci->row;
 c->r_row      += ci->r_row;
 c->env->upper += ci->r_row;
 c->env->left  += ci->r_col;
 if( ci->pos_inc&erect_rot )
   {
   c->pos_inc   = erect_rot;
   c->stick_inc = ci->stick_inc;
   }
 else
   {
   c->stick_inc = NO_INCLINE;
   c->pos_inc   = erect_no;
   }
 insert_cell(c,ci);
 return c;
}
Пример #6
0
void init_solution(Puzzle *puz, Solution *sol, int set)
{
    line_t i, j, n;
    color_t col;
    Cell *c;
    
    puz->nsolved= 0;
    
    /* Copy number of directions from puzzle */
    sol->nset= puz->nset;
    
    n= 0;
    if (puz->type == PT_GRID)
    {
        /* Build the array of rows */
        sol->line[D_ROW]= (Cell ***)malloc(sizeof(Cell **) * sol->n[D_ROW]);
        puz->ncells= sol->n[D_ROW] * sol->n[D_COL];
        
        for (i= 0; i < sol->n[D_ROW]; i++)
        {
            sol->line[D_ROW][i]=
	    	(Cell **)malloc(sizeof(Cell *) * (sol->n[D_COL] + 1));
            
            for (j= 0; j < sol->n[D_COL]; j++)
            {
                sol->line[D_ROW][i][j]= c= new_cell(puz->ncolor);
                c->id= n++;
                if (set)
                    for (col= 0; col < puz->ncolor; col++)
                        bit_set(c->bit, col);
                c->line[D_ROW]= i; c->index[D_ROW]= j;
                c->line[D_COL]= j; c->index[D_COL]= i;
            }
            sol->line[D_ROW][i][sol->n[D_COL]]= NULL;
        }
        
        /* Build the redundant array of cols, pointing to the same cells */
        sol->line[D_COL]= (Cell ***)malloc(sizeof(Cell **) * sol->n[D_COL]);
        
        for (i= 0; i < sol->n[D_COL]; i++)
        {
            sol->line[D_COL][i]=
	    	(Cell **)malloc(sizeof(Cell *) * (sol->n[D_ROW] + 1));
            
            for (j= 0; j < sol->n[D_ROW]; j++)
                sol->line[D_COL][i][j]= sol->line[D_ROW][j][i];
            sol->line[D_COL][i][sol->n[D_ROW]]= NULL;
        }
    }
    else
    {
    	/* This will be much like the above, except we build two redundant
         * arrays, and the lines are not all the same length */
        
        fail("Not yet implemented for triddlers!\n");
    }
    
    sol->spiral= NULL;
}
Пример #7
0
/**********************************************************************
 *  p u s h
 *
 *  Create a list element.  Push the second parameter (the node) onto
 *  the first parameter (the list). Return the new list to the caller.
 **********************************************************************/
LIST push(LIST list, void *element) {
  LIST t;

  t = new_cell ();
  t->node = (LIST) element;
  set_rest(t, list);
  return (t);
}
Пример #8
0
Файл: io.c Проект: mschaef/vcsh
lref_t portcons(struct port_class_t * cls,
                lref_t port_name,
                enum port_mode_t mode,
                lref_t user_object,
                void *user_data)
{
     return initialize_port(new_cell(TC_PORT),
                            cls, port_name, mode, user_object, user_data);
}
Пример #9
0
	void SquareGrid::InitGrid() 
	{
		for(unsigned int x = 0; x < size_x_; ++x) {
			std::vector<SquareCell> new_column;
			for(unsigned int y = 0; y < size_y_; ++y) {
				SquareCell new_cell(x, y);
				new_column.push_back(new_cell);
			}
			square_grid_.push_back(new_column);
		}
	}
Пример #10
0
int pdt_add_to_hash(pdt_hash_t *hash, str *sp, str *sd)
{
	int hash_entry=0;
	unsigned int dhash;
	pd_t *it, *tmp;
	pd_t *cell;
	
	if(hash==NULL || sp==NULL || sd==NULL)
	{
		LOG(L_ERR, "PDT:pdt_add_to_hash: bad parameters\n");
		return -1;
	}
    
	dhash = pdt_compute_hash(sd->s);
	hash_entry = get_hash_entry(dhash, hash->hash_size);

	lock_get(&hash->dhash[hash_entry].lock);
	
	it = hash->dhash[hash_entry].e;

	tmp = NULL;
	while(it!=NULL && it->dhash < dhash)
	{
		tmp = it;
		it = it->n;
	}
    
	/* we need a new entry for this cell */
	cell = new_cell(sp, sd);	
	if(cell == NULL)
	{
		lock_release(&hash->dhash[hash_entry].lock);
		return -1;
	}
	

	if(tmp)
		tmp->n=cell;
	else
		hash->dhash[hash_entry].e = cell;
	
	cell->p=tmp;
	cell->n=it;
	
	if(it)
		it->p=cell;

	lock_release(&hash->dhash[hash_entry].lock);

	return 0;
}
Пример #11
0
cell *generate_cell_list()
{
  /*
   * input: Input dictionary file name
   * output: Each line from input except empty lines in an single-linked list
   *         with each line being a single element in the list.
   * returns: First element in list with counter of how many object is
   *          occupying the list.
   */
  char *input_file = "dictionary.example";
  FILE *dict_file = fopen(input_file, "r");
  int datacount = 0;
  char line[167];
  cell *c = NULL;
  cell *next = NULL;
  cell *first_cell = NULL;

  for (;fgets(line, 167, dict_file) != NULL; c = next)
  {
    if (*line != '\n') /* skip empty lines in the dictionary */
    {
      next = new_cell(line);
      datacount++;
      if (first_cell == NULL)
      {
        first_cell = next;
      }

      if (c != NULL)
      {
        c->next = next;
      }
    }
  }

  fclose(dict_file);

  /* Trying to generate a bingo sheet from no values is kinda.. impossible? */
  if (datacount == 0)
  {
    fprintf(stderr, "It doesn't seem like %s contains anything.\n", 
      input_file);
    exit(1);
  }

  /* Set count-variable in the first element to the total number of elements */
  first_cell->count = datacount;

  return first_cell;
}
Пример #12
0
// -----------------------------------------------------
// Insert key-value pair in symbol
// Ownership of the value object transfers to the symbab
void insert( symtab table, const char* key, void* value )
{
  int bn = hash( key, NBuckets );
  cell c = find_cell( table->bucket[ bn ], key );
  if ( c == NULL ) {		/* key not present */
    cell c = new_cell( key, value );
    c->next = table->bucket[ bn ];
    table->bucket[ bn ] = c;
  }
  else {			/* key found */
    free( c->value );
    c->value = value;
  }
}
Пример #13
0
static lref_t faslreadercons(lref_t port)
{
    lref_t frdr = new_cell(TC_FASL_READER);

    SET_FASL_READER_PORT(frdr, port);

    struct fasl_stream_t *stream = gc_malloc(sizeof(*stream));

    memset(stream, 0, sizeof(*stream));

    SET_FASL_READER_STREAM(frdr, stream);

    return frdr;
}
Пример #14
0
lref_t lcopy_structure(lref_t st)
{
     if (!STRUCTUREP(st))
          vmerror_wrong_type_n(1, st);

     lref_t new_st = new_cell(TC_STRUCTURE);

     size_t len = STRUCTURE_DIM(st);;

     SET_STRUCTURE_DIM(new_st, len);
     SET_STRUCTURE_LAYOUT(new_st, STRUCTURE_LAYOUT(st));
     SET_STRUCTURE_DATA(new_st, (lref_t *) gc_malloc(len * sizeof(lref_t)));

     for (size_t ii = 0; ii < len; ii++)
          SET_STRUCTURE_ELEM(new_st, ii, STRUCTURE_ELEM(st, ii));

     return new_st;
}
Пример #15
0
int add_to_hash(hash_t *hash, str *sp, str *sd)
{
	int hash_entry=0;
	unsigned int dhash;
	pd_t *it, *prev, *cell;
	
	if(hash==NULL || sp==NULL || sp->s==NULL
			|| sd==NULL || sd->s==NULL)
	{
		LM_ERR("bad parameters\n");
		return -1;
	}

	dhash = pdt_compute_hash(sd);
	
	hash_entry = get_hash_entry(dhash, hash->hash_size);

	it = hash->dhash[hash_entry];

	prev = NULL;
	while(it!=NULL && it->dhash < dhash)
	{
		prev = it;
		it = it->n;
	}

	/* we need a new entry for this cell */
	cell = new_cell(sp, sd);	
	if(cell == NULL)
		return -1;

	if(prev)
		prev->n=cell;
	else
		hash->dhash[hash_entry]= cell;
	
	cell->p=prev;
	cell->n=it;
	
	if(it)
		it->p=cell;

	return 0;
}
Пример #16
0
void
Map::CreatureRelocation(Creature *creature, uint16 x, uint16 y)
{
	Cell old_cell = creature->GetCurrentCell();

	CellPair new_val = LeGACY::ComputeCellPair(x, y);
	Cell new_cell(new_val);

	// delay creature move for grid/cell to grid/cell moves
	if( old_cell.DiffCell(new_cell) || old_cell.DiffGrid(new_cell) )
	{
		AddCreatureToMoveList(creature, x, y);
	}
	else
	{
		creature->Relocate(x, y);
		CreatureRelocationNotify(creature,new_cell,new_val);
	}
}
Пример #17
0
Cell*
new_cell(const char *src) {
    Cell *c;
    int alen, len;

    if (NULL == src) return new_cell("");
    
    c = (Cell*)GC_MALLOC(sizeof(Cell));
    ALLOC_SAFE(c);

    len = strlen(src)+1;
    alen = cell_get_alloc_size(CELL_INIT_ALLOC, len);
    c->length = len-1;
    c->allocsize = alen;
    c->data = (char*) GC_MALLOC_ATOMIC(alen);
    ALLOC_SAFE(c->data);
    strncpy(c->data, src, len);

    return c;
}
Пример #18
0
lref_t lstructurecons(lref_t slots, lref_t layout)
{
     if (!VECTORP(slots))
          vmerror_wrong_type_n(1, slots);

     size_t len = slots->as.vector.dim;

     validate_structure_layout(len, layout);

     lref_t st = new_cell(TC_STRUCTURE);

     SET_STRUCTURE_DIM(st, len);
     SET_STRUCTURE_LAYOUT(st, layout);
     SET_STRUCTURE_DATA(st, (lref_t *) gc_malloc(len * sizeof(lref_t)));

     for (size_t ii = 0; ii < len; ii++)
          SET_STRUCTURE_ELEM(st, ii, slots->as.vector.data[ii]);

     return st;
}
Пример #19
0
int main(int argc, char **argv, char **envp) {
    char buff[BUFFSIZE];
    Toy_Type *any, *result;
    Toy_Type *script;
    Toy_Type *err;
    Bulk *b;
    Cell *c;
    Toy_Interp *interp;
    jmp_buf jmp_env;

error_reset:
    interp = new_interp("main", STACKSIZE, NULL, argc, argv, envp);
    b = new_bulk();

    if (0 == setjmp(jmp_env)) {
        cstack_set_jmpbuff(0, &jmp_env);
    } else {
        fprintf(stderr, "Catch the main co-routine stack overflow, to be restarted.\n");
        goto error_reset;
    }

    if (argv[1] && (strcmp(argv[1], "-") != 0)) {
        if (0 == bulk_load_file(b, argv[1])) {
            fprintf(stderr, "file not open: %s\n", argv[1]);
            exit(1);
        }
        any = toy_parse_start(b);
        if (NULL == any) {
            fprintf(stderr, "no memory\n");
            exit(1);
        }
        switch (GET_TAG(any)) {
        case EXCEPTION:
            err = any;
            fprintf(stderr, "parse error: %s\n",
                    to_string(list_get_item(err->u.exception.msg_list)));
            exit(0);

        case SCRIPT:
            script = any;
            result = toy_eval_script(interp, script);

            if (GET_TAG(result) != EXCEPTION) {
                fprintf(stdout, "result[%s]=> ", toy_get_type_str(result));
                fprintf(stdout, "%s\n", to_print(result));
            } else {
                fprintf(stdout, "EXCEPTION: %s\n", to_string(result));
            }
            exit(0);

        default:
            fprintf(stderr, "parse error: type=%s\n", toy_get_type_str(any));
            exit(1);
        }
    }

    while (! feof(stdin)) {
        fputs("> ", stderr);
        if (NULL == fgets(buff, BUFFSIZE, stdin)) break;
        buff[BUFFSIZE-1] = 0;

        if (buff[0] == '!') {
            buff[strlen(buff)-1] = 0;
            if (0 == bulk_load_file(b, &buff[1])) {
                fprintf(stderr, "file not open: %s\n", &buff[1]);
                continue;
            }
        } else {
            bulk_set_string(b, buff);
        }

        any = toy_parse_start(b);
        if (NULL == any) {
            fprintf(stderr, "no memory\n");

        } else {
            switch (GET_TAG(any)) {
            case EXCEPTION:
                err = any;
                fprintf(stderr, "parse error: %s\n",
                        to_string(list_get_item(err->u.exception.msg_list)));
                if (cell_eq_str(err->u.exception.code, TE_PARSEBADCHAR) == 0) break;
                if (buff[0] == '!') break;

                c = new_cell(buff);
                while (1) {
                    fputs("=> ", stderr);
                    if (NULL == fgets(buff, BUFFSIZE, stdin)) goto exit_loop;

                    buff[BUFFSIZE-1] = 0;

                    cell_add_str(c, buff);
                    bulk_set_string(b, cell_get_addr(c));
                    any = toy_parse_start(b);

                    if (GET_TAG(any) == EXCEPTION) {
                        err = any;
                        if (cell_eq_str(err->u.exception.code, TE_PARSEBADCHAR) == 0) {
                            fprintf(stderr, "parse error: %s\n",
                                    to_string(list_get_item(err->u.exception.msg_list)));
                            goto next_loop;
                        }
                    } else {
                        break;
                    }
                }

            case SCRIPT:
                script = any;
                result = toy_eval_script(interp, script);

                if (GET_TAG(result) != EXCEPTION) {
                    char *p;
                    fprintf(stdout, "result[%s]=> ", toy_get_type_str(result));
                    p = to_print(result);
                    if (strlen(p) > 512) {
                        fprintf(stdout, "%-.512s ...\n", p);
                    } else {
                        fprintf(stdout, "%s\n", p);
                    }
                } else {
                    fprintf(stdout, "EXCEPTION: %s\n", to_string(result));
                }
                break;

            default:
                fprintf(stderr, "parse error: type=%s\n", toy_get_type_str(any));

            }
        }
next_loop:
        /* dummy */
        0;
    }
exit_loop:

    return 0;
}
Пример #20
0
int bf_run(char *buff,int size)
{
    char *code=buff;
    int idx=0,tmp;
    int nbrace=0;   /*counts the no. of loops encountered inside an unexcuted loop*/

    first=current=new_cell();


    while(idx<size){

        if(nbrace){
            if(code[idx]=='[')++nbrace;
            else if(code[idx]==']')--nbrace;
        }

        else{
            switch(code[idx]){
                case '+':
                    current->value++;
                    break;
                case '-':
                    current->value--;
                    break;
                case '>':
                    if(current->next==NULL){
                        current->next=new_cell();
                        (current->next)->prev=current;
                    }
                    current=current->next;
                    break;
                case '<':
                    if(current->prev==NULL){
                        first=current->prev=new_cell();
                        (current->prev)->next=current;
                    }
                    current=current->prev;
                    break;
                case '.':
                    putchar(current->value);
                    break;
                case ',':
                    current->value=getchar();
                    break;
                case '[':
                    //push(idx+1);
                    if(current->value==0)
                        ++nbrace;
                    else
                        push(idx+1);
                    break;
               case ']':
                    tmp=pop();
                    if(tmp!=-1){
                        if(current->value!=0){
                            push(tmp);
                            idx=tmp-1;   /*remember ++idx at the end of the loop?*/
                            break;
                        }
                    }

                    break;
            }
        }
        ++idx;
    }

    clearall(first);
}
Пример #21
0
int debug_put_wall(Object * obj, Object * obj2)
{
	Cell c = new_cell('#', 0, 2, 1, 1);
	put_cell(obj->world, obj->x, obj->y, c);
	return 1;
}
Пример #22
0
/*
 
	Fifo command example:
 
	":get_domaincode:[response_file]\n
	 domain_name\n
	 authorization_to_register_domains\n
	 \n
 	"
 
 */
int get_domainprefix(FILE *stream, char *response_file)
{
	db_key_t db_keys[NR_KEYS];
	db_val_t db_vals[NR_KEYS];
	db_op_t  db_ops[NR_KEYS] = {OP_EQ, OP_EQ};

	code_t code;
	dc_t* cell; 
	
	char domain_name[256];
	str sdomain;

	char authorization[10];
	str sauth;
	int authorized=0;
		
	/* read a line -the domain name parameter- from the fifo */
	sdomain.s = domain_name;
	if(!read_line(sdomain.s, 255, stream, &sdomain.len) || sdomain.len==0)	
	{
		LOG(L_ERR, "PDT: get_domaincode: could not read from fifo\n");
		fifo_reply(response_file, "400 |get_domaincode: could not " 
						"read from fifo\n");
		return 1;
	}
	domain_name[sdomain.len] = '\0';

	/* read a line -the authorization to register new domains- from the fifo */
	sauth.s = authorization;
	if(!read_line(sauth.s, 3, stream, &sauth.len) || sauth.len==0)
	{	
		LOG(L_ERR, "PDT: get_domaincode: could not read from fifo\n");
		fifo_reply(response_file, "400 |get_domaincode: could not "
						"read from fifo\n");
		return 1;
	}

	/* see what kind of user we have */
	authorized = sauth.s[0]-'0';

	lock_get(&l);

	/* search the domain in the hashtable */
	cell = get_code_from_hash(hash->dhash, hash->hash_size, domain_name);
	
	/* the domain is registered */
	if(cell)
	{

		lock_release(&l);
			
		/* domain already in the database */	
		fifo_reply(response_file, "201 |Domain name= %.*s"
				"Domain code= %d%d\n",
				sdomain.len, sdomain.s, cell->code, code_terminator);
		return 0;
		
	}
	
	/* domain not registered yet */
	/* user not authorized to register new domains */	
	if(!authorized)
	{
		lock_release(&l);
		fifo_reply(response_file, "203 |Domain name not registered yet\n");
		return 0;
	}

	code = *next_code;
	*next_code = apply_correction(code+1);
		

	/* prepare for insertion into database */
	db_keys[0] = DB_KEY_CODE;
	db_keys[1] = DB_KEY_NAME;

	db_vals[0].type = DB_INT;
	db_vals[0].nul = 0;
	db_vals[0].val.int_val = code;

	db_vals[1].type = DB_STR;
	db_vals[1].nul = 0;
	db_vals[1].val.str_val.s = sdomain.s;
	db_vals[1].val.str_val.len = sdomain.len;
	DBG("%d %.*s\n", code, sdomain.len, sdomain.s);
			
	/* insert a new domain into database */
	if(db_insert(db_con, db_keys, db_vals, NR_KEYS)<0)
	{
		/* next available code is still code */
		*next_code = code;
		lock_release(&l);
		LOG(L_ERR, "PDT: get_domaincode: error storing a"
				" new domain\n");
		fifo_reply(response_file, "204 |Cannot register the new domain in a"
					" consistent way\n");
		return -1;
	}
	
	/* insert the new domain into hashtables, too */
	cell = new_cell(sdomain.s, code);
	if(add_to_double_hash(hash, cell)<0)
		goto error;		

	lock_release(&l);

	/* user authorized to register new domains */
	fifo_reply(response_file, "202 |Domain name= %.*s"
		"	New domain code=  %d%d\n",
		sdomain.len, sdomain.s, code, code_terminator);

	return 0;

	
error:
	/* next available code is still code */
	*next_code = code;
	/* delete from database */
	if(db_delete(db_con, db_keys, db_ops, db_vals, NR_KEYS)<0)
		LOG(L_ERR,"PDT: get_domaincode: database/share-memory are inconsistent\n");
	lock_release(&l);
	
	return -1;
}
Пример #23
0
/**
 * init module function
 */
static int mod_init(void)
{
	db_res_t* db_res = NULL;
	code_t i, code;
	dc_t* cell; 

		
	DBG("PDT: initializing...\n");
	
	if(hs_two_pow<0)
	{
		LOG(L_ERR, "PDT: mod_init: hash_size_two_pow must be"
					" positive and less than %d\n", MAX_HSIZE_TWO_POW);
		return -1;
	}
	
	if(code_terminator>9 || code_terminator<0)
	{
		LOG(L_ERR, "PDT: mod_init: code_terminator must be a digit\n");
		return -1;
	}

	if(!prefix_valid())
		return -1;

	next_code = (code_t*)shm_malloc(sizeof(code_t));
	if(!next_code)
	{
		LOG(L_ERR, "PDT: mod_init: cannot allocate next_code!\n");
		return -1;
	}
	if(lock_init(&l) == 0)
	{		
		shm_free(next_code);
		LOG(L_ERR, "PDT: mod_init: cannot init the lock\n");
		return -1;	
	}	
	
	if(register_fifo_cmd(get_domainprefix, "get_domainprefix", 0)<0)
	{
		LOG(L_ERR, "PDT: mod_init: cannot register fifo command 'get_domaincode'\n");
		goto error1;
	}	


	/* binding to mysql module */
	if(bind_dbmod())
	{
		LOG(L_ERR, "PDT: mod_init: Database module not found\n");
		goto error1;
	}

	/* open a connection with the database */
	db_con = db_init(db_url);
	if(!db_con)
	{
	
		LOG(L_ERR, "PDT: mod_init: Error while connecting to database\n");        
		goto error1;
	}
	else
	{
		db_use_table(db_con, db_table);
		DBG("PDT: mod_init: Database connection opened successfully\n");
	}
	
	/* init hashes in share memory */
	if( (hash = init_double_hash(hs_two_pow)) == NULL)
	{
		LOG(L_ERR, "PDT: mod_init: hash could not be allocated\n");	
		goto error2;
	}
	
	/* loading all information from database */
	*next_code = 0;
	if(db_query(db_con, NULL, NULL, NULL, NULL, 0, 0, "code", &db_res)==0)
	{
		for(i=0; i<RES_ROW_N(db_res); i++)
		{
			
			code = RES_ROWS(db_res)[i].values[0].val.int_val; 
			if (!code_valid(code))
			{
				LOG(L_ERR, "PDT: mod_init: existing code contains the terminator\n");
				goto error;
			}
			
			if (*next_code < code)
				*next_code = code;

			cell=new_cell(
			(char*)(RES_ROWS(db_res)[i].values[1].val.string_val), code);
			
			if(cell == NULL)
					goto error;
			
			if(add_to_double_hash(hash, cell)<0)
			{
				LOG(L_ERR, "PDT: mod_init: could not add information from database"
								" into shared-memory hashes\n");
				goto error;
			}
			
 		}
		// clear up here
		//print_hash(hash->dhash, hash->hash_size);
		//print_hash(hash->chash, hash->hash_size);

		(*next_code)++;
		if (*next_code < start_range)
				*next_code = start_range;
		*next_code = apply_correction(*next_code);

		DBG("PDT: mod_init: next_code:%d\n", *next_code);
		

		/* free up the space allocated for response */
		if(db_free_query(db_con, db_res)<0)
		{
			LOG(L_ERR, "PDT: mod_init: error when freeing"
				" up the response space\n");
		}
	}
	else
	{
		/* query to database failed */
		LOG(L_ERR, "PDT: mod_init: query to database failed\n");
		goto error;
	}

	db_close(db_con); /* janakj - close the connection */
	/* success code */
	return 0;

error:
	free_double_hash(hash);
error2:
	db_close(db_con);
error1:	
	shm_free(next_code);
	lock_destroy(&l);
	return -1;
}	
Пример #24
0
static void rpc_add(rpc_t* rpc, void* c)
{
	db_key_t db_keys[NR_KEYS] = {prefix_column, domain_column};
	db_val_t db_vals[NR_KEYS];
	db_op_t  db_ops[NR_KEYS] = {OP_EQ, OP_EQ};

	pd_t* cell;
	pd_op_t *ito, *tmp;
	str sd, sp;
	char* t;

	if(_dhash==NULL) {
		LOG(L_ERR, "PDT:pdt_fifo_add: strange situation\n");
		rpc->fault(c, 500, "Server Error");
		return;
	}

	     /* Use 's' to make sure strings are zero terminated */
	if (rpc->scan(c, "ss", &sp.s, &sd.s) < 2) {
		rpc->fault(c, 400, "Invalid Parameter Value");
		return;
	}
	sp.len = strlen(sp.s);
	sd.len = strlen(sd.s);

	t = sp.s;
	while(t!=NULL && *t!='\0') {
		if(*t < '0' || *t > '9') {
			LOG(L_ERR, "PDT:pdt_fifo_add: bad prefix [%s]\n", sp.s);
			rpc->fault(c, 400, "Bad Prefix");
			return;
		}
		t++;
	}

	if(pdt_check_pd(_dhash, &sp, &sd)!=0) {
		LOG(L_ERR, "PDT:pdt_fifo_add: prefix or domain exists\n");
		rpc->fault(c, 400, "Prefix Or Domain Exists");
		return;
	}

	db_vals[0].type = DB_STR;
	db_vals[0].nul = 0;
	db_vals[0].val.str_val = sp;

	db_vals[1].type = DB_STR;
	db_vals[1].nul = 0;
	db_vals[1].val.str_val= sd;

	DBG("PDT:pdt_fifo_add: [%.*s] <%.*s>\n", sp.len, sp.s, sd.len, sd.s);

	     /* insert a new domain into database */
	if(pdt_dbf.insert(db_con, db_keys, db_vals, NR_KEYS)<0) {
		LOG(L_ERR, "PDT:pdt_fifo_add: error storing new prefix/domain\n");
		rpc->fault(c, 430, "Cannot Store Prefix/domain");
		return;
	}

	     /* insert the new domain into hashtables, too */
	cell = new_cell(&sp, &sd);
	if(cell==NULL) {
		LOG(L_ERR, "PDT:pdt_fifo_add: no more shm\n");
		rpc->fault(c, 431, "Out Of Shared Memory");
		goto error1;
	}
	tmp = new_pd_op(cell, 0, PDT_ADD);
	if(tmp==NULL) {
		LOG(L_ERR, "PDT:pdt_fifo_add: no more shm!\n");
		rpc->fault(c, 431, "Out Of Shared Memory");
		goto error2;
	}

	lock_get(&_dhash->diff_lock);

	if(pdt_add_to_hash(_dhash, &sp, &sd)!=0) {
		LOG(L_ERR, "PDT:pdt_fifo_add: could not add to cache\n");
		rpc->fault(c, 431, "Could Not Add To Cache");
		goto error3;
	}

	_dhash->max_id++;
	tmp->id = _dhash->max_id;
	if(_dhash->diff==NULL) {
		_dhash->diff = tmp;
		goto done;
	}
	ito = _dhash->diff;
	while(ito->n!=NULL)
		ito = ito->n;

	ito->n = tmp;
	tmp->p = ito;

 done:
	DBG("PDT:pdt_fifo_add: op[%d]=%d...\n", tmp->id, tmp->op);
	lock_release(&_dhash->diff_lock);
	return;

 error3:
	lock_release(&_dhash->diff_lock);
	free_pd_op(tmp);
 error2:
	free_cell(cell);
 error1:
	if(pdt_dbf.delete(db_con, db_keys, db_ops, db_vals, NR_KEYS)<0)
		LOG(L_ERR,"PDT:pdt_fifo_add: database/cache are inconsistent\n");
}
Пример #25
0
struct table *parse_table(unsigned char *html, unsigned char *eof, unsigned char **end, struct rgb *bgcolor, int sh, struct s_e **bad_html, int *bhp)
{
	int qqq;
	struct table *t;
	struct table_cell *cell;
	unsigned char *t_name, *t_attr, *en;
	int t_namelen;
	int x = 0, y = -1;
	int p = 0;
	unsigned char *lbhp = NULL;
	int l_al = AL_LEFT;
	int l_val = VAL_MIDDLE;
	int csp, rsp;
	int group = 0;
	int i, j, k;
	struct rgb l_col;
	int c_al = AL_TR, c_val = VAL_TR, c_width = W_AUTO, c_span = 0;
	memcpy(&l_col, bgcolor, sizeof(struct rgb));
	*end = html;
	if (bad_html) {
		*bad_html = DUMMY;
		*bhp = 0;
	}
	if (!(t = new_table())) return NULL;
	se:
	en = html;
	see:
	html = en;
	if (bad_html && !p && !lbhp) {
		if (!(*bhp & (ALLOC_GR-1))) {
			if ((unsigned)*bhp > MAXINT / sizeof(struct s_e) - ALLOC_GR) overalloc();
			*bad_html = mem_realloc(*bad_html, (*bhp + ALLOC_GR) * sizeof(struct s_e));
		}
		lbhp = (*bad_html)[(*bhp)++].s = html;
	}
	while (html < eof && *html != '<') html++;
	if (html >= eof) {
		if (p) CELL(t, x, y)->end = html;
		if (lbhp) (*bad_html)[*bhp-1].e = html;
		goto scan_done;
	}
	if (html + 2 <= eof && (html[1] == '!' || html[1] == '?')) {
		html = skip_comment(html, eof);
		goto se;
	}
	if (parse_element(html, eof, &t_name, &t_namelen, &t_attr, &en)) {
		html++;
		goto se;
	}
	if (t_namelen == 5 && !casecmp(t_name, "TABLE", 5)) {
		en = skip_table(en, eof);
		goto see;
	}
	if (t_namelen == 6 && !casecmp(t_name, "/TABLE", 6)) {
		if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
		if (p) CELL(t, x, y)->end = html;
		if (lbhp) (*bad_html)[*bhp-1].e = html;
		goto scan_done;
	}
	if (t_namelen == 8 && !casecmp(t_name, "COLGROUP", 8)) {
		if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
		if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
		c_al = AL_TR;
		c_val = VAL_TR;
		c_width = W_AUTO;
		get_align(t_attr, &c_al);
		get_valign(t_attr, &c_val);
		get_c_width(t_attr, &c_width, sh);
		if ((c_span = get_num(t_attr, "span")) == -1) c_span = 1;
		goto see;
	}
	if (t_namelen == 9 && !casecmp(t_name, "/COLGROUP", 9)) {
		if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
		if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
		c_span = 0;
		c_al = AL_TR;
		c_val = VAL_TR;
		c_width = W_AUTO;
		goto see;
	}
	if (t_namelen == 3 && !casecmp(t_name, "COL", 3)) {
		int sp, wi, al, val;
		if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
		if ((sp = get_num(t_attr, "span")) == -1) sp = 1;
		wi = c_width;
		al = c_al;
		val = c_val;
		get_align(t_attr, &al);
		get_valign(t_attr, &val);
		get_c_width(t_attr, &wi, sh);
		new_columns(t, sp, wi, al, val, !!c_span);
		c_span = 0;
		goto see;
	}
	if (t_namelen == 3 && (!casecmp(t_name, "/TR", 3) || !casecmp(t_name, "/TD", 3) || !casecmp(t_name, "/TH", 3))) {
		if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
		if (p) CELL(t, x, y)->end = html, p = 0;
		if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
	}
	if (t_namelen == 2 && !casecmp(t_name, "TR", 2)) {
		if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
		if (p) CELL(t, x, y)->end = html, p = 0;
		if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
		if (group) group--;
		l_al = AL_LEFT;
		l_val = VAL_MIDDLE;
		memcpy(&l_col, bgcolor, sizeof(struct rgb));
		get_align(t_attr, &l_al);
		get_valign(t_attr, &l_val);
		get_bgcolor(t_attr, &l_col);
		y++, x = 0;
		goto see;
	}
	if (t_namelen == 5 && ((!casecmp(t_name, "THEAD", 5)) || (!casecmp(t_name, "TBODY", 5)) || (!casecmp(t_name, "TFOOT", 5)))) {
		if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
		if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
		group = 2;
	}
	if (t_namelen != 2 || (casecmp(t_name, "TD", 2) && casecmp(t_name, "TH", 2))) goto see;
	if (c_span) new_columns(t, c_span, c_width, c_al, c_val, 1);
	if (lbhp) (*bad_html)[*bhp-1].e = html, lbhp = NULL;
	if (p) CELL(t, x, y)->end = html, p = 0;
	if (y == -1) y = 0, x = 0;
	nc:
	cell = new_cell(t, x, y);
	if (cell->used) {
		if (cell->colspan == -1) goto see;
		x++;
		goto nc;
	}
	cell->mx = x;
	cell->my = y;
	cell->used = 1;
	cell->start = en;
	p = 1;
	cell->align = l_al;
	cell->valign = l_val;
	if ((cell->b = upcase(t_name[1]) == 'H')) cell->align = AL_CENTER;
	if (group == 1) cell->group = 1;
	if (x < t->c) {
		if (t->cols[x].align != AL_TR) cell->align = t->cols[x].align;
		if (t->cols[x].valign != VAL_TR) cell->valign = t->cols[x].valign;
	}
	memcpy(&cell->bgcolor, &l_col, sizeof(struct rgb));
	get_align(t_attr, &cell->align);
	get_valign(t_attr, &cell->valign);
	get_bgcolor(t_attr, &cell->bgcolor);
	if ((csp = get_num(t_attr, "colspan")) == -1) csp = 1;
	if (!csp) csp = -1;
	if ((rsp = get_num(t_attr, "rowspan")) == -1) rsp = 1;
	if (!rsp) rsp = -1;
	if (csp >= 0 && rsp >= 0 && csp * rsp > 100000) {
		if (csp > 10) csp = -1;
		if (rsp > 10) rsp = -1;
	}
	cell->colspan = csp;
	cell->rowspan = rsp;
	if (csp == 1) {
		int w = W_AUTO;
		get_c_width(t_attr, &w, sh);
		if (w != W_AUTO) set_td_width(t, x, w, 0);
	}
	qqq = t->x;
	for (i = 1; csp != -1 ? i < csp : x + i < qqq; i++) {
		struct table_cell *sc = new_cell(t, x + i, y);
		if (sc->used) {
			csp = i;
			for (k = 0; k < i; k++) CELL(t, x + k, y)->colspan = csp;
			break;
		}
		sc->used = sc->spanned = 1;
		sc->rowspan = rsp;
		sc->colspan = csp;
		sc->mx = x;
		sc->my = y;
	}
	qqq = t->y;
	for (j = 1; rsp != -1 ? j < rsp : y + j < qqq; j++) {
		for (k = 0; k < i; k++) {
			struct table_cell *sc = new_cell(t, x + k, y + j);
			if (sc->used) {
				int l, m;
				if (sc->mx == x && sc->my == y) continue;
				/*internal("boo");*/
				for (l = 0; l < k; l++) memset(CELL(t, x + l, y + j), 0, sizeof(struct table_cell));
				rsp = j;
				for (l = 0; l < i; l++) for (m = 0; m < j; m++) CELL(t, x + l, y + m)->rowspan = j;
				goto brk;
			}
			sc->used = sc->spanned = 1;
			sc->rowspan = rsp;
			sc->colspan = csp;
			sc->mx = x;
			sc->my = y;
		}
	}
	brk:
	goto see;

	scan_done:
	*end = html;

	for (x = 0; x < t->x; x++) for (y = 0; y < t->y; y++) {
		struct table_cell *c = CELL(t, x, y);
		if (!c->spanned) {
			if (c->colspan == -1) c->colspan = t->x - x;
			if (c->rowspan == -1) c->rowspan = t->y - y;
		}
	}

	if ((unsigned)t->y > MAXINT / sizeof(int)) overalloc();
	t->r_heights = mem_alloc(t->y * sizeof(int));
	memset(t->r_heights, 0, t->y * sizeof(int));

	for (x = 0; x < t->c; x++) if (t->cols[x].width != W_AUTO) set_td_width(t, x, t->cols[x].width, 1);
	set_td_width(t, t->x, W_AUTO, 0);

	return t;
}
Пример #26
0
int debug_put_space(Object * obj, Object * obj2)
{
	Cell c = new_cell('.', 0, 6, 0, 0);
	put_cell(obj->world, obj->x, obj->y, c);
	return 1;
}