-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandler.c
More file actions
186 lines (151 loc) · 4.24 KB
/
Copy pathfilehandler.c
File metadata and controls
186 lines (151 loc) · 4.24 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "header.h"
void saveToFile(Multilist list)
{
FILE* file = fopen(NOTA_FILE, "wb");
if (!file) {
printf("Error membuka file untuk menulis (biner).\n");
return;
}
fwrite(MEJA, sizeof(int), 20, file);
AddressParent parent = list.firstParent;
while (parent != NULL) {
fwrite(&parent->dataParent, sizeof(DataParent), 1, file);
AddressChild child = parent->firstChild;
int childCount = 0;
while (child != NULL) {
childCount++;
child = child->next;
}
fwrite(&childCount, sizeof(int), 1, file);
child = parent->firstChild;
while (child != NULL) {
fwrite(&child->dataChild, sizeof(DataChild), 1, file);
child = child->next;
}
parent = parent->next;
}
fclose(file);
printf("\n\t[*] Data berhasil disimpan ke file %s.\n", NOTA_FILE);
}
void loadFromFile(Multilist* list)
{
FILE* file = fopen(NOTA_FILE, "rb");
if (!file) {
printf("\n [!] Error membuka file %s untuk membaca (biner).\n", NOTA_FILE);
return;
}
// Muat data array MEJA
fread(MEJA, sizeof(int), 20, file);
// Muat data multilist
list->firstParent = NULL;
AddressParent lastParent = NULL;
while (1) {
// Baca data parent
AddressParent newParent = (AddressParent)malloc(sizeof(NodeParent));
if (fread(&newParent->dataParent, sizeof(DataParent), 1, file) != 1) {
free(newParent);
break; // End of file
}
newParent->firstChild = NULL;
newParent->next = NULL;
if (list->firstParent == NULL) {
list->firstParent = newParent;
} else {
lastParent->next = newParent;
}
lastParent = newParent;
// Baca jumlah child
int childCount;
fread(&childCount, sizeof(int), 1, file);
// Baca data child
int i;
AddressChild lastChild = NULL;
for (i = 0; i < childCount; i++) {
AddressChild newChild = (AddressChild)malloc(sizeof(NodeChild));
fread(&newChild->dataChild, sizeof(DataChild), 1, file);
newChild->next = NULL;
if (lastChild == NULL) {
newParent->firstChild = newChild;
} else {
lastChild->next = newChild;
}
lastChild = newChild;
}
}
fclose(file);
printf("Data berhasil dimuat dari file biner %s.\n", NOTA_FILE);
}
void saveStatis(Statis *data) {
FILE *file = fopen(STATIC_FILE, "wb");
if (file == NULL) {
printf("\n [!] ERROR: Unable to open file.");
return;
}
fwrite(data, sizeof(Statis), 1, file);
fclose(file);
}
void loadStatis(Statis *data) {
FILE *file = fopen(STATIC_FILE, "rb");
if (file == NULL) {
printf("\n [!] Tidak ada data tersimpan. Mulai dari awal.\n");
data->omset = 0.0f;
return;
}
fread(data, sizeof(Statis), 1, file);
fclose(file);
}
int readCounter()
{
FILE *file = fopen(FILENAME, "r");
int counter = 0;
if (file == NULL)
{
counter = 0;
}
else
{
fscanf(file, "%d", &counter);
fclose(file);
}
return counter;
}
void saveCounter(int counter)
{
FILE *file = fopen(FILENAME, "w");
if (file == NULL)
{
printf("\n [!] Error: Unable to open file for writing.\n");
exit(1);
}
fprintf(file, "%d", counter);
fclose(file);
}
int getNomorNota()
{
int counter = readCounter();
counter++;
saveCounter(counter);
return counter;
}
void resetNota(){
FILE *file = fopen("nota.bin", "wb");
if (file == NULL) {
printf("\n\t[!] Gagal membuka file nota.bin untuk reset.\n");
return;
}
fclose(file);
printf("\n\t[+] File nota.bin berhasil direset.");
}
void resetStatis(){
FILE *file = fopen("statis.bin", "wb");
if (file == NULL) {
printf("\n\t[!] Gagal membuka file statis.bin untuk reset.\n");
return;
}
Statis dataStatis = {0};
dataStatis.omset = 0.0;
memset(dataStatis.ID, 0, sizeof(dataStatis.ID));
fwrite(&dataStatis, sizeof(Statis), 1, file);
fclose(file);
printf("\n\t[+] File statis.bin berhasil direset.");
}