This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Utils
justinemclevy edited this page Jan 23, 2014
·
3 revisions
To make the developing process quicker and also the same coding guide can be followed across the libraries, some commonly used types and functions have been declared in utils.h.
Time
extern const boost::posix_time::ptime kMaidSafeEpoch;
// Returns the duration since kMaidsafeEpoch (1st January 2000).
boost::posix_time::time_duration GetDurationSinceEpoch();
// Returns the number of seconds since kMaidsafeEpoch (1st January 2000).
uint32_t GetTimeStamp();
int64_t MillisecondTimeStamp();
std::string GetLocalTime();
Version
extern const int kInvalidVersion;
// Takes a version as an int and returns the string form, e.g. 901 returns "0.09.01"
std::string VersionToString(int version,
std::string* major_version = nullptr,
std::string* minor_version = nullptr,
std::string* patch_version = nullptr);
// Takes a version as a string and returns the int form, e.g. "0.09.01" returns 901
int VersionToInt(const std::string& version);
Port
extern const uint16_t kLivePort;
boost::asio::ip::address GetLocalIp(
boost::asio::ip::udp::endpoint peer_endpoint =
boost::asio::ip::udp::endpoint(
boost::asio::ip::address_v4::from_string("203.0.113.0"), 80));
SI Units
typedef std::chrono::duration<uint64_t > Bytes;
typedef std::chrono::duration<uint64_t, std::kilo> KiloBytes;
typedef std::chrono::duration<uint64_t, std::mega> MegaBytes;
typedef std::chrono::duration<uint64_t, std::giga> GigaBytes;
typedef std::chrono::duration<uint64_t, std::tera> TeraBytes;
typedef std::chrono::duration<uint64_t, std::peta> PetaBytes;
typedef std::chrono::duration<uint64_t, std::exa> ExaBytes;
// Converts num bytes to nearest integral decimal SI value.
std::string BytesToDecimalSiUnits(const uint64_t &num);
// Converts num bytes to nearest integral binary SI value.
std::string BytesToBinarySiUnits(const uint64_t &num);
Random Generator
// Generates a cryptographically-secure 32bit signed integer.
int32_t SRandomInt32();
// Generates a non-cryptographically-secure 32bit signed integer.
int32_t RandomInt32();
// Generates a cryptographically-secure 32bit unsigned integer.
uint32_t SRandomUint32();
// Generates a non-cryptographically-secure 32bit unsigned integer.
uint32_t RandomUint32();
// Generates a cryptographically-secure random string.
std::string SRandomString(const size_t &length);
// Generates a non-cryptographically-secure random string.
std::string RandomString(const size_t &length);
template<typename StringType>
StringType RandomSafeString(const size_t &length);
// Generates a non-cryptographically-secure random string containing only alphanumeric characters.
std::string RandomAlphaNumericString(const size_t &length);
template<typename StringType>
StringType RandomAlphaNumericSafeString(const size_t& length);
Encoder / Decoder
// Encodes a string to hex.
std::string EncodeToHex(const std::string &non_hex_input);
template<size_t min, size_t max>
std::string EncodeToHex(const detail::BoundedString<min, max> &non_hex_input) {
return EncodeToHex(non_hex_input.string());
}
template<typename StringType>
StringType EncodeStringToHex(const StringType& non_hex_input);
template<size_t min, size_t max, typename StringType>
StringType EncodeStringToHex(const detail::BoundedString<min, max, StringType> &non_hex_input);
// Encodes a string to Base64.
std::string EncodeToBase64(const std::string &non_base64_input);
template<size_t min, size_t max>
std::string EncodeToBase64(const detail::BoundedString<min, max> &non_base64_input);
// Encodes a string to Base32.
std::string EncodeToBase32(const std::string &non_base32_input);
template<size_t min, size_t max>
std::string EncodeToBase32(const detail::BoundedString<min, max> &non_base32_input);
// Decodes a string from hex.
std::string DecodeFromHex(const std::string &hex_input);
// Decodes a string from Base64.
std::string DecodeFromBase64(const std::string &base64_input);
// Decodes a string from Base32.
std::string DecodeFromBase32(const std::string &base32_input);
Substr
// Returns an appreviated hex representation of a hash or other small data.
std::string HexSubstr(const std::string &non_hex);
template<size_t min, size_t max>
std::string HexSubstr(const detail::BoundedString<min, max> &non_hex);
template<typename StringType>
StringType HexStringSubstr(const StringType& non_hex);
template<size_t min, size_t max, typename StringType>
StringType HexStringSubstr(const detail::BoundedString<min, max, StringType> &non_hex);
// Returns an appreviated Base32 representation of a hash or other small data.
std::string Base32Substr(const std::string &non_base32);
template<size_t min, size_t max>
std::string Base32Substr(const detail::BoundedString<min, max> &non_base32);
// Returns an appreviated Base64 representation of a hash or other small data.
std::string Base64Substr(const std::string &non_base64);
template<size_t min, size_t max>
std::string Base64Substr(const detail::BoundedString<min, max> &non_base64);
// Returns an abbreviated hex representation of id.
std::string DebugId(const Identity& id);
Environment
// Return CPU size (i.e. 32 64 bit etc.)
int32_t CpuSize();
// Retrieve homedir from environment
boost::filesystem::path GetHomeDir();
// Application support directory in userspace; uses kCompanyName and kApplicationName
boost::filesystem::path GetUserAppDir();
// Application support directory for all users; uses kCompanyName and kApplicationName
boost::filesystem::path GetSystemAppSupportDir();
// Application install directory; uses kCompanyName and kApplicationName
boost::filesystem::path GetAppInstallDir();
// Returns max of (2, hardware_concurrency)
unsigned int Concurrency();
File IO
// Reads the given file and returns the contents as a string.
bool ReadFile(const boost::filesystem::path &file_path, std::string *content);
NonEmptyString ReadFile(const boost::filesystem::path &file_path);
// Writes the given content string to a file, overwriting if applicable.
bool WriteFile(const boost::filesystem::path &file_path, const std::string &content);
Other
// Causes running thread to sleep for specified duration. Returns true if sleep completes full duration, returns false if the sleep is interrupted.
bool Sleep(const boost::posix_time::time_duration &duration);
template<typename T>
bool IsReady(std::future<T>& future) {
return future.wait_for(std::chrono::seconds::zero()) == std::future_status::ready;
}
MaidSafe Common Library
MaidSafe Project
- MaidSafe
- MaidSafe-API
- MaidSafe-Common
- MaidSafe-Passport
- MaidSafe-RUDP
- MaidSafe-Routing
- MaidSafe-Encrypt
- MaidSafe-Drive
- MaidSafe-Network-Filesystem
- MaidSafe-Vault
- MaidSafe-Vault-Manager
MaidSafe Papers