Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Hash.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2018-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Hash.h"
22 
23 #include <fstream>
24 #include <iostream>
25 #include <sstream>
26 #include <vector>
27 #include <assert.h>
28 
29 using namespace HFCore;
30 
31 #include "debug/CrtDbgNew.h"
32 
33 template <class T>
34 void Hash::combine(std::size_t &hash, const T &v)
35 {
36  std::hash<T> hasher;
37  hash ^= hasher(v) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
38 }
39 
40 void Hash::combine(std::size_t &hash, size_t v)
41 {
42  hash ^= v + 0x9e3779b9 + (hash << 6) + (hash >> 2);
43 }
44 
45 void Hash::string(size_t &hash, const std::string &s)
46 {
47  combine(hash, s);
48 }
49 
50 bool Hash::file(size_t &hash, const std::string &filePath)
51 {
52  std::ifstream stream(filePath, std::ios::binary | std::ios::ate);
53  if (!stream)
54  {
55  return false;
56  }
57 
58  std::streamsize size = stream.tellg();
59  stream.seekg(0, std::ios::beg);
60  std::vector<char> buffer((uint32_hf)(size + 1));
61 
62  if (!stream.read(buffer.data(), size))
63  {
64  return false;
65  }
66 
67  buffer[(uint32_hf)size] = '\0';
68  std::string text(buffer.begin(), buffer.end());
69  combine(hash, text);
70 
71  return true;
72 }
73 
74 void Hash::data(size_t &hash, const int8_hf *data, size_t dataSize)
75 {
76  std::string buf(data, dataSize);
77  combine(hash, buf);
78 }
79 
80 bool Hash::load(size_t &hash, const std::string &filePath)
81 {
82  std::ifstream stream(filePath);
83 
84  if (stream)
85  {
86  std::stringstream ss;
87  ss << stream.rdbuf();
88  ss >> hash;
89  return true;
90  }
91 
92  return false;
93 }
94 
95 bool Hash::save(HFCore::FileDirUtils *fdu, size_t &hash, const std::string &outDir,
96  const std::string &outFileName)
97 {
98  // make sure out directory exists:
100 
101  std::string filePath = outDir + outFileName;
102  std::ofstream stream(filePath);
103 
104  if (stream)
105  {
106  stream << hash;
107  return true;
108  }
109 
110  return false;
111 }
unsigned int uint32_hf
A type definition for unsigned int.
Definition: HFDataTypes.h:354
static void data(size_t &hash, const int8_hf *data, size_t dataSize)
Combine hash checksums.
Definition: Hash.cpp:74
static std::wstring StringToWString(const std::string &string)
Convert std::string to std::wstring.
Definition: StringUtil.cpp:482
static void string(size_t &hash, const std::string &s)
Combine hash checksums.
Definition: Hash.cpp:45
static void combine(std::size_t &hash, const T &v)
Combine hash checksums.
Definition: Hash.cpp:34
static bool save(HFCore::FileDirUtils *fdu, size_t &hash, const std::string &outDir, const std::string &outFileName)
Save hash value in output file.
Definition: Hash.cpp:95
Interface useful for simple hash checksums.
virtual void createDirectory(const std::wstring sDirectoryPath)=0
Create directory.
char int8_hf
A type definition for char.
Definition: HFDataTypes.h:329
static bool load(size_t &hash, const std::string &filePath)
Load file containing hash value and set output value in hash variable.
Definition: Hash.cpp:80
static bool file(size_t &hash, const std::string &filePath)
Combine hash checksums.
Definition: Hash.cpp:50
Interface useful for generic directory and file operations.
Definition: FileDirUtils.h:57
Definition: Actor.h:34