Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / simulator / simulator / core / logger.c @ 1163

History | View | Annotate | Download (1.56 KB)

1
#include "logger.h"
2
#include "robot.h"
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6

    
7
int setWriteFile(char *name, char* worldName)
8
{
9
        file = fopen(name, "w");
10
        
11
        if(file == 0)
12
                return -1;
13

    
14
        int size = strlen(worldName) + 1;
15
    if(fwrite(&size, sizeof(int), 1, file) < 1)
16
        {
17
                fprintf(stderr, "Logging Error\n");
18
                return -1;
19
        }
20

    
21
        if(fwrite(worldName, strlen(worldName) + 1, 1, file) < 1)
22
        {
23
                fprintf(stderr, "Logging Error\n");
24
                return -1;
25
        }
26
        
27
        return 0;
28
}
29

    
30
int setReadFile(char *name, char **worldName)
31
{
32
   file = fopen(name, "r");
33
   if(file == 0)
34
           return -1;
35

    
36
   int size;
37
   if(fread(&size, sizeof(int), 1, file) < 1)
38
   {
39
           fprintf(stderr, "Logging Error\n");
40
           return -1;
41
   } 
42

    
43
   *worldName = malloc(size);
44

    
45
   if(fread(*worldName, size, 1, file) < 1)
46
   {
47
           fprintf(stderr, "Logging Error\n");
48
           return -1;
49
   }
50

    
51
   return 0;
52
}
53

    
54
void commit(Robot* robot, int index, int timeStep)
55
{
56
        if(!file)
57
        {
58
                return;
59
        }
60
        if(fwrite(&timeStep, sizeof(int), 1, file) < 1)
61
        {
62
                printf("Logging Error\n");
63
                return;
64
        }
65
        if(fwrite(&index, sizeof(int), 1, file) < 1)
66
        {
67
                printf("Logging Error\n");
68
                return;
69
        }
70
        if(fwrite(robot, sizeof(Robot), 1, file) < 1)
71
        {
72
                printf("Logging Error\n");
73
                return;
74
        }
75
}
76

    
77
int getStep()
78
{
79
        int toReturn;
80
        
81
        if(!file)
82
        {
83
                return 0;
84
        }
85
        
86
        if(fread(&toReturn, sizeof(int), 1, file) < 1)
87
        {
88
                printf("Logging Error\n");
89
                return -1;
90
        }
91
        return toReturn;
92
}
93

    
94
int getLog(Robot* robot)
95
{
96
        if(!file)
97
        {
98
                return 0;
99
        }
100
        if(fread(robot, sizeof(Robot), 1, file) < 1)
101
        {
102
                printf("Logging Error\n");
103
                return -1;
104
        }
105
        return 0;
106
}                          
107