Example #1
0
uint32_t string2uint32(StringRange input)
{
	char buf[16] = {0};
	size_t inputLength = input.second - input.first;
	if (inputLength >= sizeof(buf))
	{
		fatal2(__("too long number string"));
	}
	memcpy(buf, input.first, inputLength);
	errno = 0;
	long long number = strtoll(buf, NULL, 10);
	if (errno)
	{
		fatal2e(__("invalid number '%s'"), buf);
	}
	if (number < 0)
	{
		fatal2(__("negative number '%s'"), buf);
	}
	if (number >= 0x100000000LL) // uint32_t upper limit
	{
		fatal2(__("too big number '%s'"), buf);
	}
	return uint32_t(number);
}
Example #2
0
	string copyFile(const string& sourcePath, File& sourceFile, const string& targetPath,
			const std::function< void (const vector< string >&) >& callback)
	{
		// preparing target
		string openError;
		File targetFile(targetPath, "a", openError);
		if (!openError.empty())
		{
			return format2("unable to open the file '%s' for appending: %s", targetPath, openError);
		}
		auto totalBytes = targetFile.tell();
		callback(vector< string > { "downloading",
				lexical_cast< string >(totalBytes), lexical_cast< string >(0)});

		{ // determing the size
			struct stat st;
			if (::stat(sourcePath.c_str(), &st) == -1)
			{
				fatal2e(__("%s() failed: '%s'"), "stat", sourcePath);
			}
			callback(vector< string > { "expected-size",
					lexical_cast< string >(st.st_size) });
		}

		{ // writing
			char buffer[4096];
			size_t size = sizeof(buffer);
			while (sourceFile.getBlock(buffer, size), size)
			{
				targetFile.put(buffer, size);
				totalBytes += size;
				callback(vector< string > { "downloading",
						lexical_cast< string >(totalBytes), lexical_cast< string >(size)});
			}
		}

		return string();
	}