Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #37 from Geek2France/master
Browse files Browse the repository at this point in the history
The new online-resume mode resumes from where it was stopped
  • Loading branch information
adepasquale authored Nov 6, 2017
2 parents adfca03 + edda972 commit 9e4eeb1
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CC = $(GCC_PREFIX)/bin/$(CC_CMD)
LD = $(GCC_PREFIX)/bin/$(LD_CMD)

EXE_NAME = fw1-loggrabber
OBJ_FILES = thread.o queue.o fw1-loggrabber.o
OBJ_FILES = thread.o queue.o fw1-cursor.o fw1-loggrabber.o

CFLAGS += -m32 -g -Wall -fpic -DLINUX -DUNIXOS=1 -DDEBUG
SYSTEM_LIBS = -lpthread -lresolv -ldl -lnsl -lelf -lstdc++ -lz
Expand Down
77 changes: 77 additions & 0 deletions fw1-cursor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "fw1-cursor.h"

int read_fw1_cursorfile () {
rewind (cursorstream);
fgets (cursorline, (POSITION_MAX_SIZE + 1), cursorstream);

return atoi (cursorline);
}

/* Write next log position
* return number of characters written
*/
int write_fw1_cursorfile (const char *message, const char separator) {
char position[POSITION_MAX_SIZE];
int i, j = 0;

rewind (cursorstream);

// Extract cuurent position from message
for (i=4; i<strlen (message); i++)
{
if ((char)message[i] != separator)
{
position[j] = message[i];
j++;
}
else
{
break;
}
}

return fprintf (cursorstream, "%0" TOSTRING(POSITION_MAX_SIZE) "d\n", atoi (position)+1);
}

char* get_fw1_cursorname(const char *LogfileName) {
char *cursorname =
(char *) malloc (strlen (LogfileName) + 7);
if (cursorname == NULL)
{
fprintf (stderr, "ERROR: Out of memory\n");
exit(EXIT_FAILURE);
}
strcpy (cursorname, LogfileName);
strcat (cursorname, ".cursor");

return cursorname;
}

void open_fw1_cursorfile (const char *LogfileName) {
char *cursorname = get_fw1_cursorname (LogfileName);

// Open the file in "a" mode first to create it if it doesn't exist yet
cursorstream = fopen (cursorname,"a");
if (cursorstream == NULL)
{
fprintf (stderr, "Error while opening the file %s in a mode.\n", cursorname);
free (cursorname);
exit(EXIT_FAILURE);
}
fclose (cursorstream);

// Open the file in "a" mode first to create it if it doesn't exist yet
cursorstream = fopen (cursorname,"r+");
if (cursorstream == NULL)
{
fprintf (stderr, "Error while opening the file %s in r+ mode.\n", cursorname);
free (cursorname);
exit(EXIT_FAILURE);
}

free (cursorname);
}

void close_fw1_cursorfile() {
fclose (cursorstream);
}
21 changes: 21 additions & 0 deletions fw1-cursor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef FW1CURSOR_H
#define FW1CURSOR_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define POSITION_MAX_SIZE 32

FILE *cursorstream;
char cursorline[POSITION_MAX_SIZE + 1];

int read_fw1_cursorfile (); // Return next log position from cursor file
int write_fw1_cursorfile (const char *message, const char separator); // Deduce next log position from current message
char* get_fw1_cursorname (const char *LogfileName); // Give cursor name associated with log file
void open_fw1_cursorfile (const char *LogfileName); // Initialize cursorstream file handler
void close_fw1_cursorfile (); // Close cursorstream file handler

#endif
Loading

0 comments on commit 9e4eeb1

Please sign in to comment.