-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathical.c
518 lines (440 loc) · 12.6 KB
/
ical.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/************************************************************************
* $Id: ical.c 10994 2009-03-09 04:17:36Z thamer $
*
* ------------
* Description:
* ------------
* Copyright (c) 2004, Arabeyes, Nadim Shaikli
*
* This is an application to primarily display Islamic/Hijri with
* association to Gregorian/Meladi dates in 'cal' format (a month
* at a time).
*
* -----------------
* Revision Details: (Updated by Revision Control System)
* -----------------
* $Date: 2009-03-09 05:17:36 +0100 (Mon, 09 Mar 2009) $
* $Author: thamer $
* $Revision: 10994 $
* $Source$
*
* (www.arabeyes.org - under GPL license)
************************************************************************/
/* TODO:
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* for strlen/strcat/etc */
/* For time_t */
#ifdef TM_IN_SYS_TIME
#include <sys/time.h>
#else
#include <time.h>
#endif
#include <itl/hijri.h>
#define PROG_NAME "ical";
/* This holds the current date info. */
typedef struct
{
int year;
int month;
int day;
} Date;
/**
Print out the command-line usage of this application
**/
void
usage(int leave)
{
char *pspaces;
char *pname = PROG_NAME;
pspaces = (char *) malloc(strlen(pname));
strncpy(pspaces, " ", strlen(pname));
fprintf(stderr, "%s [--gregorian yyyymmdd] [--hijri yyyymmdd]\n", pname);
fprintf(stderr, "%s [--umm_alqura]\n", pspaces);
fprintf(stderr, "%s [--fixed_view]\n", pspaces);
fprintf(stderr, "%s [--dual]\n", pspaces);
fprintf(stderr, "%s [--help]\n", pspaces);
free(pspaces);
if (leave)
exit(20);
}
/**
Error printing (& possibly exit) apparatus.
**/
void
error(int leave,
char* err_msg)
{
char *prog_name = PROG_NAME;
fprintf(stderr, "[%s]: %s", prog_name, err_msg);
if (leave)
exit(leave);
}
/**
Initizlize various variables/structures
**/
void
do_init(Date *mydate,
sDate *pdate)
{
mydate->year = (int) NULL;
mydate->month = (int) NULL;
mydate->day = (int) NULL;
pdate->day = (int) NULL;
pdate->month = (int) NULL;
pdate->year = (int) NULL;
pdate->weekday = (int) NULL;
pdate->frm_numdays = (int) NULL;
pdate->to_numdays = (int) NULL;
pdate->to_numdays2 = (int) NULL;
pdate->frm_dname = (char *) NULL;
pdate->frm_mname = (char *) NULL;
pdate->frm_dname_sh = (char *) NULL;
pdate->frm_mname_sh = (char *) NULL;
pdate->to_dname = (char *) NULL;
pdate->to_mname = (char *) NULL;
pdate->to_dname_sh = (char *) NULL;
pdate->to_mname_sh = (char *) NULL;
pdate->event = (char **) NULL;
}
/**
Printout the Resulting date struct along with itsoriginating month view
**/
void
display_dual(int using_umm_alqura,
int doing_hijri,
int fixed_view,
Date *udate,
sDate *rdate)
{
int i;
int column;
int calc_day;
int end_calc_day;
int wk_remap[7] = {1, 2, 3, 4, 5, 6, 0};
int wk_day;
if (using_umm_alqura)
printf("[Using Umm-AlQura Algorithm]\n");
/* Give us a month, year header */
printf("From/To (%s %d / %s", rdate->frm_mname,
udate->year, rdate->to_mname);
/* Figure out if we are dealing with 2 months on the result or just one */
if ((rdate->frm_numdays > rdate->to_numdays) ||
((rdate->day + rdate->to_numdays - 1) > rdate->frm_numdays))
{
printf(" - %s %d", rdate->to_mname2, rdate->year);
if (rdate->month == 12)
printf("-%d):\n", (rdate->year + 1));
else
printf("):\n");
}
else
{
printf(" %d):\n", rdate->year);
}
printf("\n");
/* Set columns headers */
if (doing_hijri)
{
if (fixed_view)
{
printf(" Sun/ Mon/ Tue/ Wed/ Thu/ Fri/ Sat/\n");
printf(" Ahd Ith Tha Arb Kha Jum Sab\n");
}
else
{
printf(" Sat/ Sun/ Mon/ Tue/ Wed/ Thu/ Fri/\n");
printf(" Sab Ahd Ith Tha Arb Kha Jum\n");
}
}
else
{
printf(" Ahd/ Ith/ Tha/ Arb/ Kha/ Jum/ Sab/\n");
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
}
printf("-------------------------------------------------\n");
/* Do some remapping based on Fri being last day of week in Hijri
- This is done as a nicety as most hijri calendars end on Friday
*/
if (doing_hijri & !fixed_view)
wk_day = wk_remap[rdate->weekday];
else
wk_day = rdate->weekday;
/* Pad with empty space if starting within the week */
for(i = 0; i < wk_day; i++)
{
printf(" ");
}
column = wk_day + 1;
calc_day = rdate->day;
end_calc_day = 0;
/* Fill in the month view with both FROM and TO values */
for(i = 1; i <= rdate->frm_numdays; i++)
{
if (i == udate->day)
printf("[%02d/%02d]", i, calc_day++);
else
printf(" %02d/%02d ", i, calc_day++);
/* Know when to start a new line */
if (!(column++ % 7))
printf("\n");
/* Know when to flip the secondary month's day roll-over */
if (!end_calc_day && (calc_day > rdate->to_numdays))
{
calc_day = 1;
end_calc_day = 1;
}
/* In the VERY unlikely case where the secondary has less days
than primary such that 3 of the secondary's months are needed,
know when to start over (yup, sheer paranoia).
*/
if (end_calc_day && (calc_day > rdate->to_numdays2))
{
calc_day = 1;
}
}
/* Print a new line if necessary */
if ((column % 7) != 1)
printf("\n");
}
/**
Printout the Resulting date struct in minmal single format (akin to 'cal')
**/
void
display_single(int using_umm_alqura,
int doing_hijri,
int fixed_view,
Date *udate,
sDate *rdate)
{
int i;
int shift;
int column;
int str_length;
int dig_length;
int start_point;
int calc_day;
int end_calc_day;
int wk_remap[7] = {1, 2, 3, 4, 5, 6, 0};
int wk_day;
if (using_umm_alqura)
printf("[Using Umm-AlQura Algorithm]\n");
/* Since we get a particular day's info (not day #1), make adjustments */
shift = ((rdate->day % 7) - 1);
/* Do some remapping based on Fri being last day of week in Hijri
- This is done as a nicety as most hijri calendars end on Friday
*/
if (doing_hijri & !fixed_view)
wk_day = wk_remap[rdate->weekday];
else
wk_day = rdate->weekday;
/* Based on day and weekday value of returned result, shift things about */
if (shift > wk_day)
column = (wk_day - shift + 7 + 1);
else
column = (wk_day - shift + 1);
/* Catch spill-over condition when dealing with lastday of week */
column = ( (column == 8) ? 1 : column);
/* Give us a month, year header (nicely centered) */
if (rdate->year/10)
dig_length = 1;
if (rdate->year/100)
dig_length += 1;
if (rdate->year/1000)
dig_length += 1;
/* Try to center the title as much as possible over the output */
str_length = ( strlen(rdate->to_mname) + dig_length + 6);
start_point = ( 14 - (str_length / 2));
for(i = 1; i < start_point; i++)
printf(" ");
printf("%s %d (%s)\n", rdate->to_mname, rdate->year, rdate->units);
/* Set columns headers based on what we're converting to */
if (doing_hijri)
if (fixed_view)
{
printf(" Ah I T Ar K J S\n");
}
else
{
printf(" S Ah I T Ar K J\n");
}
else
printf(" S M Tu W Th F S\n");
/* Pad with empty space if starting within the week */
for(i = 0; i < column-1; i++)
{
printf(" ");
}
calc_day = rdate->day;
end_calc_day = 0;
/* Fill in the month view with both FROM and TO values */
for(i = 1; i <= rdate->to_numdays; i++)
{
if (i == rdate->day)
printf("[%2d]", i);
else
printf(" %2d ", i);
/* Know when to start a new line */
if (!(column++ % 7))
printf("\n");
/* Know when to flip the secondary month's day roll-over */
if (!end_calc_day && (calc_day > rdate->to_numdays))
{
calc_day = 1;
end_calc_day = 1;
}
/* In the very unlikely case were the secondary has less days
than primary such that 3 of the secondary's months are needed,
know when to start over
*/
if (end_calc_day && (calc_day > rdate->to_numdays2))
{
calc_day = 1;
}
}
/* Print a new line if necessary */
if ((column % 7) != 1)
printf("\n");
}
/**
Main procedure
**/
int main(int argc, char *argv[])
{
int i;
int g_to_h = 0;
int h_to_g = 0;
int use_umm_alqura = 0;
int show_dual = 0;
int fixed_week_view = 0;
int error_lib = 0;
int start_day;
Date indate;
sDate outdate;
/* Current time/date specifics */
time_t mytime;
struct tm *t_ptr;
do_init(&indate, &outdate);
/* Process the command-line */
for (i = 1; i < argc; i++)
{
if (strcasecmp(argv[i], "-h") == 0 ||
strcasecmp(argv[i], "-help") == 0 ||
strcasecmp(argv[i], "--help") == 0)
{
/* We really need a full-fledged help here */
usage(1);
}
if (strcasecmp(argv[i], "-g") == 0 ||
strcasecmp(argv[i], "-gregorian") == 0 ||
strcasecmp(argv[i], "--gregorian") == 0)
{
g_to_h = 1;
if (argv[i+1] != NULL)
{
sscanf(&(argv[i + 1][0]), "%4d", &indate.year);
sscanf(&(argv[i + 1][4]), "%2d", &indate.month);
sscanf(&(argv[i + 1][6]), "%2d", &indate.day);
}
else
{
error(1, "Exiting, invalid argument to --gregorian\n");
}
}
if (strcasecmp(argv[i], "-hi") == 0 ||
strcasecmp(argv[i], "-hijri") == 0 ||
strcasecmp(argv[i], "--hijri") == 0)
{
h_to_g = 1;
if (argv[i+1] != NULL)
{
sscanf(&(argv[i + 1][0]), "%4d", &indate.year);
sscanf(&(argv[i + 1][4]), "%2d", &indate.month);
sscanf(&(argv[i + 1][6]), "%2d", &indate.day);
}
else
{
error(1, "Exiting, invalid argument to --hijri\n");
}
}
if (strcasecmp(argv[i], "-u") == 0 ||
strcasecmp(argv[i], "-umm_alqura") == 0 ||
strcasecmp(argv[i], "--umm_alqura") == 0)
{
use_umm_alqura = 1;
}
if (strcasecmp(argv[i], "-f") == 0 ||
strcasecmp(argv[i], "--fixed_view") == 0)
{
fixed_week_view = 1;
}
if (strcasecmp(argv[i], "-d") == 0 ||
strcasecmp(argv[i], "--dual") == 0)
{
show_dual = 1;
}
}
/* Make sure user knows what they are doing */
if (g_to_h && h_to_g)
error(2, "Exiting, can't defined both Gregorian and Hijri\n");
/* Go with default current date if not user specifications */
if (!g_to_h && !h_to_g)
{
g_to_h = 1;
/* Get current time structure */
time(&mytime);
t_ptr = localtime(&mytime);
/* Set current time values */
indate.day = t_ptr->tm_mday;
indate.month = t_ptr->tm_mon + 1;
indate.year = t_ptr->tm_year + 1900;
}
/* Inspect user's input or default settings - within range ? */
if ((indate.month > 12) || (indate.month < 1))
error(3, "Exiting, input 'month' not within allowable range\n");
if ((indate.day > 31) || (indate.day < 1))
error(3, "Exiting, input 'day' not within allowable range\n");
/* Specify our appropriate start day based on user mode */
start_day = ( show_dual ? 1 : indate.day );
/* TEMPORARY note due to no functionality */
if (use_umm_alqura)
printf("NOTE: Umm Al-Qura has NOT been included YET (using other)\n");
/* Do the actual conversions */
if (g_to_h)
{
/* Dealing with Gregorian to Hijri conversion */
/*
if (use_umm_alqura)
G2H(&outdate, indate.day, indate.month, indate.year);
else
*/
error_lib = h_date(&outdate, start_day, indate.month, indate.year);
}
else
{
/* Dealing with Hijri to Gregorian conversion */
/*
if (use_umm_alqura)
H2G(&outdate, indate.day, indate.month, indate.year);
else
*/
error_lib = g_date(&outdate, start_day, indate.month, indate.year);
}
/* Let user know if the library freaked-out and returned with an error */
if (error_lib)
{
char msg[80];
sprintf(msg, "Exiting, error code %d generated by library call\n",
error_lib);
error(error_lib, msg);
}
/* Spill-out and display the output results */
if (show_dual)
// display_dual(use_umm_alqura, g_to_h, &indate, &outdate);
display_dual(0, g_to_h, fixed_week_view, &indate, &outdate);
else
// display_single(use_umm_alqura, g_to_h, &indate, &outdate);
display_single(0, g_to_h, fixed_week_view, &indate, &outdate);
return(0);
}