COMMON: Add String::asUint64()

Instead of all these atoull() I've added everywhere.
This commit is contained in:
Alexander Tkachev 2016-06-10 14:06:06 +06:00
parent e6242b0be8
commit c99b24c16d
6 changed files with 18 additions and 50 deletions

View file

@ -43,17 +43,6 @@ CloudManager::~CloudManager() {
delete _activeStorage;
}
namespace {
uint64 atoull(Common::String s) {
uint64 result = 0;
for (uint32 i = 0; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9') break;
result = result * 10L + (s[i] - '0');
}
return result;
}
}
Common::String CloudManager::getStorageConfigName(uint32 index) const {
switch (index) {
case kStorageNoneId: return "<none>";
@ -101,7 +90,7 @@ void CloudManager::init() {
if (ConfMan.hasKey("storage_" + name + "_lastSync", "cloud"))
config.lastSyncDate = ConfMan.get("storage_" + name + "_lastSync", "cloud");
if (ConfMan.hasKey("storage_" + name + "_usedBytes", "cloud"))
config.usedBytes = atoull(ConfMan.get("storage_" + name + "_usedBytes", "cloud"));
config.usedBytes = ConfMan.get("storage_" + name + "_usedBytes", "cloud").asUint64();
_storages.push_back(config);
}

View file

@ -67,17 +67,6 @@ void GoogleDriveListDirectoryByIdRequest::makeRequest(Common::String pageToken)
_workingRequest = ConnMan.addRequest(request);
}
namespace {
uint64 atoull(Common::String s) {
uint64 result = 0;
for (uint32 i = 0; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9') break;
result = result * 10L + (s[i] - '0');
}
return result;
}
}
void GoogleDriveListDirectoryByIdRequest::responseCallback(Networking::JsonResponse response) {
_workingRequest = nullptr;
if (_ignoreCallback) return;
@ -113,7 +102,7 @@ void GoogleDriveListDirectoryByIdRequest::responseCallback(Networking::JsonRespo
bool isDirectory = (item.getVal("mimeType")->asString() == "application/vnd.google-apps.folder");
uint32 size = 0, timestamp = 0;
if (item.contains("size") && item.getVal("size")->isString())
size = atoull(item.getVal("size")->asString());
size = item.getVal("size")->asString().asUint64();
if (item.contains("modifiedTime") && item.getVal("modifiedTime")->isString())
timestamp = ISO8601::convertToTimestamp(item.getVal("modifiedTime")->asString());

View file

@ -137,17 +137,6 @@ Common::String GoogleDriveStorage::name() const {
return "Google Drive";
}
namespace {
uint64 atoull(Common::String s) {
uint64 result = 0;
for (uint32 i = 0; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9') break;
result = result * 10L + (s[i] - '0');
}
return result;
}
}
void GoogleDriveStorage::infoInnerCallback(StorageInfoCallback outerCallback, Networking::JsonResponse response) {
Common::JSONValue *json = response.value;
if (!json) {
@ -175,8 +164,8 @@ void GoogleDriveStorage::infoInnerCallback(StorageInfoCallback outerCallback, Ne
Common::JSONObject storageQuota = info.getVal("storageQuota")->asObject();
Common::String usage = storageQuota.getVal("usage")->asString();
Common::String limit = storageQuota.getVal("limit")->asString();
quotaUsed = atoull(usage);
quotaAllocated = atoull(limit);
quotaUsed = usage.asUint64();
quotaAllocated = limit.asUint64();
}
CloudMan.setStorageUsername(kStorageGoogleDriveId, email);

View file

@ -207,17 +207,6 @@ void GoogleDriveUploadRequest::uploadNextPart() {
_workingRequest = ConnMan.addRequest(request);
}
namespace {
uint64 atoull(Common::String s) {
uint64 result = 0;
for (uint32 i = 0; i < s.size(); ++i) {
if (s[i] < '0' || s[i] > '9') break;
result = result * 10L + (s[i] - '0');
}
return result;
}
}
bool GoogleDriveUploadRequest::handleHttp308(const Networking::NetworkReadStream *stream) {
//308 Resume Incomplete, with Range: X-Y header
if (!stream) return false;
@ -238,7 +227,7 @@ bool GoogleDriveUploadRequest::handleHttp308(const Networking::NetworkReadStream
if (c == '\n' || c == '\r') break;
result += c;
}
_serverReceivedBytes = atoull(result) + 1;
_serverReceivedBytes = result.asUint64() + 1;
uploadNextPart();
return true;
}
@ -285,7 +274,7 @@ void GoogleDriveUploadRequest::partUploadedCallback(Networking::JsonResponse res
bool isDirectory = (object.getVal("mimeType")->asString() == "application/vnd.google-apps.folder");
uint32 size = 0, timestamp = 0;
if (object.contains("size") && object.getVal("size")->isString())
size = atoull(object.getVal("size")->asString());
size = object.getVal("size")->asString().asUint64();
if (object.contains("modifiedTime") && object.getVal("modifiedTime")->isString())
timestamp = ISO8601::convertToTimestamp(object.getVal("modifiedTime")->asString());

View file

@ -335,6 +335,15 @@ bool String::contains(char x) const {
return strchr(c_str(), x) != NULL;
}
uint64 String::asUint64() const {
uint64 result = 0;
for (uint32 i = 0; i < _size; ++i) {
if (_str[i] < '0' || _str[i] > '9') break;
result = result * 10L + (_str[i] - '0');
}
return result;
}
bool String::matchString(const char *pat, bool ignoreCase, bool pathMode) const {
return Common::matchString(c_str(), pat, ignoreCase, pathMode);
}

View file

@ -162,6 +162,9 @@ public:
bool contains(const char *x) const;
bool contains(char x) const;
/** Return uint64 corrensponding to String's contents. */
uint64 asUint64() const;
/**
* Simple DOS-style pattern matching function (understands * and ? like used in DOS).
* Taken from exult/files/listfiles.cc