//============================================================================ // NDataEncoder::B64_Encode : Encode to Base64. //---------------------------------------------------------------------------- NString NDataEncoder::B64_Encode(const NData &theValue) { NData theBuffer; NString theString; base64_encodestate theState; char *dataPtr; NIndex dataSize; // Get the state we need base64_init_encodestate(&theState); if (theValue.IsEmpty() || !theBuffer.SetSize(theValue.GetSize() * 2)) return(theString); // Encode the value dataPtr = (char *) theBuffer.GetData(); dataSize = base64_encode_block((const char *) theValue.GetData(), theValue.GetSize(), dataPtr, &theState); theString = NString(dataPtr, dataSize); dataSize = base64_encode_blockend(dataPtr, &theState); if (dataSize != 0) theString += NString(dataPtr, dataSize); // Remove the trailing newline NN_ASSERT(theString.GetRight(1) == "\n"); theString.TrimRight(1); return(theString); }
NString Socket::recv() { if (_status == SocketStatus::CONNECTED) { return NString(); } memset(_buf, 0, 8192); int result = ::recv(_socket, _buf, 8192, 0); if (result == 0) { setError(SocketError::RECV_ERROR); } else if (result == -1) { setError(SocketError::BROKEN_PIPE); } if (result > 0) { return NString(_buf, result); } else { return NString(); } }
//============================================================================ // NDataEncoder::Hex_Encode : Encode to hex. //---------------------------------------------------------------------------- NString NDataEncoder::Hex_Encode(const NData &theValue) { NIndex n, theSize; NData tmpBuffer; NString theResult; const uint8_t *dataPtr; char *textPtr; // Get the state we need theSize = theValue.GetSize(); dataPtr = theValue.GetData(); textPtr = (char *) tmpBuffer.AppendData((theSize * 2) + 1); if (theSize == 0 || dataPtr == NULL || textPtr == NULL) return(theResult); // Convert to a string for (n = 0; n < theSize; n++) { sprintf(textPtr, "%.2X", dataPtr[n]); textPtr += 2; } theResult = NString(tmpBuffer); return(theResult); }
//============================================================================ // NURL::NURL : Constructor. //---------------------------------------------------------------------------- NURL::NURL(const char *theURL) { // Initialize ourselves mValue = NString(theURL); }
//============================================================================ // NFileUtilities::GetFileText : Get a file as text. //---------------------------------------------------------------------------- NString NFileUtilities::GetFileText(const NFile &theFile, NStringEncoding theEncoding) { NStringEncoder theEncoder; NData theData; NString theText; // Get the state we need theData = GetFileData(theFile); if (theData.IsEmpty()) return(theText); // Get the text if (theEncoding == kNStringEncodingInvalid) { theEncoding = theEncoder.GetEncoding(theData); NN_ASSERT(theEncoding != kNStringEncodingInvalid); } if (theEncoding != kNStringEncodingInvalid) theText = NString(theData, theEncoding); return(theText); }
NServiceClientFactoryConfig * NServiceClientFactoryConfigFile::parseConfigEntry(pugi::xml_node & node) { NServiceClientFactoryConfig * config = NULL; try { config = new NServiceClientFactoryConfig(); for (pugi::xml_node setting : node.children()) { if (setting.type() == pugi::node_element) { if (NString("name").compare(setting.name()) == 0) { config->setName(setting.child_value()); } else if (NString("factory").compare(setting.name()) == 0) { config->setFactory(setting.child_value()); } else if (NString("component").compare(setting.name()) == 0) { config->setComponent(setting.child_value()); } else if (NString("library").compare(setting.name()) == 0) { config->setLibrary(setting.child_value()); } } } if (config->getName().empty() || config->getFactory().empty() || config->getComponent().empty() || config->getLibrary().empty()) { throw NRuntimeException("Service client factory configuratin is incomplete"); } } catch (...) { if (config != NULL) delete config; return NULL; } return config; }
NConnectionConfig * NConnectionConfigFile::parseConfigEntry(pugi::xml_node & node) { NConnectionConfig * config = NULL; try { config = new NConnectionConfig(); for (pugi::xml_node setting : node.children()) { if (setting.type() == pugi::node_element) { if (NString("name").compare(setting.name()) == 0) { config->setName(setting.child_value()); } else if (NString("host").compare(setting.name()) == 0) { config->setHost(setting.child_value()); } else if (NString("port").compare(setting.name()) == 0) { config->setPort(NUnsignedShort::fromString(setting.child_value())); } else if (NString("user").compare(setting.name()) == 0) { config->setUser(setting.child_value()); } else if (NString("password").compare(setting.name()) == 0) { config->setPassword(setting.child_value()); } else if (NString("schema").compare(setting.name()) == 0) { config->setSchema(setting.child_value()); } else if (NString("type").compare(setting.name()) == 0) { config->setConnectionType(NConnectionType::getByValue(setting.child_value())); } } } if (config->getName().empty() || config->getHost().empty() || config->getUser().empty() || config->getPassword().empty() || config->getSchema().empty() || config->getPort() == NUnsignedShort(0) || config->getConnectionType() == NConnectionType::UNKNOWN) { throw NRuntimeException("Connection configuration is incomplete"); } } catch (...) { if (config != NULL) delete config; return NULL; } return config; }
std::string NString::trim() { int i = 0; while (i < (int)size() && at(i) > 0 && isspace(at(i))) i++; NString str = NString(substr(i)); if (str != "") { i = size() - 1; while (i > 0 && at(i) > 0 && isspace(at(i))) i--; if (++i < (int)this->size()) str.at(i) = '\0'; } return str; }
//============================================================================ // NFileUtilities::GetCWD : Get the current working directory. //---------------------------------------------------------------------------- NFile NFileUtilities::GetCWD(void) { char *thePath; NFile theFile; // Get the directory thePath = NTargetPOSIX::getcwd(NULL, 0); if (thePath != NULL) { theFile = NFile(NString(thePath, kNStringLength)); free(thePath); } return(theFile); }
//============================================================================ // NDBResult::GetValueString : Get a string value. //---------------------------------------------------------------------------- NString NDBResult::GetValueString(NIndex theIndex) const { NString theResult; const void *thePtr; NIndex theSize; // Validate our parameters NN_ASSERT(theIndex < GetSize()); // Get the value thePtr = (const void *) sqlite3_column_text( (sqlite3_stmt *) mResult, theIndex); theSize = (NIndex ) sqlite3_column_bytes((sqlite3_stmt *) mResult, theIndex); if (thePtr != NULL && theSize != 0) theResult = NString(thePtr, theSize, kNStringEncodingUTF8); return(theResult); }
INList<NConnectionConfig *> * NConnectionConfigFile::parseXmlDocument(pugi::xml_document * document) { INList<NConnectionConfig *> * result = new NList<NConnectionConfig *>(); pugi::xml_node connections = document->child("databases"); if (connections.type() != pugi::node_null) { for (pugi::xml_node connection : connections.children()) { if (connection.type() == pugi::node_element) { if (NString("database").compare(connection.name()) == 0) { NConnectionConfig * config = parseConfigEntry(connection); if (config != NULL) result->add(config); } } } } return result; }
INList<NServiceClientFactoryConfig *> * NServiceClientFactoryConfigFile::parseXmlDocument(pugi::xml_document * document) { INList<NServiceClientFactoryConfig *> * result = new NList<NServiceClientFactoryConfig *>(); pugi::xml_node factories = document->child("factories"); if (factories.type() != pugi::node_null) { for (pugi::xml_node factory : factories.children()) { if (factory.type() == pugi::node_element) { if (NString("factory").compare(factory.name()) == 0) { NServiceClientFactoryConfig * config = parseConfigEntry(factory); if (config != NULL) result->add(config); } } } } return result; }
static NString GetLongProductName(const NLicenseProductInfo & productInfo) { HNString handle; NCheck(NLicManGetLongProductNameForProductInfoN(productInfo.GetHandle(), &handle)); return NString(handle, true); }
static NString GetShortProductName(NUInt productId, NLicenseType type) { HNString handle; NCheck(NLicManGetShortProductNameN(productId, type, &handle)); return NString(handle, true); }
// Test case //---------------------------------------------------------------------------- TEST_NENCODER("Encoding") { NData dataXML, dataBinary; UInt32 adlerData; NDataDigest theDigest; NVariant theValue; NString textXML; // Perform the test dataXML = theEncoder.Encode(theObject, kNEncoderXML); dataBinary = theEncoder.Encode(theObject, kNEncoderBinary); textXML = NString(dataXML); adlerData = theDigest.GetAdler32(dataBinary); REQUIRE(textXML == kResultXML); REQUIRE(adlerData == kResultBinary); } //============================================================================ // Test case //---------------------------------------------------------------------------- TEST_NENCODER("Decoding") { NData dataXML, dataBinary;
NString GetVendorValue() const { return NString(hVendorValue, false); }
namespace nkit { NString NString::Empty = NString(""); NString::NString() : std::string() { } NString::NString(const std::string& value) : std::string(value) { } NString::NString(cstr value) : std::string(value) { } int NString::indexOf(char ch) { size_t s = find(ch); return (s >= size()) ? -1 : s; } static NString Empty; std::string NString::trim_start() { int i = 0; while (i < (int)size() && isspace(at(i))) i++; return substr(i); } std::string NString::trim() { int i = 0; while (i < (int)size() && at(i) > 0 && isspace(at(i))) i++; NString str = NString(substr(i)); if (str != "") { i = size() - 1; while (i > 0 && at(i) > 0 && isspace(at(i))) i--; if (++i < (int)this->size()) str.at(i) = '\0'; } return str; } bool NString::ends_with(cstr string) { int lg = strlen(string); return memcmp(&(c_str()[size() - lg]), string, lg) == 0; } std::string NString::extract_until(char ch, int from, char escape) { std::string pfx; int k = -1; while (k < 0) { k = find(ch, from); if (k < 0) return ""; if (at(k-1) == escape) { from = k+1; k = -1; } } pfx = substr(0, k+1); assign(&c_str()[k+1]); return pfx; } void NString::split(cstr delim, NStrings* strings) { char* ctx; char* buf = strdup(this->c_str()); char* ptr = strtok_r(buf, delim, &ctx); while (ptr) { strings->push_back(new NString(ptr)); ptr = strtok_r(NULL, delim, &ctx); } free(buf); } NString NString::Concat(cstr str, ...) { va_list ap; NString string(str); va_start(ap, str); for (;;) { cstr nx = va_arg(ap, cstr); if (nx) string += nx; else break; } va_end(ap); return string; } NString::operator cstr() { return this->c_str(); } NString::operator std::string() { return std::string(this->c_str()); } } // namespace nkit
NString NString::clone() { return NString(_buffer->c_str(), _buffer->size()); }
NString GetVersion() const { return NString(hVersion, false); }
static bool TryParse(const NType & type, const NStringWrapper & value, NInt32 * pValue) { return TryParse(type, value, NString(), pValue); }
int main(void) { NDataStream data; NDataStream num; nint16 val = 666; // 0x29a // Operators: you can use then to set or append to a NDataStream data = "teste"; data += 0; // This one goes as a nint32, thus will use 4 bytes. data += "abc"; data += val; data += "-end-"; // The size() method displays the current size of the data NMessage::print() << "Size: " << data.size(); // You can use subscript operators to access data. for (nuint32 i = 0; i < data.size(); i++) { NMessage::print() << NString().arg("%02x = %c", data[i], data[i]); } // Operators: they accept several data types, including short ints num = val; val = 0; NMessage::print() << NString().arg("%02x = %i", val, val); // And you can convert it back to its original type val = num.toInt16(); NMessage::print() << NString().arg("%02x = %i\n", val, val); // You can convert it to a string, too NMessage::print() << "1) Data: " << data.toString(); try { // You can also define the range ... NMessage::print() << "2) Data: " << data.toString(2, 10); // However, if you exceed the bounds of the stream it will // throw a NException exception. Be careful. NMessage::print() << "3) Data: " << data.toString(89, 1023); } catch (NException &e) { NWarning::print() << "Out of bounds: " << e.getDescription(); } val = 0; // You may set the byte ordering: num.setByteOrder(NDataStream::N_LITTLE_ENDIAN); num = 666; // 0x29a // Again, be careful: it affects the order in which bytes are stored for (nuint32 i = 0; i < num.size(); i++) { NMessage::print() << NString().arg("%02x = %i", num[i], num[i]); } // ... but not how they are converted val = num.toInt16(); NMessage::print() << NString().arg("%02x = %i", val, val); return 0; }
operator NString() { NString str = NString::Empty; str += NString(namespace_) + NString(name_); return str; }
NString ReadToEnd() { HNString hValue; NCheck(NTextReaderReadToEnd(GetHandle(), &hValue)); return NString(hValue, true); }
void LootModule::loot(uint32_t id) { auto packet = NString("get 1 ") << _player->getIngameID() << ' ' << id; Sender::get()->send(_session, packet); }
static NString ToString(const NType & type, NInt32 value, const NStringWrapper & format = NString()) { HNString hValue; NCheck(NEnumToStringN(type.GetHandle(), value, format.GetHandle(), &hValue)); return NString(hValue, true); }
NIndexOutOfBoundsException::NIndexOutOfBoundsException() : NRuntimeException(NString("Index is out of bounds")) { }
static NInt32 Parse(const NType & type, const NStringWrapper & value, const NStringWrapper & format = NString()) { NInt32 result; NCheck(NEnumParseN(type.GetHandle(), value.GetHandle(), format.GetHandle(), &result)); return result; }
NString GetName() const { return NString(hName, false); }