, Erlang , .
, ...
while (read_cmd(buf) > 0) {
fn = buf[0];
arg = buf[1];
:
while (arg=read_cmd(buf) > 0) {
fn = buf[0];
ints, .
.
------------ -----------------
, c:
#include <stdio.h>
#include <math.h>
union u_unionalue
{
char char_union;
unsigned char u_char_union;
short s_int_union;
unsigned short us_int_union;
int int_union;
unsigned int u_int_union;
long l_int_union;
unsigned long ul_int_union;
long long ll_int_union;
unsigned long long ull_int_union;
float float_union;
double double_union;
long double l_double_union;
};
enum e_type
{
char_enum ,
u_char_enum ,
s_int_enum ,
us_int_enum ,
int_enum ,
u_int_enum ,
l_int_enum ,
ul_int_enum ,
ll_int_enum ,
ull_int_enum ,
float_enum ,
double_enum ,
l_double_enum,
last_type
};
struct s_type
{
int type;
char *name;
union u_unionalue value;
int size;
char *stringFormat;
} as_typeList[]=
{
{ char_enum , "char" , {.char_union=(1 << (-1 + sizeof( char ) * 8 ))}, sizeof( char ),"%+d" },
{ u_char_enum , "unsigned char" , {.u_char_union=-1} , sizeof( unsigned char ) ,"%+d" },
{ s_int_enum , "short" , {.s_int_union=((short)1 << (-1 + sizeof( short ) * 8))} , sizeof( short ),"%+d" },
{ us_int_enum , "unsigned short" , {.us_int_union=-1}, sizeof( unsigned short ),"%+u" },
{ int_enum , "int" , {.int_union = ((int)1<< (-1 + sizeof( int) * 8 ))}, sizeof( int), "%+i" },
{ u_int_enum , "unsigned int" , {.u_int_union=-1}, sizeof( unsigned int ), "%+u" },
{ l_int_enum , "long" , {.l_int_union=((long)1<< (-1 + sizeof( long) * 8 ))}, sizeof( long ), "%+li" },
{ ul_int_enum , "unsigned long" , {.ul_int_union=(long)-1}, sizeof( unsigned long ), "%+lu" },
{ ll_int_enum , "long long" , {.ll_int_union=(long long)-1 }, sizeof( long long ), "%+lli"},
{ ull_int_enum , "unsigned long long", {.ull_int_union=((unsigned long long)1<< (-1 + sizeof( unsigned long long) * 8 ))}, sizeof( unsigned long long ), "%+llu"},
{ float_enum , "float" , {.float_union=1e+37L}, sizeof( float ), "%+f" },
{ double_enum , "double" , {.double_union=1e+37L}, sizeof( double ), "%+lf" },
{ l_double_enum, "long double" , {.l_double_union=1e+37L}, sizeof( long double), "%+lle"}
};
void sprintVal(struct s_type *typeVal, char*buf)
{
switch(typeVal->type)
{
case char_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.char_union);
break;
case u_char_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.u_char_union);
break;
case s_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.s_int_union);
break;
case us_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.us_int_union);
break;
case int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.int_union);
break;
case u_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.u_int_union);
break;
case l_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.l_int_union);
break;
case ul_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.ul_int_union);
break;
case ll_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.ll_int_union);
break;
case ull_int_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.ull_int_union);
break;
case float_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.float_union);
break;
case double_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.double_union);
break;
case l_double_enum :
sprintf(buf, typeVal->stringFormat, typeVal->value.l_double_union);
break;
}
}
void print_types()
{
int i=0;
char buf[100];
while(i < last_type )
{
sprintVal( &as_typeList[i], buf);
printf( "Type: %-18s value=%-30s size= %-dBytes \n", as_typeList[i].name, buf, as_typeList[i].size );
i++;
};
}
int main(int argc, char** argv)
{
print_types();
return(0);
}
The biggest problem in your problem is the messaging between erlang and c. Your current format is / CMD / VALUE / minimum should be / CMD / TYPE / VALUE /, although the common message header and the checksum footer are common. Types should be encouraged so that greater value can be returned. Knowing what you are going through is necessary.
Oh, if you pass the data as a string, you can change your type to some extent. It also prevents problems that occur when there is a difference between the ends of the pipe.