Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite the very real enterprise project of totally not a satire project to C++ (real) #694

Open
wants to merge 37 commits into
base: uinverse
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cbd5a88
Get rid of slow code
GEOEGII555 Jul 8, 2024
f901147
convert into a visual studio project
GEOEGII555 Jul 8, 2024
a801e21
remove temporary folder
GEOEGII555 Jul 8, 2024
56a216a
Add a .gitignore file
GEOEGII555 Jul 8, 2024
5e5a652
add a console interface and a typedef for a string that uses TCHARs
GEOEGII555 Jul 8, 2024
f333340
Add an argument parser
GEOEGII555 Jul 8, 2024
943d122
Add an input reader
GEOEGII555 Jul 8, 2024
d0e0477
Create _FizzBuzzer
GEOEGII555 Jul 8, 2024
1d8dbcf
Add an output writer
GEOEGII555 Jul 8, 2024
6e6808f
Add a fake input stream, which just spams numbers from 1 to 100
GEOEGII555 Jul 8, 2024
4362181
Check if the file is empty.
GEOEGII555 Jul 8, 2024
304a98f
fix
GEOEGII555 Jul 8, 2024
55295bc
more bug fixes
GEOEGII555 Jul 8, 2024
7723292
Add a readme
GEOEGII555 Jul 8, 2024
a4531d8
Fix file writing, update .gitignore
GEOEGII555 Jul 8, 2024
57f3932
Already fixed
GEOEGII555 Jul 8, 2024
b4e8a09
Add persistent cache (not tested)
GEOEGII555 Jul 8, 2024
f79272c
Close the file handles.
GEOEGII555 Jul 8, 2024
65242b6
Fix persistent caching.
GEOEGII555 Jul 8, 2024
6c1455b
Made flags be constant expressions.
GEOEGII555 Jul 15, 2024
580325c
Use getter functions instead of direct class member access.
GEOEGII555 Jul 15, 2024
f6ab067
Added 2 interfaces.
GEOEGII555 Jul 15, 2024
c9d36c2
Change the loop to std::ranges::for_each with std::views::iota
GEOEGII555 Jul 15, 2024
8c00abc
Use tostringstreams instead of normal strings in baseoutputwriter
GEOEGII555 Jul 15, 2024
f4550d9
Use inline instead of extern
GEOEGII555 Jul 15, 2024
2254cd1
Change the loop to use iterators instead. Also fix a couple of bugs.
GEOEGII555 Jul 15, 2024
950127b
rvalue ref
GEOEGII555 Jul 15, 2024
ff091b1
Update README.md
GEOEGII555 Oct 29, 2024
836e4bb
Update argparser.cpp
GEOEGII555 Nov 12, 2024
12eac55
Update argparser.cpp
GEOEGII555 Nov 12, 2024
b95d1fb
Update argparser.cpp
GEOEGII555 Nov 12, 2024
0b5ff92
Update argparser.hpp
GEOEGII555 Nov 12, 2024
383dc7e
Update argparser.cpp
GEOEGII555 Nov 12, 2024
bdf2e21
Update main.cpp
GEOEGII555 Nov 12, 2024
05383b9
Update input_reader.cpp
GEOEGII555 Nov 12, 2024
655cfb9
Update input_reader.cpp
GEOEGII555 Nov 12, 2024
ea747b3
Update input_reader.cpp
GEOEGII555 Nov 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add an output writer
  • Loading branch information
GEOEGII555 committed Jul 8, 2024
commit 1d8dbcf78b2aae12b704eb91abf6075dcb926f21
2 changes: 2 additions & 0 deletions FizzBuzz Enterprise Edition.vcxproj
Original file line number Diff line number Diff line change
@@ -132,13 +132,15 @@
<ClCompile Include="fizz_buzz.cpp" />
<ClCompile Include="input_reader.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="output_writer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="argparser.hpp" />
<ClInclude Include="console.hpp" />
<ClInclude Include="cpp_tstring.hpp" />
<ClInclude Include="fizz_buzz.hpp" />
<ClInclude Include="input_reader.hpp" />
<ClInclude Include="output_writer.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
6 changes: 6 additions & 0 deletions FizzBuzz Enterprise Edition.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -34,6 +34,9 @@
<ClCompile Include="fizz_buzz.cpp">
<Filter>Source files</Filter>
</ClCompile>
<ClCompile Include="output_writer.cpp">
<Filter>Header files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="cpp_tstring.hpp">
@@ -51,5 +54,8 @@
<ClInclude Include="fizz_buzz.hpp">
<Filter>Header files</Filter>
</ClInclude>
<ClInclude Include="output_writer.hpp">
<Filter>Header files</Filter>
</ClInclude>
</ItemGroup>
</Project>
16 changes: 15 additions & 1 deletion fizz_buzz.cpp
Original file line number Diff line number Diff line change
@@ -18,4 +18,18 @@ FizzBuzzResult _FizzBuzzer::fizzBuzz(const unsigned long long int number) {
return calculatedResult;
}

_FizzBuzzer fizzbuzzer = {};
_FizzBuzzer fizzbuzzer = {};

FizzBuzzResult operator|(FizzBuzzResult lhs, FizzBuzzResult rhs) {
return static_cast<FizzBuzzResult>(
static_cast<std::underlying_type<FizzBuzzResult>::type>(lhs) |
static_cast<std::underlying_type<FizzBuzzResult>::type>(rhs)
);
}

FizzBuzzResult operator&(FizzBuzzResult lhs, FizzBuzzResult rhs) {
return static_cast<FizzBuzzResult>(
static_cast<std::underlying_type<FizzBuzzResult>::type>(lhs) &
static_cast<std::underlying_type<FizzBuzzResult>::type>(rhs)
);
}
5 changes: 4 additions & 1 deletion fizz_buzz.hpp
Original file line number Diff line number Diff line change
@@ -12,4 +12,7 @@ struct _FizzBuzzer {
FizzBuzzResult fizzBuzz(const unsigned long long int number);
};

extern _FizzBuzzer fizzbuzzer;
extern _FizzBuzzer fizzbuzzer;
GEOEGII555 marked this conversation as resolved.
Show resolved Hide resolved

FizzBuzzResult operator|(FizzBuzzResult lhs, FizzBuzzResult rhs);
FizzBuzzResult operator&(FizzBuzzResult lhs, FizzBuzzResult rhs);
4 changes: 2 additions & 2 deletions input_reader.cpp
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@ std::vector<unsigned long long int> BaseInputReader::readRemainingInput() {
}

FileInputReader::FileInputReader(tstring file) {
this->hFile = CreateFile(file.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError() != NO_ERROR && GetLastError() != ERROR_ALREADY_EXISTS) {
this->hFile = CreateFile(file.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError() != NO_ERROR) {
DWORD error = GetLastError();
TCHAR* arr;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, LANG_USER_DEFAULT, reinterpret_cast<LPTSTR>(&arr), 1, NULL)) abort();
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
#include <tchar.h>
#include "console.hpp"
#include "argparser.hpp"
#include "output_writer.hpp"
#include "input_reader.hpp"

// Welcome to 2024, where open source projects have a copyright.
@@ -11,6 +12,7 @@ TCHAR copyrightMessage[] = {

ArgumentParser argParser;
BaseInputReader* inputReader;
BaseOutputWriter* outputWriter;

int _tmain(unsigned int argc, TCHAR* argv[]) {
argParser.parse(argc, argv);
52 changes: 52 additions & 0 deletions output_writer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "output_writer.hpp"
#include "cpp_tstring.hpp"
#include "fizz_buzz.hpp"
#include <vector>
#include <algorithm>
#include "console.hpp"
#include <tchar.h>

void BaseOutputWriter::writeOne(unsigned long long int number, FizzBuzzResult result) {
tstring str;
if (number == 0) str = TEXT("0");
else while (number != 0) {
str.push_back((TCHAR)(number + TEXT('0')));
}
str.reserve();
str.append(TEXT(":"));

if ((result & FizzBuzzResult::FIZZ) != FizzBuzzResult::UNKNOWN) str.append(TEXT("Fizz"));
GEOEGII555 marked this conversation as resolved.
Show resolved Hide resolved
if ((result & FizzBuzzResult::BUZZ) != FizzBuzzResult::UNKNOWN) str.append(TEXT("Buzz"));
if ((result & FizzBuzzResult::NONE) != FizzBuzzResult::UNKNOWN) str.append(str.substr(0, str.length() - 1));
str.push_back('\n');
_write(str);
}

void BaseOutputWriter::writeMany(std::vector<std::pair<unsigned long long int, FizzBuzzResult>> values) {
std::for_each(values.begin(), values.end(), [&](std::pair<unsigned long long int, FizzBuzzResult>& pair) {
writeOne(pair.first, pair.second);
});
}

void ConsoleOutputWriter::_write(tstring str) {
console.writeOutput(str);
}

FileOutputWriter::FileOutputWriter(tstring file) {
this->hFile = CreateFile(file.c_str(), GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError() != NO_ERROR && GetLastError() != ERROR_ALREADY_EXISTS) {
DWORD error = GetLastError();
TCHAR* arr;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, LANG_USER_DEFAULT, reinterpret_cast<LPTSTR>(&arr), 1, NULL)) abort();
console.writeError(TEXT("Failed to open the output file for writing: "));
console.writeError(arr);
console.writeError(TEXT("\n"));
abort();
}
}

void FileOutputWriter::_write(tstring str) {
DWORD dataWritten;
WriteFile(this->hFile, str.c_str(), str.length(), &dataWritten, NULL);
if (dataWritten != str.length()) abort();
}
22 changes: 22 additions & 0 deletions output_writer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "cpp_tstring.hpp"
#include "fizz_buzz.hpp"
#include <vector>

struct BaseOutputWriter {
virtual void _write(tstring str) = 0;
void writeOne(unsigned long long int number, FizzBuzzResult result);
void writeMany(std::vector<std::pair<unsigned long long int, FizzBuzzResult>> values);
};

struct ConsoleOutputWriter : BaseOutputWriter {
virtual void _write(tstring str);
};

struct FileOutputWriter : BaseOutputWriter {
HANDLE hFile;

FileOutputWriter(tstring file);

virtual void _write(tstring str);
};