cmpsc330hw2/main.cpp

102 lines
1.8 KiB
C++
Raw Normal View History

2024-10-09 23:39:24 +00:00
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#define pt cout <<
#define nl '\n'
#define in cin >>
using namespace std;
// -------- START RAII HELPERS--------
unsigned long allocated_mem = 0;
void *alloc(size_t size) {
void *ptr = malloc(size);
if (ptr == nullptr) {
cerr << "Memory allocation failed" << endl;
return nullptr;
}
allocated_mem += 1;
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocated_mem -= 1;
}
bool assert_alloc() {
return !allocated_mem;
}
// -------- END RAII HELPERS--------
struct Move {
char player;
int moveX, moveY;
};
struct Moves {
int x{}, y{};
vector<Move> moves;
};
Moves serialize(const string &s) {
Moves result;
istringstream iss(s);
iss >> result.x >> result.y;
string player;
int x, y;
while (iss >> player >> x >> y) {
if (player == "END") break;
result.moves.push_back({player[0], x, y});
}
return result;
}
void solve(const string& fileInp) {
Moves moves = serialize(fileInp);
}
int main(int argc, char **argv) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <input_file>" << nl;
return 1;
}
char* fileName = argv[1];
FILE *inp = fopen(fileName, "r");
if (inp == nullptr) {
cerr << "File not found" << endl;
return 1;
}
fseek(inp, 0L, SEEK_END);
long sz = ftell(inp);
fseek(inp, 0L, SEEK_SET);
char *fileInpPtr = static_cast<char *>(alloc(sz + 1 * sizeof(char)));
fileInpPtr[sz] = '\0';
for (int i = 0; i < sz; ++i) {
fscanf(inp, "%c", &fileInpPtr[i]);
}
solve(fileInpPtr);
dealloc(fileInpPtr);
fclose(inp);
if (!assert_alloc()) {
cerr << "Memory leak detected" << endl;
return 1;
}
return 0;
}