-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
116 lines (97 loc) · 2.84 KB
/
main.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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) 2017 M.J.Ahmadi <[email protected]>
*
* pjdate is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* pjdate is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "helper.h"
#include "pjdate.h"
#include "about.h"
#include "funcs.h"
char *_format = "%Y-%M-%D";
int main (int argc, char *argv[])
{
int opt;
const char *short_options = "f:j:g:c:r:d:";
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "format", 1, NULL, 'f' },
{ "to-jalali", 1, NULL, 'j' },
{ "to-gregorian", 1, NULL, 'g' },
{ "compare", 1, NULL, 'c' },
{ "reference", 1, NULL, 'r' },
{ "datediff", 1, NULL, 'd' },
{ "version", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
pjdate _pjdate = get_cur_pjdate();
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
//--help
case 'h':
print_usage(stdout, EXIT_SUCCESS);
break;
//--format
case 'f':
_format = argv[optind - 1];
break;
//--to-jalali
case 'j':
_pjdate = gregorian_to_jalali(atoi(_substring(argv[optind - 1], 0, 4)),
atoi(_substring(argv[optind - 1], 5, 2)),
atoi(_substring(argv[optind - 1], 8, 2)));
break;
//--to-gregorian
case 'g':
_pjdate = jalali_to_gregorian(atoi(_substring(argv[optind - 1], 0, 4)),
atoi(_substring(argv[optind - 1], 5, 2)),
atoi(_substring(argv[optind - 1], 8, 2)));
break;
//--compare
case 'c':
_pjdate = compare_pjdate(argv[optind - 1]);
break;
//--reference
case 'r':
if (_file_exist(argv[optind - 1])) {
_pjdate = file_modification_date(argv[optind - 1]);
} else {
print_error("Referenced file does not exist.", 1);
}
break;
//-FIXME --difference
case 'd':
//_pjdate = pjdate_difference(argv[optind - 1]);
break;
//--version
case 'v':
print_version();
break;
default:
print_usage(stderr, EXIT_FAILURE);
exit(EXIT_SUCCESS);
break;
}
}
print_pjdate(_pjdate, _format);
if (optind < argc) {
fprintf(stderr, "Expected argument after options.\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}