aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 7f961e7412cf13f2e2310c865b80876c610b427d (plain)
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
#include <stdio.h>
#include "args.h"
#include "pattern/pattern.h"
#include "io/text.h"
#include "io/svg.h"

/*
 * EX stands for EXIT STATUS and
 * EXE stands for EXIT STATUS ERROR
 */
#define EX_OK 0
#define EXE_ARGS 1
#define EXE_IO 2

int main(int argc, char **argv)
{
    struct args args;
    enum alr alr = load_args(argc, argv, &args);
    if (alr != ALR_OK)
    {
        if (alr == ALR_INVALID_OPTION)
            fprintf(stderr, "Invalid option!");
        else
            fprintf(stderr, "Args processing error!\n");
        return EXE_ARGS;
    }
    struct pattern *pattern = &args.pattern;
    struct coord path[16];
    pattern->path = path;

    args.params.count = 0;
    FILE *out = stdout;
    if (!args.standard_output && args.single_output)
    {
        if ((out = fopen(args.output, "w")) == NULL)
        {
            fprintf(stderr, "%s file cannot be opened.", args.output);
            return EXE_IO;
        }
    }
    while (args.gen(pattern) && (args.gen_count < 0 || args.params.count < args.gen_count))
    {
        args.params.count++;
        if ((args.from_count <= (int)args.params.count) && (((int)args.params.count <= args.to_count) || (args.to_count < 0)))
        {
            if (!args.standard_output && !args.single_output)
            {
                char file_path[512];
                sprintf(file_path, args.output, args.params.count);
                if ((out = fopen(file_path, "w")) == NULL)
                {
                    fprintf(stderr, "%s file cannot be opened.", file_path);
                    return EXE_IO;
                }
            }

            args.print(out, args.params, pattern);
            if (args.wait)
            {
                char c;
                getchar();
            }

            if (!args.standard_output && !args.single_output)
                fclose(out);
        }
    }
    if (!args.standard_output && args.single_output)
        fclose(out);

    return EX_OK;
}