-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.h
More file actions
89 lines (73 loc) · 2.43 KB
/
Copy pathTable.h
File metadata and controls
89 lines (73 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// include guard or header guard
#ifndef Table_H
#define Table_H
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "TableHeader.h"
class Table
{
private:
std::string tableName;
TableHeader header;
public:
Table()
{
tableName = "";
}
Table(std::string tableName)
{
this->tableName = "customdatabase/" + tableName + ".tbl";
};
uint32_t getSchemaOffset() const
{
return header.getSchemaOffset();
}
void CreateTable()
{
int acknowledge_custom_db_create = system("mkdir -p customdatabase"); // works as if we are executing this command in cmd and create customdatabase folder if wont exists only
// Log database folder creation sucessfull or not
if (acknowledge_custom_db_create == 0)
{
std ::cout << "---Custom Database Created Successfully---" << std ::endl;
}
else
{
std ::cout << "Return Code : " << acknowledge_custom_db_create << "\n"
<< "---Error in Custom Database Creation---" << std ::endl;
}
// create a file and open in binary mode
std ::ofstream file(tableName, std::ios::binary);
if (!file.is_open())
{
/*
cerr is a standard output stream object used to output errors.
It is an unbuffered stream, meaning that output is written immediately to the standard error stream (stderr), without any intermediate buffering.
This ensures that error messages are displayed as soon as they are generated, which is crucial for debugging and error handling.
*/
std::cerr << "Failed to create table file: " << tableName << "\n";
return;
}
TableHeader header;
header.serialize(file);
file.close();
std::cout << "Table '" << tableName << "' created successfully as " << tableName << "\n";
}
void readTableHeader(const std::string &tableName)
{
std::string filename = "customdatabase/" + tableName + ".tbl";
std::ifstream file(filename, std::ios::binary);
if (!file.is_open())
{
std::cerr << "Failed to open table file: " << filename << "\n";
return;
}
if (header.deserialize(file))
{
std::cout << "Table Header for '" << tableName << "':\n";
header.print();
}
file.close();
}
};
#endif