2 Copyright (C) 2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 @interface NSCalendarDate(CalMatrix)
27 - (NSArray *)calendarMatrixWithStartDayOfWeek:(unsigned)_caldow
28 onlyCurrentMonth:(BOOL)_onlyThisMonth;
34 @interface NSString(DayOfWeek)
38 @implementation NSString(DayOfWeek)
43 if ([self length] == 0)
46 if (isdigit([self characterAtIndex:0]))
47 return [self intValue];
49 s = [self lowercaseString];
50 switch ([s characterAtIndex:0]) {
51 case 'm': // Monday, Montag, Mittwoch
52 return ([s characterAtIndex:1] == 'i') ? 3 : 1;
54 return ([s characterAtIndex:1] == 'u') ? 2 : 4;
55 case 'f': // Fri, Frei
57 case 's': // Sat, Sun, Sam, Sonn
58 return ([s characterAtIndex:1] == 'a') ? 6 : 0;
66 @end /* NSString(DayOfWeek) */
69 static void usage(NSArray *args) {
70 printf("Usage: %s [[[month] year] startday]\n\n"
72 " month - month as a decimal (1-12)\n"
73 " year - year as a decimal (1976-2030)\n"
74 " startday - first column in matrix (Sunday=0...Saturday=6)\n"
75 , [[args objectAtIndex:0] cString]);
79 static void printMatrix(NSArray *weeks, int dow) {
80 unsigned week, weekCount;
83 NSLog(@"ERROR: got no week matrix!");
87 for (week = 0; week < 7; week++) {
92 case 0: c = 'S'; break;
93 case 1: c = 'M'; break;
94 case 2: c = 'T'; break;
95 case 3: c = 'W'; break;
96 case 4: c = 'T'; break;
97 case 5: c = 'F'; break;
98 case 6: c = 'S'; break;
104 for (week = 0, weekCount = [weeks count]; week < weekCount; week++) {
106 unsigned day, dayCount;
108 days = [weeks objectAtIndex:week];
109 dayCount = [days count];
111 /* pad first week (could also print old dates) */
113 for (day = 7; day > dayCount; day--)
117 for (day = 0; day < dayCount; day++) {
118 NSCalendarDate *dayDate;
120 dayDate = [days objectAtIndex:day];
121 printf(" %2i", [dayDate dayOfMonth]);
128 static int doCalArgs(NSArray *args) {
129 NSCalendarDate *now, *start;
130 unsigned startDayOfWeek, month, year;
134 now = [NSCalendarDate date];
135 startDayOfWeek = 1 /* Monday */;
136 month = [now monthOfYear];
137 year = [now yearOfCommonEra];
141 if ([args count] > 1)
142 month = [[args objectAtIndex:1] intValue];
143 if ([args count] > 2) {
144 year = [[args objectAtIndex:2] intValue];
148 if ([args count] > 3)
149 startDayOfWeek = [[args objectAtIndex:3] dayOfWeekInEnglishOrGerman];
153 start = [NSCalendarDate dateWithYear:year month:month day:1
154 hour:0 minute:0 second:0
155 timeZone:[now timeZone]];
157 printMatrix([start calendarMatrixWithStartDayOfWeek:startDayOfWeek
158 onlyCurrentMonth:NO],
164 int main(int argc, char **argv, char **env) {
165 NSAutoreleasePool *pool;
168 pool = [[NSAutoreleasePool alloc] init];
169 #if LIB_FOUNDATION_LIBRARY
170 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
174 Note: we cannot check for those in the tool function because the - args
177 if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--help"]) {
178 usage([[NSProcessInfo processInfo] arguments]);
181 if ([[[NSProcessInfo processInfo] arguments] containsObject:@"-h"]) {
182 usage([[NSProcessInfo processInfo] arguments]);
186 res = doCalArgs([[NSProcessInfo processInfo] argumentsWithoutDefaults]);
191 [NGExtensions class];