reading, and sorting chunks

This commit is contained in:
bronku 2025-10-16 15:26:56 +02:00
parent 36035058f2
commit d22c0606c1
5 changed files with 78 additions and 15 deletions

17
TODO.md
View file

@ -1,7 +1,7 @@
- [ ] file operations
- [ ] write to file (at index)
- [ ] print_file (maybe just calling cat???)
- [ ] a tape type - representing a file?
- [x] file operations
- [x] write to file (at index)
- [x] read file
- [x] a tape type - representing a file?
- [x] a record type - x1,x2,x3,x4,x5
- [x] comparing records
- [x] buffer quick sort
@ -9,5 +9,14 @@
- [x] cmd args - man getopts
- [x] file input
- [x] keyboard input
- [ ] stage 1
- [x] reading chunks
- [x] sorting
- [ ] writing chunks
- [ ] stage 2
- [ ] reading chunks
- [ ] multiway merge
- [ ] writing chunks
- [ ] repeat untill done
- [ ] testing
- [ ] report

36
main.c
View file

@ -110,20 +110,40 @@ void write_file(options* opts)
{
}
void sort_file(options* opts)
int sort_block(tape* t, buffer* buff)
{
static int counter = 0;
int status = read_buffer(buff, t);
if (status != SUCCESS && status != EOF) {
fprintf(stderr, "Error reading file: %d\n", status);
return status;
}
sort_buffer(buff);
printf("sorted the buffer %d, the results:\n", counter);
print_buffer(buff);
counter++;
return status;
}
int sort_file(options* opts)
{
tape a = { 0, 0, opts->input };
buffer buff;
buff.capacity = 100;
buff.capacity = opts->b * opts->n;
buff.location = malloc(sizeof(record) * buff.capacity);
int status = read_buffer(&buff, &a);
if (status != SUCCESS && status != EOF) {
printf("Error reading file: %d\n", status);
int status = 0;
while (status == SUCCESS) {
status = sort_block(&a, &buff);
}
fclose(a.file);
a.file = stdout;
sort_buffer(&buff);
if (status != EOF) {
free(buff.location);
fclose(a.file);
return status;
}
free(buff.location);
fclose(a.file);
return SUCCESS;
}
int main(int argc, char** argv)

View file

@ -16,9 +16,9 @@ clean:
rm -rf build
run: build/main
build/main -f tests/1.in
build/main -f tests/2.in | less
debug: build/main
lldb -- build/main -f tests/1.in
lldb -- build/main -f tests/2.in
.PHONY: clean run
.PHONY: clean run debug

33
tape.c
View file

@ -60,6 +60,39 @@ int read_buffer(buffer* buff, tape* tape)
return 0;
}
void print_full_buffer(buffer* buff)
{
for (int i = 0; i < buff->length; i++) {
record* current = buff->location;
current += i;
printf("i: %d\tg: %d\t", i, g(current));
print_record(stdout, current);
printf("\n");
}
}
void print_buffer(buffer* buff)
{
if (buff->length <= 10) {
print_full_buffer(buff);
return;
}
for (int i = 0; i < 5; i++) {
record* current = buff->location;
current += i;
printf("i: %d\tg: %d\t", i, g(current));
print_record(stdout, current);
printf("\n");
}
for (int i = buff->length - 5; i < buff->length; i++) {
record* current = buff->location;
current += i;
printf("i: %d\tg: %d\t", i, g(current));
print_record(stdout, current);
printf("\n");
}
}
int write_buffer(buffer* buff, tape* tape)
{
record* current = buff->location;

1
tape.h
View file

@ -20,5 +20,6 @@ void sort_buffer(buffer* buff);
int read_buffer(buffer* buff, tape* tape);
// writes everything
int write_buffer(buffer* buff, tape* tape);
void print_buffer(buffer* buff);
void clear_tape(tape* tape);