]> err.no Git - sope/blob - sope-core/samples/ngcal.m
added PCH support
[sope] / sope-core / samples / ngcal.m
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #include "common.h"
23
24 #if 0
25 @interface NSCalendarDate(CalMatrix)
26
27 - (NSArray *)calendarMatrixWithStartDayOfWeek:(unsigned)_caldow
28   onlyCurrentMonth:(BOOL)_onlyThisMonth;
29
30 @end
31
32
33
34 @interface NSString(DayOfWeek)
35 - (int)dayOfWeek;
36 @end
37
38 @implementation NSString(DayOfWeek)
39
40 - (int)dayOfWeek {
41   NSString *s;
42   
43   if ([self length] == 0)
44     return -1;
45   
46   if (isdigit([self characterAtIndex:0]))
47     return [self intValue];
48   
49   s = [self lowercaseString];
50   switch ([s characterAtIndex:0]) {
51   case 'm': // Monday, Montag, Mittwoch
52     return ([s characterAtIndex:1] == 'i') ? 3 : 1;
53   case 't': // Tue, Thu
54     return ([s characterAtIndex:1] == 'u') ? 2 : 4;
55   case 'f': // Fri, Frei
56     return 5;
57   case 's': // Sat, Sun, Sam, Sonn
58     return ([s characterAtIndex:1] == 'a') ? 6 : 0;
59   case 'w': // Wed
60     return 3;
61   }
62   
63   return -1;
64 }
65
66 @end /* NSString(DayOfWeek) */
67 #endif
68
69 static void usage(NSArray *args) {
70   printf("Usage: %s [[[month] year] startday]\n\n"
71          "Arguments:\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]);
76 }
77
78
79 static void printMatrix(NSArray *weeks, int dow) {
80   unsigned week, weekCount;
81
82   if (weeks == nil) {
83     NSLog(@"ERROR: got no week matrix!");
84     return;
85   }
86
87   for (week = 0; week < 7; week++) {
88     int dd = dow + week;
89     char c;
90     if (dd > 7 ) dd -= 7;
91     switch (dd) {
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;
99     }
100     printf(" %2c", c);
101   }
102   puts("");
103   
104   for (week = 0, weekCount = [weeks count]; week < weekCount; week++) {
105     NSArray *days;
106     unsigned day, dayCount;
107     
108     days     = [weeks objectAtIndex:week];
109     dayCount = [days count];
110     
111     /* pad first week (could also print old dates) */
112     if (week == 0) {
113       for (day = 7; day > dayCount; day--)
114         printf("   ");
115     }
116     
117     for (day = 0; day < dayCount; day++) {
118       NSCalendarDate *dayDate;
119       
120       dayDate = [days objectAtIndex:day];
121       printf(" %2i", [dayDate dayOfMonth]);
122     }
123     puts("");
124   }
125 }
126
127
128 static int doCalArgs(NSArray *args) {
129   NSCalendarDate *now, *start;
130   unsigned startDayOfWeek, month, year;
131
132   /* defaults */
133   
134   now = [NSCalendarDate date];
135   startDayOfWeek = 1 /* Monday */;
136   month          = [now monthOfYear];
137   year           = [now yearOfCommonEra];
138
139   /* arguments */
140
141   if ([args count] > 1)
142     month = [[args objectAtIndex:1] intValue];
143   if ([args count] > 2) {
144     year = [[args objectAtIndex:2] intValue];
145     if (year < 100)
146       year += 2000;
147   }
148   if ([args count] > 3)
149     startDayOfWeek = [[args objectAtIndex:3] dayOfWeekInEnglishOrGerman];
150
151   /* focus date */
152   
153   start = [NSCalendarDate dateWithYear:year month:month day:1
154                           hour:0 minute:0 second:0
155                           timeZone:[now timeZone]];
156
157   printMatrix([start calendarMatrixWithStartDayOfWeek:startDayOfWeek
158                      onlyCurrentMonth:NO], 
159               startDayOfWeek);
160   
161   return 0;
162 }
163
164 int main(int argc, char **argv, char **env) {
165   NSAutoreleasePool *pool;
166   int res;
167   
168   pool = [[NSAutoreleasePool alloc] init];
169 #if LIB_FOUNDATION_LIBRARY  
170   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
171 #endif
172
173   /* 
174      Note: we cannot check for those in the tool function because the - args
175            are stripped out
176   */
177   if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--help"]) {
178     usage([[NSProcessInfo processInfo] arguments]);
179     exit(0);
180   }
181   if ([[[NSProcessInfo processInfo] arguments] containsObject:@"-h"]) {
182     usage([[NSProcessInfo processInfo] arguments]);
183     exit(0);
184   }
185   
186   res = doCalArgs([[NSProcessInfo processInfo] argumentsWithoutDefaults]);
187   
188   [pool release];
189   exit(0);
190   /* static linking */
191   [NGExtensions class];
192   return 0;
193 }