This repository was archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
109 lines (106 loc) · 2.71 KB
/
main.c
File metadata and controls
109 lines (106 loc) · 2.71 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#define _POSIX_SOURCE
#include <sys/stat.h>
#include <unistd.h>
#undef _POSIX_SOURCE
#include "generacja.h"
#include "komorka.h"
#include "zapis.h"
#include "sasiedztwo.h"
const char *usage = "Usage: \n"
"./Life -n N -l path [-s prefix] [-o] [-w file_name] [-f F]\n"
"\tN - the number of generations\n"
"\tpath - path to the file with data about generation zero\n"
"\tprefix - prefix of the png files\n"
"\t-o - switch on von Neumann neighbourhood\n"
"\tfile_name - the name of the file which will have data about last generation\n"
"\tF - the number of generations which will be written to png files\n";
int main(int argc, char **argv) {
int a, opt, n, f, last_to_txt = 0;
char *gen_zero;
char *png_pattern = "generacja";
char png_title[100];
char number[10];
const char *format = ".png";
char *txt_pattern;
const char *new_dir = "created_files";
FILE *in = NULL;
int (*neighbourhood)(generation_t *, int, int) = Moore_ngbh;
generation_t *current, *new, *tmp;
if (argc < 5) {
puts(usage);
return EXIT_FAILURE;
}
while ((opt = getopt(argc, argv, "n:l:s:f:w:o")) != -1) {
switch (opt) {
case 'n':
n = atoi(optarg);
f = n;
break;
case 'l':
gen_zero = optarg;
break;
case 's':
png_pattern = optarg;
break;
case 'f':
f = atoi(optarg);
break;
case 'w':
last_to_txt = 1;
txt_pattern = optarg;
break;
case 'o':
neighbourhood = vN_ngbh;
break;
default:
fprintf(stderr, "%s: Nie prawidłowy argument wywołania %c\n", argv[0], opt);
puts(usage);
exit(EXIT_FAILURE);
}
}
if (gen_zero == NULL || ((in = fopen(gen_zero, "r")) == NULL)) {
fprintf(stderr, "%s: program nie moze odczytac pliku pierwszej generacji\n", argv[0]);
return EXIT_FAILURE;
}
current = load_from_file(in);
new = init_gen(current->rows, current->cols);
if (mkdir(new_dir, S_IRWXU|S_IRGRP|S_IXGRP) != 0) {
if (errno == EEXIST) {
if (chdir(new_dir) != 0) {
perror("error");
}
} else {
perror("error");
}
} else if (chdir(new_dir) != 0) {
perror("error");
}
strcpy(png_title, png_pattern);
strcat(png_title, "0.png");
save_to_png(current, png_title);
for (a = 1; a <= n; a++) {
strcpy(png_title, png_pattern);
next_generation(current, new, neighbourhood);
if (a <= f) {
sprintf(number, "%d", a);
strcat(png_title, number);
strcat(png_title, format);
save_to_png(new, png_title);
}
if (a == n && last_to_txt == 1)
save_to_txt(new,txt_pattern);
tmp = current;
current = new;
new = tmp;
}
free_gen(current);
free_gen(new);
if (chdir("..") != 0)
perror("error");
exit(EXIT_SUCCESS);
}