Skip to content

Commit

Permalink
Add: Hash cache checker.
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatgec committed Feb 17, 2021
1 parent 7dbd803 commit 93b756b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
33 changes: 33 additions & 0 deletions include/Kalem_Hash_Checker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* MIT License
#
# Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
# Distributed under the terms of the MIT License.
#
# */

#ifndef KALEM_HASH_CHECKER_HPP
#define KALEM_HASH_CHECKER_HPP

#include <iostream>
#include <string>
#include <cstddef>

typedef struct {
std::size_t old_hash,
hash;

std::string file;

bool changed = false;
} kl_hash;

class KalemHashChecker {
kl_hash hash;
public:
bool HashInit(std::string file);

void HashCreate(std::string file);
};


#endif //KALEM_HASH_CHECKER_HPP
2 changes: 1 addition & 1 deletion include/libs/FileSystemPlusPlus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ namespace fsplusplus {

readfile.close();
} else
std::cout << "Unable to open file\n";
std::cout << "Unable to open file : " << file + "\n";

return data;
}
Expand Down
2 changes: 1 addition & 1 deletion make.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

c++ -std=c++20 src/Kalem_Structure.cpp src/Kalem_Codegen.cpp src/Kalem.cpp -o kalem
c++ -std=c++20 src/Kalem_Hash_Checker.cpp src/Kalem_Structure.cpp src/Kalem_Codegen.cpp src/Kalem.cpp -o kalem
29 changes: 20 additions & 9 deletions src/Kalem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../include/Kalem.hpp"
#include "../include/Kalem_Structure.hpp"
#include "../include/Kalem_Codegen.hpp"
#include "../include/Kalem_Hash_Checker.hpp"

#include "../include/libs/FileSystemPlusPlus.hpp"
#include "../include/libs/ExecutePlusPlus.hpp"
Expand Down Expand Up @@ -42,6 +43,8 @@ int main(int argc, char** argv) {

Kalem kalem;
KalemStructure _structure;
KalemHashChecker hash_checker;

ExecutePlusPlus _exec;

kl_codegen __codegen_;
Expand Down Expand Up @@ -74,7 +77,9 @@ int main(int argc, char** argv) {

_main = kalem.Init(kl_source_file + ".kalem");

__codegen_ = _structure.ReadSource(_main);
if(hash_checker.HashInit(kl_source_file + ".kalem") == false) {
__codegen_ = _structure.ReadSource(_main);
}

if(fsplusplus::IsExistFile(fsplusplus::GetCurrentWorkingDir()
+ "/"
Expand All @@ -88,16 +93,22 @@ int main(int argc, char** argv) {
for (auto i = __codegen_.kl_source_files.begin(); i != __codegen_.kl_source_files.end(); ++i) {
_main = kalem.Init(*i + ".kalem");

temp_codegen = _structure.ReadSource(_main);
if(hash_checker.HashInit(*i + ".kalem") == false) {
temp_codegen = _structure.ReadSource(_main);

if(fsplusplus::IsExistFile(fsplusplus::GetCurrentWorkingDir()
+ "/"
+ *i
+ ".hpp")) {
_exec.RunFunction("rm -f " + *i + ".hpp");
}
if(fsplusplus::IsExistFile(fsplusplus::GetCurrentWorkingDir()
+ "/"
+ *i
+ ".hpp")) {
_exec.RunFunction("rm -f " + *i + ".hpp");
}

fsplusplus::CreateFile(*i + ".hpp", temp_codegen.kl_generated);
fsplusplus::CreateFile(*i + ".hpp", temp_codegen.kl_generated);

hash_checker.HashCreate(*i + ".kalem");
} else {
std::cout << "Skipped : " + *i + ".kalem\n";
}
}

if(kl_output_file == "") {
Expand Down
78 changes: 78 additions & 0 deletions src/Kalem_Hash_Checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* MIT License
#
# Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
# Distributed under the terms of the MIT License.
#
# */

#include <string>
#include <fstream>
#include <functional>
#include <sstream>

#include "../include/Kalem_Hash_Checker.hpp"

#include "../include/libs/FileSystemPlusPlus.hpp"

bool KalemHashChecker::HashInit(std::string file) {
std::string _data = fsplusplus::ReadFileWithReturn(file);

hash.hash = std::hash<std::string>{}(_data);

_data.erase();

if(fsplusplus::IsExistFile(fsplusplus::GetCurrentWorkingDir()
+ "/"
+ file
+ ".__kalem_hash_cache__")) {
const std::string old_hash = fsplusplus::ReadFileWithReturn(file
+ ".__kalem_hash_cache__");

// TODO: Implement size_t to string converter
std::stringstream temp_stream(old_hash);

std::size_t temp_hash;

temp_stream >> temp_hash;

if(hash.hash == temp_hash) {
return true;
} else {
return false;
}
} else {
return false;
}

return false;
}

void KalemHashChecker::HashCreate(std::string file) {
std::string _data = fsplusplus::ReadFileWithReturn(file);

hash.hash = std::hash<std::string>{}(_data);

_data.erase();

if(fsplusplus::IsExistFile(fsplusplus::GetCurrentWorkingDir()
+ "/"
+ file
+ ".__kalem_hash_cache__") == false) {
fsplusplus::CreateFile(file + ".__kalem_hash_cache__", std::to_string(hash.hash));
} else {
_data = fsplusplus::ReadFileWithReturn(file
+ ".__kalem_hash_cache__");

if(std::to_string(hash.hash) != _data) {
std::ofstream output_stream;
output_stream.open(fsplusplus::GetCurrentWorkingDir()
+ "/"
+ file
+ ".__kalem_hash_cache__", std::ofstream::trunc);

output_stream << std::to_string(hash.hash);

output_stream.close();
}
}
}
2 changes: 2 additions & 0 deletions test.kalem
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
@my_test void {
@print "Greetings from test.kalem!\n"
}


0 comments on commit 93b756b

Please sign in to comment.