예제 #1
0
DjVuDebug& 
DjVuDebug::lock(int lvl, int noindent)
{
  int threads_num=1;
  debug_lock.lock();
  // Get per-thread debug object
  long threadid = (long) GThread::current();
  DjVuDebug &dbg = debug_map()[threadid];
  threads_num=debug_map().size();
  // Check level
  dbg.block = (lvl > debug_level);
  // Output thread id and indentation
  if (! noindent)
    {
      if (threads_num>1)
        dbg.format("[T%d] ", dbg.id);
      int ind = dbg.indent;
      char buffer[257];
      memset(buffer,' ', sizeof(buffer)-1);
      buffer[sizeof(buffer)-1] = 0;
      while (ind > (int)sizeof(buffer)-1)
        {
          dbg.format("%s", buffer);
          ind -= sizeof(buffer)-1;
        }
      if (ind > 0)
        {
          buffer[ind] = 0;
          dbg.format("%s", buffer);
        }
    }
  // Return
  return dbg;
}
예제 #2
0
파일: map.c 프로젝트: odrevet/GE2
void map_draw(map* p_map, SDL_Renderer* renderer){
  int x_from = -(p_map->o_camera.x);
  int x_to   =  (p_map->width * p_map->tile_width) + x_from;
  int y_from = -(p_map->o_camera.y);
  int y_to   =  (p_map->height * p_map->tile_height) + y_from;
  
  int tile_y_index = 0;
  for (int y=y_from;y<y_to;y+=p_map->tile_height/2){
    int tile_x_index = 0;
    for (int x=x_from;x<x_to;x+=p_map->tile_width/2){
      //get the tile in the map tiles
      tile* p_tile = map_get_tile(p_map,
				  tile_x_index,
				  tile_y_index);
      //draw the tile
      map_draw_tile(p_map, p_tile, x, y, renderer);
     
      //increment the tile index and check if it's in the map size rang
      if(++tile_x_index >= p_map->width)break;
    }
    //increment the tile index and check if it's in the map size rang
    if(++tile_y_index >= p_map->height)break;
  }

#ifdef DEBUG
  debug_map(p_map, renderer,
	    x_from, x_to,
	    y_from, y_to,
	    p_map->o_camera.x / p_map->tile_width*8,
	    p_map->o_camera.y / p_map->tile_height*8);
#endif
}
예제 #3
0
/** Convert value_pair_map_t to VALUE_PAIR(s) and add them to a REQUEST.
 *
 * Takes a single value_pair_map_t, resolves request and list identifiers
 * to pointers in the current request, then attempts to retrieve module
 * specific value(s) using callback, and adds the resulting values to the
 * correct request/list.
 *
 * @param request The current request.
 * @param map specifying destination attribute and location and src identifier.
 * @param func to retrieve module specific values and convert them to
 *	VALUE_PAIRS.
 * @param ctx to be passed to func.
 * @param src name to be used in debugging if different from map value.
 * @return -1 if the operation failed, -2 in the source attribute wasn't valid, 0 on success.
 */
int radius_map2request(REQUEST *request, value_pair_map_t const *map,
		       UNUSED char const *src, radius_tmpl_getvalue_t func, void *ctx)
{
	int rcode, num;
	VALUE_PAIR **list, *vp, *head = NULL;
	REQUEST *context;
	TALLOC_CTX *parent;
	vp_cursor_t cursor;

	/*
	 *	Sanity check inputs.  We can have a list or attribute
	 *	as a destination.
	 */
	if ((map->dst->type != VPT_TYPE_LIST) &&
	    (map->dst->type != VPT_TYPE_ATTR)) {
		REDEBUG("Invalid mapping destination");
		return -2;
	}

	context = request;
	if (radius_request(&context, map->dst->request) < 0) {
		REDEBUG("Mapping \"%s\" -> \"%s\" invalid in this context", map->src->name, map->dst->name);
		return -2;
	}

	/*
	 *	If there's no CoA packet and we're updating it,
	 *	auto-allocate it.
	 */
	if (((map->dst->list == PAIR_LIST_COA) ||
	     (map->dst->list == PAIR_LIST_DM)) &&
	    !request->coa) {
		request_alloc_coa(context);
		if (map->dst->list == PAIR_LIST_COA) {
			context->coa->proxy->code = PW_CODE_COA_REQUEST;
		} else {
			context->coa->proxy->code = PW_CODE_DISCONNECT_REQUEST;
		}
	}

	list = radius_list(context, map->dst->list);
	if (!list) {
		REDEBUG("Mapping \"%s\" -> \"%s\" invalid in this context", map->src->name, map->dst->name);

		return -2;
	}

	parent = radius_list_ctx(context, map->dst->list);

	/*
	 *	The callback should either return -1 to signify operations error, -2 when it can't find the
	 *	attribute or list being referenced, or 0 to signify success.
	 *	It may return "sucess", but still have no VPs to work with.
	 */
	rcode = func(&head, request, map, ctx);
	if (rcode < 0) {
		rad_assert(!head);
		return rcode;
	}

	if (!head) return 0;

	/*
	 *	Reparent the VP
	 */
	for (vp = fr_cursor_init(&cursor, &head); vp; vp = fr_cursor_next(&cursor)) {

		VERIFY_VP(vp);
		if (debug_flag) debug_map(request, map, vp);

		(void) talloc_steal(parent, vp);
	}

	/*
	 *	List to list copies.
	 */
	if (map->dst->type == VPT_TYPE_LIST) {
		switch (map->op) {
		case T_OP_CMP_FALSE:
			rad_assert(head == NULL);
			pairfree(list);

			if (map->dst->list == PAIR_LIST_REQUEST) {
				context->username = NULL;
				context->password = NULL;
			}
			break;

		case T_OP_SET:
			if (map->src->type == VPT_TYPE_LIST) {
				pairfree(list);
				*list = head;
			} else {
		case T_OP_EQ:

				rad_assert(map->src->type == VPT_TYPE_EXEC);
				pairmove(parent, list, &head);
				pairfree(&head);
			}

			if (map->dst->list == PAIR_LIST_REQUEST) {
				context->username = pairfind(head, PW_USER_NAME, 0, TAG_ANY);
				context->password = pairfind(head, PW_USER_PASSWORD, 0, TAG_ANY);
			}
			break;

		case T_OP_ADD:
			pairadd(list, head);
			break;

		default:
			pairfree(&head);
			return -1;
		}

		return 0;
	}

	/*
	 *	We now should have only one destination attribute, and
	 *	only one source attribute.
	 */
	rad_assert(head->next == NULL);

	/*
	 *	Find the destination attribute.  We leave with either
	 *	the cursor and vp pointing to the attribute, or vp is
	 *	NULL.
	 */
	num = map->dst->num;
	for (vp = fr_cursor_init(&cursor, list);
	     vp != NULL;
	     vp = fr_cursor_next(&cursor)) {
		VERIFY_VP(vp);
		if ((vp->da == map->dst->da) && (!vp->da->flags.has_tag || (map->dst->tag == TAG_ANY) || (vp->tag == map->dst->tag))) {
			if (num == 0) break;
			num--;
		}
	}

	/*
	 *	Figure out what to do with the source attribute.
	 */
	switch (map->op) {
	case T_OP_CMP_FALSE:	/* remove matching attributes */
		pairfree(&head);
		if (!vp) return 0;

		/*
		 *	Wildcard: delete all of the matching ones,
		 *	based on tag.
		 */
		if (!map->dst->num) {
			pairdelete(list, map->dst->da->attr, map->dst->da->vendor,
				   map->dst->tag);
			vp = NULL;
		} else {
			/*
			 *	We've found the Nth one.  Delete it, and only
			 *	it.
			 */
			vp = fr_cursor_remove(&cursor);
		}

		/*
		 *	Check that the User-Name and User-Password
		 *	caches point to the correct attribute.
		 */
	fixup:
		if (map->dst->list == PAIR_LIST_REQUEST) {
			context->username = pairfind(*list, PW_USER_NAME, 0, TAG_ANY);
			context->password = pairfind(*list, PW_USER_PASSWORD, 0, TAG_ANY);
		}
		pairfree(&vp);
		return 0;

	case T_OP_EQ:		/* set only if not already set */
		if (vp) {
			pairfree(&head);
			return 0;
		}
		fr_cursor_insert(&cursor, head);
		goto fixup;

	case T_OP_SET:		/* over-write if existing, or else add */
		if (vp) vp = fr_cursor_remove(&cursor);
		fr_cursor_insert(&cursor, head);
		goto fixup;

	case T_OP_ADD:		/* append no matter what */
		vp = NULL;
		pairadd(list, head);
		goto fixup;

	case T_OP_SUB:		/* delete if it matches */
		head->op = T_OP_CMP_EQ;
		rcode = radius_compare_vps(NULL, head, vp);
		pairfree(&head);

		if (rcode == 0) {
			vp = fr_cursor_remove(&cursor);
			goto fixup;
		}
		return 0;

	default:		/* filtering operators */
		/*
		 *	If the VP doesn't exist, the filters will add
		 *	it with the given value.
		 */
		if (!vp) {
			fr_cursor_insert(&cursor, head);
			goto fixup;
		}
		break;
	}

	/*
	 *	The LHS exists.  We need to limit it's value based on
	 *	the operator, and the value of the RHS.
	 */
	head->op = map->op;
	rcode = radius_compare_vps(NULL, head, vp);
	head->op = T_OP_SET;

	switch (map->op) {
	case T_OP_CMP_EQ:
		if (rcode == 0) {
	leave:
			pairfree(&head);
			break;
		}
	replace:
		vp = fr_cursor_remove(&cursor);
		fr_cursor_insert(&cursor, head);
		goto fixup;

	case T_OP_LE:
		if (rcode <= 0) goto leave;
		goto replace;

	case T_OP_GE:
		if (rcode >= 0) goto leave;
		goto replace;

	default:
		pairfree(&head);
		return -1;
	}

	return 0;
}
예제 #4
0
파일: map.c 프로젝트: odrevet/GE2
void map_draw(map* p_map, SDL_Renderer* renderer){
  //offset the map drawing to the left in order to display the chipset
  const int x_offset = p_map->x_offset;
  
  const int x_from = -(p_map->o_camera.x % p_map->tile_width) + x_offset;
  const int x_to   =   x_from + SCREEN_WIDTH + (x_from == 0 ? 0 : p_map->tile_width) + x_offset;
  const int y_from = -(p_map->o_camera.y % p_map->tile_height);
  const int y_to   =   y_from + SCREEN_HEIGHT + (y_from == 0 ? 0 : p_map->tile_height);
  
  int tile_y_index = p_map->o_camera.y / p_map->tile_height;
  for (int y=y_from;y<y_to;y+=p_map->tile_height){
    int tile_x_index = p_map->o_camera.x / p_map->tile_width;
    for (int x=x_from;x<x_to;x+=p_map->tile_width){
      //get the tile in the map tiles
      tile* p_tile = map_get_tile(p_map,
				  tile_x_index,
				  tile_y_index);
      
      //draw the tile
      map_draw_tile(p_map, p_tile, x, y, renderer);
      
      //draw a horizontal grid line
      SDL_RenderDrawLine(renderer,
			 0,
			 y,
			 SCREEN_WIDTH,
			 y);

      //draw a vertical grid line
      SDL_RenderDrawLine(renderer,
			 x,
			 0,
			 x,
			 SCREEN_HEIGHT);
 
      //increment the tile index and check if it's in the map size rang
      if(++tile_x_index >= p_map->width)break;
    }
    //increment the tile index and check if it's in the map size rang
    if(++tile_y_index >= p_map->height)break;
  }

  //draw a rectangle on the selected map tile
  SDL_Rect rect = {.x= p_map->map_tile_x_index * p_map->tile_width,
		   .y= p_map->map_tile_y_index * p_map->tile_height + y_from,
		   .w= p_map->tile_width,
		   .h= p_map->tile_height};  
  SDL_SetRenderDrawColor(renderer, 0xFF, 0x0, 0x0, 0xFF);
  SDL_RenderDrawRect(renderer, &rect);
    
#ifdef DEBUG
  //Draw the debug menu
  debug_map(p_map, renderer,
	    x_from, x_to,
	    y_from, y_to,
	    p_map->o_camera.x / p_map->tile_width,
	    p_map->o_camera.y / p_map->tile_height);
#endif
}

void map_mouse_move(map* p_map, SDL_Renderer* renderer, int x, int y){
  const int x_from = p_map->o_camera.x % p_map->tile_width;
  const int y_from = p_map->o_camera.y % p_map->tile_height;
  
  int map_tile_x_index = (x + x_from) / p_map->tile_width;
  int map_tile_y_index = (y + y_from) / p_map->tile_height;
    
  p_map->map_tile_x_index = map_tile_x_index;
  p_map->map_tile_y_index = map_tile_y_index;
}

void map_tile_click(map* p_map,
		    int x,
		    int y,
		    Uint8 button){
  int x_offset = p_map->x_offset;
  
  if(x < x_offset){
    //the click was on the chipset part
    //set the selected tile in the chipset
    p_map->chipset_tile_x_index = x / p_map->tile_width;
    p_map->chipset_tile_y_index = y / p_map->tile_height;
  }
  else{
    //the clic was on the map part
    //change the clicked tile with the selected tile
    int clicked_tile_x_index = (x + p_map->o_camera.x - x_offset) / p_map->tile_width;
    int clicked_tile_y_index = (y + p_map->o_camera.y) / p_map->tile_height;
#ifdef DEBUG
    printf("click at index %d %d\n",
	   clicked_tile_x_index,
	   clicked_tile_y_index);
#endif
    //check if the index is in the map boundaries
    if(clicked_tile_x_index >= 0 && clicked_tile_x_index < p_map->width &&
       clicked_tile_y_index >= 0 && clicked_tile_y_index < p_map->height){    
      tile* p_tile = map_get_tile(p_map,
				  clicked_tile_x_index,
				  clicked_tile_y_index);
      p_tile->x = p_map->chipset_tile_x_index;
      p_tile->y = p_map->chipset_tile_y_index;
    }
  }
}