Esempio n. 1
0
File: pe02.c Progetto: jyshi/ece264
// a sample test function to call char_to_int
//
void test_char_to_int()
{
    printf("%d\n", char_to_int('9'));
    printf("%d\n", char_to_int('z'));
    printf("%d\n", char_to_int('Z'));
}
Esempio n. 2
0
superint superint::operator *(superint x)// *重载
{
    superint mul=0,t;
    t.strint=superint::strint;
    int mark=0,m1=1,m2=1;
    int n1,n2,n;
    n1=t.strint.length();
    n2=x.strint.length();
//倒置字符串
    convert(t.strint);
    convert(x.strint);
//检测是否为负数
    if(t.strint[n1-1]==45)
    {
        n1--;
        m1=-1;
        t.strint.resize(n1);//消除负号
    }
    if(x.strint[n2-1]==45)
    {
        n2--;
        m2=-1;
        x.strint.resize(n2);//消除负号
    }

    n=n1>n2?n1:n2;

//补齐字符串
    if(n==n1)
    {
        for(int i=0;i<n1-n2;i++) x.strint+="0";
    }
    else
    {
        for(int i=0;i<n2-n1;i++) t.strint+="0";
    }
//符号相同
        int tem;
        superint tm;
        for(int j=0;j<n;j++)
        {
            mark=0;
            tm.strint.clear();
            for(int k=0;k<j;k++) tm.strint+="0";
            for(int i=0;i<n;i++)
            {
                tem=char_to_int(t.strint[j])*char_to_int(x.strint[i])+mark;
                mark=tem/10;
                tem=tem%10;
                tm.strint+=int_to_char(tem);
            }
            tm.strint+=int_to_char(mark);
            remove0(tm.strint);
            convert(tm.strint);
            mul=mul+tm;
        }
//符号相异
    if(m1!=m2) mul=(-mul);

    return mul;
}
Esempio n. 3
0
void state_calibration_play(){
	uint8_t c;
	char t[6];
	uint32_t now;
	value_sensor value_sensor_d;
	switch(calibration_fsm_state)
	{
			case CALFSM_STATE_CHOICE_DELAY:
				DCALIB_PRINT("\r\nPlease enter the delay between readings (in ms) : \r\n");	
				calibration_fsm_state=CALFSM_STATE_READING_DELAY;
				break;
			case CALFSM_STATE_READING_DELAY:
				c=usb_serial_get_byte();
				if (c>=48 && c<=57)
				{
					fprintf(&USBSerialStream,"%c",c);
					Value_T[fillpos++]=c;
				}
				else if (c=='\r')
				{
					Value_T[fillpos++]=c;
					delay_between_readings=char_to_int(Value_T,0,fillpos-1);
					fillpos=0;
					calibration_fsm_state=CALFSM_STATE_INITIALIZING;
				}
				break;
			case CALFSM_STATE_INITIALIZING:
				DCALIB_PRINT("\r\nPlease chose the calibration type (1 for PM, 2 for temperature, 3 for Humidity) \r\n");
				calibration_fsm_state=CALFSM_STATE_CHOICE_CALIB;
				break;
			case CALFSM_STATE_CHOICE_CALIB:
				c=usb_serial_get_byte();
				if (c>48 && c<=51)
				{
					type_of_calib=c-'0';
					calibration_fsm_state=CALFSM_STATE_READING_SENSOR;
				}				
				break;
			case CALFSM_STATE_READING_SENSOR:
				sensor_update();
				switch(type_of_calib)
				{
					case 1:
						Sensor_t[number_of_reading-1] = (int32_t) (sensors_acc.pm_instant*100);
						break;
					case 2:
						Sensor_t[number_of_reading-1]	=	(uint32_t) (sensors_acc.tmp_instant*100.0);
						break;
					case 3:
						Sensor_t[number_of_reading-1]	=	(uint32_t) (sensors_acc.hum_instant*100.0);
						break;
				}
				if (number_of_reading==1 || number_of_reading ==2)
				{
					number_of_reading++;
					now=millis();
					waiting_end=set_timeout(now+delay_between_readings);
					calibration_fsm_state=CALFSM_STATE_WAITING;
				}
				else 
				{
					DCALIB_PRINTF("T_0 : %lu, T_5 : %lu, T_10 : %lu \r\n", Sensor_t[0], Sensor_t[1], Sensor_t[2]);
					DCALIB_PRINT("Enter the reference values (ref_0;ref_5;ref_10) : \r\n");
					number_of_reading=1;
					calibration_fsm_state=CALFSM_STATE_READING_VALUE_T;
				}
				break;
			case CALFSM_STATE_WAITING:
				//DCALIB_PRINTF("\r\n %li", (int32_t) (millis()-waiting_end));
				if (millis()>waiting_end)
				{
						calibration_fsm_state=CALFSM_STATE_READING_SENSOR;
				}
				break;
			case CALFSM_STATE_READING_VALUE_T:
				c=usb_serial_get_byte();
				if (c>=48 && c<=57 || c==';')
				{
					fprintf(&USBSerialStream,"%c",c);
					Value_T[fillpos++]=c;
				}
				else if (c=='\r')
				{
					Value_T[fillpos++]=c;
					value_sensor_d=parse_ref_value(Value_T,fillpos);
					Sensor_Ref_0=char_to_int(Value_T,0,value_sensor_d.length_S_0);
					Sensor_Ref_5=char_to_int(Value_T,value_sensor_d.length_S_0+1,value_sensor_d.length_S_5);
					Sensor_Ref_10=char_to_int(Value_T,value_sensor_d.length_S_5+1,value_sensor_d.length_S_10);
					//DCALIB_PRINTF("T_0 : %lu, T_5 : %lu, T_10 %lu\r\n",Sensor_Ref_0, Sensor_Ref_5 ,Sensor_Ref_10);
					fillpos=0;
					calibration_fsm_state=CALFSM_STATE_CALCULATE_K;	
				}
				break;
			case CALFSM_STATE_CALCULATE_K:
				coef=calculate_k(Sensor_Ref_0, Sensor_t[0], Sensor_Ref_10, Sensor_t[2]);
				coef1=calculate_k(Sensor_Ref_0, Sensor_t[0], Sensor_Ref_5, Sensor_t[1]);
				coef2=calculate_k(Sensor_Ref_5, Sensor_t[1], Sensor_Ref_10, Sensor_t[2]);
				fprintf(&USBSerialStream,"\n\r 0-10	a	: %u ,	b : %li	, Calibrated_t_5	 : %lu ",coef.a, coef.b, (uint32_t) round(((double)(Sensor_t[1]*coef.a+coef.b)/100)));
				fprintf(&USBSerialStream,"\n\r 0-5	a	: %u ,	b : %li	, Calibrated_t_10	 : %lu  ",coef1.a, coef1.b, (uint32_t) round(((double)(Sensor_t[2]*coef1.a+coef1.b)/100)));
				fprintf(&USBSerialStream,"\n\r 5-10	a	: %u ,	b : %li	, Calibrated_t_0	 : %lu  \r\n",coef2.a, coef2.b, (uint32_t) round(((double)(Sensor_t[0]*coef2.a+coef2.b)/100)) );
				DCALIB_PRINT("Press 1, 2, 3 to save the corresponding coefficient. Press 0 to skip saving.\r\n");
				calibration_fsm_state=CALFSM_STATE_CHOICE_SAVE;
				break;	
			case CALFSM_STATE_CHOICE_SAVE:
				c=usb_serial_get_byte();
				switch (c)
				{
					case '0':
						calibration_fsm_state=CALFSM_STATE_END;
						break;
					case '1':
							sensor_calib_set_k(coef.a,coef.b,SIGNIFICATIVE_DIGITS_K,type_of_calib);
						calibration_fsm_state=CALFSM_STATE_SAVE;
						break;
					case '2':
						sensor_calib_set_k(coef1.a,coef1.b,SIGNIFICATIVE_DIGITS_K,type_of_calib);
						calibration_fsm_state=CALFSM_STATE_SAVE;
						break;
					case '3':
						sensor_calib_set_k(coef2.a,coef2.b,SIGNIFICATIVE_DIGITS_K,type_of_calib);
						calibration_fsm_state=CALFSM_STATE_SAVE;
						break;
					case '-':
						DCALIB_PRINT("Enter the reference values (ref_0;ref_5;ref_10) : \r\n");
						calibration_fsm_state=CALFSM_STATE_READING_VALUE_T;
						break;
					default :
						calibration_fsm_state=CALFSM_STATE_CHOICE_SAVE;
						break;
				}
				break;				
			case CALFSM_STATE_SAVE:	
				sensor_calib_save_k(type_of_calib);
				DCALIB_PRINT("Coefficients saved\r\n");
				calibration_fsm_state=CALFSM_STATE_END;
				break;
			case CALFSM_STATE_END:
				state_set_next(&main_fsm, &state_normal);
				break;
			default:
				state_set_next(&main_fsm, &state_normal);
				break;		
		}
}
Esempio n. 4
0
int protocol_authorize(const char * n) {
    return (atoi(n) * char_to_int(n[1])) + (char_to_int(n[3])*5);
}
Esempio n. 5
0
superint superint::operator -(superint x)// -重载
{
    superint sub;
    superint t;
    t.strint=superint::strint;
    int mark=0,m1=1,m2=1;
    int n1,n2,n;
    n1=t.strint.length();
    n2=x.strint.length();
//倒置字符串
    convert(t.strint);
    convert(x.strint);
//检测是否为负数
    if(t.strint[n1-1]==45)
    {
        n1--;
        m1=-1;
        t.strint.resize(n1);//消除负号
    }
    if(x.strint[n2-1]==45)
    {
        n2--;
        m2=-1;
        x.strint.resize(n2);//消除负号
    }

    n=n1>n2?n1:n2;

//补齐字符串
    if(n==n1)
    {
        for(int i=0;i<n1-n2;i++) x.strint+="0";
    }
    else
    {
        for(int i=0;i<n2-n1;i++) t.strint+="0";
    }
//符号相同
    if(m1==m2)
    {
        superint subx("1");
        int x1,x2,tem;
        for(int i=0;i<n;i++)
        {
            x1=char_to_int(t.strint[i]);
            x2=char_to_int(x.strint[i]);
            tem=x1-x2+mark;
            if(tem<0)
            {
                mark=-1;
                tem+=10;
            }
            else mark=0;

            sub.strint+=int_to_char(tem);
        }
        remove0(sub.strint);
        convert(sub.strint);
        if(mark==-1)
        {
            int ti=sub.strint.length();
            for(int i=0;i<ti;i++) subx.strint+="0";
            sub=subx-sub;
            sub=-sub;
        }
        if(m1+m2==-2) sub=-sub;
    }
//符号相异
    convert(t.strint);
    convert(x.strint);
    if(m1>m2) sub=t+x;
    if(m1<m2)
    {
        sub=t+x;
        sub=-sub;
    }

    return sub;
}
Esempio n. 6
0
static int
dopr(char *buffer, size_t maxlen, const char *format, va_list args_in)
{
	char ch;
	intmax_t value;
	LDOUBLE fvalue;
	char *strvalue;
	int min;
	int max;
	int state;
	int flags;
	int cflags;
	size_t currlen;
	va_list args;

	VA_COPY(args, args_in);
	
	state = DP_S_DEFAULT;
	currlen = flags = cflags = min = 0;
	max = -1;
	ch = *format++;
	
	while (state != DP_S_DONE) {
		if (ch == '\0') 
			state = DP_S_DONE;

		switch(state) {
		case DP_S_DEFAULT:
			if (ch == '%') 
				state = DP_S_FLAGS;
			else
				DOPR_OUTCH(buffer, currlen, maxlen, ch);
			ch = *format++;
			break;
		case DP_S_FLAGS:
			switch (ch) {
			case '-':
				flags |= DP_F_MINUS;
				ch = *format++;
				break;
			case '+':
				flags |= DP_F_PLUS;
				ch = *format++;
				break;
			case ' ':
				flags |= DP_F_SPACE;
				ch = *format++;
				break;
			case '#':
				flags |= DP_F_NUM;
				ch = *format++;
				break;
			case '0':
				flags |= DP_F_ZERO;
				ch = *format++;
				break;
			default:
				state = DP_S_MIN;
				break;
			}
			break;
		case DP_S_MIN:
			if (isdigit((unsigned char)ch)) {
				min = 10*min + char_to_int (ch);
				ch = *format++;
			} else if (ch == '*') {
				min = va_arg (args, int);
				ch = *format++;
				state = DP_S_DOT;
			} else {
				state = DP_S_DOT;
			}
			break;
		case DP_S_DOT:
			if (ch == '.') {
				state = DP_S_MAX;
				ch = *format++;
			} else { 
				state = DP_S_MOD;
			}
			break;
		case DP_S_MAX:
			if (isdigit((unsigned char)ch)) {
				if (max < 0)
					max = 0;
				max = 10*max + char_to_int (ch);
				ch = *format++;
			} else if (ch == '*') {
				max = va_arg (args, int);
				ch = *format++;
				state = DP_S_MOD;
			} else {
Esempio n. 7
0
static int dopr (char *buffer, size_t maxlen, const char *format, va_list args)
{
  char ch;
  LLONG value;
  LDOUBLE fvalue;
  char *strvalue;
  int min;
  int max;
  int state;
  int flags;
  int cflags;
  int total;
  size_t currlen;
  
  state = DP_S_DEFAULT;
  currlen = flags = cflags = min = 0;
  max = -1;
  ch = *format++;
  total = 0;

  while (state != DP_S_DONE)
  {
    if (ch == '\0')
      state = DP_S_DONE;

    switch(state) 
    {
    case DP_S_DEFAULT:
      if (ch == '%') 
	state = DP_S_FLAGS;
      else 
	total += dopr_outch (buffer, &currlen, maxlen, ch);
      ch = *format++;
      break;
    case DP_S_FLAGS:
      switch (ch) 
      {
      case '-':
	flags |= DP_F_MINUS;
        ch = *format++;
	break;
      case '+':
	flags |= DP_F_PLUS;
        ch = *format++;
	break;
      case ' ':
	flags |= DP_F_SPACE;
        ch = *format++;
	break;
      case '#':
	flags |= DP_F_NUM;
        ch = *format++;
	break;
      case '0':
	flags |= DP_F_ZERO;
        ch = *format++;
	break;
      default:
	state = DP_S_MIN;
	break;
      }
      break;
    case DP_S_MIN:
      if ('0' <= ch && ch <= '9')
      {
	min = 10*min + char_to_int (ch);
	ch = *format++;
      } 
      else if (ch == '*') 
      {
	min = va_arg (args, int);
	ch = *format++;
	state = DP_S_DOT;
      } 
      else 
	state = DP_S_DOT;
      break;
    case DP_S_DOT:
      if (ch == '.') 
      {
	state = DP_S_MAX;
	ch = *format++;
      } 
      else 
	state = DP_S_MOD;
      break;
    case DP_S_MAX:
      if ('0' <= ch && ch <= '9')
      {
	if (max < 0)
	  max = 0;
	max = 10*max + char_to_int (ch);
	ch = *format++;
      } 
      else if (ch == '*') 
      {
	max = va_arg (args, int);
	ch = *format++;
	state = DP_S_MOD;
      } 
Esempio n. 8
0
static size_t dopr(char *buffer, size_t maxlen, const char *format, va_list args)
{
    char ch;
    LLONG value;
    LDOUBLE fvalue;
    char *strvalue;
    int min;
    int max;
    int state;
    int flags;
    int cflags;
    size_t currlen;

    // change to EPT-M3 2009.06.15
    char fmtintstr[10];
    // change to EPT-M3 2009.06.15

    state = DP_S_DEFAULT;
    currlen = flags = cflags = min = 0;
    max = -1;
    ch = *format++;

    while (state != DP_S_DONE)
    {
        if (ch == '\0')
            state = DP_S_DONE;

        switch(state)
        {
        case DP_S_DEFAULT:
            if (ch == '%')
                state = DP_S_FLAGS;
            else
                dopr_outch (buffer, &currlen, maxlen, ch);
            ch = *format++;
            break;
        case DP_S_FLAGS:
            switch (ch)
            {
            case '-':
                flags |= DP_F_MINUS;
                ch = *format++;
                break;
            case '+':
                flags |= DP_F_PLUS;
                ch = *format++;
                break;
            case ' ':
                flags |= DP_F_SPACE;
                ch = *format++;
                break;
            case '#':
                flags |= DP_F_NUM;
                ch = *format++;
                break;
            case '0':
                flags |= DP_F_ZERO;
                ch = *format++;
                break;
            default:
                state = DP_S_MIN;
                break;
            }
            break;
        case DP_S_MIN:
            if (IsDigit((unsigned char)ch))
            {
                min = 10 * min + char_to_int (ch);
                ch = *format++;
            }
            else if (ch == '*')
            {
                min = va_arg (args, int);
                ch = *format++;
                state = DP_S_DOT;
            }
            else
            {
                state = DP_S_DOT;
            }
            break;
        case DP_S_DOT:
            if (ch == '.')
            {
                state = DP_S_MAX;
                ch = *format++;
            }
            else
            {
                state = DP_S_MOD;
            }
            break;
        case DP_S_MAX:
            if (IsDigit((unsigned char)ch))
            {
                if (max < 0)
                    max = 0;
                max = 10 * max + char_to_int (ch);
                ch = *format++;
            }
            else if (ch == '*')
            {
                max = va_arg (args, int);
                ch = *format++;
                state = DP_S_MOD;
            }
Esempio n. 9
0
Stringset *new_stringset(SEXP str, int bytes, int intdist){
  size_t nstr = length(str);
  Stringset *s;
  s = (Stringset *) malloc(sizeof(Stringset));

  // get and set string lengths.
  s->str_len = (int *) malloc(nstr * sizeof(int));

  size_t nbytes = 0L;

  if ( intdist ){
    for (size_t i=0; i<nstr; i++){
      nbytes += length(VECTOR_ELT(str,i));
    }
  } else {
    for (size_t i=0; i<nstr; i++){
      nbytes += length(STRING_ELT(str,i));
    }
  }

  s->string = (unsigned int **) malloc(nstr * sizeof(int *));
  // room for int rep of strings, including a trailing zero (needed by e.g. by full dl-distance)
  // this is enough room for byte-by-byte translation, so for UTF-8 it will be too much.
  s->data = (unsigned int *) malloc( (nstr + nbytes) * sizeof(int));

  int *t = s->str_len;
  unsigned int *d = s->data;

  if ( intdist ){
    for (size_t i=0L; i < nstr; i++, t++){
      if ( INTEGER(VECTOR_ELT(str,i))[0] == NA_INTEGER ){
        (*t) = NA_INTEGER; 
      } else {
        (*t) = length(VECTOR_ELT(str,i));
        memcpy(d, INTEGER(VECTOR_ELT(str,i)), (*t)*sizeof(int) );
        s->string[i] = d;
        (*(d + (*t))) = 0L; // append a zero.
        d += (*t) + 1L;
      }
    }
  } else if ( bytes ){
    for (size_t i=0L; i < nstr; i++, t++){
      if ( STRING_ELT(str,i) == NA_STRING ){
        (*t) = NA_INTEGER; 
      } else {
        (*t) = char_to_int(CHAR(STRING_ELT(str,i)), d);
        s->string[i] = d;
        (*(d + (*t))) = 0L; // append a zero.
        d += (*t) + 1L;
      }
    }
  } else {
    for (size_t i=0L; i < nstr; i++, t++){
      if ( STRING_ELT(str,i) == NA_STRING ){
        (*t) = NA_INTEGER; 
      } else {
        (*t) = utf8_to_int(CHAR(STRING_ELT(str,i)), d); 
        s->string[i] = d;
        (*(d + (*t))) = 0L; // append a zero.
        d += (*t) + 1L;
      }
    }
  }

  return s;
}
Esempio n. 10
0
  TEST(ParserTest, CharToInt) {
   EXPECT_EQ(5, char_to_int('5'));
 }
Esempio n. 11
0
// you should try to re-use a majority of the algorithms in PE02 and PE03
// for this programming assignment.
// However, you have to take care of endptr, base 0, and base 16
// these are the three additional things you have to deal with in
// this assignment, as compared to the str_to_long_int function
// that you wrote for PE02 and PE03.
//
// it is really IMPORTANT that you extensively try out the function
// strtol on your own.
//
long int my_strtol(const char *nptr, char **endptr, int base)
{
   long int ret_value = 0;  // return value
   long int count = 0;
   int negative = 0;
   int first = 0;
	int ct = 0;// length of array
	int index = 0;
	const char *sum = 0;
   if (base > 36 || base == 1)
   {
      errno = EINVAL;
      return(ret_value);
   }
		sum = nptr;
		for (ct = 0; nptr[ct] != '\0'; ct++)
		{
		}
      while(isspace(*nptr)){
        nptr++;
      }
      if (*nptr == '+' || *nptr == '-')
      {
		negative = (*nptr == '-');
		nptr++;
      }
		if (base == 0 || base == 16)
		{
			if (*nptr == '0')
			{
				nptr++;
				if (*nptr == 'x' || *nptr == 'X'){
					base = 16;
					nptr++;
					if (*nptr == '\0'){
						nptr--;
						*endptr = (char *) (nptr);
						return(ret_value);
					}
					count = char_to_int(*nptr);
					if(count > (base - 1)) 
					{
						*endptr = (char *) (nptr - 1);
						return(ret_value);
					}
				}
				else if(*nptr == '0' && base != 16){
					base = 8;
					nptr++;
					if (*nptr == '\0'){
						nptr--;
						*endptr = (char *)(nptr + 1);
						return(ret_value);
					}
					count = char_to_int(*nptr);
					if(count > (base - 1))
					{
						*endptr = (char *)(nptr);
						return(ret_value);
					}
				}
				else if(*nptr == '\0' || base != 16)
				{
					nptr--;
					base = 10;
				}
				else  
				{
					base = 16;
				}
			}
			else if (base == 16)
			{
				base = 16;
			}
			else 
			{
				base = 10;
			}
		}
		while (*nptr != '\0')
		{
			if(negative == 0){
	    		count = char_to_int(*nptr);
	    		if (count <= (base - 1)){
					if ((LONG_MAX / base) >= ret_value){
		   			ret_value *= base;
		   			if ((LONG_MAX - count) >= ret_value){
		       			index++;
							ret_value += count;
							*endptr = (char *) (nptr + 1);
						}
		   			else{
						  while(*nptr != '\0')
						  {
							  count = char_to_int(*nptr);
							  if (count <= (base - 1))
							  {
							     *endptr = (char *)(nptr + 1);
							  }
							  nptr++;
						  }
						  ret_value = LONG_MAX;
		       		  errno = ERANGE;
		       		  return(LONG_MAX); 
		   			}
	        		}
	        		else{
						while (*nptr != '\0')
						{
							count = char_to_int(*nptr);
							if (count <= (base - 1))
							{
								*endptr = (char *)(nptr + 1);
							}
							nptr++;
						}
	            	ret_value = LONG_MAX;
	            	errno = ERANGE;
						return(LONG_MAX);
            	}
	    		}
				else
				{
					if (index >= 1){
					*endptr = (char *)(nptr);
					}
					else{
					*endptr = (char *) (sum);
					}
					return(ret_value);
				}
	    		nptr++;
        }
		  else{
			count = char_to_int(*nptr);
			if (count <= (base - 1)){
				if((LONG_MIN / base) <= ret_value){
					if(first == 1){
						ret_value *= base;
					}
					else{
						ret_value *= -base;
					}
					if ((LONG_MIN + count <= ret_value)){
						index++;
						ret_value -= count;
						*endptr = (char *)(nptr + 1);
					}
					else{	
						  while(*nptr != '\0')
						  {
							  count = char_to_int(*nptr);
							  if (count <= (base - 1))
							  {
							     *endptr = (char *)(nptr + 1);
							  }
							  nptr++;
						  }
						ret_value = LONG_MIN;
						errno = ERANGE;
						return(LONG_MIN);	
					}
				}
				else{
				  	while(*nptr != '\0')
				   {
						count = char_to_int(*nptr);
					   if (count <= (base - 1))
					   {
					   	*endptr = (char *)(nptr + 1);
					   }
					   nptr++;
				   }
					ret_value = LONG_MIN;
					errno = ERANGE;
					return(LONG_MIN);
				}
			}
			else{
				if (index >= 1){
					*endptr = (char *)(nptr);
				}
				else{
					*endptr = (char *) (sum);
				}
				return(ret_value);
			}
			nptr++;
			first = 1;
		}
	}
   // if invalid base, set errno and return immediately

   // valid base, skip over white space

   // if encounter a sign, expect a number after that 
   // if a negative sign is encountered, change the sign

   
   // the exact algorithm is not given, but these are 
   // the tasks the function has to perform:  
   //
   // determine the actual base if the base is 0
   // if it starts with '0', it is octal
   // if '0' is followed by 'x' or 'X' it is hexadecimal or base 16
   // otherwise, it is decimal
   // therefore you have to determine the actual base and the starting
   // location to perform conversion
   //
   // for bases 2 to 15, perform conversion as in PE02
   //
   // for base 16, you have to decide when the conversion should
   // start: after the optional "0x" or "0X" or immediately
   // question: if you have "0xG", what should be converted?
   //
   // for bases 17 to 36, perform conversion as in PE02
   // 
   // after you have determine the real base, and where to start,
   // perform the conversion

   // clean up, set ret_value properly if the conversion went out of range
   // set errno if necessary
   // set *endptr properly if the endptr is not NULL

   return ret_value;
}
Esempio n. 12
0
Enigma *buildfromfile(const char *file)
{
	FILE *rc = fopen(file, "r");
	if (!rc)
		return 0;

	char buf[BUFSIZE], *p;
	char *patchboardconf = 0, *reflectorconf = 0;
	/* FIXME */
	int rotor_count = 0, alph_len = strlen(alphabet);
	char *rotor_permutations[10],
		 rotor_ticks[10],
		 rotor_stars[10];

	while ((p = fgets(buf, BUFSIZE - 1, rc))) {
		buf[strlen(buf) - 1] = 0;
		p = eat_space(p);

		if (*p == '#')
			continue;
		else if (strncmp(p, "alphabet", 8) == 0) {
			if (rotor_count || patchboardconf || reflectorconf ) {
				fprintf(stderr, _("Alphabet must be specified before all"
						" other components of the enigma\n"));
				return 0;
			}

			p = eat_space(p + 8);
			if (debug) {
				printf(_("Found alphabet: %s\n"), p);
			}
			alph_len = strlen(p);
			alphabet = malloc(sizeof(char) * alph_len);
			strncpy((char *)alphabet, p, alph_len);
		} else if (strncmp(p, "rotor", 5) == 0) {
			/* FIXME */
			if (rotor_count >= 10)
				continue;
			p = eat_space(p + 5);
			if (debug) {
				printf(_("Found rotor: %s\n"), p);
			}
			rotor_permutations[rotor_count] = malloc(sizeof(char) * alph_len);
			strncpy(rotor_permutations[rotor_count], p, alph_len);
			if (debug)
				printf(_("permutation is %s, "), rotor_permutations[rotor_count]);

			p = eat_space(p + alph_len);
			rotor_ticks[rotor_count] = *(p++);

			p = eat_space(p);
			rotor_stars[rotor_count] = *p;
			if (debug)
				printf(_("tick is %c, start is %c\n"), rotor_ticks[rotor_count],
						rotor_stars[rotor_count]);

			rotor_count++;
		} else if (strncmp(p, "reflector", 9) == 0) {
			if (reflectorconf) {
				fprintf(stderr, _("Duplicate reflector configuration\n"));
				return 0;
			}
			p = eat_space(p + 9);
			if (debug)
				printf(_("Found reflector: %s\n"), p);
			reflectorconf = malloc(sizeof(char) * alph_len);
			strncpy(reflectorconf, p, alph_len);
		} else if (strncmp(p, "patchboard", 10) == 0) {
			if (patchboardconf) {
				fprintf(stderr, _("Duplicate patchboard configuration\n"));
				return 0;
			}
			p = eat_space(p + 10);
			if (debug)
				printf(_("Found patchboard: %s\n"), p);
			patchboardconf = malloc(sizeof(char) * alph_len);
			strncpy(patchboardconf, p, alph_len);
		} else {
			fprintf(stderr, _("Illegal input in %s: %s"), file, buf);
			return 0;
		}
	}

	/* FIXME */
	if (!reflectorconf) {
		fprintf(stderr, _("Incomplete configuration, no reflector specified\n"));
		return 0;
	}

	/* FIXME */
	int i, *intperms[10], intticks[10], intstars[10];
	for (i = 0; i < rotor_count; i++) {
		intperms[i] = string_to_int(alphabet, rotor_permutations[i]);
		free(rotor_permutations[i]);

		intticks[i] = char_to_int(alphabet, rotor_ticks[i]);
		intstars[i] = char_to_int(alphabet, rotor_stars[i]);
	}
	int *intpatch = patchboardconf ? string_to_int(alphabet, patchboardconf) : 0;

	Enigma *e = enigma_create(intpatch,
			string_to_int(alphabet, reflectorconf), rotor_count, intperms,
			intticks, intstars);
	if (!e) {
		fprintf(stderr, _("Enigma creation failed\n"));
		return 0;
	}

	if (patchboardconf)
		free(patchboardconf);

	if (reflectorconf)
		free(reflectorconf);

	return e;
}
Esempio n. 13
0
int main(int argc, char **argv)
{
	alphabet = DEFALPHABET;
	FILE *in = stdin;
	FILE *out = stdout;
	char *config = DEFRCFILE;
	bool convert_toupper = false;

	int opt;
	while ((opt = getopt(argc, argv, "di:o:c:uh")) != -1) {
		switch (opt) {
			case 'd':
				debug = true;
				break;
			case 'o':
				if(!(out = fopen(optarg, "w"))) {
					perror(_("Error opening output file"));
					exit(EXIT_FAILURE);
				}
				break;
			case 'c':
				config = optarg;
				break;
			case 'u':
				convert_toupper = true;
				break;
			case 'h':
				usage(argv[0]);
				exit(EXIT_SUCCESS);
				break;
			default:
				fprintf(stderr, _("Unsupported option %c (%d)\n"), opt, opt);
				usage(argv[0]);
				exit(EXIT_FAILURE);
		}
	}

	Enigma *e = buildfromfile(config);
	if (!e) {
		fprintf(stderr, _("Could not parse config file %s\n"), config);
		exit(EXIT_FAILURE);
	}

	int fcount;
	for (fcount = 0; !fcount || optind < argc; optind++, fcount++) {
		if ((optind == argc)
			|| (argv[optind][0] == '-' && argv[optind][1] == 0)) {
			in = stdin;
		} else if (!(in = fopen(argv[optind], "r"))) {
			fprintf(stderr, _("Error opening input file %s: "),
					argv[optind]);
			perror(0);
			continue;
		}

		int c;
		while((c = fgetc(in)) != EOF) {
			if (convert_toupper)
				c = toupper(c);
			if (char_in_alphabeth(alphabet, c))
				fputc(int_to_char(alphabet,
						enigma_encode(e,
						char_to_int(alphabet,
						c))), out);
			else
				fputc(c, out);
		}
		fclose(in);
	}

	enigma_delete(e);
	fclose(out);

	return EXIT_SUCCESS;
}
Esempio n. 14
0
void supprimer_nuplet () {
    Meta_relation *relation = proposer_relation();
    printf("\nSur quel attribut voulez-vous rechercher le n-uplet à supprimer ? ");
    Meta_attribut *attribut = proposer_attribut(relation);
    Meta_attribut ** attributs = relation->attributs;
    
    int cpt_suppr = 0;
    char critere_suppr[9];
    
    printf("\nVeuillez saisir votre critère de suppression (type ");
    if (attribut->domaine->type == 0) {
        printf("INT): ");
    }
    else {
        printf("VARCHAR(%d): ", attribut->domaine->taille);
    }
    scanf("%s", critere_suppr);
    
    Assoc_rel_page *assoc;
    for (int cpt=0; cpt<annuaire->nb_rel_pages; cpt++) {    // PARCOURS DES PAGES
        
        if ((assoc = annuaire->rel_pages[cpt])->id_rel[0] != '\0') {    //SELECTION DES PAGES NON VIDES
            Page *page = donnees_BD->pages[assoc->index_page];
            
            if (strcmp(assoc->id_rel, relation->id) == 0) {
                
                int nb_nuplet = (int) page->enregistrement[63];
                int limite_zg = limite_zone_de_gestion(62-2*(nb_nuplet), page);
                int debut_attribut = 0;
                
                for (int i = 62; i > limite_zg; i=i-2) {        // PARCOURS DE LA ZONE DE GESTION
                    if (page->enregistrement[i-1] == '1') {   // SELECTION DES NUPLETS NON EFFACES
                        if (i != 62) {
                            debut_attribut = (int) page->enregistrement[i+2];
                        }
                        for (int ind_att = 0; ind_att<relation->nb_attr; ind_att++) {   //  PARCOURS DE CHAQUE ATTRIBUT
                            if (ind_att == attribut->rang) {    // SELECTION DE L'ATTRIBUT A RECHERCHER
                                
                                
                                int comparaison = 0; // BOOLEAN RESULTANT DE LA COMPARAISON
                                
                                // ATTRIBUT DE TYPE INT
                                if (attributs[ind_att]->domaine->type == 0) {
                                    unsigned char octets[4];
                                    memcpy(octets, page->enregistrement + debut_attribut, sizeof(char) * 4);
                                    int res = bytes_to_int(octets);
                                    int critere_de_recherche = (int) strtol(critere_suppr, NULL, 10);
                                    
                                    // COMPARAISON SELON LE CRITERE DE RECHERCHE (POUR UN INT)
                                    if (res == critere_de_recherche)
                                        comparaison = 1;
                                }
                                // ATTRIBUT DE TYPE VARCHAR
                                else {
                                    int taille_varchar = char_to_int (page->enregistrement[debut_attribut]);
                                    char *varchar = (char *) malloc(sizeof(char) * taille_varchar);
                                    memcpy(varchar, page->enregistrement + debut_attribut + 1, sizeof(char) * taille_varchar);
                                    
                                    // COMPARAISON
                                    if (strcmp(critere_suppr, varchar) == 0)
                                        comparaison = 1;
                                }
                                
                                // SUPRESSION DU NUPLET SI COMPARAISON POSITIVE
                                if (comparaison == 1) {
                                    page->enregistrement[i-1]='0';
                                    page->enregistrement[63] = int_to_bytes (((int) page->enregistrement[63]) - 1)[3];
                                    cpt_suppr++;
                                    nettoyer_espace_vide (page, limite_zg, i-1);
                                }
                            }
                            // PASSAGE A L'ATTRIBUT SUIVANT
                            if (attributs[ind_att]->domaine->type == 0) {
                                debut_attribut += 4;
                            }
                            else {
                                int taille_varchar = char_to_int(page->enregistrement[debut_attribut]);
                                debut_attribut = debut_attribut + taille_varchar + 1;
                            }
                            
                        }
                    }
                }
                desallocation_si_page_vide(page, assoc);
            }
        }
    }
}
Esempio n. 15
0
static size_t dopr(do_outch_t* dopr_outch, void *buffer, size_t maxlen, const char *fmt, va_list args)
{
	char ch;
	LLONG value;
	LDOUBLE fvalue;
	char *strvalue;
    wchar_t *wstrvalue;
	int min;
	int max;
	int state;
	int flags;
	int cflags;
	size_t currlen;

	state = DP_S_DEFAULT;
	currlen = flags = cflags = min = 0;
	max = -1;
	ch = next_char(dopr_outch,&fmt);

	while (state != DP_S_DONE) {
		if (ch == '\0')
			state = DP_S_DONE;

		switch(state) {
		case DP_S_DEFAULT:
			if (ch == '%')
				state = DP_S_FLAGS;
			else
				dopr_outch (buffer, &currlen, maxlen, ch);
			ch = next_char(dopr_outch,&fmt);
			break;
		case DP_S_FLAGS:
			switch (ch) {
			case '-':
				flags |= DP_F_MINUS;
				ch = next_char(dopr_outch,&fmt);
				break;
			case '+':
				flags |= DP_F_PLUS;
				ch = next_char(dopr_outch,&fmt);
				break;
			case ' ':
				flags |= DP_F_SPACE;
				ch = next_char(dopr_outch,&fmt);
				break;
			case '#':
				flags |= DP_F_NUM;
				ch = next_char(dopr_outch,&fmt);
				break;
			case '0':
				flags |= DP_F_ZERO;
				ch = next_char(dopr_outch,&fmt);
				break;
			default:
				state = DP_S_MIN;
				break;
			}
			break;
		case DP_S_MIN:
			if (isdigit((unsigned char)ch)) {
				min = 10*min + char_to_int (ch);
				ch = next_char(dopr_outch,&fmt);
			} else if (ch == '*') {
				min = va_arg (args, int);
				ch = next_char(dopr_outch,&fmt);
				state = DP_S_DOT;
			} else {
				state = DP_S_DOT;
			}
			break;
		case DP_S_DOT:
			if (ch == '.') {
				state = DP_S_MAX;
				ch = next_char(dopr_outch,&fmt);
			} else {
				state = DP_S_MOD;
			}
			break;
		case DP_S_MAX:
			if (isdigit((unsigned char)ch)) {
				if (max < 0)
					max = 0;
				max = 10*max + char_to_int (ch);
				ch = next_char(dopr_outch,&fmt);
			} else if (ch == '*') {
				max = va_arg (args, int);
				ch = next_char(dopr_outch,&fmt);
				state = DP_S_MOD;
			} else {
Esempio n. 16
0
int bvsnprintf(char *buffer, int32_t maxlen, const char *format, va_list args)
{
   char ch;
   int64_t value;
   char *strvalue;
   wchar_t *wstrvalue;
   int min;
   int max;
   int state;
   int flags;
   int cflags;
   int32_t currlen;
   int base;
#ifdef FP_OUTPUT
   LDOUBLE fvalue;
#endif

   state = DP_S_DEFAULT;
   currlen = flags = cflags = min = 0;
   max = -1;
   ch = *format++;
   *buffer = 0;

   while (state != DP_S_DONE) {
      if ((ch == '\0') || (currlen >= maxlen)) {
         state = DP_S_DONE;
      }
      switch (state) {
      case DP_S_DEFAULT:
         if (ch == '%') {
            state = DP_S_FLAGS;
         } else {
            outch(ch);
         }
         ch = *format++;
         break;
      case DP_S_FLAGS:
         switch (ch) {
         case '-':
            flags |= DP_F_MINUS;
            ch = *format++;
            break;
         case '+':
            flags |= DP_F_PLUS;
            ch = *format++;
            break;
         case ' ':
            flags |= DP_F_SPACE;
            ch = *format++;
            break;
         case '#':
            flags |= DP_F_NUM;
            ch = *format++;
            break;
         case '0':
            flags |= DP_F_ZERO;
            ch = *format++;
            break;
         default:
            state = DP_S_MIN;
            break;
         }
         break;
      case DP_S_MIN:
         if (isdigit((unsigned char)ch)) {
            min = 10 * min + char_to_int(ch);
            ch = *format++;
         } else if (ch == '*') {
            min = va_arg(args, int);
            ch = *format++;
            state = DP_S_DOT;
         } else
            state = DP_S_DOT;
         break;
      case DP_S_DOT:
         if (ch == '.') {
            state = DP_S_MAX;
            flags |= DP_F_DOT;
            ch = *format++;
         } else
            state = DP_S_MOD;
         break;
      case DP_S_MAX:
         if (isdigit((unsigned char)ch)) {
            if (max < 0)
               max = 0;
            max = 10 * max + char_to_int(ch);
            ch = *format++;
         } else if (ch == '*') {
            max = va_arg(args, int);
            ch = *format++;
            state = DP_S_MOD;
         } else
Esempio n. 17
0
float char_to_float(const char * const memory_block,
                     const bool little_endian) {

    int tmp = char_to_int(memory_block, little_endian);
    return reinterpret_cast<float&>(tmp);
}
Esempio n. 18
0
long int my_strtol(const char *nptr, char **endptr, int base)
{
   long int ret_value = 0;  // return value

   int cnt; //counter for for loop
   int state_white = 1; //is program looking for white space? 1 if it is, 0 if not
   int state_sign = 1; //is number being built positive or negative? 1 if positive, 0 if negative. positive by default
   int state_prefix = 1; //is program checking if a prefix (i.e. 0x for hex)? 1 if it is, 0 if not

   // if invalid base, set errno and return immediately
   
   if ( ( base == 1 ) || ( base > 36 ) || ( base < 0 ) )
   {
      errno = EINVAL;
      if (*endptr != NULL)
      {
         *endptr = (char *) nptr + sizeof(char) * strlen(nptr);
      }
      return ret_value;
   }

   // valid base, skip over white space

   for (cnt = 0; cnt < strlen(nptr); cnt++)
   {
      if ( state_white )
      {
         if ( ( (isalnum( nptr[cnt] ) ) || ( (int) nptr[cnt] == 43) || ( (int) nptr[cnt] == 45) ) ) //check passes if we are checking for white space, but a valid symbol is seen
         {
            state_white = 0; //stop checking for white space

            if ( (int) nptr[cnt] == 45) //negative sign given
            {
               state_sign = 0;
               continue;
            }

            else if ( (int) nptr[cnt] == 43) //positive sign given
            {
               continue;
            }
         }
      }

      // all following occurs if program is no longer checking for white space

      if (!( state_white ) )
      {
         if ( ( (int) nptr[cnt] != 45) && ( (int) nptr[cnt] != 43) && ( !(isalnum(nptr[cnt]) ) ) ) //check passes if an invalid symbol is found
         {
            if (*endptr != NULL)
            {
               *endptr = (char *) nptr + sizeof(char) * cnt;
            }
            return ret_value;
         }

         else if ( state_prefix == 1 ) //check to see if a prefix exists; must be done before next else if statement to prevent dividing by 0
         {
            if ( base == 0 ) //unknown base atm
            {
               if ( char_to_int( nptr[cnt] ) == 0 ) //base is either 8 or 16
               {
                  if ( char_to_int( nptr[cnt + 1] ) == 33 ) //'x'; this is hex
                  {
                     base = 16;
                     cnt++; //advance cnt so 'x' is not seen next loop, ending the program
                  }
                
                  else //octal; note that cnt is not advanced, so if this is invalid, it will be caught on next loop
                  {
                     base = 8;
                  }

                  state_prefix = 0;
                  continue; //we dont want to add these prefixes into ret_value, even though this is unlikely to cause any issues (except in the case of 'x')

               }

               else if ( ( char_to_int( nptr[cnt] ) > 0 ) && ( char_to_int( nptr[cnt] ) <= 9 ) ) //base 10
               {
                  base = 10; //this is the one case where we want to immediately process this number (no prefix was found)
                  state_prefix = 0;
               }
            
               else //invalid symbol
               {
                  if (*endptr != NULL)
                  {
                     *endptr = (char *) nptr + sizeof(char) * cnt;
                  }
                  return ret_value;
               }
            }
            
            else //base was known, but state_prefix was on (we only need a prefix if the base was 16)
            {
               if ( ( base == 16 ) && ( char_to_int( nptr[cnt] ) == 0 ) && ( char_to_int( nptr[cnt + 1] ) == 33 ) ) //only case where prefix exists
               {
                  cnt++;
                  state_prefix = 0;
                  continue;
               }
               
               state_prefix = 0;
               
               if ( (isalnum( nptr[cnt]) ) && ( char_to_int( nptr[cnt] ) >= base ) ) //copy of next if statement. there is different behavior if this is the very first integer that is converted
               {
                  *endptr = (char *) nptr;
                  return ret_value;
               }
            }
         }
   
         if ( (isalnum( nptr[cnt]) ) && ( char_to_int( nptr[cnt] ) >= base ) ) //check passes if a valid symbol is found, but is larger than the given base
         {
            *endptr = (char *) nptr + sizeof(char) * cnt;
            return ret_value;
         }

         if ( ( state_sign == 1 ) ) //invalid symbols are gone at this point. check passes if we have a "positive" string given
         {
            if ( ( ret_value != 0 ) && ( ( ( LONG_MAX / base ) < ret_value ) || ( ( LONG_MAX - char_to_int( nptr[cnt] ) ) / ( base ) ) < ret_value ) ) //overflow occurs
            {
               errno = ERANGE;
               *endptr = (char *) nptr + sizeof(char) * strlen(nptr);
               return LONG_MAX;
            }
            else
            {
               ret_value = ret_value * base + char_to_int( nptr[cnt] ); //formula given in README
            }
         }

         else if ( ( state_sign == 0 ) ) //check passes if we have a "negative" string given
         {
            if ( ( ret_value != 0 ) && ( ( (float) ( LONG_MIN / base ) > ret_value ) || ( (float) ( ( LONG_MIN + char_to_int( nptr[cnt] ) ) / ( base ) ) ) > ret_value ) )//underflow occurs
            {
               errno = ERANGE;           
               *endptr = (char *) nptr + sizeof(char) * strlen(nptr);
               return LONG_MIN;
            }
            else
            {
               ret_value = ret_value * base - char_to_int( nptr[cnt] );
            }
         }
      }
   }




   // if encounter a sign, expect a number after that 
   // if a negative sign is encountered, change the sign





   
   // the exact algorithm is not given, but these are 
   // the tasks the function has to perform:  
   //
   // determine the actual base if the base is 0
   // if it starts with '0', it is octal
   // if '0' is followed by 'x' or 'X' it is hexadecimal or base 16
   // otherwise, it is decimal
   // therefore you have to determine the actual base and the starting
   // location to perform conversion
   //
   // for bases 2 to 15, perform conversion as in PE02
   //
   // for base 16, you have to decide when the conversion should
   // start: after the optional "0x" or "0X" or immediately
   // question: if you have "0xG", what should be converted?
   //
   // for bases 17 to 36, perform conversion as in PE02
   // 
   // after you have determine the real base, and where to start,
   // perform the conversion






















   // clean up, set ret_value properly if the conversion went out of range
   // set errno if necessary
   // set *endptr properly if the endptr is not NULL






   *endptr = (char *) nptr + sizeof(char) * strlen(nptr);
   return ret_value;
}
Esempio n. 19
0
static void
_dopr(char **sbuffer,
      char **buffer,
      size_t *maxlen,
      size_t *retlen, int *truncated, const char *format, va_list args)
{
    char ch;
    LLONG value;
    LDOUBLE fvalue;
    char *strvalue;
    int min;
    int max;
    int state;
    int flags;
    int cflags;
    size_t currlen;

    state = DP_S_DEFAULT;
    flags = currlen = cflags = min = 0;
    max = -1;
    ch = *format++;

    while (state != DP_S_DONE) {
        if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
            state = DP_S_DONE;

        switch (state) {
        case DP_S_DEFAULT:
            if (ch == '%')
                state = DP_S_FLAGS;
            else
                doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
            ch = *format++;
            break;
        case DP_S_FLAGS:
            switch (ch) {
            case '-':
                flags |= DP_F_MINUS;
                ch = *format++;
                break;
            case '+':
                flags |= DP_F_PLUS;
                ch = *format++;
                break;
            case ' ':
                flags |= DP_F_SPACE;
                ch = *format++;
                break;
            case '#':
                flags |= DP_F_NUM;
                ch = *format++;
                break;
            case '0':
                flags |= DP_F_ZERO;
                ch = *format++;
                break;
            default:
                state = DP_S_MIN;
                break;
            }
            break;
        case DP_S_MIN:
            if (isdigit((unsigned char)ch)) {
                min = 10 * min + char_to_int(ch);
                ch = *format++;
            } else if (ch == '*') {
                min = va_arg(args, int);
                ch = *format++;
                state = DP_S_DOT;
            } else
                state = DP_S_DOT;
            break;
        case DP_S_DOT:
            if (ch == '.') {
                state = DP_S_MAX;
                ch = *format++;
            } else
                state = DP_S_MOD;
            break;
        case DP_S_MAX:
            if (isdigit((unsigned char)ch)) {
                if (max < 0)
                    max = 0;
                max = 10 * max + char_to_int(ch);
                ch = *format++;
            } else if (ch == '*') {
                max = va_arg(args, int);
                ch = *format++;
                state = DP_S_MOD;
            } else
Esempio n. 20
0
static int activate_proc(struct menu_item *item)
{
  struct item_data *data = item->data;
  if (data->active) {
    if (data->callback_proc && data->callback_proc(item,
                                                   MENU_CALLBACK_DEACTIVATE,
                                                   data->callback_data))
    {
      return 1;
    }
    int sig = 0;
    int exp = 0;
    char *p = data->item->text;
    int sig_sign;
    int exp_sign;
    int mul;

    if (data->sig_sign->text[0] == '+')
      sig_sign = 1;
    else {
      sig_sign = -1;
      *p++ = '-';
    }
    data->item->text[0] = data->sig_sign->text[0];
    mul = 1;
    for (int i = data->sig_precis - 1; i >= 0; --i) {
      int x = i;
      if (i > 0)
        ++x;
      int n = data->sig_digits[i]->text[0];
      sig += char_to_int(n) * mul;
      mul *= 10;
      p[x] = n;
    }
    p[1] = '.';
    p += data->sig_precis + 1;
    *p++ = 'e';

    for (int i = 0; i < data->exp_precis; ++i) {
      if (data->exp_digits[i]->text[0] != '0')
        break;
      if (i == data->exp_precis - 1)
        data->exp_sign->text[0] = '+';
    }
    if (data->exp_sign->text[0] == '+')
      exp_sign = 1;
    else {
      exp_sign = -1;
      *p++ = '-';
    }
    data->item->text[3 + data->sig_precis] = data->exp_sign->text[0];
    mul = 1;
    for (int i = data->exp_precis - 1; i >= 0; --i) {
      int n = data->exp_digits[i]->text[0];
      exp += char_to_int(n) * mul;
      mul *= 10;
      p[i] = n;
    }
    p[data->exp_precis] = 0;

    float exp_mul = pow(10., exp * exp_sign - (data->sig_precis - 1));
    data->value = sig * sig_sign * exp_mul;

    if (data->callback_proc)
      data->callback_proc(item, MENU_CALLBACK_CHANGED, data->callback_data);
  }
  else {
    if (data->callback_proc && data->callback_proc(item,
                                                   MENU_CALLBACK_ACTIVATE,
                                                   data->callback_data))
    {
      return 1;
    }
  }
  data->active = !data->active;
  return 1;
}
Esempio n. 21
0
static void 
dopr(char *buffer, size_t maxlen, const char *format, va_list args)
{
	char *strvalue, ch;
	long value;
	long double fvalue;
	int min = 0, max = -1, state = DP_S_DEFAULT, flags = 0, cflags = 0;
	size_t currlen = 0;
  
	ch = *format++;

	while (state != DP_S_DONE) {
		if ((ch == '\0') || (currlen >= maxlen)) 
			state = DP_S_DONE;

		switch(state) {
		case DP_S_DEFAULT:
			if (ch == '%') 
				state = DP_S_FLAGS;
			else 
				dopr_outch(buffer, &currlen, maxlen, ch);
			ch = *format++;
			break;
		case DP_S_FLAGS:
			switch (ch) {
			case '-':
				flags |= DP_F_MINUS;
				ch = *format++;
				break;
			case '+':
				flags |= DP_F_PLUS;
				ch = *format++;
				break;
			case ' ':
				flags |= DP_F_SPACE;
				ch = *format++;
				break;
			case '#':
				flags |= DP_F_NUM;
				ch = *format++;
				break;
			case '0':
				flags |= DP_F_ZERO;
				ch = *format++;
				break;
			default:
				state = DP_S_MIN;
				break;
			}
			break;
		case DP_S_MIN:
			if (isdigit((unsigned char)ch)) {
				min = 10 * min + char_to_int (ch);
				ch = *format++;
			} else if (ch == '*') {
				min = va_arg (args, int);
				ch = *format++;
				state = DP_S_DOT;
			} else 
				state = DP_S_DOT;
			break;
		case DP_S_DOT:
			if (ch == '.') {
				state = DP_S_MAX;
				ch = *format++;
			} else 
				state = DP_S_MOD;
			break;
		case DP_S_MAX:
			if (isdigit((unsigned char)ch)) {
				if (max < 0)
					max = 0;
				max = 10 * max + char_to_int(ch);
				ch = *format++;
			} else if (ch == '*') {
				max = va_arg (args, int);
				ch = *format++;
				state = DP_S_MOD;
			} else 
Esempio n. 22
0
void debug_printf(void) {
    char buffer[4096];
    size_t maxlen = sizeof(buffer);

    int fp = GET_REG16(7)+2;
    const char *format = (char*) &memory[GET_WORD(fp)];
    fp += 2;

    char ch;
    long value;
    char *strvalue;
    int min;
    int max;
    int state;
    int flags;
    int cflags;
    size_t currlen;
    
    state = DP_S_DEFAULT;
    currlen = flags = cflags = min = 0;
    max = -1;
    ch = *format++;
    
    while (state != DP_S_DONE) {
	if (ch == '\0') 
	    state = DP_S_DONE;
	
	switch(state) {
	case DP_S_DEFAULT:
	    if (ch == '%') 
		state = DP_S_FLAGS;
	    else 
		dopr_outch (buffer, &currlen, maxlen, ch);
	    ch = *format++;
	    break;
	case DP_S_FLAGS:
	    switch (ch) {
	    case '-':
		flags |= DP_F_MINUS;
		ch = *format++;
		break;
	    case '+':
		flags |= DP_F_PLUS;
		ch = *format++;
		break;
	    case ' ':
		flags |= DP_F_SPACE;
		ch = *format++;
		break;
	    case '#':
		flags |= DP_F_NUM;
		ch = *format++;
		break;
	    case '0':
		flags |= DP_F_ZERO;
		ch = *format++;
		break;
	    default:
		state = DP_S_MIN;
		break;
	    }
	    break;
	case DP_S_MIN:
	    if (isdigit((unsigned char)ch)) {
		min = 10*min + char_to_int (ch);
		ch = *format++;
	    } else if (ch == '*') {
		min = (short) GET_WORD(fp);
		fp += 2;
		ch = *format++;
		state = DP_S_DOT;
	    } else {
		state = DP_S_DOT;
	    }
	    break;
	case DP_S_DOT:
	    if (ch == '.') {
		state = DP_S_MAX;
		ch = *format++;
	    } else { 
		state = DP_S_MOD;
	    }
	    break;
	case DP_S_MAX:
	    if (isdigit((unsigned char)ch)) {
		if (max < 0)
		    max = 0;
		max = 10*max + char_to_int (ch);
		ch = *format++;
	    } else if (ch == '*') {
		min = (short) GET_WORD(fp);
		fp += 2;
		ch = *format++;
		state = DP_S_MOD;
	    } else {
		state = DP_S_MOD;
	    }
	    break;
	case DP_S_MOD:
	    switch (ch) {
	    case 'h':
		cflags = DP_C_SHORT;
		ch = *format++;
		break;
	    case 'l':
		cflags = DP_C_LONG;
		ch = *format++;
		break;
	    default:
		break;
	    }
	    state = DP_S_CONV;
	    break;
	case DP_S_CONV:
	    switch (ch) {
	    case 'd':
	    case 'i':
		if (cflags == DP_C_LONG) {
		    value = ((int) (short) GET_WORD(fp)) << 16
			| GET_WORD(fp+2);
		    fp += 4;
		} else {
		    value = (short) GET_WORD(fp);
		    fp += 2;
		}
		fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
		break;
	    case 'o':
		flags |= DP_F_UNSIGNED;
		if (cflags == DP_C_LONG) {
		    value = ((unsigned int) GET_WORD(fp)) << 16
			| GET_WORD(fp+2);
		    fp += 4;
		} else {
		    value = GET_WORD(fp);
		    fp += 2;
		}
		fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
		break;
	    case 'u':
		flags |= DP_F_UNSIGNED;
		if (cflags == DP_C_LONG) {
		    value = ((unsigned int) GET_WORD(fp)) << 16
			| GET_WORD(fp+2);
		    fp += 4;
		} else {
		    value = GET_WORD(fp);
		    fp += 2;
		}
		fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
		break;
	    case 'X':
		flags |= DP_F_UP;
	    case 'x':
		flags |= DP_F_UNSIGNED;
		if (cflags == DP_C_LONG) {
		    value = ((unsigned int) GET_WORD(fp)) << 16
			| GET_WORD(fp+2);
		    fp += 4;
		} else {
		    value = GET_WORD(fp);
		    fp += 2;
		}
		fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
		break;
	    case 'c':
		dopr_outch (buffer, &currlen, maxlen, 
			    (int)(short)GET_WORD(fp));
		fp += 2;
		break;
	    case 's':
		{
		    int addr = GET_WORD(fp);
		    strvalue = (char*) &memory[addr];
		    if (!addr) 
			strvalue = "(NULL)";
		    if (max == -1) {
			max = strlen(strvalue);
		    }
		    if (min > 0 && max >= 0 && min > max) max = min;
		    fmtstr (buffer, &currlen, maxlen, strvalue, flags, 
			    min, max);
		}
		break;
	    case 'p':
		value = GET_WORD(fp);
		fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
		break;
	    case '%':
		dopr_outch (buffer, &currlen, maxlen, ch);
		break;
	    default:
		/* Unknown, skip */
		break;
	    }
	    ch = *format++;
	    state = DP_S_DEFAULT;
	    flags = cflags = min = 0;
	    max = -1;
	    break;
	case DP_S_DONE:
	    break;
	default:
	    /* hmm? */
	    break; /* some picky compilers need this */
	}
    }
    if (maxlen != 0) {
	if (currlen < maxlen - 1) 
	    buffer[currlen] = '\0';
	else if (maxlen > 0) 
	    buffer[maxlen - 1] = '\0';
    }
    
    printf("Program said: `%s'\n", buffer);
}