-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.c
More file actions
50 lines (41 loc) · 1.55 KB
/
delete.c
File metadata and controls
50 lines (41 loc) · 1.55 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
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
#include "compiled_query.h"
extern FILE * getDataFile(char * tableName, char * mode);
int delete(CompiledQuery * compiledQuery, Table * table, FILE * dataFile, int filePointer)
{
rewind(dataFile);
fseek(dataFile, filePointer, SEEK_CUR);
unsigned char buffer[table->info.rowSize];
int tableSize = table->info.rowSize * table->info.rowCount;
while (ftell(dataFile) <= tableSize - (table->info.rowSize * 2)) {
printf("%d = %d \n", ftell(dataFile), tableSize);
fseek(dataFile, filePointer + table->info.rowSize, SEEK_SET);
printf("%d \n", ftell(dataFile));
fread(buffer, 1, sizeof buffer, dataFile);
printf("%d \n", ftell(dataFile));
fseek(dataFile, filePointer -(table->info.rowSize * 2), SEEK_SET);
printf("%d \n", ftell(dataFile));
fwrite(buffer, 1, sizeof buffer, dataFile);
printf("%d \n", ftell(dataFile));
fseek(dataFile, filePointer + table->info.rowSize, SEEK_SET);
printf("%d \n", ftell(dataFile));
}
ftruncate(fileno(dataFile), tableSize - table->info.rowSize);
table->info.rowCount -= 1;
}
int executeDelete(CompiledQuery * compiledQuery, Table * table)
{
int i = 0;
int pointer = 0;
int status = 0;
FILE * dataFile = getDataFile(compiledQuery->target, "rb");
for (i = 0; i < compiledQuery->columnCount; i++)
{
status = delete(compiledQuery, table, dataFile, pointer);
pointer += table->info.rowSize;
}
fclose(dataFile);
return status;
}