-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsauvegarde.c
132 lines (115 loc) · 3.62 KB
/
sauvegarde.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sauvegarde.h"
#include "moteur.h"
#include <string.h>
#include <dirent.h>
#define nbplanete 8
void sauvegarder(char *nomdesauvegarde, ElementAstre *ptElementAstreInitial,
time_t t) {// a pas utiliser car est dans nomsauvegarde
FILE *f = fopen(nomdesauvegarde, "w");
ElementAstre *ptElementAstreCourant = ptElementAstreInitial;
ptElementAstreCourant = ptElementAstreCourant->ptElementAstreSuivant;
fprintf(f, "%ld\n", t);
while (ptElementAstreCourant != NULL) {
Astre *ptAstre = ptElementAstreCourant->ptAstre;
if (ptAstre != NULL) {
printf("Bonjour, je suis %s\n", ptAstre->nom);
}
fprintf(f, "%f\n", ptAstre->x);
fprintf(f, "%f\n", ptAstre->y);
fprintf(f, "%f\n", ptAstre->vt);
fprintf(f, "%f\n", ptAstre->vx);
fprintf(f, "%f\n", ptAstre->vy);
fprintf(f, "%f\n", ptAstre->F);
fprintf(f, "%f\n", ptAstre->distanceCentreGravitation);
ptElementAstreCourant = ptElementAstreCourant->ptElementAstreSuivant;
}
fclose(f);
}
int nbsave(DIR *d) {//quand vous voulez avoir la liste des sauvegardes, juste utiliser la fonction
struct dirent *dir;
d = opendir("saves/");
int nbSave = 0;
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
nbSave++;
}
//nbSave=nbSave;
}
closedir(d);
return nbSave;
}
return 0;
}
void listesauvegarde(char **a)//quand vous voulez avoir la liste des sauvegardes, juste utiliser la fonction
{
DIR *d;
struct dirent *dir;
d = opendir("saves/");
int i = 0;
if (d) {
while ((dir = readdir(d)) != NULL) {
if (dir->d_type == DT_REG) {
int length = strlen(dir->d_name);
a[i] = malloc(sizeof(char) * length);
strcpy(a[i], dir->d_name);
i++;
}
}
closedir(d);
}
}
void loadsave(char *save, ElementAstre *ptElementAstreInitial, time_t *t) {
char *b = malloc(sizeof(char) * 50);
strcpy(b, "saves/");
strcat(b, save);
FILE *f = fopen(b, "r");
f = fopen(b, "r");
int i = 0;
ElementAstre *ptElementAstreCourant = ptElementAstreInitial;
ptElementAstreCourant = ptElementAstreCourant->ptElementAstreSuivant;
fscanf(f, "%ld\n", t);
printf("Le temps : %ld\n", *t);
while (ptElementAstreCourant != NULL) {
Astre *ptAstre = ptElementAstreCourant->ptAstre;
if (ptAstre != NULL) {
fscanf(f, "%f\n", &ptAstre->x);
fscanf(f, "%f\n", &ptAstre->y);
fscanf(f, "%f\n", &ptAstre->vt);
fscanf(f, "%f\n", &ptAstre->vx);
fscanf(f, "%f\n", &ptAstre->vy);
fscanf(f, "%f\n", &ptAstre->F);
fscanf(f, "%f\n", &ptAstre->distanceCentreGravitation);
i++;
ptElementAstreCourant = ptElementAstreCourant->ptElementAstreSuivant;
}
}
}
time_t tps(char *save) {
FILE *f;
time_t t;
char* b=malloc(sizeof(char) * 50);
strcpy(b, "saves/");
strcat(b, save);
f = fopen(b, "r");
fscanf(f, "%ld", &t);
fclose(f);
return t;
}
void nomdesauvegarde(ElementAstre *ptElementAstreInitial, time_t t) {//fonction qui permet de creer la sauvegarde
char a[100];
DIR *d;
//struct dirent *dir;
d = opendir("saves/");
int j;
j = nbsave(d)+1;
closedir(d);
snprintf(a, 6, "%d\n", j);
char *b = malloc(sizeof(char) * 50);
strcpy(b, "saves/");
strcat(b, a);
sauvegarder(b, ptElementAstreInitial, t);
}