Beispiel #1
0
/**
 * Append one directory to the context's include path.
 */
XKB_EXPORT int
xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
{
    struct stat stat_buf;
    int err;
    char *tmp;

    tmp = strdup(path);
    if (!tmp)
        goto err;

    err = stat(path, &stat_buf);
    if (err != 0)
        goto err;
    if (!S_ISDIR(stat_buf.st_mode))
        goto err;

#if defined(HAVE_EACCESS)
    if (eaccess(path, R_OK | X_OK) != 0)
        goto err;
#elif defined(HAVE_EUIDACCESS)
    if (euidaccess(path, R_OK | X_OK) != 0)
        goto err;
#endif

    darray_append(ctx->includes, tmp);
    return 1;

err:
    darray_append(ctx->failed_includes, tmp);
    return 0;
}
static void unbreak_backslash_broken_lines(struct token_list *tl, tok_message_queue *mq) {
	const char *s = tl->orig, *e = s+tl->orig_size;
	darray_char         *txt    = talloc_darray(tl);
	darray(const char*) *olines = talloc_darray(tl);
	darray(const char*) *tlines = talloc_darray(tl);
	
	do {
		const char *line_start = s, *line_end;
		const char *lnw; //last non-white
		size_t start_offset = txt->size;
		
		//scan to the next line and find the last non-white character in the line
		while (s<e && !creturn(*s)) s++;
		line_end = s;
		lnw = s;
		while (lnw>line_start && cspace(lnw[-1])) lnw--;
		if (s<e && creturn(*s)) {
			s++;
			//check for non-standard newlines (i.e. "\r", "\r\n", or "\n\r")
			if (s<e && *s=='\n'+'\r'-s[-1])
				s++;
		}
		
		//add the backslash-break-free version of the text
		if (lnw>line_start && lnw[-1]=='\\' && line_end<e) {
			darray_append_items(*txt, line_start, lnw-1-line_start);
			if (lnw<e && cspace(*lnw)) {
				tok_msg_warn(spaces_after_backslash_break, lnw,
					"Trailing spaces after backslash-broken line");
			}
		} else
			darray_append_items(*txt, line_start, s-line_start);
		
		//add the line starts for this line
		darray_append(*olines, line_start);
		darray_append(*tlines, (const char*)start_offset);
			//Since the txt buffer moves when expanded, we're storing offsets
			//  for now.  Once we're done building txt, we can add the base
			//  of it to all the offsets to make them pointers.
	} while (s<e);
	
	//stick a null terminator at the end of the text
	darray_realloc(*txt, txt->size+1);
	txt->item[txt->size] = 0;
	
	//convert the line start offsets to pointers
	{
		const char **i;
		darray_foreach(i, *tlines)
			*i = txt->item + (size_t)(*i);
	}
	
	tl->olines = olines->item;
	tl->olines_size = olines->size;
	tl->txt = txt->item;
	tl->txt_size = txt->size;
	tl->tlines = tlines->item;
	tl->tlines_size = tlines->size;
}
Beispiel #3
0
tree_ptr tree_new_assign(tree_ptr l, tree_ptr r) {
	// TODO: Check l's type.
	tree_ptr t = tree_new(eval_assign);
	t->child = darray_new();
	darray_append(t->child, l);
	darray_append(t->child, r);
	return t;
}
Beispiel #4
0
void type_function_init(struct type *t, struct type *arg_type, struct type *ret_type)
{
    t->category = TYPE_CATEGORY_OPERATOR;
    t->operator.type = TYPEOP_IMPLICATION;

    darray_append(t->operator.operands, arg_type);
    darray_append(t->operator.operands, ret_type);
}
Beispiel #5
0
tree_ptr tree_new_define(tree_ptr id, tree_ptr parm, tree_ptr body) {
	tree_ptr t = tree_new(eval_define);
	t->child = darray_new();
	darray_append(t->child, id);
	darray_append(t->child, parm);
	darray_append(t->child, body);
	return t;
}
Beispiel #6
0
tree_ptr tree_new_ternary(tree_ptr cond, tree_ptr t1, tree_ptr t2) {
	tree_ptr t = tree_new(eval_ternary);
	t->child = darray_new();
	darray_append(t->child, cond);
	darray_append(t->child, t1);
	darray_append(t->child, t2);
	return t;
}
void problem_02_test_basic(){
 	/* declare and initialize a new array */
 	DArray array;
 	int majority = 0;

 	darray_init(&array);

 	/* initialize array */
 	darray_append(&array, 3);
 	darray_append(&array, 3);
 	darray_append(&array, 4);
 	darray_append(&array, 2);
 	darray_append(&array, 4);
 	darray_append(&array, 4);
 	darray_append(&array, 2);
 	darray_append(&array, 4);
 	darray_append(&array, 4);

 	majority = find_majority_basic(&array);

 	if(majority == FAILURE){
 		printf("No majority value found!\n");
 	}else{
 		printf("Majority value: %d\n", majority);
 	}

 	/* free up the underlying array */
 	darray_free(&array);
 }
Beispiel #8
0
DArray* graph_vertex_get_out_edges(const Graph *g, const Vertex *v)
{
    unsigned long index;
    DArray *out_edges;
    Edge *e;

    assert(g != NULL);
    assert(v != NULL);

    out_edges = darray_create();

    if(NULL == out_edges) {
        fprintf(stderr, "Cannot create out-edge list (%s:%d)\n",
                __FUNCTION__, __LINE__);
        return NULL;
    }

    for(index = 0; index < darray_size(v->edges); index++) {
        e = (Edge *)darray_index(v->edges, index);
        if(v == e->source) {
            if(darray_append(out_edges, e) < 0) {
                fprintf(stderr, "Cannot construct out-edge list (%s:%d)\n",
                        __FUNCTION__, __LINE__);
                darray_free(out_edges);
                return NULL;
            }
        }
    }

    return out_edges;
}
Beispiel #9
0
int graph_edge_add(Graph *g, Edge *e)
{
    unsigned long index;
    Vertex *v;

    assert(g != NULL);
    assert(e != NULL);

    for(index = 0; index < darray_size(g->vertices); index++) {
        v = (Vertex *)darray_index(g->vertices, index);

        if(v == e->source) {
            v->out_degree++;
            darray_append(v->edges, e);
            g->edge_count++;
        }

        if((v == e->target)) {
            /* For undirected graphs, insert an extra edge to allow
             * traversal from the target vertex back to the source
             * vertex, except in the case where an edge is a loop.
             */
            if(graph_is_undirected(g)) {
                v->out_degree++;

                if(e->target != e->source) {
                    darray_append(v->edges,
                        edge_create(e->target, e->source, e->weight));
                } else {
                    /* Loops are counted twice */
                    v->out_degree++;
                }
            } else {
                /* Keep track of the number of edges directed
                 * toward this vertex, but not which edges
                 *
                 * TODO Keep a separate in-edge list for each
                 * node?
                 */
                v->in_degree++;
            }
        }
    }

    return 0;
}
Beispiel #10
0
int graph_vertex_add(Graph *g, Vertex *v)
{
    assert(g != NULL);
    assert(v != NULL);

    v->idx = darray_size(g->vertices);

    return darray_append(g->vertices, v);
}
Beispiel #11
0
/* Complexity: O(log n), worst-case */
int heap_push(Heap *heap, void *data)
{
    assert(heap != NULL);

    darray_append(heap->h, data);
    heapify_up(heap, darray_size(heap->h) - 1);

    return 0;
}
Beispiel #12
0
char* mml_serialize_compact(MMLObject* mml) {
	DArray out;
	out = darray_create(sizeof(char), 0);

	_serialize_compact(mml, &out, 0);
	darray_append(&out, "\0");

	// Look at mml_serialize for explanation
	return (char*)out.data;
}	
void problem_05_test_sum(){
	/* declare and initialize a new array */
	DArray array;
 	int missing;
 	darray_init(&array);

 	/* initialize array */
 	darray_append(&array, 1);
 	darray_append(&array, 2);
 	darray_append(&array, 4);
 	darray_append(&array, 5);
 	darray_append(&array, 6);

 	missing = get_missing_number_sum_formula(&array, 5);

 	printf("The missing number is: %d\n", missing);

 	/* free up the underlying array */
 	darray_free(&array);
 }
Beispiel #14
0
char* mml_serialize(MMLObject* mml) {
	DArray out;
	out = darray_create(sizeof(char), 0);

	_serialize(mml, &out, 0, 0);
	darray_append(&out, "\0");

	// This is a hack, but saves one alloc/free pair.
	// Freeing out.data is mostly the same as calling darray_free.
	return (char*)out.data;
}	
Beispiel #15
0
static void record(element_t asum, element_t bsum, element_t snark,
                   darray_t hole, mpz_t counter) {
  snapshot_ptr ss = pbc_malloc(sizeof(struct snapshot_s));
  element_init_same_as(ss->a, asum);
  element_init_same_as(ss->b, bsum);
  element_init_same_as(ss->snark, snark);
  element_set(ss->a, asum);
  element_set(ss->b, bsum);
  element_set(ss->snark, snark);
  darray_append(hole, ss);
  element_printf("snark %Zd: %B\n", counter, snark);
}
Beispiel #16
0
void _serialize_compact(MMLObject* mml, DArray* out, NodeIdx node) {
	assert(mml);
	assert(out);

	MMLNode* node_ptr = mml_get_nodeptr(mml, node);
	
	const char* name = mml_get_str(mml, node_ptr->name_start);
	const char* value = mml_get_str(mml, node_ptr->value_start);

	darray_append(out, "(");
	mml_insert_escapes(name, out);
	darray_append(out, " ");
	mml_insert_escapes(value, out);

	NodeIdx child = node_ptr->first_child_idx;
	while(child) {
		_serialize_compact(mml, out, child);
		child = mml_get_next(mml, child);
	}	
	darray_append(out, ")");
}	
Beispiel #17
0
void symtab_put(symtab_t t, void *data, const char *key) {
  int i, n = t->list->count;
  entry_ptr e;
  for (i=0; i<n; i++) {
    e = t->list->item[i];
    if (!strcmp(e->key, key)) goto doit;
  }
  e = pbc_malloc(sizeof(entry_t));
  e->key = pbc_strdup(key);
  darray_append(t->list, e);
doit:
  e->data = data;
}
Beispiel #18
0
void _serialize(MMLObject* mml, DArray* out, NodeIdx node, uint padding) {
	assert(mml);
	assert(out);

	MMLNode* node_ptr = mml_get_nodeptr(mml, node);
	
	uint i = padding;
	while(i--) {
		darray_append(out, " ");
	}	

	const char* name = mml_get_str(mml, node_ptr->name_start);
	const char* value = mml_get_str(mml, node_ptr->value_start);

	darray_append_multi(out, "( ", 2);
	mml_insert_escapes(name, out);
	darray_append(out, " ");
	mml_insert_escapes(value, out);

	if(node_ptr->first_child_idx == 0) {
		// node has no children, print it on a single line
		darray_append_multi(out, " )\n", 3);
		return;
	}
	else {
		// node has children, recurse
		darray_append(out, "\n");
		NodeIdx child = node_ptr->first_child_idx;
		while(child) {
			_serialize(mml, out, child, padding + 4);
			child = mml_get_next(mml, child);
		}	
		i = padding;
		while(i--) {
			darray_append(out, " ");
		}	
		darray_append_multi(out, ")\n", 2);
	}	
}	
Beispiel #19
0
void video_draw_line(uint layer, const Vector2* start,
					 const Vector2* end, Color color) {
	assert(layer < bucket_count);
	assert(start);
	assert(end);

	LineDesc new_line;
	new_line.start = *start;
	new_line.end = *end;
	new_line.color = color;

	if(!line_buckets[layer].reserved)
		line_buckets[layer] = darray_create(sizeof(LineDesc), 32);
	darray_append(&line_buckets[layer], &new_line);
}
Beispiel #20
0
int main(int argc, char *argv[])
{

  dyn_array_t arr = darray_make(4);

  // Store 1,2,3,4 in array
  for (int i = 0; i < darray_size(arr); ++i) darray_set(arr, i, i);

  // Print arr[i] = i for i=[1,4]
  for (int i = 0; i < darray_size(arr); ++i) printf("arr[%d] = %d\n", i, *darray_get(arr, i));

  darray_append(&arr, 5);
  darray_append(&arr, 6);
  darray_prepend(&arr, 7);
  darray_prepend(&arr, 8);

  // Print arr[i] = j for i=[1,8]
  for (int i = 0; i < darray_size(arr); ++i) printf("arr[%d] = %d\n", i, *darray_get(arr, i));

  darray_free(&arr);
  arr = NULL;
  
  return 0;
}
Beispiel #21
0
static struct tcmu_conf_option *
tcmu_register_option(char *key, tcmu_option_type type)
{
	struct tcmu_conf_option option, *opt;

	option.key = key;
	option.type = type;

	darray_append(tcmu_options, option);

	darray_foreach(opt, tcmu_options) {
		if (!strcmp(opt->key, key))
			return opt;
	}

	tcmu_err("failed to register new option!\n");
	return NULL;
}
Beispiel #22
0
DArray _gen_edges(DArray points, DArray geometry) {
	DArray edges = darray_create(sizeof(Edge), 0);
	Vector2* points_vec2 = DARRAY_DATA_PTR(points, Vector2);
	for(uint i = 0; i < points.size; ++i) {
		for(uint j = 0; j < points.size; ++j) {
			if(i >= j)
				continue;

			float sqr_dist = _node_distance_sq(points_vec2[i], points_vec2[j]);

			if(sqr_dist <= max_edge_distance*max_edge_distance) {
				Edge n = {i, j};

				Segment s = ai_shortest_path(points_vec2[i], points_vec2[j]);
				Segment s1, s2;
				bool split = ai_split_path(s, &s1, &s2);

				// Check for wall intersection
				Segment* geometry_s = DARRAY_DATA_PTR(geometry, Segment);
				bool intersects_arena = false;
				for(uint k = 0; k < geometry.size; ++k) {
					if(segment_intersect(s1, geometry_s[k], NULL)) {
						intersects_arena = true;
						break;
					}
					if(split && segment_intersect(s2, geometry_s[k], NULL)) {
						intersects_arena = true;
						break;
					}	
				}

				// Check distance to walls at midpoint
				Vector2 midpoint = vec2_scale(vec2_add(s1.p1, s1.p2), 0.5f);
				if(ai_wall_distance(midpoint, geometry) < min_wall_distance)
					intersects_arena = true;

				if(!intersects_arena)
					darray_append(&edges, (void*)&n);
			}	
		}
	}
	return edges;
}	
Beispiel #23
0
DArray _gen_navpoints(DArray geometry, DArray platforms) {
	DArray points = darray_create(sizeof(Vector2), 0);
	darray_append_multi(&points, platforms.data, platforms.size);

	uint fail_count = 0;
	do {
		Vector2 navpoint = vec2(rand_float_range(0.0f, SCREEN_WIDTH),
			rand_float_range(0.0f, SCREEN_HEIGHT));

		float wdist = ai_wall_distance(navpoint, geometry);
		float pdist = _point_distance(navpoint, points);

		if(wdist >= min_wall_distance && pdist >= min_point_distance) {
			darray_append(&points, (void*)&navpoint);
			fail_count = 0;
		}
		else {
			fail_count++;
		}	
	} while(fail_count < 1200);	
	return points;
}	
Beispiel #24
0
int main(int argc, char** argv)
{
	CU_pSuite pSuite;

    CU_initialize_registry();

    pSuite = CU_add_suite("TestHello", TestInit, TestClean);
    CU_add_test(pSuite, "testAssertTrue", testAssertTrue);

    CU_console_run_tests();
    CU_cleanup_registry();	
	
	DArray* darray = (DArray*) malloc( sizeof( DArray ));
    memset( darray, 0x00 ,sizeof(DArray));
	char* name = (char*) malloc( sizeof(char)*100);
	
	sprintf( name, "kakasi");
	darray_append( darray, sizeof(char)*strlen(name), name);
	
	darray_for_each( darray, print_content_char, NULL );
	
	darray_destory( darray );
	return 0;
}
Beispiel #25
0
int main(int argc, char* argv[])
{
    int i = 0;
    int n = 100;
    int data = 0;

    DArray* darray = darray_create(NULL, NULL);

    for (i = 0; i < n; i++) {
        darray_append(darray, (void*)(rand() % n));
    }

    darray_sort(darray, quick_sort, cmp_int);

    darray_foreach(darray, print_int, NULL);
    printf("\n");
    darray_get_by_index(darray, 0, (void*)&data);
    data = !data;
    darray_foreach(darray, unique_print_int, &data);
    printf("\n");
    darray_destroy(darray);

    return 0;
}
void problem_01_test_hash(){
 	/* declare and initialize a new array */
 	DArray array;
 	int search = 16;

 	darray_init(&array);

 	/* initialize array */
 	darray_append(&array, 1);
 	darray_append(&array, 4);
 	darray_append(&array, 45);
 	darray_append(&array, 6);
 	darray_append(&array, 10);
 	darray_append(&array, -8);

 	array_has_sum_of_hash(&array, array.size, search);

 	/* free up the underlying array */
 	darray_free(&array);
 }
void problem_01_test_sort(){
	/* declare and initialize a new array */
	DArray array;
 	int search = 16;
 	darray_init(&array);

 	/* initialize array */
 	darray_append(&array, 1);
 	darray_append(&array, 4);
 	darray_append(&array, 45);
 	darray_append(&array, 6);
 	darray_append(&array, 10);
 	darray_append(&array, -8);

 	if(!array_has_sum_of(&array, array.capacity, search)){
 		printf("Array has two elements with sum %d\n", search);
 	}else{
 		printf("Array doesn't have two elements with sum %d\n", search);
 	}
 	/* free up the underlying array */
 	darray_free(&array);
 }
Beispiel #28
0
void minimap_track(ObjRabbit* rabbit){
	darray_append(&minimap_pointers, &rabbit);
	results[minimap_pointers.size-1] = rabbit;
}
Beispiel #29
0
void tree_set_fun(tree_ptr f, tree_ptr src) {
	darray_append(f->child, src);
}
Beispiel #30
0
void tree_append(tree_ptr f, tree_ptr p) {
	darray_append(f->child, p);
}