FINALLY created code for loading logFile as pointer-to-pointer char

I should probably fix the valgrind issues heh..
This commit is contained in:
Václav Šmejkal 2023-01-24 11:56:33 +01:00
parent f2a62fdb7b
commit 000021b9a2
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59

View File

@ -18,7 +18,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/logger/utils.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <why2/logger/flags.h>
@ -28,3 +30,92 @@ void deallocateLogger(logFile logger)
close(logger.file);
free(logger.fileName);
}
void removeSpaces(char* string)
{
char* d = string;
do
{
while (*d == ' ')
{
++d;
}
} while ((*string++ = *d++));
}
void decryptLogger(logFile logger) //TODO: Fix valgrind issues
{
FILE *file = fdopen(logger.file, "r"); //OPEN logFile AS FILE POINTER
char *rawContent;
char **linesContent;
int rawContentLength;
int lines = 0;
int buffer = 0;
int buffer2;
int buffer3 = 0;
//int buffer4 = 0;
fseek(file, 0, SEEK_END);
rawContentLength = ftell(file);
rewind(file); //REWIND FILE
//ALLOCATE rawContent
rawContent = malloc(rawContentLength + 1);
memset(rawContent, '\0', rawContentLength + 1);
(void) (fread(rawContent, rawContentLength, 1, file) + 1); //TODO: Try to create some function for processing exit value
//GET lines
for (int i = 0; i < rawContentLength; i++)
{
if (rawContent[i] == '\n') lines++;
}
linesContent = malloc(lines + 1);
for (int i = 0; i < rawContentLength; i++) //LOAD/SPIT rawContent INTO linesContent
{
if (rawContent[i] == '\n')
{
buffer2 = i - buffer;
if (buffer != 0) buffer2--;
//printf("%d\t%d\t%d\n", i, buffer, buffer2);
//printf("%d\t%d\n", buffer3, buffer2);
linesContent[buffer3] = malloc((buffer2 + 1) - strlen(WRITE_FORMAT));
//memset(linesContent[buffer3], '\0', buffer2 + 1);
// for (int j = 0; j <= buffer2; j++)
// {
// linesContent[buffer3][j] = '\0';
// }
for (int j = buffer + strlen(WRITE_FORMAT); j < i; j++)
{
linesContent[buffer3][j - (buffer + strlen(WRITE_FORMAT))/* - buffer4*/] = rawContent[j];
//printf("%d ", j - buffer);
//printf("%c", rawContent[j]);
}
linesContent[buffer3][(buffer2 + 1) - strlen(WRITE_FORMAT)] = '\0';
removeSpaces(linesContent[buffer3]);
//printf("\n");
buffer = i;
buffer3++;
//buffer4 = 1; | e
}
}
//TODO: Decrypt
for (int i = 0; i < buffer3; i++)
{
free(linesContent[i]);
}
free(linesContent);
free(rawContent);
return;
}