made work: line numbers in files with the correct indentation level (tested on a > 1000 lines file)
This commit is contained in:
+34
@@ -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);
|
||||
}
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
#include <odweta/util.h>
|
||||
#include <odweta/vector.h>
|
||||
|
||||
char*
|
||||
intToString(int x) {
|
||||
vector_t* string = vectorInit(CHAR);
|
||||
|
||||
if (x > 0 && x < 10) {
|
||||
vectorPush(string, &(char){x+48});
|
||||
char* retval = (char*)vectorToString(string);
|
||||
vectorCleanup(string);
|
||||
return retval;
|
||||
} else {
|
||||
while (x > 0) {
|
||||
int lastDigit = x % 10;
|
||||
x /= 10;
|
||||
vectorInsertAt(string, 0, &(char){lastDigit+48});
|
||||
}
|
||||
vectorPop(string);
|
||||
char* retval = (char*)vectorToString(string);
|
||||
vectorCleanup(string);
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
long
|
||||
pow(int x, int n) {
|
||||
long retval = 1;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
retval *= x;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
maxLowerPowerOf10(int x) {
|
||||
int i = 0;
|
||||
long prev = pow(10, i++);
|
||||
|
||||
while (1) {
|
||||
long now = pow(10, i);
|
||||
if (now >= x) {
|
||||
return (int)prev;
|
||||
}
|
||||
prev = now;
|
||||
i++;
|
||||
}
|
||||
|
||||
}
|
||||
+6
-2
@@ -324,7 +324,9 @@ vectorGetAt(vector_t* v, int index) {
|
||||
|
||||
void
|
||||
vectorSetAt(vector_t* v, int index, void* element) {
|
||||
if (index >= v->size) {
|
||||
if (v->size == 0) {
|
||||
vectorPush(v, element);
|
||||
} else if (index >= v->size) {
|
||||
fprintf(stderr, "index out of range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -350,7 +352,9 @@ vectorSetAt(vector_t* v, int index, void* element) {
|
||||
|
||||
void
|
||||
vectorInsertAt(vector_t* v, int index, void* element) {
|
||||
if (index >= v->size) {
|
||||
if (v->size == 0) {
|
||||
vectorPush(v, element);
|
||||
} else if (index >= v->size) {
|
||||
fprintf(stderr, "index out of range\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user