CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Text/Parsers.cpp
Views: 1401
#include <climits>1#include <cstdio>2#include <string>34#include "Common/Data/Text/Parsers.h"5#include "Common/StringUtils.h"67// Not strictly a parser...8void NiceSizeFormat(uint64_t size, char *out, size_t bufSize) {9int s = 0;10int frac = 0;11while (size >= 1024) {12s++;13frac = (int)size & 1023;14size /= 1024;15}16float f = (float)size + ((float)frac / 1024.0f);17if (s == 0)18snprintf(out, bufSize, "%d B", (int)size);19else {20static const char* const sizes[] = { "B","KB","MB","GB","TB","PB","EB" };21snprintf(out, bufSize, "%3.2f %s", f, sizes[s]);22}23}2425std::string NiceSizeFormat(uint64_t size) {26char buffer[16];27NiceSizeFormat(size, buffer, sizeof(buffer));28return std::string(buffer);29}3031bool Version::ParseVersionString(std::string str) {32if (str.empty())33return false;34if (str[0] == 'v')35str = str.substr(1);36if (3 != sscanf(str.c_str(), "%i.%i.%i", &major, &minor, &sub)) {37sub = 0;38if (2 != sscanf(str.c_str(), "%i.%i", &major, &minor))39return false;40}41return true;42}4344std::string Version::ToString() const {45char temp[128];46snprintf(temp, sizeof(temp), "%i.%i.%i", major, minor, sub);47return std::string(temp);48}4950int Version::ToInteger() const {51// This allows for ~2000 major versions, ~100 minor versions, and ~10000 sub versions.52return major * 1000000 + minor * 10000 + sub;53}5455bool ParseMacAddress(const std::string &str, uint8_t macAddr[6]) {56unsigned int mac[6];57if (6 != sscanf(str.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5])) {58return false;59}60for (int i = 0; i < 6; i++) {61macAddr[i] = mac[i];62}63return true;64}6566static bool TryParseUnsigned32(const std::string &str, uint32_t *const output) {67char *endptr = NULL;6869// Holy crap this is ugly.7071// Reset errno to a value other than ERANGE72errno = 0;7374unsigned long value = strtoul(str.c_str(), &endptr, 0);7576if (!endptr || *endptr)77return false;7879if (errno == ERANGE)80return false;8182if (ULONG_MAX > UINT_MAX) {83#ifdef _MSC_VER84#pragma warning (disable:4309)85#endif86// Note: The typecasts avoid GCC warnings when long is 32 bits wide.87if (value >= static_cast<unsigned long>(0x100000000ull)88&& value <= static_cast<unsigned long>(0xFFFFFFFF00000000ull))89return false;90}9192*output = static_cast<uint32_t>(value);93return true;94}9596bool TryParse(const std::string &str, uint32_t *const output) {97if (str[0] != '#') {98return TryParseUnsigned32(str, output);99} else {100// Parse it as "#RGBA" and convert to a ABGR interger101std::string s = ReplaceAll(str, "#", "0x");102if (TryParseUnsigned32(s, output)) {103int a = (*output >> 24) & 0xff;104int b = (*output >> 16) & 0xff;105int g = (*output >> 8) & 0xff;106int r = *output & 0xff;107*output = (r << 24) | (g << 16) | (b << 8) | a;108return true;109} else {110return false;111}112}113}114115bool TryParse(const std::string &str, uint64_t *const output) {116char *endptr = NULL;117118// Holy crap this is ugly.119120// Reset errno to a value other than ERANGE121errno = 0;122123uint64_t value = strtoull(str.c_str(), &endptr, 0);124125if (!endptr || *endptr)126return false;127128if (errno == ERANGE)129return false;130131*output = value;132return true;133}134135bool TryParse(const std::string &str, bool *const output) {136if ("1" == str || !strcasecmp("true", str.c_str()))137*output = true;138else if ("0" == str || !strcasecmp("false", str.c_str()))139*output = false;140else141return false;142143return true;144}145146147