Example #1
0
Rlist *RlistFromSplitRegex(const char *string, const char *regex, size_t max_entries, bool allow_blanks)
{
    assert(string);
    if (!string)
    {
        return NULL;
    }

    const char *sp = string;
    size_t entry_count = 0;
    int start = 0;
    int end = 0;
    Rlist *result = NULL;
    Buffer *buffer = BufferNewWithCapacity(CF_MAXVARSIZE);

    pcre *rx = CompileRegex(regex);
    if (rx)
    {
        while ((entry_count < max_entries) &&
               StringMatchWithPrecompiledRegex(rx, sp, &start, &end))
        {
            if (end == 0)
            {
                break;
            }

            BufferClear(buffer);
            BufferAppend(buffer, sp, start);

            if (allow_blanks || BufferSize(buffer) > 0)
            {
                RlistAppendScalar(&result, BufferData(buffer));
                entry_count++;
            }

            sp += end;
        }

        pcre_free(rx);
    }

    if (entry_count < max_entries)
    {
        BufferClear(buffer);
        size_t remaining = strlen(sp);
        BufferAppend(buffer, sp, remaining);

        if ((allow_blanks && sp != string) || BufferSize(buffer) > 0)
        {
            RlistAppendScalar(&result, BufferData(buffer));
        }
    }

    BufferDestroy(buffer);

    return result;
}
Example #2
0
static int UpdateInput(equalizer* p)
{
	PCMRelease(p->PCM);
	p->PCM = NULL;
	BufferClear(&p->Buffer);

	if (p->Codec.In.Format.Type == PACKET_AUDIO)
	{
		if (!p->Enabled || p->Codec.In.Format.Format.Audio.Channels>MAXPLANES)
			return ERR_INVALID_PARAM;

		PacketFormatPCM(&p->Codec.Out.Format,&p->Codec.In.Format,FIX_FRACBITS+1);
		p->Codec.Out.Format.Format.Audio.Bits = sizeof(fix_t)*8;
		p->Codec.Out.Format.Format.Audio.Flags = PCM_PLANES;
#ifndef FIXED_POINT
		p->Codec.Out.Format.Format.Audio.Flags |= PCM_FLOAT;
#endif

		p->PCM = PCMCreate(&p->Codec.Out.Format.Format.Audio,&p->Codec.In.Format.Format.Audio,0,0);
		if (!p->PCM)
			return ERR_OUT_OF_MEMORY;
		p->Scale = FIXC(1);
		UpdateParam(p);
		Flush(p);
	}
	return ERR_NONE;
}
Example #3
0
void BufferSet(Buffer *buffer, const char *bytes, unsigned int length)
{
    assert(buffer);
    assert(bytes);

    BufferClear(buffer);

    BufferAppend(buffer, bytes, length);
}
Example #4
0
File: idct.c Project: Jsoucek/q3ce
static int Flush(codecidct* p)
{
	Discontinuity(p);
	p->FrameEnd = 0;
	p->Dropping = 0;
	BufferClear(&p->Buffer);
	if (p->Flush)
		p->Flush(p);
	return ERR_NONE;
}
Example #5
0
static void test_extract_scalar_prefix()
{
    Buffer *b = BufferNew();
    assert_int_equal(sizeof("hello ") - 1, ExtractScalarPrefix(b, "hello $(world) xy", sizeof("hello $(world) xy") -1));
    assert_string_equal("hello ", BufferData(b));

    BufferClear(b);
    assert_int_equal(sizeof("hello (world) xy") -1, ExtractScalarPrefix(b, "hello (world) xy", sizeof("hello (world) xy") -1));
    assert_string_equal("hello (world) xy", BufferData(b));

    BufferClear(b);
    assert_int_equal(sizeof("hello$)") -1, ExtractScalarPrefix(b, "hello$)$(world)xy", sizeof("hello$)$(world)xy") -1));
    assert_string_equal("hello$)", BufferData(b));

    BufferClear(b);
    assert_int_equal(0, ExtractScalarPrefix(b, "", 0));
    assert_string_equal("", BufferData(b));

    BufferDestroy(b);
}
Example #6
0
File: expand.c Project: awsiv/core
static PromiseResult ExpandPromiseAndDo(EvalContext *ctx, const Promise *pp,
                                        Rlist *lists, Rlist *containers,
                                        PromiseActuator *ActOnPromise, void *param)
{
    const char *handle = PromiseGetHandle(pp);

    EvalContextStackPushPromiseFrame(ctx, pp, true);

    PromiseIterator *iter_ctx = NULL;
    size_t i = 0;
    PromiseResult result = PROMISE_RESULT_NOOP;
    Buffer *expbuf = BufferNew();
    for (iter_ctx = PromiseIteratorNew(ctx, pp, lists, containers); PromiseIteratorHasMore(iter_ctx); i++, PromiseIteratorNext(iter_ctx))
    {
        if (handle)
        {
            // This ordering is necessary to get automated canonification
            BufferClear(expbuf);
            ExpandScalar(ctx, NULL, "this", handle, expbuf);
            CanonifyNameInPlace(BufferGet(expbuf));
            EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "handle", BufferData(expbuf), CF_DATA_TYPE_STRING, "source=promise");
        }
        else
        {
            EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "handle", PromiseID(pp), CF_DATA_TYPE_STRING, "source=promise");
        }

        const Promise *pexp = EvalContextStackPushPromiseIterationFrame(ctx, i, iter_ctx);
        if (!pexp)
        {
            // excluded
            result = PromiseResultUpdate(result, PROMISE_RESULT_SKIPPED);
            continue;
        }

        PromiseResult iteration_result = ActOnPromise(ctx, pexp, param);

        NotifyDependantPromises(ctx, pexp, iteration_result);
        result = PromiseResultUpdate(result, iteration_result);

        if (strcmp(pp->parent_promise_type->name, "vars") == 0 || strcmp(pp->parent_promise_type->name, "meta") == 0)
        {
            VerifyVarPromise(ctx, pexp, true);
        }

        EvalContextStackPopFrame(ctx);
    }

    BufferDestroy(expbuf);
    PromiseIteratorDestroy(iter_ctx);
    EvalContextStackPopFrame(ctx);

    return result;
}
// Try and decode a frame from com state
void CommunicationHandleFrame(CommunicationState *com) {
    int keep_decoding = 1;

    while(keep_decoding && !RingBufferIsEmpty(&com->uart_in_ringbuffer)) {
        BufferClear(&com->deframed_buffer);
        keep_decoding = afproto_ringbuffer_pop_frame(&com->uart_in_ringbuffer,
                                                     &com->deframed_buffer);

        if(com->deframed_buffer.used > 0)
            CommandHandleRaw(com->deframed_buffer.data,
                             com->deframed_buffer.used);
    }
}
Example #8
0
void ParserStream(parser* p, stream* Stream)
{
	p->Stream = Stream;
	if (Stream)
	{
		if (!p->Buffer.Data)
		{
			BufferAlloc(&p->Buffer,4096,1);
			BufferStream(&p->Buffer,p->Stream);
		}
	}
	else
		BufferClear(&p->Buffer);
}
Example #9
0
static int UpdateInput(tiff* p)
{
	p->ErrorShowed = 0;
	BufferClear(&p->Buffer);
	if (p->Codec.In.Format.Type == PACKET_VIDEO)
	{
		PacketFormatCopy(&p->Codec.Out.Format,&p->Codec.In.Format);
		p->Codec.Out.Format.Format.Video.Pixel.Flags = PF_RGB;
		p->Codec.Out.Format.Format.Video.Pixel.BitCount = 24;
		p->Codec.Out.Format.Format.Video.Pixel.BitMask[0] = 0xFF;
		p->Codec.Out.Format.Format.Video.Pixel.BitMask[1] = 0xFF00;
		p->Codec.Out.Format.Format.Video.Pixel.BitMask[2] = 0xFF0000;
		DefaultPitch(&p->Codec.Out.Format.Format.Video);
	}
	return ERR_NONE;
}
Example #10
0
static void test_extract_reference_(const char *scalar, bool expect_success, const char *outer, const char *inner)
{
    Buffer *b = BufferNew();
    size_t len = strlen(scalar);

    bool success = ExtractScalarReference(b, scalar, len, false);
    assert_true(success == expect_success);
    assert_string_equal(outer, BufferData(b));

    BufferClear(b);
    success = ExtractScalarReference(b, scalar, len, true);
    assert_true(success == expect_success);
    assert_string_equal(inner, BufferData(b));

    BufferDestroy(b);
}
Example #11
0
format_stream* Format_LoadSubTitle(format_base* p, stream* Input)
{
	buffer m = {NULL};
	BufferAlloc(&m,4096,1);
	BufferStream(&m,Input);

	//!SUBTITLE
	/*				
		p->SubTitle = (format_stream*) CAlloc(sizeof(format_stream),1);
		if (p->SubTitle)
		{
			*int* Format = (int*)Format_StreamFormat(p->SubTitle,PACKET_SUBTITLE,sizeof(int));
			if (Format)
				*Format = STRING_OEM;
			p->SubTitle->LastTime = -1;
		}
	*/

	BufferClear(&m);
	return NULL;
}
Example #12
0
static int UpdateInput(amrwb* p)
{
	if (p->Decoder)
	{
		D_IF_exit(p->Decoder);
		p->Decoder = NULL;
	}
	BufferClear(&p->Buffer);

	if (p->Codec.In.Format.Type == PACKET_AUDIO)
	{
	    p->Decoder = D_IF_init();
		if (!p->Decoder)
			return ERR_OUT_OF_MEMORY;

		p->Codec.In.Format.Format.Audio.SampleRate = 16000;
		p->Codec.In.Format.Format.Audio.Channels = 1;

		PacketFormatPCM(&p->Codec.Out.Format,&p->Codec.In.Format,16);
	}

	return ERR_NONE;
}
Example #13
0
/**
 @brief parse elements in a list passed through use_module
 
 @param[in] str: is the string to parse
 @param[out] newlist: rlist of elements found

 @retval 0: successful > 0: failed
 */
static int LaunchParsingMachine(const char *str, Rlist **newlist)
{
    const char *s = str;
    state current_state = ST_OPENED;
    int ret;

    Buffer *buf = BufferNewWithCapacity(CF_MAXVARSIZE);

    assert(newlist);

    while (current_state != ST_CLOSED && *s)
    {
        switch(current_state)
        {
            case ST_ERROR:
                Log(LOG_LEVEL_ERR, "Parsing error : Malformed string");
                ret = 1;
                goto clean;
            case ST_OPENED:
                if (CLASS_BLANK(*s))
                {
                    current_state = ST_OPENED;
                }
                else if (CLASS_BRA1(*s)) 
                {
                    current_state = ST_IO;
                }
                else if (CLASS_ANY0(*s))
                {
                    current_state = ST_ERROR;
                }
                s++;
                break;
            case ST_IO:
                if (CLASS_BLANK(*s))
                {
                    current_state = ST_IO;
                }
                else if (CLASS_START1(*s))
                {
                    BufferClear(buf);
                    current_state = ST_ELM1;
                }
                else if (CLASS_START2(*s))
                {
                    BufferClear(buf);
                    current_state = ST_ELM2;
                }
                else if (CLASS_ANY1(*s))
                {
                    current_state = ST_ERROR;
                }
                s++;
                break;
            case ST_ELM1:
                if (CLASS_END1(*s))
                {
                    RlistAppendScalar(newlist, BufferData(buf));
                    BufferClear(buf);
                    current_state = ST_END1;
                }
                else if (CLASS_ANY2(*s))
                {
                    BufferAppendChar(buf, *s);
                    current_state = ST_ELM1;
                }
                s++;
                break;
            case ST_ELM2:
                if (CLASS_END2(*s))
                {
                    RlistAppendScalar(newlist, BufferData(buf));
                    BufferClear(buf);
                    current_state = ST_END2;
                }
                else if (CLASS_ANY3(*s))
                {
                    BufferAppendChar(buf, *s);
                    current_state = ST_ELM2;
                }
                s++;
                break;
            case ST_END1:
                if (CLASS_SEP(*s))
                {
                    current_state = ST_SEP;
                }
                else if (CLASS_BRA2(*s))
                {
                    current_state = ST_PRECLOSED;
                }
                else if (CLASS_BLANK(*s))
                {
                    current_state = ST_END1;
                }
                else if (CLASS_ANY4(*s))
                {
                    current_state = ST_ERROR;
                }
                s++;
                break;
            case ST_END2:
                if (CLASS_SEP(*s))
                {
                    current_state = ST_SEP;
                }
                else if (CLASS_BRA2(*s))
                {
                    current_state = ST_PRECLOSED;
                }
                else if (CLASS_BLANK(*s))
                {
                    current_state = ST_END2;
                }
                else if (CLASS_ANY5(*s))
                {
                    current_state = ST_ERROR;
                }
                s++;
                break;
            case ST_SEP:
                if (CLASS_BLANK(*s))
                {
                    current_state = ST_SEP;
                }
                else if (CLASS_START1(*s))
                {
                    current_state = ST_ELM1;
                }
                else if (CLASS_START2(*s))
                {
                    current_state = ST_ELM2;
                }
                else if (CLASS_ANY6(*s))
                {
                    current_state = ST_ERROR;
                }
                s++;
                break;
            case ST_PRECLOSED:
                if (CLASS_BLANK(*s))
                {
                    current_state = ST_PRECLOSED;
                }
                else if (CLASS_EOL(*s))
                {
                    current_state = ST_CLOSED;
                }
                else if (CLASS_ANY7(*s))
                {
                    current_state = ST_ERROR;
                }
                s++;
                break;
            default:
                Log(LOG_LEVEL_ERR, "Parsing logic error: unknown state");
                ret = 2;
                goto clean;
                break;
        }
    }

    if (current_state != ST_CLOSED && current_state != ST_PRECLOSED )
    {
        Log(LOG_LEVEL_ERR, "Parsing error : Malformed string (unexpected end of input)");
        ret = 3;
        goto clean;
    }

    BufferDestroy(buf);
    return 0;

clean:
    BufferDestroy(buf);
    RlistDestroy(*newlist);
    assert(ret != 0);
    return ret;
}
Example #14
0
File: expand.c Project: awsiv/core
bool ExpandScalar(const EvalContext *ctx,
                  const char *ns, const char *scope, const char *string,
                  Buffer *out)
{
    assert(string);
    if (strlen(string) == 0)
    {
        return true;
    }

    bool fully_expanded = true;

    Buffer *current_item = BufferNew();
    for (const char *sp = string; *sp != '\0'; sp++)
    {
        BufferClear(current_item);
        ExtractScalarPrefix(current_item, sp, strlen(sp));

        BufferAppend(out, BufferData(current_item), BufferSize(current_item));
        sp += BufferSize(current_item);
        if (*sp == '\0')
        {
            break;
        }

        BufferClear(current_item);
        char varstring = sp[1];
        ExtractScalarReference(current_item,  sp, strlen(sp), true);
        sp += BufferSize(current_item) + 2;

        if (IsCf3VarString(BufferData(current_item)))
        {
            Buffer *temp = BufferCopy(current_item);
            BufferClear(current_item);
            ExpandScalar(ctx, ns, scope, BufferData(temp), current_item);
            BufferDestroy(temp);
        }

        if (!IsExpandable(BufferData(current_item)))
        {
            DataType type = CF_DATA_TYPE_NONE;
            const void *value = NULL;
            {
                VarRef *ref = VarRefParseFromNamespaceAndScope(BufferData(current_item), ns, scope, CF_NS, '.');
                value = EvalContextVariableGet(ctx, ref, &type);
                VarRefDestroy(ref);
            }

            if (value)
            {
                switch (DataTypeToRvalType(type))
                {
                case RVAL_TYPE_SCALAR:
                    BufferAppendString(out, value);
                    continue;

                case RVAL_TYPE_CONTAINER:
                    if (JsonGetElementType((JsonElement*)value) == JSON_ELEMENT_TYPE_PRIMITIVE)
                    {
                        BufferAppendString(out, JsonPrimitiveGetAsString((JsonElement*)value));
                        continue;
                    }
                    break;

                default:
                    break;
                }
            }
        }

        if (varstring == '{')
        {
            BufferAppendF(out, "${%s}", BufferData(current_item));
        }
        else
        {
            BufferAppendF(out, "$(%s)", BufferData(current_item));
        }
    }

    BufferDestroy(current_item);

    return fully_expanded;
}
Example #15
0
static int UpdateInput( ffmpeg_video* p )
{
	if (p->Context)
		avcodec_close(p->Context);
    av_free(p->Context);
    av_free(p->Picture);
	p->Context = NULL;
	p->Picture = NULL;
	BufferClear(&p->Buffer);

	if (p->Codec.In.Format.Type == PACKET_VIDEO)
	{
	    AVCodec *Codec;

		const codecinfo *i;
		for (i=Info;i->Id;++i)
			if (i->Id == p->Codec.Node.Class)
				break;
		if (!i->Id)
			return ERR_INVALID_DATA;
		
		Codec = avcodec_find_decoder(i->CodecId);
		if (!Codec)
			return ERR_INVALID_DATA;

		p->Context = avcodec_alloc_context();
		p->Picture = avcodec_alloc_frame();

		if (!p->Context || !p->Picture)
			return ERR_OUT_OF_MEMORY;

	    if ((p->Codec.In.Format.Format.Video.Pixel.Flags & PF_FRAGMENTED) && 
			(Codec->capabilities & CODEC_CAP_TRUNCATED))
			p->Context->flags|= CODEC_FLAG_TRUNCATED;

		UpdateSettings(p);
		p->Context->palctrl = NULL;
	    p->Context->bit_rate = 0;
		p->Context->extradata = p->Codec.In.Format.Extra;
		p->Context->extradata_size = p->Codec.In.Format.ExtraLength;
		p->Context->width = p->Codec.In.Format.Format.Video.Width;
		p->Context->height = p->Codec.In.Format.Format.Video.Height;
		p->Context->bits_per_coded_sample = p->Codec.In.Format.Format.Video.Pixel.BitCount;
		if (p->Codec.In.Format.Format.Video.Pixel.Palette && 
			p->Codec.In.Format.Format.Video.Pixel.BitCount<=8)
		{
			int i,n = 1 << p->Codec.In.Format.Format.Video.Pixel.BitCount;
			for (i=0;i<n;++i)
				p->Palette.palette[i] = INT32LE(p->Codec.In.Format.Format.Video.Pixel.Palette[i].v);
			p->Palette.palette_changed = 1;
			p->Context->palctrl = &p->Palette;
		}

		p->CodecId = i->CodecId;

	    if (avcodec_open(p->Context,Codec)<0)
		{
			// avoid calling avcodec_close at next UpdateInput
		    av_free(p->Context);
			p->Context = NULL;
			return ERR_INVALID_DATA;
		}

		if (!BuildOutputFormat(p))
			return ERR_INVALID_DATA;

		p->SkipToKey = 1;
		p->DropToKey = 1;
		p->Dropping = 0;
	}

	return ERR_NONE;
}
Example #16
0
/**
 * Expand a #string into Buffer #out, returning the pointer to the string
 * itself, inside the Buffer #out. If #out is NULL then the buffer will be
 * created and destroyed internally.
 *
 * @retval NULL something went wrong
 */
char *ExpandScalar(const EvalContext *ctx, const char *ns, const char *scope,
                   const char *string, Buffer *out)
{
    bool out_belongs_to_us = false;

    if (out == NULL)
    {
        out               = BufferNew();
        out_belongs_to_us = true;
    }

    assert(string != NULL);
    assert(out != NULL);
    Buffer *current_item = BufferNew();

    for (const char *sp = string; *sp != '\0'; sp++)
    {
        BufferClear(current_item);
        ExtractScalarPrefix(current_item, sp, strlen(sp));

        BufferAppend(out, BufferData(current_item), BufferSize(current_item));
        sp += BufferSize(current_item);
        if (*sp == '\0')
        {
            break;
        }

        BufferClear(current_item);
        char varstring = sp[1];
        ExtractScalarReference(current_item,  sp, strlen(sp), true);
        sp += BufferSize(current_item) + 2;

        if (IsCf3VarString(BufferData(current_item)))
        {
            Buffer *temp = BufferCopy(current_item);
            BufferClear(current_item);
            ExpandScalar(ctx, ns, scope, BufferData(temp), current_item);
            BufferDestroy(temp);
        }

        if (!IsExpandable(BufferData(current_item)))
        {
            VarRef *ref = VarRefParseFromNamespaceAndScope(
                BufferData(current_item),
                ns, scope, CF_NS, '.');
            DataType value_type;
            const void *value = EvalContextVariableGet(ctx, ref, &value_type);
            VarRefDestroy(ref);

            switch (DataTypeToRvalType(value_type))
            {
            case RVAL_TYPE_SCALAR:
                assert(value != NULL);
                BufferAppendString(out, value);
                continue;
                break;

            case RVAL_TYPE_CONTAINER:
            {
                assert(value != NULL);
                const JsonElement *jvalue = value;      /* instead of casts */
                if (JsonGetElementType(jvalue) == JSON_ELEMENT_TYPE_PRIMITIVE)
                {
                    BufferAppendString(out, JsonPrimitiveGetAsString(jvalue));
                    continue;
                }
                break;
            }
            default:
                /* TODO Log() */
                break;
            }
        }

        if (varstring == '{')
        {
            BufferAppendF(out, "${%s}", BufferData(current_item));
        }
        else
        {
            BufferAppendF(out, "$(%s)", BufferData(current_item));
        }
    }

    BufferDestroy(current_item);

    LogDebug(LOG_MOD_EXPAND, "ExpandScalar( %s : %s . %s )  =>  %s",
             SAFENULL(ns), SAFENULL(scope), string, BufferData(out));

    return out_belongs_to_us ? BufferClose(out) : BufferGet(out);
}
Example #17
0
void KDBuildJob::KDBuild(int maxleafsize, int extradepth, bool root)
{
	kdbuffer_t l_kdb;
	kdbuffer_t r_kdb;

	int b = 0;

	if(BufferEmpty(kdbuffer[b]))
	{
		for(int i=0; i < njobs; i++)
			sem_post(&jobs[i]->sem);
	}

	// Make nodes
	while(! BufferEmpty(kdbuffer[b]) )
	{

		kdbuffer_t *kdb = (kdbuffer_t*)kdbuffer[b]->buffer;
		int size = BufferNumElements(kdbuffer[b]);
		int i;
		
		BufferClear(aabb_buffer[1-b]);
		BufferClear(kdbuffer[1-b]);

		for(i=0; i < size; i++)
		{
			l_kdb.node = curnode++;
			r_kdb.node = curnode++;		

			nodes[ kdb[i].node ].split = kdb[i].plane;
			nodes[ kdb[i].node ].axis =  kdb[i].axis;
			nodes[ kdb[i].node ].left =  l_kdb.node;
			nodes[ kdb[i].node ].right = r_kdb.node;				

			KDBufferAllocate(&l_kdb, kdb[i].left_size, aabb_buffer[1-b]);

			if(curjob < njobs)
				KDBufferAllocate(&r_kdb, kdb[i].right_size, jobs[curjob]->aabb_buffer[0]);
			else
				KDBufferAllocate(&r_kdb, kdb[i].right_size, aabb_buffer[1-b]);

			KDPartition(&kdb[i], &l_kdb, &r_kdb);

			if(l_kdb.depth == maxdepth || l_kdb.size <= maxleafsize)
			{
				l_kdb.aabb = (aabb_t*)BufferCopyTo(leaf_aabb_buffer, l_kdb.aabb, l_kdb.count);
				BufferCopyTo(leafbuffer, &l_kdb, 1);
			}			
			else
				BufferCopyTo(kdbuffer[1-b], &l_kdb, 1);

			if(r_kdb.depth == maxdepth || r_kdb.size <= maxleafsize)
			{
				if(curjob < njobs)
				{
					r_kdb.aabb = (aabb_t*)BufferCopyTo(jobs[curjob]->leaf_aabb_buffer, r_kdb.aabb, r_kdb.count);
					BufferCopyTo(jobs[curjob]->leafbuffer, &r_kdb, 1);
				}
				else
				{
					r_kdb.aabb = (aabb_t*)BufferCopyTo(leaf_aabb_buffer, r_kdb.aabb, r_kdb.count);
					BufferCopyTo(leafbuffer, &r_kdb, 1);
				}
			}
			else
			{
				if(curjob < njobs)
					BufferCopyTo(jobs[curjob]->kdbuffer[0], &r_kdb, 1);
				else
					BufferCopyTo(kdbuffer[1-b], &r_kdb, 1);
			}
	
		}
	
		if(curjob < njobs)
		{
			// Start other job
			sem_post(&jobs[curjob]->sem);
			curjob++;
		}

		b =  1 - b;

	}

	// Check if still remaining jobs and start them
	while(curjob < njobs)
	{
		// Start other job
		sem_post(&jobs[curjob]->sem);
		curjob++;
	}

	
	// Make leaves
	kdbuffer_t *kdb = (kdbuffer_t*)leafbuffer->buffer;
	int size = BufferNumElements(leafbuffer);

	//cout << "Make leaves " << size << endl;

	int i;

	for(i=0; i < size; i++)
	{
		MakeLeaf(&kdb[i]);
	}

	numleaves = size;
	curleaf = size;
}
Example #18
0
static void test_isipaddress(void)
{
    /*
     * This test is just a summary of the other tests.
     * We just check that this interface works accordingly, most of the
     * functionality has already been tested.
     * 1.2.3.4         -> ok
     * 1.2..3          -> not
     * 1.a.2.3         -> not
     * 256.255.255.255 -> not
     * 255.255.255.255 -> ok
     * 1:0:0:0:0:0:0:1 -> ok
     * 1:1:1:1:0:1:1:1 -> ok
     * a:b:c:d:e:f:0:1 -> ok
     * a:b:c:d:e:f:g:h -> not
     * ffff:ffff:fffff:0:0:0:0:1 -> not
     */
    IPAddress *address = NULL;
    Buffer *bufferAddress = NULL;

    bufferAddress = BufferNew();
    assert_true (bufferAddress != NULL);

    BufferSet(bufferAddress, "1.2.3.4", strlen("1.2.3.4"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1.2..3", strlen("1.2..3"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "1.a.2.3", strlen("1.a.2.3"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "256.255.255.255", strlen("256.255.255.255"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address == NULL);
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "255.255.255.255", strlen("255.255.255.255"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV4);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1:0:0:0:0:0:0:1", strlen("1:0:0:0:0:0:0:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "1:1:1:1:0:1:1:1", strlen("1:1:1:1:0:1:1:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "a:b:c:d:e:f:0:1", strlen("a:b:c:d:e:f:0:1"));
    assert_true(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_true(IPAddressIsIPAddress(bufferAddress, &address));
    assert_true(address != NULL);
    assert_int_equal(IPAddressType(address), IP_ADDRESS_TYPE_IPV6);
    BufferClear(bufferAddress);
    assert_int_equal(IPAddressDestroy(&address), 0);

    BufferSet(bufferAddress, "a:b:c:d:e:f:g:h", strlen("a:b:c:d:e:f:g:h"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    BufferClear(bufferAddress);

    BufferSet(bufferAddress, "ffff:ffff:fffff:0:0:0:0:1", strlen("ffff:ffff:fffff:0:0:0:0:1"));
    assert_false(IPAddressIsIPAddress(bufferAddress, NULL));
    assert_false(IPAddressIsIPAddress(bufferAddress, &address));
    BufferClear(bufferAddress);
}
Example #19
0
File: expand.c Project: awsiv/core
static void ExpandAndMapIteratorsFromScalar(EvalContext *ctx,
                                            const Bundle *bundle,
                                            char *string, size_t length,
                                            int level,
                                            Rlist **scalars, Rlist **lists,
                                            Rlist **containers,
                                            Rlist **full_expansion)
{
    assert(string);
    if (!string)
    {
        return;
    }

    Buffer *value = BufferNew();

    for (size_t i = 0; i < length; i++)
    {
        const char *sp = string + i;

        Rlist *tmp_list = NULL;
        BufferClear(value);
        if (ExtractScalarPrefix(value, sp, length - i))
        {
            if (full_expansion)
            {
                RlistConcatInto(&tmp_list, *full_expansion, BufferData(value));
                RlistDestroy(*full_expansion);
                *full_expansion = tmp_list;
                tmp_list = NULL;
            }

            sp += BufferSize(value);
            i += BufferSize(value);

            BufferClear(value);

            if (i >= length)
            {
                break;
            }
        }

        if (*sp == '$')
        {
            BufferClear(value);
            ExtractScalarReference(value, sp, length - i, true);
            if (BufferSize(value) > 0)
            {
                Rlist *inner_expansion = NULL;
                Rlist *exp = NULL;
                int success = 0;

                VarRef *ref = VarRefParse(BufferData(value));

                int increment = BufferSize(value) - 1 + 3;

                // Handle any embedded variables
                char *substring = string + i + 2;
                ExpandAndMapIteratorsFromScalar(ctx, bundle, substring, BufferSize(value), level+1, scalars, lists, containers, &inner_expansion);

                for (exp = inner_expansion; exp != NULL; exp = exp->next)
                {
                    // If a list is non-local, i.e. $(bundle.var), map it to local $(bundle#var)

                    // NB without modifying variables as we map them, it's not
                    // possible to handle remote lists referenced by a variable
                    // scope. For example:
                    //  scope => "test."; var => "somelist"; $($(scope)$(var)) fails
                    //  varname => "test.somelist"; $($(varname)) also fails
                    // TODO Unless the consumer handles it?

                    const char *inner_ref_str = RlistScalarValue(exp);
                    VarRef *inner_ref = VarRefParseFromBundle(inner_ref_str, bundle);

                    // var is the expanded name of the variable in its native context
                    // finalname will be the mapped name in the local context "this."

                    DataType value_type = CF_DATA_TYPE_NONE;
                    const void *value = EvalContextVariableGet(ctx, inner_ref, &value_type);
                    if (value)
                    {
                        char *mangled_inner_ref = xstrdup(inner_ref_str);
                        MangleVarRefString(mangled_inner_ref, strlen(mangled_inner_ref));

                        success++;
                        switch (DataTypeToRvalType(value_type))
                        {
                        case RVAL_TYPE_LIST:
                            if (level > 0)
                            {
                                RlistPrependScalarIdemp(lists, mangled_inner_ref);
                            }
                            else
                            {
                                RlistAppendScalarIdemp(lists, mangled_inner_ref);
                            }

                            if (full_expansion)
                            {
                                for (const Rlist *rp = value; rp != NULL; rp = rp->next)
                                {
                                    // append each slist item to each of full_expansion
                                    RlistConcatInto(&tmp_list, *full_expansion, RlistScalarValue(rp));
                                }
                            }
                            break;

                        case RVAL_TYPE_SCALAR:
                            RlistAppendScalarIdemp(scalars, mangled_inner_ref);

                            if (full_expansion)
                            {
                                // append the scalar value to each of full_expansion
                                RlistConcatInto(&tmp_list, *full_expansion, value);
                            }
                            break;

                        case RVAL_TYPE_CONTAINER:
                            if (level > 0)
                            {
                                RlistPrependScalarIdemp(containers, mangled_inner_ref);
                            }
                            else
                            {
                                RlistAppendScalarIdemp(containers, mangled_inner_ref);
                            }
                            break;

                        case RVAL_TYPE_FNCALL:
                        case RVAL_TYPE_NOPROMISEE:
                            break;
                        }

                        free(mangled_inner_ref);
                    }

                    VarRefDestroy(inner_ref);
                }
                RlistDestroy(inner_expansion);

                if (full_expansion)
                {
                    RlistDestroy(*full_expansion);
                    *full_expansion = tmp_list;
                    tmp_list = NULL;
                }

                // No need to map this.* even though it's technically qualified
                if (success && IsQualifiedVariable(BufferData(value)) && strcmp(ref->scope, "this") != 0)
                {
                    char *dotpos = strchr(substring, '.');
                    if (dotpos)
                    {
                        *dotpos = CF_MAPPEDLIST;    // replace '.' with '#'
                    }

                    if (strchr(BufferData(value), ':'))
                    {
                        char *colonpos = strchr(substring, ':');
                        if (colonpos)
                        {
                            *colonpos = '*';
                        }
                    }
                }

                VarRefDestroy(ref);

                sp += increment;
                i += increment;
            }
        }
    }

    BufferDestroy(value);
}
Example #20
0
int main(void)
{

	byte bCount;
	long lTemp;

	System_Configuration();

	dxl_set_power(OFF);
	//dxl_set_power(ON);
	Zigbee_SetState(ON);
	//gbDxlPwr = ON;

	//LED_SetState(LED_POWER, ON);
	//	LED_RGB_SetState(LED_R|LED_G|LED_B);
	//LED_RGB_SetState(OFF);

	BufferClear(USART_DXL);
	BufferClear(USART_PC);
	BufferClear(USART_ZIGBEE);

	setBuzzerOff();

/*
	for(bCount =0; bCount < 50; bCount++ )
	{
		setBuzzerData(bCount);
		setBuzzerPlayLength(10);
		PlayBuzzer();
		//mDelay(3000);
		while(getBuzzerState());
		mDelay(500);
	}
*/


/*
	//BKP_WriteBackupRegister((P_OPERATING_MODE+1)<<2, 0xffff);

	  if(BKP_ReadBackupRegister((P_OPERATING_MODE+1)<<2) == 0xffff) //Initialize to Factory Default or reset mode restart
	  {
	    for(bCount=0; bCount < ROM_CONTROL_TABLE_LEN; bCount++)
	    {
	        //ROM_CAST(bCount) = gbpControlTable[bCount] = ROM_INITIAL_DATA[bCount];
	    	gbpControlTable[bCount] = ROM_INITIAL_DATA[bCount];
	        BKP_WriteBackupRegister((bCount+1)<<2, WORD_CAST(gbpControlTable[bCount]));
	    }
	    gbLEDBlinkCounter = 32;
	  }
	  else
	  {
	    for(bCount=0; bCount < ROM_CONTROL_TABLE_LEN; bCount++)
	    {
	      //gbpControlTable[bCount] = (byte)(BKP_ReadBackupRegister((bCount+1)<<2)); !!!!!
	      gbpControlTable[bCount] = ROM_INITIAL_DATA[bCount];
	    }
	    gbLEDBlinkCounter = 8;
	  }

	for (bCount = 0; bCount < 3; bCount++) {
		LED_SetState(LED_MANAGE|LED_EDIT|LED_PLAY, ON);
		mDelay(50);
		LED_SetState(LED_MANAGE|LED_EDIT|LED_PLAY, OFF);
		mDelay(50);
	}
*/
	/*
	if( (EEPROM_Read(P_OPERATING_MODE) == 0xffff) || (EEPROM_Read(P_OPERATING_MODE) == 0xff) ) //Initialize to Factory Default or reset mode restart
	{
		EEPROM_Write( P_BAUD_RATE, ROM_INITIAL_DATA[P_BAUD_RATE] );
		EEPROM_Write( P_OPERATING_MODE, 0);
	}
	else if(EEPROM_Read(P_OPERATING_MODE) == 0x11) //Digital Reset
	{
		EEPROM_Write( P_BAUD_RATE, ROM_INITIAL_DATA[P_BAUD_RATE] );
		EEPROM_Write( P_OPERATING_MODE, 0);
	}
	*/


	for(bCount=0; bCount < ROM_CONTROL_TABLE_LEN; bCount++)
	{
		  gbpControlTable[bCount] = ROM_INITIAL_DATA[bCount];
	}
	//GB_BAUD_RATE = EEPROM_Read(P_BAUD_RATE);
/*
	lTemp = 2000000;
	lTemp /= (GB_BAUD_RATE+1);
	USART_Configuration(USART_DXL,lTemp);
	USART_Configuration(USART_PC,lTemp);
*/


    gbLEDBlinkCounter = 8;





	for (bCount = 0; bCount < 3; bCount++) {
		LED_SetState(LED_MANAGE|LED_EDIT|LED_PLAY, ON);
		mDelay(50);
		LED_SetState(LED_MANAGE|LED_EDIT|LED_PLAY, OFF);
		mDelay(50);
	}
/*
	dxl_set_power(1);
	gbDxlPwr = 1;
	mDelay(100);

	GPIO_ResetBits(PORT_ENABLE_RXD, PIN_ENABLE_RXD);	// RX Disable
	GPIO_SetBits(PORT_ENABLE_TXD, PIN_ENABLE_TXD);	// TX Enable
	  while(1)
	  {
		GPIO_ResetBits(PORT_ENABLE_RXD, PIN_ENABLE_RXD);	// RX Disable
		GPIO_SetBits(PORT_ENABLE_TXD, PIN_ENABLE_TXD);	// TX Enable

		  USART_SendData(USART1, 'a');
			while( USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET );

			GPIO_ResetBits(PORT_ENABLE_TXD, PIN_ENABLE_TXD);	// TX Disable
			GPIO_SetBits(PORT_ENABLE_RXD, PIN_ENABLE_RXD);	// RX Enable
			mDelay(100);

	  }

*/

	Process();

	while(1);

}
Example #21
0
void MakeNodes()
{
	uint put_tag[2];

	put_tag[0] = mfc_tag_reserve();
	put_tag[1] = mfc_tag_reserve();

	ushort b = 0;

	kdbuffer_t l_kdb ALIGNED(16);
	kdbuffer_t r_kdb ALIGNED(16);

	kdnode_t node ALIGNED(16);
	kdbuffer_t kdb	ALIGNED(16);
	DoubleBufInit(&aabb_db, 0, 0, sizeof(aabb_t), NUM_AABBS, aabbbuffer[0], aabbbuffer[1]);

	// printf("Empty? %i\n", BufferEmpty(&arg.kdbuffer[b]));

	while(! BufferEmpty(&arg.kdbuffer[b]) )
	{
		kdbuffer_t *pkdb = (kdbuffer_t*)arg.kdbuffer[b].buffer;
		int size = BufferNumElements(&arg.kdbuffer[b]);
		int i;
		
		BufferClear(&arg.aabb_buffer[1-b]);
		BufferClear(&arg.kdbuffer[1-b]);
	
		// printf("size %i\n", size);

		for(i=0; i < size; i++)
		{
			l_kdb.node = arg.curnode++;
			r_kdb.node = arg.curnode++;		

			memcpy_ls(&kdb, &pkdb[i], sizeof(kdbuffer_t));

			node.split = kdb.plane;
			node.axis =  kdb.axis;
			node.left =  l_kdb.node;
			node.right = r_kdb.node;	


			memcpy_ea(&arg.nodes[ kdb.node ], &node, sizeof(kdnode_t));


			KDBufferAllocate(&l_kdb, kdb.left_size, &arg.aabb_buffer[1-b]);

			if(curjob < arg.njobs)
				KDBufferAllocate(&r_kdb, kdb.right_size, &arg.job_aabb_buffer[curjob]);
			else
				KDBufferAllocate(&r_kdb, kdb.right_size, &arg.aabb_buffer[1-b]);

			KDPartitionAll(&kdb, &l_kdb, &r_kdb);

			if(l_kdb.depth == arg.maxdepth || l_kdb.size <= arg.maxleafsize)
			{
				total_leaf_size += l_kdb.count;

				l_kdb.aabb = (aabb_t*)BufferCopyTo(&arg.leaf_aabb_buffer, l_kdb.aabb, l_kdb.count);
				BufferCopyToLS(&arg.leafbuffer, &l_kdb, 1);
			}			
			else
			{	
				BufferCopyToLS(&arg.kdbuffer[1-b], &l_kdb, 1);
			}

			if(r_kdb.depth == arg.maxdepth || r_kdb.size <= arg.maxleafsize)
			{
				total_leaf_size += r_kdb.count;
		
				if(curjob < arg.njobs)
				{
					r_kdb.aabb = (aabb_t*)BufferCopyTo(&arg.job_leaf_aabb_buffer[curjob], r_kdb.aabb, r_kdb.count);
					BufferCopyToLS(&arg.job_leafbuffer[curjob], &r_kdb, 1);

					spu_mfcdma32(&arg.job_leafbuffer[curjob], (uint)arg.pjob_leafbuffer[curjob], sizeof(buffer_t), jobtag, MFC_PUT_CMD);					
					DmaWait(jobtag);
		
				}
				else
				{
					r_kdb.aabb = (aabb_t*)BufferCopyTo(&arg.leaf_aabb_buffer, r_kdb.aabb, r_kdb.count);
					BufferCopyToLS(&arg.leafbuffer, &r_kdb, 1);

				}

			}
			else
			{
				if(curjob < arg.njobs)
				{
					BufferCopyToLS(&arg.job_kdbuffer[curjob], &r_kdb, 1);

					spu_mfcdma32(&arg.job_kdbuffer[curjob], (uint)arg.pjob_kdbuffer[curjob], sizeof(buffer_t), jobtag, MFC_PUT_CMD);					
					DmaWait(jobtag);

				}
				else
					BufferCopyToLS(&arg.kdbuffer[1-b], &r_kdb, 1);
			}


			/*
			if(curjob < njobs)
				KDBufferAllocate(&r_kdb, kdb[i].right_size, &jobs[curjob]->aabb_buffer[0]);
			else
				KDBufferAllocate(&r_kdb, kdb[i].right_size, &aabb_buffer[1-b]);

			KDPartition(&kdb[i], &l_kdb, &r_kdb);

			if(l_kdb.depth == maxdepth || l_kdb.size <= maxleafsize)
			{
				l_kdb.aabb = (aabb_t*)BufferCopyTo(&leaf_aabb_buffer, l_kdb.aabb, l_kdb.count);
				BufferCopyTo(&leafbuffer, &l_kdb, 1);
			}			
			else
				BufferCopyTo(&kdbuffer[1-b], &l_kdb, 1);

			if(r_kdb.depth == maxdepth || r_kdb.size <= maxleafsize)
			{
				if(curjob < njobs)
				{
					r_kdb.aabb = (aabb_t*)BufferCopyTo(&jobs[curjob]->leaf_aabb_buffer, r_kdb.aabb, r_kdb.count);
					BufferCopyTo(&jobs[curjob]->leafbuffer, &r_kdb, 1);
				}
				else
				{
					r_kdb.aabb = (aabb_t*)BufferCopyTo(&leaf_aabb_buffer, r_kdb.aabb, r_kdb.count);
					BufferCopyTo(&leafbuffer, &r_kdb, 1);
				}
			}
			else
			{
				if(curjob < njobs)
					BufferCopyTo(&jobs[curjob]->kdbuffer[0], &r_kdb, 1);
				else
					BufferCopyTo(&kdbuffer[1-b], &r_kdb, 1);
			}
			*/



			if(curjob < arg.njobs)
			{
				// Start other job
				
				ppe_post_sema(arg.sema[curjob]);
				curjob++;
			}


	
		}
	
	
		b =  1 - b;

	}

	while( curjob < arg.njobs)
	{
		ppe_post_sema(arg.sema[curjob]);
		curjob++;
	}



	// Transfer back
	spu_mfcdma32(&arg.curnode, (unsigned int)arg.pcurnode, (unsigned int)sizeof(int), put_tag[0], MFC_PUT_CMD);
	spu_mfcdma32(&total_leaf_size, (unsigned int)arg.ptotal_leaf_size, (unsigned int)sizeof(int), put_tag[1], MFC_PUT_CMD);
	

	DmaWait(put_tag[0]);
	DmaWait(put_tag[1]);


	spu_mfcdma32(&arg.leafbuffer, (unsigned int)arg.pleafbuffer, (unsigned int)sizeof(buffer_t), put_tag[0], MFC_PUT_CMD);
	spu_mfcdma32(&arg.leaf_aabb_buffer, (unsigned int)arg.pleaf_aabb_buffer, (unsigned int)sizeof(buffer_t), put_tag[1], MFC_PUT_CMD);

	DmaWaitAll();

	mfc_tag_release(put_tag[0]);
	mfc_tag_release(put_tag[1]);

}
Example #22
0
int LoadFileAsItemList(Item **liststart, const char *file, EditDefaults edits)
{
    {
        struct stat statbuf;
        if (stat(file, &statbuf) == -1)
        {
            Log(LOG_LEVEL_VERBOSE, "The proposed file '%s' could not be loaded. (stat: %s)", file, GetErrorStr());
            return false;
        }

        if (edits.maxfilesize != 0 && statbuf.st_size > edits.maxfilesize)
        {
            Log(LOG_LEVEL_INFO, "File '%s' is bigger than the edit limit. max_file_size = %jd > %d bytes", file,
                  (intmax_t) statbuf.st_size, edits.maxfilesize);
            return (false);
        }

        if (!S_ISREG(statbuf.st_mode))
        {
            Log(LOG_LEVEL_INFO, "%s is not a plain file", file);
            return false;
        }
    }

    FILE *fp = safe_fopen(file, "rt");
    if (!fp)
    {
        Log(LOG_LEVEL_INFO, "Couldn't read file '%s' for editing. (fopen: %s)", file, GetErrorStr());
        return false;
    }

    Buffer *concat = BufferNew();

    size_t line_size = CF_BUFSIZE;
    char *line = xmalloc(line_size);
    bool result = true;

    for (;;)
    {
        ssize_t num_read = CfReadLine(&line, &line_size, fp);
        if (num_read == -1)
        {
            if (!feof(fp))
            {
                Log(LOG_LEVEL_ERR,
                    "Unable to read contents of '%s'. (fread: %s)",
                    file, GetErrorStr());
                result = false;
            }
            break;
        }

        if (edits.joinlines && *(line + strlen(line) - 1) == '\\')
        {
            *(line + strlen(line) - 1) = '\0';

            BufferAppend(concat, line, num_read);
        }
        else
        {
            BufferAppend(concat, line, num_read);
            if (!feof(fp) || (BufferSize(concat) > 0))
            {
                AppendItem(liststart, BufferData(concat), NULL);
            }
        }

        BufferClear(concat);
    }

    free(line);
    BufferDestroy(concat);
    fclose(fp);
    return result;
}