made work: line numbers in files with the correct indentation level (tested on a > 1000 lines file)

This commit is contained in:
Odweta
2023-12-24 12:44:13 +01:00
parent 490a7cc973
commit 342ad58b51
10 changed files with 180 additions and 13 deletions
+34
View File
@@ -1,5 +1,6 @@
#include <odweta/file.h>
#include <odweta/vector.h>
#include <odweta/util.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
@@ -189,6 +190,7 @@ fileCopy(file_t* src, char* dst) {
FILE* fd = fopen(dst, "w");
if (!fd) {
fprintf(stderr, "could not open file for writing\n");
fclose(fd);
exit(EXIT_FAILURE);
}
@@ -213,6 +215,7 @@ fileDelete(file_t* f) {
// fileCleanup(f); // don't know if this is necessary... :shrug:
// exit(EXIT_FAILURE);
}
fileCleanup(f);
}
@@ -222,3 +225,34 @@ fileMove(file_t* src, char* dst) {
fileDelete(src);
}
void
fileNumberLines(file_t* f) {
FILE* fd = fopen(f->path, "w");
if (!fd) {
fprintf(stderr, "could not open file for writing\n");
fclose(fd);
exit(EXIT_FAILURE);
}
int mlp = maxLowerPowerOf10(f->length);
for (int i = 0; i < f->length; i++) {
// print to the file descriptor
fprintf(fd, "%d %s\n", i+1, (char*)vectorToString((vector_t *)vectorGetAt(f->lines, i)));
// print to f->lines
vectorInsertAt((vector_t*)vectorGetAt(f->lines, i), 0, &(char){' '});
char* string = intToString(i+1);
for (int j = strlen(string)-1; j >= 0; j--) {
vectorInsertAt((vector_t*)vectorGetAt(f->lines, i), 0, &(char){string[j]});
}
// prepend the correct amount of ' '
for (int j = 0; j < strlen(intToString(mlp))-strlen(intToString(i+1)); j++) {
vectorInsertAt((vector_t*)vectorGetAt(f->lines, i), 0, &(char){' '});
}
}
fclose(fd);
}