File: DublinCore.m

package info (click to toggle)
rsskit 0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 656 kB
  • ctags: 572
  • sloc: objc: 2,041; makefile: 48; sh: 4
file content (228 lines) | stat: -rw-r--r-- 5,556 bytes parent folder | download | duplicates (4)
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
/*  -*-objc-*-
 *
 *  GNUstep RSS Kit
 *  Copyright (C) 2006 Guenther Noack
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation, in version 2.1
 *  of the License
 * 
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// Dublin Core Module
// Written by Guenther Noack <guenther@unix-ag.uni-kl.de>


// For the date format, please consider taking a look at
// http://www.w3.org/TR/NOTE-datetime

#import "DublinCore.h"
//#define DEBUG 1

// -------------------------------------------------------------
//                G L O B A L   V A R I A B L E S


// index always points to the *next* character
// this is a global variable so that you can find out where it failed.
int position;

NSString* dc_error;


// -------------------------------------------------------------
//                          M A C R O S


#define nextIsNumber (isdigit(str[position]))

#define breakMethod(reason) \
{                           \
  free(str);                \
  dc_error = reason;        \
  return nil;               \
}


#define eatNextNumber(item)                           \
{                                                     \
  (item) = (item)*10 + (str[position]-(unichar)'0');  \
  position++;                                         \
}



#define indexOutOfBounds (position>=length)


#define eatNextNumberOrBreak(item) \
{                                  \
  if (indexOutOfBounds)            \
    breakMethod(@"index out of bounds"); \
                                   \
  if (nextIsNumber)                \
    eatNextNumber((item))          \
  else                             \
    breakMethod(@"not a number");  \
}

#define eatNextNumberOrBreakTimes(item,n) \
{                                         \
  for(i=0;i<(n);i++)                      \
    {                                     \
      eatNextNumberOrBreak((item));       \
    }                                     \
}

#define eat(character,retval) \
{                             \
  if (indexOutOfBounds)       \
      return retval;          \
			      \
  if (str[position]!=character)  \
    breakMethod(@"expected something else"); \
                              \
  position++;                 \
}

#define successReturnValue            \
[ NSCalendarDate                      \
  dateWithYear: ((year!=0)?year:2005) \
  month: ((month!=0)?month:1)         \
  day: ((dayOfMonth!=0)?dayOfMonth:1) \
  hour: hours                         \
  minute: minutes                     \
  second: seconds                     \
  timeZone: timeZone                  ]

#define eatOrSucceed(character) \
 eat(character,successReturnValue);

#define eatOrDie(character) \
 eat(character,nil);



// -------------------------------------------------------------
//                     D A T E   P A R S E R


NSDate* parseDublinCoreDate( NSString* dateStr )
{
  unichar* str;
  int length;
  int i; // just for small loops
  
  int year        =0;
  int month       =0;
  int dayOfMonth  =0;
  int hours       =0;
  int minutes     =0;
  int seconds     =0;
  int deciseconds =0;
  
  NSTimeZone* timeZone = nil;
  int tzHrs  =0;
  int tzMins =0;
  int tzSign =0;
    
  length = [dateStr length];
  
  str = (unichar*) malloc(length*sizeof(unichar));
  [dateStr getCharacters: str];
  
  // reset position
  position = 0;
  
  eatNextNumberOrBreakTimes(year,4);
  
  eatOrSucceed('-');
  
  eatNextNumberOrBreakTimes(month,2);
  
  eatOrSucceed('-');
  
  eatNextNumberOrBreakTimes(dayOfMonth,2);
  
  eatOrSucceed('T');
  
  eatNextNumberOrBreakTimes(hours,2);
  
  eatOrDie(':');
  
  eatNextNumberOrBreakTimes(minutes,2);
  
  // Optional Seconds
  
  if (indexOutOfBounds)
    breakMethod(@"out of bounds where seconds or TZD should follow");
  
  if (str[position] == (unichar)':')
    {
      position++;
      
      eatNextNumberOrBreakTimes(seconds,2);
      
      if (indexOutOfBounds)
	breakMethod(@"out of bounds where deciseconds or TZD should follow");
      
      if (str[position] == (unichar)'.')
	{
	  position++;
	  eatNextNumberOrBreak(deciseconds);
	}
    }
  
  // Time Zone
  if (indexOutOfBounds)
    breakMethod(@"out of bounds where time zone definition should follow");
  
  if (str[position] == (unichar)'-')
    {
      eatOrDie('-');
      tzSign = -1;
      // GOTO PARSETZDREST
    }
  else if (str[position] == (unichar)'+')
    {
      eatOrDie('+');
      tzSign = 1;
      // GOTO PARSETZDREST
    }
  
  // putting together the time zone
  if (tzSign != 0)
    {
      // PARSETZDREST:
      
      eatNextNumberOrBreakTimes(tzHrs,2);
      eatOrDie(':');
      eatNextNumberOrBreakTimes(tzMins,2);
      
      timeZone =
	[NSTimeZone
	  timeZoneForSecondsFromGMT: tzSign * ((tzHrs*60 + tzMins)*60)];
    }
  else
    {
      NSString* subStr = [dateStr substringFromIndex: position];
      
      timeZone = [NSTimeZone timeZoneWithAbbreviation: subStr];
    }

  /* Process is done. It should be safe to free str */
  free(str);
  
  return successReturnValue;
}