ostring and symbol work

This commit is contained in:
Louis 2015-02-27 18:24:14 +01:00
parent e810a0fbe4
commit d32a1cf701
2 changed files with 62 additions and 29 deletions

View file

@ -199,24 +199,38 @@ static bool get_symbol(const string& entry, object*& obj, unsigned int& obj_len)
{ {
bool ret = false; bool ret = false;
int entry_len = entry.size(); int entry_len = entry.size();
if (entry_len>1 && entry[0]=='\'') if (entry_len>=1 && entry[0]=='\'')
{ {
int naked_entry_len; if (entry_len == 1)
{
// total object length
obj_len = sizeof(symbol) + 1;
// entry length without prefix / postfix // allocate object
naked_entry_len = entry[entry_len-1]=='\''?(entry_len-3):(entry_len-2); obj = (symbol*)malloc(obj_len);
// total object length //set it
obj_len = sizeof(symbol) + naked_entry_len; ((symbol*)obj)->set("", 0);
}
else
{
int naked_entry_len;
// allocate object // entry length without prefix / postfix
obj = (object*)malloc(obj_len); naked_entry_len = entry[entry_len-1]=='\''?(entry_len-2):(entry_len-1);
// set it // total object length
((symbol*)obj)->set(&entry[1], naked_entry_len); obj_len = sizeof(symbol) + naked_entry_len + 1;
// allocate object
obj = (symbol*)malloc(obj_len);
// set it
((symbol*)obj)->set(entry.substr(1, naked_entry_len).c_str(), naked_entry_len);
}
ret = true; ret = true;
} }
return ret; return ret;
} }
@ -224,24 +238,38 @@ static bool get_string(const string& entry, object*& obj, unsigned int& obj_len)
{ {
bool ret = false; bool ret = false;
int entry_len = entry.size(); int entry_len = entry.size();
if (entry_len>1 && entry[0]=='"') if (entry_len>=1 && entry[0]=='"')
{ {
int naked_entry_len; if (entry_len == 1)
{
// total object length
obj_len = sizeof(ostring) + 1;
// entry length without prefix / postfix // allocate object
naked_entry_len = entry[entry_len-1]=='"'?(entry_len-3):(entry_len-2); obj = (ostring*)malloc(obj_len);
// total object length //set it
obj_len = sizeof(ostring) + naked_entry_len; ((ostring*)obj)->set("", 0);
}
else
{
int naked_entry_len;
// allocate object // entry length without prefix / postfix
obj = (object*)malloc(obj_len); naked_entry_len = entry[entry_len-1]=='"'?(entry_len-2):(entry_len-1);
// set it // total object length
((ostring*)obj)->set(&entry[1], naked_entry_len); obj_len = sizeof(ostring) + naked_entry_len + 1;
// allocate object
obj = (ostring*)malloc(obj_len);
// set it
((ostring*)obj)->set(entry.substr(1, naked_entry_len).c_str(), naked_entry_len);
}
ret = true; ret = true;
} }
return ret; return ret;
} }

View file

@ -176,7 +176,8 @@ struct ostring : public object
_type = cmd_string; _type = cmd_string;
if (value != NULL) if (value != NULL)
{ {
(void)memcpy(_value, value, len); if (len>0)
(void)memcpy(_value, value, len);
_value[len] = 0; _value[len] = 0;
_len = len; _len = len;
} }
@ -197,7 +198,8 @@ struct oprogram : public object
_type = cmd_program; _type = cmd_program;
if (value != NULL) if (value != NULL)
{ {
(void)memcpy(_value, value, len); if (len>0)
(void)memcpy(_value, value, len);
_value[len] = 0; _value[len] = 0;
_len = len; _len = len;
} }
@ -219,7 +221,8 @@ struct symbol : public object
_auto_eval = false; _auto_eval = false;
if (value != NULL) if (value != NULL)
{ {
(void)memcpy(_value, value, len); if (len>0)
(void)memcpy(_value, value, len);
_value[len] = 0; _value[len] = 0;
_len = len; _len = len;
} }
@ -242,7 +245,8 @@ struct keyword : public object
_fn = fn; _fn = fn;
if (value != NULL) if (value != NULL)
{ {
(void)memcpy(_value, value, len); if (len>0)
(void)memcpy(_value, value, len);
_value[len] = 0; _value[len] = 0;
_len = len; _len = len;
} }
@ -271,7 +275,8 @@ struct branch : public object
arg_bool = 0; arg_bool = 0;
if (value != NULL) if (value != NULL)
{ {
(void)memcpy(_value, value, len); if (len>0)
(void)memcpy(_value, value, len);
_value[len] = 0; _value[len] = 0;
_len = len; _len = len;
} }
@ -321,17 +326,17 @@ void object::show(ostream& stream)
} }
break; break;
case cmd_string: case cmd_string:
stream << "\"" << *((ostring*)this)->_value << "\""; stream << "\"" << ((ostring*)this)->_value << "\"";
break; break;
case cmd_program: case cmd_program:
stream << "<< " << *((oprogram*)this)->_value << " >>"; stream << "<< " << ((oprogram*)this)->_value << " >>";
break; break;
case cmd_symbol: case cmd_symbol:
stream << "'" << *((symbol*)this)->_value << "'"; stream << "'" << ((symbol*)this)->_value << "'";
break; break;
case cmd_keyword: case cmd_keyword:
case cmd_branch: case cmd_branch:
stream << *((keyword*)this)->_value; stream << ((keyword*)this)->_value;
break; break;
default: default:
stream << "< unknown object representation >"; stream << "< unknown object representation >";