Exemplo n.º 1
0
/*
 * Disconnects a connector from all.
 *
 * Before:
 *
 *   A --> (C) --> D
 *          |
 *          v
 *         (B)
 *
 * After:
 *
 *   A     (C)     D
 *          
 *          
 *         (B)
 *
 */
int grammar_disconnect_all(struct connector_t *connector)
{
	if(connector->from)
		return_if(grammar_disconnect_from(connector), -1);
	if(connector->sym)
		return_if(grammar_disconnect_to_symbol(connector), -1);
	if(connector->con)
		grammar_disconnect_to_connector(connector);

	return 0;
}
Exemplo n.º 2
0
Arquivo: video.c Projeto: phako/tn
int 
video_file_save_frame(struct VideoFile* video_file, 
        const char* filename, enum ImageFormat image_format)
{
    return_if(video_file == NULL, -1);
    return_if(filename == NULL, -1);
    return_if(image_format < 0 || image_format >= IMAGE_FORMAT_COUNT, -1);

    return image_save(filename, video_file->frame_rgb->data[0],
            video_file->width, video_file->height,
            image_format);
}
Exemplo n.º 3
0
int grammar_symbol_new(struct grammar_t *g, struct symbol_t **symbol, const char *name, char type)
{
	struct symbol_t *s;
	struct list_t *list;

#ifndef NDEBUG
	debug("Adding new symbol %s", name);
#endif

	if(type == NODE_VAR)
	{
		list = &(g->variables);
	}
	else
	{
		list = &(g->terminals);
	}
	
	s = list_find(list, name, (int (*)(const void *, const void *)) grammar_cmp_str_symbol);
	if(s)
	{
		*symbol = s;
		return 0;
	}

	return_if(grammar_symbol_init(symbol, name, type), -1);

	if(list_add(list, *symbol) != 0)
	{
		grammar_symbol_free(*symbol);
		return -1;
	}

	return 0;
}
Exemplo n.º 4
0
/**
 * Destroy a Vector's allocated memory
 *
 * @param Vector * v
 */
void Vector_destroy(Vector* v) {

    return_if(v == NULL);

    free(v->values);
    free(v);
}
Exemplo n.º 5
0
Arquivo: video.c Projeto: phako/tn
int
video_file_seek_frame(struct VideoFile* video_file, uint64_t frame, int slow_seek)
{
    return_if(video_file == NULL, -1);

    if (!slow_seek)
    {
        av_seek_frame(video_file->format_ctx, 
                video_file->video_stream_idx,
                frame,
                AVSEEK_FLAG_BACKWARD);
    }
    video_file->codec_ctx->skip_frame = AVDISCARD_NONREF;
    do
    {
        video_file_decode_frame(video_file);
        if (video_file->pts >= frame - 1)
        {
            printf("PTS: %"PRId64", frame: %"PRIu64"\n", video_file->pts, frame);
            break;
        }
    } while (1);
    video_file->codec_ctx->skip_frame = AVDISCARD_NONE;

    return 0;
}
Exemplo n.º 6
0
int grammar_init(struct grammar_t **g)
{
	*g = (struct grammar_t *) calloc(1, sizeof(struct grammar_t));

	return_if(!*g, -1);

	return 0;
}
Exemplo n.º 7
0
/*
 * Disconnects a connector to a symbol.
 *
 * Before:
 *
 *   ? --> (C) --> DST
 *          |
 *          v
 *         (?)
 *
 * After:
 *
 *   ? --> (C)     DST
 *          |
 *          v
 *         (?)
 *
 */
int grammar_disconnect_to_symbol(struct connector_t *connector)
{
#ifndef NDEBUG
	if(connector->sym == NULL) debug("connector->sym == NULL");
#endif
	return_if(list_remove(&(connector->sym->from), connector), -1);
	connector->sym = NULL;
	return 0;
}
Exemplo n.º 8
0
/*
 * Connects from a symbol to connector.
 *
 * Before:
 *
 *   SRC     (C) --> ?
 *            |
 *            v
 *           (?)
 *
 * After:
 *
 *   SRC --> (C) --> ?
 *            |
 *            v
 *           (?)
 */
int grammar_connect_from_symbol(struct connector_t *connector, struct symbol_t *src)
{
#ifndef NDEBUG
	if(connector->from != NULL) debug("connector->from != NULL");
#endif
	return_if(list_add(&(src->to), (void *) connector), -1);
	connector->from = src;
	return 0;
}
Exemplo n.º 9
0
/*
 * Connects from a connector to a symbol.
 *
 * Before:
 *
 *   ? --> (C)     DST
 *          |
 *          v
 *         (?)
 *
 * After:
 *
 *   ? --> (C) --> DST
 *          |
 *          v
 *         (?)
 *
 */
int grammar_connect_to_symbol(struct connector_t *connector, struct symbol_t *dst)
{
#ifndef NDEBUG
	if(connector->sym != NULL) debug("connector->sym != NULL");
#endif
	return_if(list_add(&(dst->from), (void *) connector), -1);
	connector->sym = dst;
	return 0;
}
Exemplo n.º 10
0
/*
 * Disconnects a connector from a symbol.
 *
 * Before:
 *
 *   SRC --> (C) --> ?
 *            |
 *            v
 *           (?)
 *
 * After:
 *
 *   SRC     (C) --> ?
 *            |
 *            v
 *           (?)
 *
 */
int grammar_disconnect_from_symbol(struct connector_t *connector)
{
#ifndef NDEBUG
	if(connector->from == NULL) debug("connector->from == NULL");
#endif
	struct symbol_t *from;
	from = connector->from;
	return_if(list_remove(&(from->to), connector), -1);
	connector->from = NULL;
	return 0;
}
Exemplo n.º 11
0
Arquivo: video.c Projeto: phako/tn
int
video_file_decode_until_non_black(struct VideoFile* video_file)
{
    return_if(video_file == NULL, -1);

    do
    {
        video_file_decode_frame(video_file);
    } while (histogram_heuristically_black(&(video_file->histogram)));

    return 0;
}
Exemplo n.º 12
0
int grammar_connector_new(struct grammar_t *g, struct connector_t **connector)
{
	*connector = calloc(1, sizeof(struct connector_t));
#ifndef NDEBUG
	debug("Adding new connector [%p]", *connector);
#endif
	return_if(!*connector, -1);

	(*connector)->type = NODE_CON;

	grammar_connector_add(g, *connector);

	return 0;
}
Exemplo n.º 13
0
/**
 * Copy values from a vector to another one
 *
 * @param Vector *dest
 * @param Vector *src
 * @param fromIndex index to start copying
 * @param fromSrcIndex index of the src vector to start copying
 * TODO: Allow from - to
 */
void Vector_copyFrom(Vector* dest, Vector* src, size_t fromIndex,
                     size_t fromSrcIndex) {
    size_t needed_size = (src->length - fromSrcIndex) * sizeof(VectorItem);

    return_if(fromSrcIndex && src->length <= fromSrcIndex);

    while (dest->size < needed_size) {
        Vector_extend(dest);
    }

    memcpy(dest->values + fromIndex, src->values + fromSrcIndex,
           (src->length - fromSrcIndex) * sizeof(VectorItem));

    dest->length = MAX(dest->length, fromIndex + src->length - fromSrcIndex);
}
Exemplo n.º 14
0
static int grammar_symbol_init(struct symbol_t **symbol, const char *name, int type)
{
	char *n;

	n = malloc(strlen(name) + 1);
	return_if(n == NULL, -1);
	*symbol = calloc(1, sizeof(struct symbol_t));
	if(*symbol == NULL)
	{
		free(n);
		return -1;
	}

	(*symbol)->type = type;
	(*symbol)->name = strcpy(n, name);
	return 0;
}
Exemplo n.º 15
0
Arquivo: aeon.c Projeto: sonetto/aeon
int aeon_render(
    aeon_op *op,
    aeon_glyph_info *glyph_info,
    unsigned charcode
) {
    FT_Error ft_error;
    FT_Face ft_face = op->ft_face;
    FT_Bitmap *ft_bitmap;

    void *op_bitmap = op->bitmap;
    unsigned op_bitmap_width = op->bitmap_width;
    unsigned op_bitmap_height = op->bitmap_height;

    unsigned max_glyph_width = op->max_glyph_width;
    unsigned max_glyph_height = op->max_glyph_height;

    unsigned *pen_x = &op->pen_x;
    unsigned *pen_y = &op->pen_y;

    ft_error = FT_Load_Char(ft_face, charcode, FT_LOAD_RENDER);
    return_if(ft_error);

    ft_bitmap = &ft_face->glyph->bitmap;

    bitblit(
        op_bitmap,
        op_bitmap_width,
        ft_bitmap->buffer,
        ft_bitmap->width,
        ft_bitmap->rows,
        *pen_x,
        *pen_y
    );

    *pen_x += op->max_glyph_width;

    if(*pen_x + max_glyph_width > op_bitmap_width) {
        *pen_y += max_glyph_height;
        *pen_x = 0;
    }

    assert(*pen_y + max_glyph_height <= op->bitmap_height);

    return 0;
}
Exemplo n.º 16
0
Arquivo: video.c Projeto: phako/tn
int
video_file_decode_frame(struct VideoFile* video_file)
{
    AVPacket packet;
    int frame_finished;

    return_if(video_file == NULL, -1);

    while (av_read_frame(video_file->format_ctx, &packet) >= 0)
    {
        if (packet.stream_index == video_file->video_stream_idx)
        {
            avcodec_decode_video2(
                    video_file->codec_ctx, 
                    video_file->frame,
                    &frame_finished,
                    &packet);
            if (frame_finished)
            {
                video_file->pts = packet.dts;
                /* create rgb frame */
                sws_scale(
                        video_file->scale_ctx,
                        (const uint8_t * const*) video_file->frame->data,
                        video_file->frame->linesize, 0,
                        video_file->height, 
                        video_file->frame_rgb->data, 
                        video_file->frame_rgb->linesize);
                histogram_create_from_rgb(
                        video_file->frame_rgb->data[0],
                        video_file->width,
                        video_file->height,
                        &(video_file->histogram));
                av_free_packet(&packet);
                break;
            }
        }
        av_free_packet(&packet);
    }

    return 0;
}
Exemplo n.º 17
0
Arquivo: video.c Projeto: phako/tn
int 
video_file_close(struct VideoFile* video_file)
{
    return_if (NULL == video_file, -1);

    if (video_file->rgb_buffer != NULL)
    {
        av_free(video_file->rgb_buffer);
    }

    if (video_file->frame_rgb != NULL)
    {
        av_free(video_file->frame_rgb);
    }

    if (video_file->frame != NULL)
    {
        av_free(video_file->frame);
    }

    if (video_file->scale_ctx != NULL)
    {
        sws_freeContext(video_file->scale_ctx);
    }

    if (video_file->codec_ctx != NULL)
    {
        avcodec_close(video_file->codec_ctx);
    }

    if (video_file->format_ctx)
    {
        avformat_close_input(&(video_file->format_ctx));
    }

    free(video_file);

    return 0;
}
Exemplo n.º 18
0
int grammar_connector_add(struct grammar_t *g, struct connector_t *c)
{
	return_if(list_add(&(g->connectors), (void *) c), -1);
	return 0;
}
Exemplo n.º 19
0
Arquivo: video.c Projeto: phako/tn
struct VideoFile*
video_file_open(const char* filename)
{
    struct VideoFile* video_file = NULL;

    return_if(NULL == filename, NULL);

    video_file = (struct VideoFile*)malloc(sizeof(struct VideoFile));

    if (video_file)
    {
        memset(video_file, 0, sizeof(struct VideoFile));
        if (avformat_open_input(&(video_file->format_ctx), filename, NULL, NULL) == 0)
        {
            if (avformat_find_stream_info(video_file->format_ctx, NULL) >= 0)
            {   
                int idx;
                /* find video stream */
                idx = video_file->video_stream_idx = 
                    find_video_stream(video_file->format_ctx);

                if (idx >= 0)
                {
                    video_file->video_stream = 
                        video_file->format_ctx->streams[idx];
                    video_file->codec_ctx = 
                        video_file->format_ctx->streams[idx]->codec;

                    video_file->codec = create_codec(video_file->codec_ctx);
                    if (video_file->codec)
                    {
                        /* 
                         * create software scaler context for colorspace
                         * conversion
                         */
                        video_file->scale_ctx =
                            make_scale_context(video_file->codec_ctx);
                        if (video_file->scale_ctx)
                        {
                            video_file->width = 
                                video_file->codec_ctx->width;
                            video_file->height = 
                                video_file->codec_ctx->height;
                            video_file->frame = avcodec_alloc_frame();
                            if (video_file->frame)
                            {
                                video_file->frame_rgb = avcodec_alloc_frame();
                                if (video_file->frame_rgb)
                                {
                                    int bytes = avpicture_get_size(
                                            PIX_FMT_RGB24,
                                            video_file->width,
                                            video_file->height);
                                    video_file->rgb_buffer =
                                        (uint8_t *)av_malloc(bytes * sizeof(uint8_t));
                                    if (video_file->rgb_buffer)
                                    {
                                        avpicture_fill(
                                                (AVPicture *)video_file->frame_rgb, 
                                                video_file->rgb_buffer, 
                                                PIX_FMT_RGB24,
                                                video_file->width, 
                                                video_file->height);
                                        return video_file;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        video_file_close(video_file);
        video_file = NULL;
    }

    return video_file;
}
Exemplo n.º 20
0
int main(int argc, char **argv)
{
	if (argc > 1)
		while (++argv, --argc) {
			if (!strcmp("-c", argv[0]))
				color_on = 1;
			else if (!strcmp("-h", argv[0]))
				printf("liblisp unit tests\n\tusage ./%s (-c)? (-h)?\n", argv[0]);
			else
				printf("unknown argument '%s'\n", argv[0]);
		}

	unit_test_start("liblisp");
	{
		print_note("util.c");

		test(is_number("0xfAb"));
		test(is_number("-01234567"));
		test(is_number("+1000000000000000000000000000003"));
		test(!is_number(""));
		test(!is_number("+"));
		test(!is_number("-"));

		test(unbalanced("(((", '(', ')') > 0);
		test(unbalanced("))",  '(', ')') < 0);
		test(unbalanced("",    '(', ')') == 0);
		test(unbalanced("\"(", '(', ')') == 0);
		test(unbalanced("( \"))))(()()()(()\\\"())\")",  '(', ')') == 0);
		test(unbalanced("(a (b) c (d (e (f) \")\" g)))", '(', ')') == 0);
		test(unbalanced("((a b) c", '(', ')')  > 0);

		test(!is_fnumber(""));
		test(!is_fnumber("1e"));
		test(!is_fnumber("-1e"));
		test(!is_fnumber("1-e"));
		test(is_fnumber("+0."));
		test(is_fnumber("123"));	/*this passes, see header */
		test(is_fnumber("1e-3"));
		test(is_fnumber("1.003e+34"));
		test(is_fnumber("1e34"));
		test(is_fnumber("93.04"));

		test(match("", ""));
		test(match("abc", "abc"));
		test(!match("abC", "abc"));
		test(match("aaa*", "aaaXX"));
		test(!match("aaa*", "XXaaaXX"));
		test(match(".bc", "abc"));
		test(match("a.c", "aXc"));
		test(!match("a\\.c", "aXc"));
		test(match("a\\.c", "a.c"));

		char *s = NULL;
		state(s = vstrcatsep(",", "a", "b", "c", "", "foo", "bar", NULL));
		test(!sstrcmp("a,b,c,,foo,bar", s));
		free(s);

		char *t = NULL, *s1 = "Hello,", *s2 = " World!";
		state(t = calloc(16, 1));
		state(strcpy(t, s1));
		test(((size_t) (lstrcatend(t, s2) - t)) == (strlen(s1) + strlen(s2)));
		free(t);

		/*test tr, or translate, functionality */
		size_t trinsz = 0;
		uint8_t trout[128] = { 0 }, *trin = (uint8_t *) "aaabbbcdaacccdeeefxxxa";
		tr_state_t *tr1;
		state(tr1 = tr_new());
		state(trinsz = strlen((char *)trin));
		test(tr_init(tr1, "", (uint8_t *) "abc", (uint8_t *) "def") == TR_OK);
		test(tr_block(tr1, trin, trout, trinsz) == trinsz);
		test(!strcmp((char *)trout, "dddeeefdddfffdeeefxxxd"));
		test(tr_init(tr1, "s", (uint8_t *) "abc", (uint8_t *) "def") == TR_OK);
		state(memset(trout, 0, 128));
		test(tr_block(tr1, trin, trout, trinsz) <= trinsz);
		test(!strcmp((char *)trout, "defddfdeeefxxxd"));
		state(tr_delete(tr1));

		/*know collisions for the djb2 hash algorithm */
		test(djb2("heliotropes", strlen("heliotropes")) ==
		     djb2("neurospora", strlen("neurospora")));
		test(djb2("depravement", strlen("depravement")) ==
		     djb2("serafins", strlen("serafins")));
		/*should not collide */
		test(djb2("heliotropes", strlen("heliotropes")) !=
		     djb2("serafins", strlen("serafins")));
	}

	{ /*io.c test */
		io_t *in, *out;
		print_note("io.c");

		/*string input */
		static const char hello[] = "Hello\n";
		/**@note io_sin currently duplicates "hello" internally*/
		state(in = io_sin(hello, strlen(hello)));
		test(io_is_in(in));
		test(io_getc(in) == 'H');
		test(io_getc(in) == 'e');
		test(io_getc(in) == 'l');
		test(io_getc(in) == 'l');
		test(io_getc(in) == 'o');
		test(io_getc(in) == '\n');
		test(io_getc(in) == EOF);
		test(io_getc(in) == EOF);
		test(!io_error(in));
		test(io_seek(in, 0, SEEK_SET) >= 0);
		test(io_getc(in) == 'H');
		test(io_seek(in, 3, SEEK_SET) >= 0);
		test(io_getc(in) == 'l');
		test(io_ungetc('x', in) == 'x');
		test(io_getc(in) == 'x');
		test(io_getc(in) == 'o');
		state(io_close(in));

		/*string output */
		char *s = NULL;
		static const char hello_world[] = "Hello,\n\tWorld!\n";
		/**@note io_sin currently duplicates hello_world internally*/
		state(in = io_sin(hello_world, strlen(hello_world))); 
		test(!strcmp(s = io_getline(in), "Hello,"));
		s = (free(s), NULL);
		test(!strcmp(s = io_getline(in), "\tWorld!"));
		s = (free(s), NULL);
		test(!io_getline(in));
		test(io_seek(in, 0, SEEK_SET) >= 0);
		test(!strcmp(s = io_getdelim(in, EOF), "Hello,\n\tWorld!\n"));
		s = (free(s), NULL);
		state(io_close(in));

		state(out = io_sout(1));
		test(io_puts("Hello, World", out) != EOF);
		test(!strcmp("Hello, World", io_get_string(out)));
		test(io_putc('\n', out) != EOF);
		test(!strcmp("Hello, World\n", io_get_string(out)));
		test(io_seek(out, -6, SEEK_CUR) >= 0);
		test(io_puts("Mars\n", out) != EOF);
		test(!strcmp("Hello, Mars\n\n", io_get_string(out)));
		free(io_get_string(out));
		state(io_close(out));

		static const char block_in[16] = {1, 3, 4, 6};
		static char block_out[16] = {0};
		state((in = io_sin(block_in, 16)));
		test(io_getc(in) == 1);
		test(io_read(block_out, 15, in) == 15);
		test(!memcmp(block_out, block_in+1, 15));

		state(io_close(in));
	}

	{ /* hash.c hash table tests */
		hash_table_t *h = NULL;
		print_note("hash.c");
		state(h = hash_create(1));
		return_if(!h);
		test(!hash_insert(h, "key1", "val1"));
		test(!hash_insert(h, "key2", "val2"));
		/* assuming the hash algorithm is djb2, then
		 *  "heliotropes"  collides with "neurospora"
		 *  "depravement"  collides with "serafins"
		 *  "playwright"   collides with "snush" (for djb2a)
		 * See:
		 * <https://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed> */
		test(!hash_insert(h, "heliotropes", "val3"));
		test(!hash_insert(h, "neurospora", "val4"));
		test(!hash_insert(h, "depravement", "val5"));
		test(!hash_insert(h, "serafins", "val6"));
		test(!hash_insert(h, "playwright", "val7"));
		test(!hash_insert(h, "snush", "val8"));
		test(!hash_insert(h, "", "val9"));
		test(!hash_insert(h, "nil", ""));
		test(!hash_insert(h, "a", "x"));
		test(!hash_insert(h, "a", "y"));
		test(!hash_insert(h, "a", "z"));
		test(!sstrcmp("val1", hash_lookup(h, "key1")));
		test(!sstrcmp("val2", hash_lookup(h, "key2")));
		test(!sstrcmp("val3", hash_lookup(h, "heliotropes")));
		test(!sstrcmp("val4", hash_lookup(h, "neurospora")));
		test(!sstrcmp("val5", hash_lookup(h, "depravement")));
		test(!sstrcmp("val6", hash_lookup(h, "serafins")));
		test(!sstrcmp("val7", hash_lookup(h, "playwright")));
		test(!sstrcmp("val8", hash_lookup(h, "snush")));
		test(!sstrcmp("val9", hash_lookup(h, "")));
		test(!sstrcmp("", hash_lookup(h, "nil")));
		test(!sstrcmp("z", hash_lookup(h, "a")));
		test(hash_get_load_factor(h) <= 0.75f);

		state(hash_destroy(h));
	}

	{			/* lisp.c (and the lisp interpreter in general) */
		lisp_t *l;

		print_note("lisp.c");
		/*while unit testing eschews state being held across tests it is makes
		 *little sense in this case*/
		state(l = lisp_init());
		state(io_close(lisp_get_logging(l)));
		test(!lisp_set_logging(l, io_nout()));
		return_if(!l);
		test(!lisp_eval_string(l, ""));
		test(is_int(lisp_eval_string(l, "2")));
		test(get_int(lisp_eval_string(l, "(+ 2 2)")) == 4);
		test(get_int(lisp_eval_string(l, "(* 3 2)")) == 6);

		lisp_cell_t *x = NULL, *y = NULL, *z = NULL;
		char *t = NULL;
		state(x = lisp_intern(l, lstrdup_or_abort("foo")));
		state(y = lisp_intern(l, t = lstrdup_or_abort("foo")));	/*this one needs freeing! */
		state(z = lisp_intern(l, lstrdup_or_abort("bar")));
		test(x == y && x != NULL);
		test(x != z);
		free(t);	/*free the non-interned string */

		test(is_proc(lisp_eval_string(l, "(define square (lambda (x) (* x x)))")));
		test(get_int(lisp_eval_string(l, "(square 4)")) == 16);

		test(!is_list(cons(l, gsym_tee(), gsym_tee())));
		test(is_list(cons(l, gsym_tee(), gsym_nil())));
		test(!is_list(cons(l, gsym_nil(), cons(l, gsym_tee(), gsym_tee()))));
		test(is_list(mk_list(l, gsym_tee(), gsym_nil(), gsym_tee(), NULL)));

		test(gsym_error() == lisp_eval_string(l, "(> 'a 1)"));
		test(is_sym(x));
		test(is_asciiz(x));
		test(!is_str(x));
		test(gsym_error() == lisp_eval_string(l, "(eval (cons quote 0))"));

		char *serial = NULL;
		test(!strcmp((serial = lisp_serialize(l, cons(l, gsym_tee(), gsym_error()))), "(t . error)"));
		state(free(serial));

		state(lisp_destroy(l));
	}
	return unit_test_end("liblisp");	/*should be zero! */
}
Exemplo n.º 21
0
/**
 * Extend a vector capacity
 *
 * @param Vector *v
 */
void Vector_extend(Vector* v) {
    v->values = (VectorItem*)realloc(v->values, v->size * 2);
    v->size *= 2;
    return_if(v->values == NULL);
}
Exemplo n.º 22
0
        ~Cleanup ()
        {
            return_if(ok);

            close(fd);
        }