void Codec::Checksum(const ValueList& args, KValueRef result) { args.VerifyException("checksum", "s|o ?i"); int type = CODEC_CRC32; if (args.size() == 2) { type = args.at(1)->ToInt(); } Poco::Checksum *checksum = NULL; switch(type) { case CODEC_CRC32: { checksum = new Poco::Checksum(Poco::Checksum::TYPE_CRC32); break; } case CODEC_ADLER32: { checksum = new Poco::Checksum(Poco::Checksum::TYPE_ADLER32); break; } default: { std::ostringstream msg("Unsupported type: "); msg << type; throw ValueException::FromString(msg.str()); } } if (args.at(0)->IsString()) { std::string encoded = args.at(0)->ToString(); const char *data = encoded.c_str(); checksum->update(data,encoded.size()); result->SetInt(checksum->checksum()); } else if (args.at(0)->IsObject()) { BytesRef bytes = args.at(0)->ToObject().cast<Bytes>(); if (bytes.isNull()) { delete checksum; throw ValueException::FromString("unsupported data type passed as argument 1"); } checksum->update(bytes->Pointer(),bytes->Length()); result->SetInt(checksum->checksum()); } else { delete checksum; throw ValueException::FromString("unsupported data type passed as argument 1"); } delete checksum; }
void PropertiesBinding::Getter(const ValueList& args, KValueRef result, Type type) { if (args.size() > 0 && args.at(0)->IsString()) { std::string eprefix = "PropertiesBinding::Get: "; try { std::string property = args.at(0)->ToString(); if (args.size() == 1) { switch (type) { case Bool: result->SetBool(config->getBool(property)); break; case Double: result->SetDouble(config->getDouble(property)); break; case Int: result->SetInt(config->getInt(property)); break; case String: result->SetString(config->getString(property).c_str()); break; default: break; } return; } else if (args.size() >= 2) { switch (type) { case Bool: result->SetBool(config->getBool(property, args.at(1)->ToBool())); break; case Double: result->SetDouble(config->getDouble(property, args.at(1)->ToDouble())); break; case Int: result->SetInt(config->getInt(property, args.at(1)->ToInt())); break; case String: result->SetString(config->getString(property, args.at(1)->ToString()).c_str()); break; default: break; } return; } } catch(Poco::Exception &e) { throw ValueException::FromString(eprefix + e.displayText()); } } }
void ResultSetBinding::FieldCount(const ValueList& args, KValueRef result) { if (rs.isNull()) { result->SetInt(0); } else { result->SetInt(rs->columnCount()); } }
void ApplicationBinding::_GetPID(const ValueList& args, KValueRef result) { if (this->current) { result->SetInt(GETPID()); } }
void Process::_GetPID(const ValueList& args, KValueRef result) { int pid = GetPID(); if (pid != -1) result->SetInt(GetPID()); else result->SetNull(); }
void Bytes::_ByteAt(const ValueList& args, KValueRef result) { args.VerifyException("Bytes.byteAt", "n"); size_t position = args.GetInt(0); if (position >= 0 && position < this->size) { result->SetInt(static_cast<unsigned char>(this->buffer[position])); } }
void Bytes::_LastIndexOf(const ValueList& args, KValueRef result) { // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/lastIndexOf args.VerifyException("Bytes.lastIndexOf", "s,?i"); std::string target(this->AsString()); int start = args.GetInt(1, target.length() + 1); if (start < 0) start = 0; size_t pos = target.rfind(args.GetString(0), start); if (pos == std::string::npos) { // No matches found result->SetInt(-1); } else { result->SetInt(pos); } }
void Network::_AddConnectivityListener(const ValueList& args, KValueRef result) { args.VerifyException("addConnectivityListener", "m"); KMethodRef target = args.at(0)->ToMethod(); static long nextListenerId = 0; Listener listener = Listener(); listener.id = nextListenerId++; listener.callback = target; this->listeners.push_back(listener); result->SetInt(listener.id); }
void APIBinding::_ComponentGUIDToComponentType(const ValueList& args, KValueRef result) { std::string type = args.at(0)->ToString(); if (type == RUNTIME_UUID) { result->SetInt(RUNTIME); } else if (type == MODULE_UUID) { result->SetInt(MODULE); } else if (type == SDK_UUID) { result->SetInt(SDK); } else if (type == MOBILESDK_UUID) { result->SetInt(MOBILESDK); } else if (type == APP_UPDATE_UUID) { result->SetInt(APP_UPDATE); } else { result->SetInt(UNKNOWN); } }
void ResultSetBinding::TransformValue(size_t index, KValueRef result) { MetaColumn::ColumnDataType type = rs->columnType(index); Poco::DynamicAny value = rs->value(index); if (value.isEmpty()) { result->SetNull(); } else if (type == MetaColumn::FDT_STRING) { std::string str; value.convert(str); result->SetString(str); } else if (type == MetaColumn::FDT_BOOL) { bool v = false; value.convert(v); result->SetBool(v); } else if (type == MetaColumn::FDT_FLOAT || type == MetaColumn::FDT_DOUBLE) { float f = 0; value.convert(f); result->SetDouble(f); } else if (type == MetaColumn::FDT_BLOB || type == MetaColumn::FDT_UNKNOWN) { std::string str; value.convert(str); result->SetString(str); } else { // the rest of these are ints: // FDT_INT8, // FDT_UINT8, // FDT_INT16, // FDT_UINT16, // FDT_INT32, // FDT_UINT32, // FDT_INT64, // FDT_UINT64, int i; value.convert(i); result->SetInt(i); } }
void Bytes::_Write(const ValueList& args, KValueRef result) { args.VerifyException("write", "s|o ?n"); int offset = args.GetInt(1, 0); int bytesWritten; if (args.at(0)->IsString()) { const char* str = args.at(0)->ToString(); size_t length = strlen(str); bytesWritten = Write(str, length, offset); } else { BytesRef data(args.GetObject(0).cast<Bytes>()); if (data.isNull()) { throw ValueException::FromString("May only write strings or Bytes object"); } bytesWritten = Write(data, offset); } result->SetInt(bytesWritten); }
void PlatformBinding::_GetProcessorCount(const ValueList& args, KValueRef result) { result->SetInt(GetProcessorCountImpl()); }
void HTTPCookie::GetMaxAge(const ValueList& args, KValueRef result) { result->SetInt(this->cookie.getMaxAge()); }
void DependencyBinding::_GetRequirement(const ValueList& args, KValueRef result) { result->SetInt(this->dependency->requirement); }
void DependencyBinding::_GetType(const ValueList& args, KValueRef result) { result->SetInt((int) this->dependency->type); }
void Platform::_GetProcessorCount(const ValueList& args, KValueRef result) { result->SetInt(PlatformUtils::GetProcessorCount()); }
void APIBinding::_GetLogLevel(const ValueList& args, KValueRef result) { result->SetInt(Logger::GetRootLogger()->GetLevel()); }
void ComponentBinding::_GetType(const ValueList& args, KValueRef result) { result->SetInt((int) this->component->type); }
void HttpServerRequest::GetContentLength(const ValueList& args, KValueRef result) { result->SetInt(request.getContentLength()); }