Skip to content

Commit

Permalink
re #10712 Corrected whole file formatting, as partial format look bad
Browse files Browse the repository at this point in the history
  • Loading branch information
NickDraper committed Dec 8, 2014
1 parent 1986b57 commit 6866b24
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 129 deletions.
28 changes: 12 additions & 16 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/ChecksumHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
#include "MantidKernel/DllConfig.h"
#include <string>


namespace Mantid
{
namespace Kernel
{
namespace Mantid {
namespace Kernel {

/** ChecksumHelper : A selection of helper methods for calculating checksums
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge National Laboratory & European Spallation Source
Copyright &copy; 2014 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Expand All @@ -32,21 +30,19 @@ namespace Kernel
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
namespace ChecksumHelper
{
namespace ChecksumHelper {
/// create a md5 checksum from a string
MANTID_KERNEL_DLL std::string md5FromString(const std::string &input);

///create a SHA-1 checksum from a string
MANTID_KERNEL_DLL std::string sha1FromString(const std::string& input);
///create a SHA-1 checksum from a file
MANTID_KERNEL_DLL std::string sha1FromFile(const std::string& filepath);
///create a git checksum from a file (these match the git hash-object command)
MANTID_KERNEL_DLL std::string gitSha1FromFile(const std::string& filepath);
/// create a SHA-1 checksum from a string
MANTID_KERNEL_DLL std::string sha1FromString(const std::string &input);
/// create a SHA-1 checksum from a file
MANTID_KERNEL_DLL std::string sha1FromFile(const std::string &filepath);
/// create a git checksum from a file (these match the git hash-object command)
MANTID_KERNEL_DLL std::string gitSha1FromFile(const std::string &filepath);
};


} // namespace Kernel
} // namespace Mantid

#endif /* MANTID_KERNEL_CHECKSUMHELPER_H_ */
#endif /* MANTID_KERNEL_CHECKSUMHELPER_H_ */
223 changes: 110 additions & 113 deletions Code/Mantid/Framework/Kernel/src/ChecksumHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,122 +12,119 @@
#include <sstream>
#include <string>

namespace Mantid
{
namespace Kernel
{

namespace ChecksumHelper
{
namespace //anonymous
{
/**
* Load contents of file into a string. The line endings are preserved
* @param filepath Full path to the file to be opened
* @param unixEOL If true convert all lineendings to Unix-style \n
*/
std::string loadFile(const std::string & filepath, const bool unixEOL = false)
{
std::ifstream filein(filepath.c_str(), std::ios::in | std::ios::binary);
if(!filein) return "";

std::string contents;
filein.seekg(0, std::ios::end);
contents.resize(filein.tellg());
filein.seekg(0, std::ios::beg);
filein.read(&contents[0], contents.size());
filein.close();

if(unixEOL)
{
static boost::regex eol("\\R"); // \R is Perl syntax for matching any EOL sequence
contents = boost::regex_replace(contents, eol, "\n"); // converts all to LF
}
return contents;
}

/**
* Create sha1 out of data and an optional header
* @param data Contents as a string
* @param header An optional string to prepend to the data
*/
std::string createSHA1(const std::string & data, const std::string & header = "")
{
using Poco::DigestEngine;
using Poco::SHA1Engine;
using Poco::DigestOutputStream;

SHA1Engine sha1;
DigestOutputStream outstr(sha1);
outstr << header << data;
outstr.flush(); //to pass everything to the digest engine
return DigestEngine::digestToHex(sha1.digest());
}

/**
* Create sha1 out of data and an optional header
* @param data Contents as a string
* @param header An optional string to prepend to the data
*/
std::string createMD5(const std::string &data,
const std::string &header = "") {
using Poco::DigestEngine;
using Poco::MD5Engine;
using Poco::DigestOutputStream;

MD5Engine sha1;
DigestOutputStream outstr(sha1);
outstr << header << data;
outstr.flush(); // to pass everything to the digest engine
return DigestEngine::digestToHex(sha1.digest());
}
}

/** Creates a md5 checksum from a string
* @param input The string to checksum
* @returns a checksum string
**/
std::string md5FromString(const std::string &input) {
return ChecksumHelper::createMD5(input);
}

namespace Mantid {
namespace Kernel {

/** Creates a SHA-1 checksum from a string
* @param input The string to checksum
* @returns a checksum string
**/
std::string sha1FromString(const std::string& input)
{
return ChecksumHelper::createSHA1(input);
}

/** Creates a SHA-1 checksum from a file
* @param filepath The path to the file
* @returns a checksum string
**/
std::string sha1FromFile(const std::string& filepath)
{
if(filepath.empty()) return "";
return ChecksumHelper::createSHA1(loadFile(filepath));
namespace ChecksumHelper {
namespace // anonymous
{
/**
* Load contents of file into a string. The line endings are preserved
* @param filepath Full path to the file to be opened
* @param unixEOL If true convert all lineendings to Unix-style \n
*/
std::string loadFile(const std::string &filepath, const bool unixEOL = false) {
std::ifstream filein(filepath.c_str(), std::ios::in | std::ios::binary);
if (!filein)
return "";

std::string contents;
filein.seekg(0, std::ios::end);
contents.resize(filein.tellg());
filein.seekg(0, std::ios::beg);
filein.read(&contents[0], contents.size());
filein.close();

if (unixEOL) {
static boost::regex eol(
"\\R"); // \R is Perl syntax for matching any EOL sequence
contents = boost::regex_replace(contents, eol, "\n"); // converts all to LF
}
return contents;
}

/**
* Create sha1 out of data and an optional header
* @param data Contents as a string
* @param header An optional string to prepend to the data
*/
std::string createSHA1(const std::string &data,
const std::string &header = "") {
using Poco::DigestEngine;
using Poco::SHA1Engine;
using Poco::DigestOutputStream;

SHA1Engine sha1;
DigestOutputStream outstr(sha1);
outstr << header << data;
outstr.flush(); // to pass everything to the digest engine
return DigestEngine::digestToHex(sha1.digest());
}

/**
* Create sha1 out of data and an optional header
* @param data Contents as a string
* @param header An optional string to prepend to the data
*/
std::string createMD5(const std::string &data, const std::string &header = "") {
using Poco::DigestEngine;
using Poco::MD5Engine;
using Poco::DigestOutputStream;

MD5Engine sha1;
DigestOutputStream outstr(sha1);
outstr << header << data;
outstr.flush(); // to pass everything to the digest engine
return DigestEngine::digestToHex(sha1.digest());
}
}

/** Creates a md5 checksum from a string
* @param input The string to checksum
* @returns a checksum string
**/
std::string md5FromString(const std::string &input) {
return ChecksumHelper::createMD5(input);
}

/** Creates a SHA-1 checksum from a string
* @param input The string to checksum
* @returns a checksum string
**/
std::string sha1FromString(const std::string &input) {
return ChecksumHelper::createSHA1(input);
}

/** Creates a SHA-1 checksum from a file
* @param filepath The path to the file
* @returns a checksum string
**/
std::string sha1FromFile(const std::string &filepath) {
if (filepath.empty())
return "";
return ChecksumHelper::createSHA1(loadFile(filepath));
}

/** Creates a git checksum from a file (these match the git hash-object
*command).
* This works by reading in the file, converting all line endings into linux
*style endings,
* then the following is prepended to the file contents "blob
*<content_length>\0",
* the result is then ran through a SHA-1 checksum.
* @param filepath The path to the file
* @returns a checksum string
**/
std::string gitSha1FromFile(const std::string &filepath) {
if (filepath.empty())
return "";
const bool unixEOL(true);
std::string contents = loadFile(filepath, unixEOL);
std::stringstream header;
header << "blob " << contents.size() << '\0';
return ChecksumHelper::createSHA1(contents, header.str());
}

/** Creates a git checksum from a file (these match the git hash-object command).
* This works by reading in the file, converting all line endings into linux style endings,
* then the following is prepended to the file contents "blob <content_length>\0",
* the result is then ran through a SHA-1 checksum.
* @param filepath The path to the file
* @returns a checksum string
**/
std::string gitSha1FromFile(const std::string& filepath)
{
if(filepath.empty()) return "";
const bool unixEOL(true);
std::string contents = loadFile(filepath, unixEOL);
std::stringstream header;
header << "blob " << contents.size() << '\0';
return ChecksumHelper::createSHA1(contents, header.str());
}

} // namespace ChecksumHelper
} // namespace Kernel
} // namespace Mantid

0 comments on commit 6866b24

Please sign in to comment.