-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksort.c
94 lines (69 loc) · 1.98 KB
/
checksort.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "index.h"
#include "defines.h"
void showProgress(uint64_t entry, uint64_t entryCount)
{
float percents = (float) entry / (float) entryCount * 100;
printf("\033[A\r\33[2K%lu / %lu (%.2f%%)\n", entry, entryCount, percents);
}
int main(int argc, char **argv)
{
FILE* indexFile = NULL;
uint64_t entriesCount, i;
uint8_t* currentEntry = NULL, *nextEntry = NULL;
IndexHeader indexHeader;
uint8_t indexEntrySize;
if(argc != 2)
{
printf("Usage: %s <index_file>\n", argv[0]);
return EXIT_FAILURE;
}
indexFile = fopen(argv[1], "r");
if(indexFile == NULL)
{
printf("Unable to open the index file.\n");
return EXIT_FAILURE;
}
if(readIndexHeader(indexFile, &indexHeader))
{
printf("Invalid index file.\n");
fclose(indexFile);
return EXIT_FAILURE;
}
entriesCount = getIndexesCount(&indexHeader);
if(entriesCount < 2)
{
printf("This index must be sorted...\n");
fclose(indexFile);
return EXIT_SUCCESS;
}
indexEntrySize = getIndexEntrySize(&indexHeader);
currentEntry = malloc(indexEntrySize);
nextEntry = malloc(indexEntrySize);
fread(currentEntry, indexEntrySize, 1, indexFile);
for(i=0 ; i<(entriesCount-1) ; i++)
{
fread(nextEntry, indexEntrySize, 1, indexFile);
if(memcmp(currentEntry, nextEntry, INDEX_HASH_SIZE) > 0)
{
printf("The index is not sorted!\n");
free(currentEntry);
free(nextEntry);
fclose(indexFile);
return EXIT_FAILURE;
}
memcpy(currentEntry, nextEntry, INDEX_HASH_SIZE);
if((i % PROGRESS_UPDATE_COUNT) == 0)
{
showProgress(i, entriesCount);
}
}
printf("The index is sorted!\n");
free(currentEntry);
free(nextEntry);
fclose(indexFile);
return EXIT_SUCCESS;
}