Beispiel #1
0
int main(int argc, char **argv)
{
	unsigned result = invert(atob(argv[1]), atoi(argv[2]), atoi(argv[3]));
	printf("result = \n\t");
	show_bits(result);
	putchar('\n');
	return 0;
}
Beispiel #2
0
double 
atof(char *p)
{
    double          d, t;
    int             len, val, sz, div, isneg;
    char            tmp[18];

    if (*p == '-') {
	isneg = 1;
	p++;
    } else
	isneg = 0;

    sz = strcspn (p, ".");
    if (sz > 0) {
	strncpy (tmp, p, sz);
	tmp[sz] = 0;
	if (!atob (&val, tmp, 10))
	    return (d);
    } else
	val = 0;

    d = (double)val;
    p += sz;
    if (*p)
	p++;
    if (*p) {
	len = strlen (p);
	if (!atob (&val, p, 10))
	    return (0);

	div = 1;
	for (; len > 0; len--)
	    div *= 10;

	t = (double)val;
	t /= div;

	d += t;
    }
    if (isneg)
	d = 0 - d;
    return (d);
}
Beispiel #3
0
int main(void)
{
        char a[MAX];
        int x, b;
        scanf("%d %d", &x, &b);
        atob(x, a, b);
        reverse(a);
        printf("%s", a);
        return(0);
}
Beispiel #4
0
/*
 *  int vsprintf(d,s,ap)
 */
int 
vsprintf (char *d, const char *s, va_list ap)
{
    const char *t;
    char *p, *dst, tmp[40];
    unsigned int n;
    int fmt, trunc, haddot, width, base, longlong;
#ifdef FLOATINGPT
    double dbl;

#ifndef NEWFP
    EP ex;
#endif
#endif

    dst = d;
    for (; *s;) {
        if (*s == '%') {
            s++;
            fmt = FMT_RJUST;
            width = trunc = haddot = longlong = 0;
            for (; *s; s++) {
                if (strchr("bcdefgilopPrRsuxX%", *s))
                    break;
                else if (*s == '-')
                    fmt = FMT_LJUST;
                else if (*s == '0')
                    fmt = FMT_RJUST0;
                else if (*s == '~')
                    fmt = FMT_CENTER;
                else if (*s == '*') {
                    if (haddot)
                        trunc = va_arg(ap, int);
                    else
                        width = va_arg(ap, int);
                } else if (*s >= '1' && *s <= '9') {
                    for (t = s; isdigit(*s); s++);
                    strncpy(tmp, t, s - t);
                    tmp[s - t] = '\0';
                    atob(&n, tmp, 10);
                    if (haddot)
                        trunc = n;
                    else
                        width = n;
                    s--;
                } else if (*s == '.')
                    haddot = 1;
            }
Beispiel #5
0
/*
 * traverse_animation_attributes()
 * Animation attributes traversal
 */
int traverse_animation_attributes(const parsetree_statement_t *stmt, void *animation)
{
    const char *identifier;
    const parsetree_parameter_t *param_list;
    const parsetree_parameter_t *p1, *pj;
    animation_t *anim = (animation_t*)animation;
    int j;

    identifier = nanoparser_get_identifier(stmt);
    param_list = nanoparser_get_parameter_list(stmt);

    if(str_icmp(identifier, "repeat") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_string(p1, "repeat flag must be a boolean (true or false)");
        anim->repeat = atob(nanoparser_get_string(p1));
    }
    else if(str_icmp(identifier, "fps") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_string(p1, "fps must be a positive number");
        anim->fps = max(1e-5, atof(nanoparser_get_string(p1)));
    }
    else if(str_icmp(identifier, "repeat_from") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_string(p1, "repeat_from must be a non-negative number");
        anim->repeat_from = atoi(nanoparser_get_string(p1));
    }
    else if(str_icmp(identifier, "data") == 0) {
        anim->frame_count = nanoparser_get_number_of_parameters(param_list);
        if(anim->frame_count < 1)
            fatal_error("Can't load sprites. Animation 'data' field is missing\nin\"%s\" near line %d", nanoparser_get_file(stmt), nanoparser_get_line_number(stmt));
        
        anim->data = mallocx(anim->frame_count * sizeof(*(anim->data)));
        for(j=1; j<=anim->frame_count; j++) {
            pj = nanoparser_get_nth_parameter(param_list, j);
            nanoparser_expect_string(pj, "Animation 'data' field is a list of frame numbers");
            anim->data[j-1] = max(0, atoi(nanoparser_get_string(pj)));
        }
    }

    return 0;
}
Beispiel #6
0
/*
 *  vsscanf(buf,fmt,ap)
 */
static int
vsscanf (const char *buf, const char *s, va_list ap)
{
    int             count, noassign, width, base, lflag;
    const char     *tc;
    char           *t, tmp[MAXLN];

    count = noassign = width = lflag = 0;
    while (*s && *buf) {
	while (isspace (*s))
	    s++;
	if (*s == '%') {
	    s++;
	    for (; *s; s++) {
		if (strchr ("dibouxcsefg%", *s))
		    break;
		if (*s == '*')
		    noassign = 1;
		else if (*s == 'l' || *s == 'L')
		    lflag = 1;
		else if (*s >= '1' && *s <= '9') {
		    for (tc = s; isdigit (*s); s++);
		    strncpy (tmp, tc, s - tc);
		    tmp[s - tc] = '\0';
		    atob (&width, tmp, 10);
		    s--;
		}
	    }
	    if (*s == 's') {
		while (isspace (*buf))
		    buf++;
		if (!width)
		    width = strcspn (buf, ISSPACE);
		if (!noassign) {
		    strncpy (t = va_arg (ap, char *), buf, width);
		    t[width] = '\0';
		}
		buf += width;
	    } else if (*s == 'c') {
Beispiel #7
0
int chg_heaptop(char *name, char *value)
{
	u_int32_t top;

	if (atob (&top, value, 16)) {
#ifdef HEAP_ALLOC_UPWORD
		if (top < (u_int32_t)allocp1) {
			printf("%x: heap is already above this point\n", top);
			return 0;
		}
#else
		if (top <heaptop) {
			printf("%x:  memory between %x-%x  is already been allocated,heap is already above this point\n", top,allocp1,heaptop);
			return 0;
		}
#endif
		heaptop = (char *)top;
		return 1;
	}
	printf("%s: invalid address\n", value);
	return 0;
}
/*********************************************************************************
									~MAIN~
*********************************************************************************/
void loop(){ 
	int lightsCmd;
	int shadesCmd;
	int doorCmd;
	int displayCmd;
	
	while(!connectAndRead()){ // chill here until a connection is established
		Ethernet.begin(mac, ip);  //try restarting the network connection
	}  

	String pageValue = readPage(); //connect to the server and read the output
	
	//Serial.println(pageValue); //print out the findings.
	specifyArrays(); // this takes raw data from php page and puts them in their arrays
	
	//LIGHTS
	for(byte i =0; i<8; i++){
		if(!(lights[i]==currentLights[i])){
			Serial.println("lights");
			Serial.println(lights); 

			// converts characters in array to a binary integer
			lightsCmd = atob(lights);
		
			//Serial.println("lightsCmd:");
			//Serial.println(lightsCmd,BIN); 

			//I am not using the binary version right now (08/4/2012)
			if(!lightsChanged && !firstTime){
				sendLightCmd();
				Serial.println("sent lights command");
			}
			lightsChanged = 0; 
	
			for(byte i = 0; i<8; i++)
				currentLights[i] = lights[i];  // make it the new current
			break; // exit the for loop after just one bit is different
		}
	}	
	delay(10);
	
	//SHADES
	for(byte i =0; i<8; i++){
		if(!(shades[i]==currentShades[i])){
			Serial.println("shades");
			Serial.println(shades);
		
			shadesCmd = atob(shades);
			
			//Serial.println("shadesCmd:");
			//Serial.println(shadesCmd, BIN);
			
			if(!shadesChanged && !firstTime){
				sendShadesCmd();
				Serial.println("sent shades command");
			}
			shadesChanged = 0;
			
			for(int i = 0; i<8; i++)
				currentShades[i] = shades[i];
			break;
		}
	}
	delay(10);

	//Door
	for(byte i=0; i<8; i++){
		if(!(door[i]==currentDoor[i])){  //if they are different
			Serial.println("door");
			Serial.println(door);
		
			doorCmd = atob(door);
			
			//Serial.println("doorCmd:");
			//Serial.println(doorCmd, BIN);
			
			if(!doorChanged && !firstTime){  //"doorChanged" is set when the client is changed independent of web
				sendDoorCmd();
				Serial.println("sent door command");
			}
			doorChanged = 0;
			
			for(int i = 0; i<8; i++)
				currentDoor[i] = door[i];
			break;
		}
	}
	delay(10);

	//DISPLAY (LED TICKER)
	if(displayChanged){
		sendDisplayCmd();
	}

	delay(10);
	
	firstTime=0;  //No longer the first time through
	
	//change data direction register for inputs
	DDRD &= B10001111;  //com pins are input now
	PORTC = B00000001; //lights control line
	delay(10);
	if(PIND & B01110000){ //check if lights are talking
		DDRD |= B01110000; //turn com pins back into output
		Serial.println("Lights are talking... Listening");
		clientLights = listenToLights();
		Serial.println(clientLights,BIN);
		updateDB_Lights();
	}
	else
		clientLights = 0x00;
		
	PORTC = B00000010;  //shades control line
	delay(10);
	if(PIND & B01110000){ //check if shades are talking
		DDRD |= B01110000; //turn com pins back into output
		Serial.println("Shades are talking... Listening");
		clientShades = listenToShades();
		Serial.println(clientShades,BIN);
		updateDB_Shades();
	}
	else
		clientShades = 0x00;
	
	PORTC = B00000100;  //door control line
	delay(10);
	if(PIND & B01110000){ //check if the door is talking
		DDRD |= B01110000; //turn com pins back into output
		Serial.println("door is talking... Listening");
		clientDoor = listenToDoor();
		Serial.println(clientDoor,BIN);
		updateDB_Door();
	}
	else
		clientDoor = 0x00;
	
	DDRD |= B01110000; //turn com pins back into output
	
	memset(&shades, 0, 8 ); //clear memory
	memset(&lights, 0, 8 ); //clear memory
	
	delay(reconnectDelay); //waits before connecting again prevent data loss
}
Beispiel #9
0
int traverse_background_attributes(const parsetree_statement_t *stmt, void *background)
{
    const char *identifier;
    const parsetree_parameter_t *param_list;
    const parsetree_parameter_t *p1, *p2, *p3, *p4, *p5, *p6, *p7;
    background_t *bg = (background_t*)background;

    identifier = nanoparser_get_identifier(stmt);
    param_list = nanoparser_get_parameter_list(stmt);

    if(str_icmp(identifier, "initial_position") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        p2 = nanoparser_get_nth_parameter(param_list, 2);

        nanoparser_expect_string(p1, "initial_position must be a pair of numbers");
        nanoparser_expect_string(p2, "initial_position must be a pair of numbers");

        bg->actor->spawn_point.x = atof(nanoparser_get_string(p1));
        bg->actor->spawn_point.y = atof(nanoparser_get_string(p2));
        bg->actor->position = bg->actor->spawn_point;
    }
    else if(str_icmp(identifier, "scroll_speed") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        p2 = nanoparser_get_nth_parameter(param_list, 2);

        nanoparser_expect_string(p1, "scroll_speed must be a pair of numbers");
        nanoparser_expect_string(p2, "scroll_speed must be a pair of numbers");

        bg->actor->speed.x = atof(nanoparser_get_string(p1));
        bg->actor->speed.y = atof(nanoparser_get_string(p2));
    }
    else if(str_icmp(identifier, "behavior") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        p2 = nanoparser_get_nth_parameter(param_list, 2);
        p3 = nanoparser_get_nth_parameter(param_list, 3);
        p4 = nanoparser_get_nth_parameter(param_list, 4);
        p5 = nanoparser_get_nth_parameter(param_list, 5);
        p6 = nanoparser_get_nth_parameter(param_list, 6);
        p7 = nanoparser_get_nth_parameter(param_list, 7);

        nanoparser_expect_string(p1, "Background behavior must be a string");

        if(str_icmp(nanoparser_get_string(p1), "DEFAULT") == 0) {
            if(bg->strategy)
                bg->strategy = bgstrategy_delete(bg->strategy);
            bg->strategy = bgstrategy_default_new(bg);
        }
        else if(str_icmp(nanoparser_get_string(p1), "LINEAR") == 0) {
            nanoparser_expect_string(p2, "Linear background behavior expects a pair of numbers");
            nanoparser_expect_string(p3, "Linear background behavior expects a pair of numbers");

            if(bg->strategy)
                bg->strategy = bgstrategy_delete(bg->strategy);
            bg->strategy = bgstrategy_linear_new(
                bg,
                atof(nanoparser_get_string(p2)),
                atof(nanoparser_get_string(p3))
            );
        }
        else if(str_icmp(nanoparser_get_string(p1), "CIRCULAR") == 0) {
            nanoparser_expect_string(p2, "Circular background behavior expects at least four numbers");
            nanoparser_expect_string(p3, "Circular background behavior expects at least four numbers");
            nanoparser_expect_string(p4, "Circular background behavior expects at least four numbers");
            nanoparser_expect_string(p5, "Circular background behavior expects at least four numbers");

            if(bg->strategy)
                bg->strategy = bgstrategy_delete(bg->strategy);
            bg->strategy = bgstrategy_circular_new(
                bg,
                atof(nanoparser_get_string(p2)),
                atof(nanoparser_get_string(p3)),
                atof(nanoparser_get_string(p4)),
                atof(nanoparser_get_string(p5)),
                atof(nanoparser_get_string(p6)),
                atof(nanoparser_get_string(p7))
            );
        }
        else
            fatal_error("Unknown background behavior: '%s'", nanoparser_get_string(p1));
    }
    else if(str_icmp(identifier, "repeat_x") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_string(p1, "repeat_x expects a boolean value");
        bg->repeat_x = atob(nanoparser_get_string(p1));
    }
    else if(str_icmp(identifier, "repeat_y") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_string(p1, "repeat_y expects a boolean value");
        bg->repeat_y = atob(nanoparser_get_string(p1));
    }
    else if(str_icmp(identifier, "zindex") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_string(p1, "Can't read background attributes: zindex expects a number between 0.0 (far) and 1.0 (near)");
        bg->zindex = clip(atof(nanoparser_get_string(p1)), 0.0f, 1.0f);
    }
    else if(str_icmp(identifier, "sprite") == 0) {
        p1 = nanoparser_get_nth_parameter(param_list, 1);
        nanoparser_expect_program(p1, "Can't read background attributes: sprite block expected");
        if(bg->data != NULL)
            spriteinfo_destroy(bg->data);
        bg->data = spriteinfo_create(nanoparser_get_program(p1));
    }
    else
        fatal_error("Can't read background attributes. Unknown identifier: '%s'", identifier);

    return 0;
}