bottleimpl.cpp
来自「一个语言识别引擎」· C++ 代码 · 共 954 行 · 第 1/2 页
CPP
954 行
for (unsigned int i=0; i<content.size(); i++) {
Storable *s = content[i];
if (speciality==0) {
YMSG(("subcode %d\n",s->getCode()));
writer.appendInt(s->getCode());
} else {
YMSG(("skipped subcode %d\n",s->getCode()));
YARP_ASSERT(speciality==s->getCode());
}
if (s->isList()) {
s->asList()->setNested(true);
}
s->write(writer);
}
//buf.write(sos);
String str = writer.toString();
data.resize(str.length(),' ');
memcpy(&data[0],str.c_str(),str.length());
dirty = false;
}
}
void BottleImpl::specialize(int subCode) {
speciality = subCode;
}
int BottleImpl::getSpecialization() {
return speciality;
}
void BottleImpl::setNested(bool nested) {
this->nested = nested;
}
////////////////////////////////////////////////////////////////////////////
// StoreInt
String StoreInt::toStringFlex() const {
char buf[256];
ACE_OS::sprintf(buf,"%d",x);
return String(buf);
}
void StoreInt::fromString(const String& src) {
//x = ACE_OS::atoi(src.c_str());
x = ACE_OS::strtol(src.c_str(), (char **)NULL, 0);
}
bool StoreInt::read(ConnectionReader& reader) {
x = reader.expectInt();
return true;
}
bool StoreInt::write(ConnectionWriter& writer) {
writer.appendInt(x);
return true;
}
////////////////////////////////////////////////////////////////////////////
// StoreVocab
String StoreVocab::toStringFlex() const {
return String(Vocab::decode(x).c_str());
}
void StoreVocab::fromString(const String& src) {
x = Vocab::encode(src.c_str());
}
String StoreVocab::toStringNested() const {
return String("[") + toStringFlex() + "]";
}
void StoreVocab::fromStringNested(const String& src) {
if (src.length()>0) {
if (src[0]=='[') {
// ignore first [ and last ]
String buf = src.substr(1,src.length()-2);
fromString(buf.c_str());
}
}
}
bool StoreVocab::read(ConnectionReader& reader) {
x = reader.expectInt();
return true;
}
bool StoreVocab::write(ConnectionWriter& writer) {
writer.appendInt(x);
return true;
}
////////////////////////////////////////////////////////////////////////////
// StoreDouble
String StoreDouble::toStringFlex() const {
char buf[256];
ACE_OS::sprintf(buf,"%f",x);
String str(buf);
if (str.strstr(".")<0) {
str += ".0";
}
int ct = 0;
for (int i=str.length()-1; i>=0; i--) {
if (str[i]!='0') {
if (str[i]=='.') {
ct--;
i++;
}
if (ct>=1) {
str = str.substr(0,i+1);
}
break;
}
ct++;
}
return str;
}
void StoreDouble::fromString(const String& src) {
x = ACE_OS::strtod(src.c_str(),NULL);
}
bool StoreDouble::read(ConnectionReader& reader) {
NetFloat64 flt = 0;
reader.expectBlock((const char*)&flt,sizeof(flt));
x = flt;
return true;
}
bool StoreDouble::write(ConnectionWriter& writer) {
//writer.appendBlockCopy(Bytes((char*)&x,sizeof(x)));
NetFloat64 flt = x;
writer.appendBlock((char*)&flt,sizeof(flt));
return true;
}
////////////////////////////////////////////////////////////////////////////
// StoreString
String StoreString::toStringFlex() const {
return x;
}
String StoreString::toStringNested() const {
// quoting code: very inefficient, but portable
String result;
bool needQuote = false;
for (unsigned int i=0; i<x.length(); i++) {
char ch = x[i];
if ((ch<'a'||ch>'z')&&
(ch<'A'||ch>'Z')&&
ch!='_') {
if ((ch>='0'&&ch<='9')||ch=='.'||ch=='-') {
if (i==0) {
needQuote = true;
break;
}
} else {
needQuote = true;
break;
}
}
}
if (!needQuote) {
return x;
}
result += "\"";
for (unsigned int j=0; j<x.length(); j++) {
char ch = x[j];
if (ch=='\\'||ch=='\"') {
result += '\\';
}
result += ch;
}
result += "\"";
return result;
}
void StoreString::fromString(const String& src) {
x = src;
}
void StoreString::fromStringNested(const String& src) {
// unquoting code: very inefficient, but portable
String result = "";
x = "";
int len = src.length();
if (len>0) {
bool skip = false;
bool back = false;
if (src[0]=='\"') {
skip = true;
}
for (int i=0; i<len; i++) {
if (skip&&(i==0||i==len-1)) {
// omit
} else {
char ch = src[i];
if (ch=='\\') {
if (!back) {
back = true;
} else {
x += '\\';
back = false;
}
} else {
back = false;
x += ch;
}
}
}
}
}
bool StoreString::read(ConnectionReader& reader) {
int len = reader.expectInt();
String buf((size_t)len);
reader.expectBlock((const char *)buf.c_str(),len);
x = buf.c_str();
//x = reader.expectString(len);
return true;
}
bool StoreString::write(ConnectionWriter& writer) {
writer.appendInt(x.length()+1);
writer.appendString(x.c_str(),'\0');
return true;
}
////////////////////////////////////////////////////////////////////////////
// StoreBlob
String StoreBlob::toStringFlex() const {
String result = "";
for (unsigned int i=0; i<x.length(); i++) {
if (i>0) {
result += " ";
}
unsigned char *src = (unsigned char *)(&x[i]);
fflush(stdout);
result += NetType::toString(*src);
}
return result;
}
String StoreBlob::toStringNested() const {
return String("{") + toStringFlex() + "}";
}
void StoreBlob::fromString(const String& src) {
Bottle bot(src.c_str());
String buf((size_t)(bot.size()));
for (int i=0; i<bot.size(); i++) {
buf[i] = (char)((unsigned char)(bot.get(i).asInt()));
}
x.set(buf.c_str(),bot.size(),1);
}
void StoreBlob::fromStringNested(const String& src) {
if (src.length()>0) {
if (src[0]=='{') {
// ignore first { and last }
String buf = src.substr(1,src.length()-2);
fromString(buf.c_str());
}
}
}
bool StoreBlob::read(ConnectionReader& reader) {
int len = reader.expectInt();
String buf((size_t)len);
reader.expectBlock((const char *)buf.c_str(),len);
x.set(buf.c_str(),(size_t)len,1);
return true;
}
bool StoreBlob::write(ConnectionWriter& writer) {
writer.appendInt(x.length());
writer.appendBlock(x.c_str(),x.length());
return true;
}
////////////////////////////////////////////////////////////////////////////
// StoreList
String StoreList::toStringFlex() const {
return String(content.toString().c_str());
}
String StoreList::toStringNested() const {
return String("(") + content.toString().c_str() + ")";
}
void StoreList::fromString(const String& src) {
content.fromString(src.c_str());
}
void StoreList::fromStringNested(const String& src) {
if (src.length()>0) {
if (src[0]=='(') {
// ignore first ( and last )
String buf = src.substr(1,src.length()-2);
content.fromString(buf.c_str());
}
}
}
bool StoreList::read(ConnectionReader& reader) {
// not using the most efficient representation
content.read(reader);
return true;
}
bool StoreList::write(ConnectionWriter& writer) {
// not using the most efficient representation
content.write(writer);
return true;
}
template <class T>
int subCoder(T& content) {
int c = -1;
bool ok = false;
for (int i=0; i<content.size(); i++) {
int sc = content.get(i).getCode();
if (c==-1) {
c = sc;
ok = true;
}
if (sc!=c) {
ok = false;
}
}
// just optimize primitive types
if ((c&GROUP_MASK)!=0) {
ok = false;
}
c = ok?c:0;
content.specialize(c);
return c;
}
int StoreList::subCode() const {
Bottle *op = (Bottle*)(&content);
return subCoder(*op);
}
int BottleImpl::subCode() {
return subCoder(*this);
}
bool BottleImpl::isInt(int index) {
if (index>=0 && index<size()) {
return content[index]->getCode() == StoreInt::code;
}
return false;
}
bool BottleImpl::isString(int index) {
if (index>=0 && index<size()) {
return content[index]->getCode() == StoreString::code;
}
return false;
}
bool BottleImpl::isDouble(int index) {
if (index>=0 && index<size()) {
return content[index]->getCode() == StoreDouble::code;
}
return false;
}
bool BottleImpl::isList(int index) {
if (index>=0 && index<size()) {
return content[index]->isList();
}
return false;
}
Storable& BottleImpl::get(int index) const {
if (index>=0 && index<size()) {
return *(content[index]);
}
return storeNull;
}
int BottleImpl::getInt(int index) {
if (!isInt(index)) { return 0; }
return content[index]->asInt();
}
yarp::os::ConstString BottleImpl::getString(int index) {
if (!isString(index)) { return ""; }
return content[index]->asString();
}
double BottleImpl::getDouble(int index) {
if (!isDouble(index)) { return 0; }
return content[index]->asDouble();
}
yarp::os::Bottle *BottleImpl::getList(int index) {
if (!isList(index)) { return NULL; }
return &(((StoreList*)content[index])->internal());
}
yarp::os::Bottle& BottleImpl::addList() {
StoreList *lst = new StoreList();
add(lst);
return lst->internal();
}
void BottleImpl::copyRange(const BottleImpl& alt, int first, int len) {
clear();
if (len==-1) { len = alt.size(); }
int last = first + len - 1;
int top = alt.size()-1;
if (first<0) { first = 0; }
if (last<0) { last = 0; }
if (first>top) { first = top; }
if (last>top) { last = top; }
for (int i=first; i<=last; i++) {
add(alt.get(i).cloneStorable());
}
}
Value& Storable::find(const char *txt) {
return BottleImpl::getNull();
}
Bottle& Storable::findGroup(const char *txt) {
return Bottle::getNullBottle();
}
bool Storable::check(const char *key) {
Bottle& val = findGroup(key);
return !val.isNull();
}
bool Storable::operator == (const Value& alt) const {
return String(toString().c_str()) == alt.toString().c_str();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?