string translateRk86(cstring in) { string out; out.resize(in.size()); const char* inp = in.c_str(); char* o = (char*)out.c_str(); while(*inp) { char c = translateRk86c(*inp++); if(c==0) raise("Имя файла "+in+" содержит неподдерживаемый РК86 символ."); *o++ = c; } return out; }
bool fromstring(cstring s, double& out) { out = 0; auto tmp_s = s.c_str(); const char * tmp_cp = tmp_s; if (*tmp_cp != '-' && !isdigit(*tmp_cp)) return false; char * tmp_cpo; double ret = strtod(drop0x(tmp_cp), &tmp_cpo); if (tmp_cpo != tmp_cp + s.length()) return false; if (!isdigit(tmp_cpo[-1])) return false; if (ret==HUGE_VAL || ret==-HUGE_VAL) return false; out = ret; return true; }
cstring ReferenceMap::newName(cstring base) { // Maybe in the future we'll maintain information with per-scope identifiers, // but today we are content to generate globally-unique identifiers. // If base has a suffix of the form _(\d+), then we discard the suffix. // under the assumption that it is probably a generated suffix. // This will not impact correctness. unsigned len = base.size(); const char digits[] = "0123456789"; const char* s = base.c_str(); while (len > 0 && strchr(digits, s[len-1])) len--; if (len > 0 && base[len - 1] == '_') base = base.substr(0, len - 1); cstring name = cstring::make_unique(usedNames, base, '_'); usedNames.insert(name); return name; }
int wunlink(const wcstring &file_name) { const cstring tmp = wcs2string(file_name); return unlink(tmp.c_str()); }
int waccess(const wcstring &file_name, int mode) { const cstring tmp = wcs2string(file_name); return access(tmp.c_str(), mode); }
int lwstat(const wcstring &file_name, struct stat *buf) { const cstring tmp = wcs2string(file_name); return lstat(tmp.c_str(), buf); }
DIR *wopendir(const wcstring &name) { const cstring tmp = wcs2string(name); return opendir(tmp.c_str()); }
void ControlBodyTranslator::compileEmitField(const IR::Expression* expr, cstring field, unsigned alignment, EBPFType* type) { unsigned widthToEmit = dynamic_cast<IHasWidth*>(type)->widthInBits(); cstring swap = ""; if (widthToEmit == 16) swap = "htons"; else if (widthToEmit == 32) swap = "htonl"; if (!swap.isNullOrEmpty()) { builder->emitIndent(); visit(expr); builder->appendFormat(".%s = %s(", field.c_str(), swap); visit(expr); builder->appendFormat(".%s)", field.c_str()); builder->endOfStatement(true); } auto program = control->program; unsigned bitsInFirstByte = widthToEmit % 8; if (bitsInFirstByte == 0) bitsInFirstByte = 8; unsigned bitsInCurrentByte = bitsInFirstByte; unsigned left = widthToEmit; for (unsigned i=0; i < (widthToEmit + 7) / 8; i++) { builder->emitIndent(); builder->appendFormat("%s = ((char*)(&", program->byteVar.c_str()); visit(expr); builder->appendFormat(".%s))[%d]", field.c_str(), i); builder->endOfStatement(true); unsigned freeBits = alignment == 0 ? (8 - alignment) : 8; unsigned bitsToWrite = bitsInCurrentByte > freeBits ? freeBits : bitsInCurrentByte; BUG_CHECK((bitsToWrite > 0) && (bitsToWrite <= 8), "invalid bitsToWrite %d", bitsToWrite); builder->emitIndent(); if (alignment == 0) builder->appendFormat("write_byte(%s, BYTES(%s) + %d, (%s) << %d)", program->packetStartVar.c_str(), program->offsetVar.c_str(), i, program->byteVar.c_str(), 8 - bitsToWrite); else builder->appendFormat("write_partial(%s + BYTES(%s) + %d, %d, (%s) << %d)", program->packetStartVar.c_str(), program->offsetVar.c_str(), i, alignment, program->byteVar.c_str(), 8 - bitsToWrite); builder->endOfStatement(true); left -= bitsToWrite; bitsInCurrentByte -= bitsToWrite; if (bitsInCurrentByte > 0) { builder->emitIndent(); builder->appendFormat( "write_byte(%s, BYTES(%s) + %d + 1, (%s << %d))", program->packetStartVar.c_str(), program->offsetVar.c_str(), i, program->byteVar.c_str(), 8 - alignment % 8); builder->endOfStatement(true); left -= bitsInCurrentByte; } alignment = (alignment + bitsToWrite) % 8; bitsInCurrentByte = left >= 8 ? 8 : left; } builder->emitIndent(); builder->appendFormat("%s += %d", program->offsetVar.c_str(), widthToEmit); builder->endOfStatement(true); }